Skip to content

Commit

Permalink
Various cleanups
Browse files Browse the repository at this point in the history
* Use simplified lambda syntax
* Use Collection.isEmpty
* Fix broken @see tag
* Use pattern matching
* Use try-with-resources
* Use StandardCharsets
* Immediate return

Complains from sonarlint in my workspace
  • Loading branch information
akurtakov committed Dec 13, 2023
1 parent 3bf52b1 commit 5fbf4ad
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public boolean removeFilters(IApiProblemFilter[] filters) {
// This filter store does not support resources so all filters are
// stored under GLOBAL
Set<IApiProblemFilter> globalFilters = fFilterMap.get(GLOBAL);
if (globalFilters != null && globalFilters.size() > 0) {
if (globalFilters != null && !globalFilters.isEmpty()) {
for (IApiProblemFilter filter : filters) {
removed &= globalFilters.remove(filter);
}
Expand Down Expand Up @@ -416,8 +416,7 @@ protected int loadIntegerAttribute(Element element, String name) {
return -1;
}
try {
int number = Integer.parseInt(value);
return number;
return Integer.parseInt(value);
} catch (NumberFormatException nfe) {
// ignore
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1006,7 +1006,7 @@ public IApiProblem createProblem(IReference reference) throws CoreException {
* @return the API problem if problem or null
*/
public IApiProblem checkAndCreateProblem(IReference reference, IProgressMonitor monitor) throws CoreException {
if (isProblem(reference, monitor) == false) {
if (!isProblem(reference, monitor)) {
return null;
}
return createProblem(reference);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ void addLabel(Label label) {

public void computeLineNumbers() {

if (this.lineInfos.size() < 1 || this.labelsAndLocations.size() < 1) {
if (this.lineInfos.isEmpty() || this.labelsAndLocations.isEmpty()) {
// nothing to do
return;
}
Expand All @@ -721,11 +721,11 @@ public void computeLineNumbers() {
// Iterate over List of Labels and SourceLocations.
List<Object> computedEntries = new ArrayList<>();
for (Object current : this.labelsAndLocations) {
if (current instanceof Label) {
if (current instanceof Label label) {
// label
Integer lineValue = this.lineMap.get(current);
if (lineValue != null) {
computedEntries.add(new LineInfo(lineValue.intValue(), (Label) current));
computedEntries.add(new LineInfo(lineValue.intValue(), label));
} else {
computedEntries.add(current);
}
Expand Down Expand Up @@ -1295,8 +1295,8 @@ public MethodVisitor visitMethod(int access, String name, String desc, String si
if (fIsVisitMembers) {
IApiMember member = this.getMember();
IApiType owner = null;
if (member instanceof IApiType) {
owner = (IApiType) member;
if (member instanceof IApiType apiType) {
owner = apiType;
} else {
try {
owner = member.getEnclosingType();
Expand Down Expand Up @@ -1417,7 +1417,7 @@ static IApiType getDefaultDefined(IApiType type, String name, String signature,
if (superclass != null) {
return superclass;
}
IApiType ints[] = type.getSuperInterfaces();
IApiType[] ints = type.getSuperInterfaces();
for (IApiType j : ints) {
IApiType superint = getDefaultDefined(j, name, signature, false);
if (superint != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,7 @@ public static IReferenceDescriptor referenceDescriptor(IComponentDescriptor orig
public static IApiTypeContainer newScope(IApiComponent[] components) throws CoreException {
LinkedList<IApiTypeContainer> compList = new LinkedList<>();
Collections.addAll(compList, components);
CompositeApiTypeContainer scope = new CompositeApiTypeContainer(components[0].getBaseline(), compList);
return scope;
return new CompositeApiTypeContainer(components[0].getBaseline(), compList);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public IReferenceDescriptor[] getExternalDependenciesFor(IApiComponent apiCompon
unavailableMembers.add(apiUseType);
}
}
if (unavailableMembers.size() > 0) {
if (!unavailableMembers.isEmpty()) {
fetch(apiComponent, unavailableMembers.toArray(new String[unavailableMembers.size()]), references,
localmonitor.split(9));
}
Expand Down Expand Up @@ -353,7 +353,7 @@ public String[] getReportLocations() {
String[] locations = apiUseScanPaths.split(ESCAPE_REGEX + LOCATION_DELIM);
ArrayList<String> locationList = new ArrayList<>(locations.length);
for (String location : locations) {
String values[] = location.split(ESCAPE_REGEX + STATE_DELIM);
String[] values = location.split(ESCAPE_REGEX + STATE_DELIM);
if (Boolean.parseBoolean(values[1])) {
locationList.add(values[0]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,7 @@ protected int getVisibility() {
* Returns all the child directories from the given directory
*/
File[] getDirectories(File file) {
File[] directories = file.listFiles((FileFilter) pathname -> pathname.isDirectory() && !pathname.isHidden());
return directories;
return file.listFiles((FileFilter) pathname -> pathname.isDirectory() && !pathname.isHidden());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ public static IProject[] getApiProjects() {
}
}
IProject[] projects = null;
if (temp.size() != 0) {
if (!temp.isEmpty()) {
projects = new IProject[temp.size()];
temp.toArray(projects);
}
Expand Down Expand Up @@ -417,7 +417,7 @@ public static IProject[] getApiProjectsMinSourceLevel(String sourcelevel) {
}
}
IProject[] projects = null;
if (temp.size() != 0) {
if (!temp.isEmpty()) {
projects = new IProject[temp.size()];
temp.toArray(projects);
}
Expand Down Expand Up @@ -1278,8 +1278,8 @@ private static IMember getMethod(IType type, String key) {
}
// Perhaps a constructor on an inner type?
IJavaElement parent = type.getParent();
if (parent instanceof IType) {
String parentTypeSig = Signature.createTypeSignature(((IType) parent).getFullyQualifiedName(), true);
if (parent instanceof IType parentType) {
String parentTypeSig = Signature.createTypeSignature(parentType.getFullyQualifiedName(), true);
if (Signatures.matches(parentTypeSig, parameterTypes[0])) {
IMethod constructor = type.getMethod(selector, Arrays.copyOfRange(parameterTypes, 1, parameterTypes.length));
try {
Expand Down Expand Up @@ -1374,7 +1374,7 @@ private static String generateBinarySignature(IMethod method) throws JavaModelEx
Map<String, String> lookup = new HashMap<>();
Stream.concat(Stream.of(typeTPs), Stream.of(methodTPs)).forEach(tp -> {
try {
String sigs[] = tp.getBoundsSignatures();
String[] sigs = tp.getBoundsSignatures();
lookup.put(tp.getElementName(), sigs.length == 1 ? sigs[0] : "Ljava.lang.Object;"); //$NON-NLS-1$
} catch (JavaModelException e) {
/* ignore */
Expand Down Expand Up @@ -1790,8 +1790,7 @@ public static InputStream getInputStreamFromString(String string) {
* @throws CoreException if unable to serialize the document
*/
public static String serializeDocument(Document document) throws CoreException {
try {
ByteArrayOutputStream s = new ByteArrayOutputStream();
try (ByteArrayOutputStream s = new ByteArrayOutputStream()) {
@SuppressWarnings("restriction")
TransformerFactory factory = org.eclipse.core.internal.runtime.XmlProcessorFactory
.createTransformerFactoryWithErrorOnDOCTYPE();
Expand All @@ -1802,7 +1801,7 @@ public static String serializeDocument(Document document) throws CoreException {
DOMSource source = new DOMSource(document);
StreamResult outputTarget = new StreamResult(s);
transformer.transform(source, outputTarget);
return s.toString(IApiCoreConstants.UTF_8);
return s.toString(StandardCharsets.UTF_8);
} catch (TransformerException | IOException e) {
throw new CoreException(Status.error("Unable to serialize XML document.", e)); //$NON-NLS-1$
}
Expand Down Expand Up @@ -2407,14 +2406,14 @@ public static IResource getResource(IProject project, IType type) {
* Default comparator that orders {@link IApiComponent} by their ID
*/
public static final Comparator<Object> componentsorter = (o1, o2) -> {
if (o1 instanceof IApiComponent && o2 instanceof IApiComponent) {
return ((IApiComponent) o1).getSymbolicName().compareTo(((IApiComponent) o2).getSymbolicName());
if (o1 instanceof IApiComponent comp1 && o2 instanceof IApiComponent comp2) {
return comp1.getSymbolicName().compareTo(comp2.getSymbolicName());
}
if (o1 instanceof SkippedComponent && o2 instanceof SkippedComponent) {
return ((SkippedComponent) o1).getComponentId().compareTo(((SkippedComponent) o2).getComponentId());
if (o1 instanceof SkippedComponent s1 && o2 instanceof SkippedComponent s2) {
return s1.getComponentId().compareTo(s2.getComponentId());
}
if (o1 instanceof String && o2 instanceof String) {
return ((String) o1).compareTo((String) o2);
if (o1 instanceof String s1 && o2 instanceof String s2) {
return s1.compareTo(s2);
}
return -1;
};
Expand Down Expand Up @@ -2501,8 +2500,8 @@ public static void collectRegexIds(String line, FilteredElements excludedElement
* Default comparator that orders {@link File}s by their name
*/
public static final Comparator<Object> filesorter = (o1, o2) -> {
if (o1 instanceof File && o2 instanceof File) {
return ((File) o1).getName().compareTo(((File) o2).getName());
if (o1 instanceof File f1 && o2 instanceof File f2) {
return f1.getName().compareTo(f2.getName());
}
return 0;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,7 @@ private Summary[] createAllSummaries(Map<String, IApiProblem[]> allProblems) {
}
List<Entry<String, IApiProblem[]>> allEntries = new ArrayList<>();
allEntries.addAll(entrySet);
Collections.sort(allEntries, (entry1, entry2) -> {
return entry1.getKey().compareTo(entry2.getKey());
});
Collections.sort(allEntries, (entry1, entry2) -> entry1.getKey().compareTo(entry2.getKey()));
Summary[] summaries = new Summary[size];
int i = 0;
for (Map.Entry<String, IApiProblem[]> entry : allEntries) {
Expand Down Expand Up @@ -255,7 +253,7 @@ private void dumpReport(Summary[] summaries, List<String> nonAPIBundleNames, Map

// Write out a list of components skipped because they aren't API Tools
// enabled
if (nonAPIBundleNames != null && nonAPIBundleNames.size() != 0) {
if (nonAPIBundleNames != null && !nonAPIBundleNames.isEmpty()) {
String contents = null;
try {
Document document = Util.newDocument();
Expand Down Expand Up @@ -570,9 +568,7 @@ private void insertAPIProblems(Element root, Document document, List<IApiProblem
root.appendChild(apiProblems);
Element element = null;
// sort the problem by type name
Collections.sort(problems, (p1, p2) -> {
return p1.getTypeName().compareTo(p2.getTypeName());
});
Collections.sort(problems, (p1, p2) -> p1.getTypeName().compareTo(p2.getTypeName()));
for (IApiProblem problem : problems) {
int severity = getSeverity(problem);
counter.addProblem(severity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,7 @@ private Summary[] createAllSummaries(Map<String, IApiProblem[]> allProblems) {
}
List<Map.Entry<String, IApiProblem[]>> allEntries = new ArrayList<>();
allEntries.addAll(entrySet);
Collections.sort(allEntries, (entry1, entry2) -> {
return entry1.getKey().compareTo(entry2.getKey());
});
Collections.sort(allEntries, (entry1, entry2) -> entry1.getKey().compareTo(entry2.getKey()));
Summary[] summaries = new Summary[size];
int i = 0;
for (Map.Entry<String, IApiProblem[]> entry : allEntries) {
Expand Down Expand Up @@ -365,9 +363,7 @@ private void insertAPIProblems(Element root, Document document, List<IApiProblem
root.appendChild(apiProblems);
Element element = null;
// sort the problem by type name
Collections.sort(problems, (p1, p2) -> {
return p1.getTypeName().compareTo(p2.getTypeName());
});
Collections.sort(problems, (p1, p2) -> p1.getTypeName().compareTo(p2.getTypeName()));
for (IApiProblem problem : problems) {
element = document.createElement(IApiXmlConstants.ELEMENT_API_PROBLEM);
element.setAttribute(IApiXmlConstants.ATTR_TYPE_NAME, String.valueOf(problem.getTypeName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
* The column Label and content Provider used to display information in context
* data TreeViewer. Two instances for label provider are created : one for key,
* one for values
*
* @see ContextDataPart
*/
public class BundleDataProvider extends ColumnLabelProvider {

Expand Down

0 comments on commit 5fbf4ad

Please sign in to comment.