Skip to content

Commit 5a3824b

Browse files
authored
Automatic cleanups in tests and in ui (eclipse-jdt#582)
* Use primitive boolean instead of the boxed value * Use String.replace instead of String.replaceAll when regexp is not needed * Use static inner classes * Use StringBuilder instead of StringBuffer * Use method references instead of lambdas * Replace String concetanation with StringBuilder * Fixing primitives to String conversion * Use pattern matching for instanceof
1 parent dde4425 commit 5a3824b

12 files changed

+33
-34
lines changed

org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/debug/ui/JavaUISourceLocator.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,10 @@ public void initializeFromMemento(String memento) throws CoreException {
322322
index = memento.indexOf("</findAll>", start); //$NON-NLS-1$
323323
if (index > 0) {
324324
String findAll = memento.substring(start, index);
325-
Boolean all = Boolean.valueOf(findAll);
325+
boolean all = Boolean.parseBoolean(findAll);
326326
String rest = memento.substring(index + 10);
327327
fJavaProject = (IJavaProject) JavaCore.create(handle);
328-
fIsFindAllSourceElements = all.booleanValue();
328+
fIsFindAllSourceElements = all;
329329
fSourceLocator.initializeFromMemento(rest);
330330
}
331331
}

org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/JavaDebugOptionsManager.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ private AskRecurrenceDialog(Shell parentShell) {
641641
MessageDialog.QUESTION, 0, //
642642
DebugUIMessages.JavaDebugOptionsManager_skip_buttonLabel, //
643643
DebugUIMessages.JavaDebugOptionsManager_suspend_buttonLabel);
644-
parentShell.getDisplay().syncExec(() -> open());
644+
parentShell.getDisplay().syncExec(this::open);
645645
}
646646

647647
@Override

org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/JavaDetailFormattersManager.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -546,28 +546,28 @@ private Expression getCompiledExpression(IJavaPrimitiveValue javaPM, IJavaDebugT
546546
private String primitiveSnippets(String snippet, String typeName, IJavaPrimitiveValue primitiveValue) {
547547
String modified = null;
548548
if (typeName.equals("float")) {
549-
String thisValue = Float.valueOf(primitiveValue.getFloatValue()).toString();
549+
String thisValue = Float.toString(primitiveValue.getFloatValue());
550550
modified = snippet.replace("this", thisValue);
551551
} else if (typeName.equals("int")) {
552-
String thisValue = Integer.valueOf(primitiveValue.getIntValue()).toString();
552+
String thisValue = Integer.toString(primitiveValue.getIntValue());
553553
modified = snippet.replace("this", thisValue);
554554
} else if (typeName.equals("byte")) {
555-
String thisValue = Byte.valueOf(primitiveValue.getByteValue()).toString();
555+
String thisValue = Byte.toString(primitiveValue.getByteValue());
556556
modified = snippet.replace("this", thisValue);
557557
} else if (typeName.equals("long")) {
558-
String thisValue = Long.valueOf(primitiveValue.getLongValue()).toString();
558+
String thisValue = Long.toString(primitiveValue.getLongValue());
559559
modified = snippet.replace("this", thisValue);
560560
} else if (typeName.equals("short")) {
561-
String thisValue = Short.valueOf(primitiveValue.getShortValue()).toString();
561+
String thisValue = Short.toString(primitiveValue.getShortValue());
562562
modified = snippet.replace("this", thisValue);
563563
} else if (typeName.equals("double")) {
564-
String thisValue = Double.valueOf(primitiveValue.getDoubleValue()).toString();
564+
String thisValue = Double.toString(primitiveValue.getDoubleValue());
565565
modified = snippet.replace("this", thisValue);
566566
} else if (typeName.equals("boolean")) {
567-
String thisValue = Boolean.valueOf(primitiveValue.getBooleanValue()).toString();
567+
String thisValue = Boolean.toString(primitiveValue.getBooleanValue());
568568
modified = snippet.replace("this", thisValue);
569569
} else if (typeName.equals("char")) {
570-
String thisValue = Character.valueOf(primitiveValue.getCharValue()).toString();
570+
String thisValue = Character.toString(primitiveValue.getCharValue());
571571
modified = snippet.replace("this", thisValue);
572572
} else {
573573
modified = snippet;

org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/LambdaUtils.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,12 @@ private static List<String> getArgumentTypeNames(IJavaElement parent) throws Cor
110110
String[] ptypes = method.getParameterTypes();
111111
for (String ps : ptypes) {
112112
@SuppressWarnings("restriction")
113-
String resolvedName = org.eclipse.jdt.internal.corext.util.JavaModelUtil.getResolvedTypeName(ps, type);
113+
StringBuilder resolvedName = new StringBuilder().append(org.eclipse.jdt.internal.corext.util.JavaModelUtil.getResolvedTypeName(ps, type));
114114
int arrayCount = Signature.getArrayCount(ps);
115115
for (int i = 0; i < arrayCount; i++) {
116-
resolvedName += "[]"; //$NON-NLS-1$
116+
resolvedName.append("[]"); //$NON-NLS-1$
117117
}
118-
psig.add(resolvedName);
118+
psig.add(resolvedName.toString());
119119
}
120120
return psig;
121121
}

org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/ObjectComparison.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ public static List<String> getInterfaces(String className) {
806806
if (interfaces.isEmpty()) {
807807
interfaces = List.of(Class.forName(className).getSuperclass().getInterfaces());
808808
}
809-
names = new ArrayList<>(interfaces.stream().map(e -> e.getCanonicalName()).toList());
809+
names = new ArrayList<>(interfaces.stream().map(Class::getCanonicalName).toList());
810810
names.add(Class.forName(className).getSuperclass().getSimpleName());
811811
} catch (Exception e) {
812812
names = new ArrayList<>();

org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/actions/CompareObjectsAction.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ private void displayMapResultsInDialogBox(Map<IJavaVariable, Object> result) thr
462462
*/
463463
@SuppressWarnings("nls")
464464
private String printListContents(String Selection, List<String> list, int displayLimit) {
465-
StringBuffer content = new StringBuffer();
465+
StringBuilder content = new StringBuilder();
466466
content.append("[");
467467
content.append(list.stream().limit(displayLimit).collect(Collectors.joining(",")));
468468
content.append("......]");

org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/actions/ToggleBreakpointAdapter.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1596,11 +1596,11 @@ private void toggleFieldOrMethodBreakpoints(IWorkbenchPart part, ISelection sele
15961596
* Additional diagnosis info for bug 528321
15971597
*/
15981598
private static void logBadAnnotation(SimpleMarkerAnnotation annotation, CoreException e) {
1599-
String message = "Editor annotation with non existing marker found: "; //$NON-NLS-1$
1600-
message += "text: " + annotation.getText(); //$NON-NLS-1$
1601-
message += ", type: " + annotation.getType(); //$NON-NLS-1$
1602-
message += ", " + annotation.getMarker(); //$NON-NLS-1$
1603-
JDIDebugUIPlugin.log(message, e);
1599+
StringBuilder message = new StringBuilder("Editor annotation with non existing marker found: "); //$NON-NLS-1$
1600+
message.append("text: ").append(annotation.getText()); //$NON-NLS-1$
1601+
message.append(", type: ").append(annotation.getType()); //$NON-NLS-1$
1602+
message.append(", ").append(annotation.getMarker()); //$NON-NLS-1$
1603+
JDIDebugUIPlugin.log(message.toString(), e);
16041604
}
16051605

16061606
/**

org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/console/JavaDebugStackTraceConsoleTracker.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ public class JavaDebugStackTraceConsoleTracker extends JavaConsoleTracker {
3030
public void matchFound(PatternMatchEvent event) {
3131
try {
3232
// add a hyperlink at "line: 123"
33-
addHyperlinkAtContent(event, line -> JavaDebugStackTraceHyperlink.extractLineText(line));
33+
addHyperlinkAtContent(event, JavaDebugStackTraceHyperlink::extractLineText);
3434
// add a hyperlink at the type
35-
addHyperlinkAtContent(event, line -> JavaDebugStackTraceHyperlink.extractTypeName(line));
35+
addHyperlinkAtContent(event, JavaDebugStackTraceHyperlink::extractTypeName);
3636
} catch (BadLocationException e) {
3737
}
3838
}

org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/console/JavaStackTraceHyperlink.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ public IStatus runInUIThread(IProgressMonitor monitor) {
226226
try {
227227
return processAmbiguousResults(matches, typeName, lineNumber, link);
228228
} catch(Exception e) {
229-
StringBuffer temp = new StringBuffer();
229+
StringBuilder temp = new StringBuilder();
230230
temp.append("Unable to parse \"" + link + "\" \n "); //$NON-NLS-1$ //$NON-NLS-2$
231231
temp.append(e.getClass().getSimpleName());
232232
exceptionHandler(temp.toString(), e);
@@ -281,7 +281,7 @@ public IStatus processAmbiguousResults(List<Object> matches, String typeName, in
281281
if (methodSignature == null) {
282282
return openClipboard(matches, line, typeName);
283283
}
284-
methodSignature = methodSignature.replaceAll(" ", ""); //$NON-NLS-1$//$NON-NLS-2$ ;
284+
methodSignature = methodSignature.replace(" ", ""); //$NON-NLS-1$//$NON-NLS-2$ ;
285285
String methodNameExtracted = methodSignature.substring(0, methodSignature.indexOf('('));
286286
for (Object obj : matches) {
287287
if (filterClasses(obj, methodSignature, methodNameExtracted, link)) {
@@ -439,7 +439,7 @@ private boolean extractFromResults(IType type, String methodSignature, String me
439439
return false;
440440
}
441441
String methodName = matcher.group();
442-
methodName = methodName.replaceAll(" ", ""); //$NON-NLS-1$//$NON-NLS-2$
442+
methodName = methodName.replace(" ", ""); //$NON-NLS-1$//$NON-NLS-2$
443443
pattern = Pattern.compile(METHOD_ARGUMENTS_REGEX);
444444
matcher = pattern.matcher(methodSignature);
445445
if (!matcher.find()) {

org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/jres/InstalledJREsBlock.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,7 @@ public void run(IProgressMonitor monitor) {
925925
}
926926

927927
if (locations.isEmpty()) {
928-
String messagePath = path.replaceAll("&", "&&"); // @see bug 29855 //$NON-NLS-1$//$NON-NLS-2$
928+
String messagePath = path.replace("&", "&&"); // @see bug 29855 //$NON-NLS-1$//$NON-NLS-2$
929929
MessageDialog.openInformation(getShell(), JREMessages.InstalledJREsBlock_12, NLS.bind(JREMessages.InstalledJREsBlock_13, new String[]{messagePath})); //
930930
} else {
931931
Iterator<IVMInstallType> iter2 = types.iterator();
@@ -1055,7 +1055,7 @@ protected void search(File directory, List<File> found, List<IVMInstallType> typ
10551055
}
10561056
File file = name == null ? directory : new File(directory, name);
10571057
monitor.subTask(NLS.bind(JREMessages.InstalledJREsBlock_14, new String[] { Integer.toString(found.size()),
1058-
file.toPath().normalize().toAbsolutePath().toString().replaceAll("&", "&&") })); // @see bug 29855 //$NON-NLS-1$ //$NON-NLS-2$
1058+
file.toPath().normalize().toAbsolutePath().toString().replace("&", "&&") })); // @see bug 29855 //$NON-NLS-1$ //$NON-NLS-2$
10591059
IVMInstallType[] vmTypes = JavaRuntime.getVMInstallTypes();
10601060
if (file.isDirectory()) {
10611061
if (ignore.add(file)) {

org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/launcher/DebugTypeSelectionDialog.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,13 @@ public Image getImage(Object element) {
8181
}
8282
@Override
8383
public String getText(Object element) {
84-
if(element instanceof IType) {
85-
IType type = (IType) element;
86-
String label = type.getTypeQualifiedName();
84+
if (element instanceof IType type) {
85+
StringBuilder label = new StringBuilder().append(type.getTypeQualifiedName());
8786
String container = getDeclaringContainerName(type);
8887
if(container != null && !"".equals(container)) { //$NON-NLS-1$
89-
label += " - "+container; //$NON-NLS-1$
88+
label.append(" - ").append(container); //$NON-NLS-1$
9089
}
91-
return label;
90+
return label.toString();
9291
}
9392
return null;
9493
}

org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/launcher/MainMethodSearchEngine.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242

4343
public class MainMethodSearchEngine{
4444

45-
private class MethodCollector extends SearchRequestor {
45+
private static class MethodCollector extends SearchRequestor {
4646
private final List<IType> fResult;
4747

4848
public MethodCollector() {

0 commit comments

Comments
 (0)