Skip to content

Commit c927f6c

Browse files
Use instanceof pattern matching (#895)
use java 16 instaceof pattern matching by automatic code clean-up in jdt.debug.ui
1 parent 2c1fccd commit c927f6c

File tree

12 files changed

+35
-70
lines changed

12 files changed

+35
-70
lines changed

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

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -310,14 +310,12 @@ protected IStatus run(IProgressMonitor monitor) {
310310

311311
static IStatus doToggleLambdaEntryMethodBreakpoints(IWorkbenchPart part, ISelection selection, String lambdaMethodName, String lambdaMethodSignature, LambdaProperties lambdaProperties, IProgressMonitor monitor) throws CoreException {
312312
ITextEditor textEditor = getTextEditor(part);
313-
if (textEditor == null || !(selection instanceof ITextSelection)) {
313+
if (textEditor == null || !(selection instanceof ITextSelection textSelection)) {
314314
return Status.OK_STATUS;
315315
}
316-
ITextSelection textSelection = (ITextSelection) selection;
317316
IEditorInput editorInput = textEditor.getEditorInput();
318317
ITypeRoot root = getTypeRoot(editorInput);
319-
if (root instanceof ICompilationUnit) {
320-
ICompilationUnit unit = (ICompilationUnit) root;
318+
if (root instanceof ICompilationUnit unit) {
321319
synchronized (unit) {
322320
unit.reconcile(ICompilationUnit.NO_AST, false, null, null);
323321
}
@@ -345,14 +343,12 @@ static IStatus doToggleLambdaEntryMethodBreakpoints(IWorkbenchPart part, ISelect
345343

346344
static IStatus doToggleLambdaMethodBreakpoints(IWorkbenchPart part, ISelection selection, ValidBreakpointLocationLocator loc, IProgressMonitor monitor) throws CoreException {
347345
ITextEditor textEditor = getTextEditor(part);
348-
if (textEditor == null || !(selection instanceof ITextSelection)) {
346+
if (textEditor == null || !(selection instanceof ITextSelection textSelection)) {
349347
return Status.OK_STATUS;
350348
}
351-
ITextSelection textSelection = (ITextSelection) selection;
352349
IEditorInput editorInput = textEditor.getEditorInput();
353350
ITypeRoot root = getTypeRoot(editorInput);
354-
if (root instanceof ICompilationUnit) {
355-
ICompilationUnit unit = (ICompilationUnit) root;
351+
if (root instanceof ICompilationUnit unit) {
356352
synchronized (unit) {
357353
unit.reconcile(ICompilationUnit.NO_AST, false, null, null);
358354
}
@@ -859,8 +855,7 @@ public boolean canToggleMethodBreakpoints(IWorkbenchPart part, ISelection select
859855
if (isRemote(part, selection)) {
860856
return false;
861857
}
862-
if (selection instanceof IStructuredSelection) {
863-
IStructuredSelection ss = (IStructuredSelection) selection;
858+
if (selection instanceof IStructuredSelection ss) {
864859
return getMethods(ss, isInterface(selection, part)).length > 0;
865860
}
866861
return (selection instanceof ITextSelection) && isMethod((ITextSelection) selection, part);
@@ -870,11 +865,9 @@ public boolean canToggleMethodBreakpoints(IWorkbenchPart part, ISelection select
870865
* Returns whether the given part/selection is remote (viewing a repository)
871866
*/
872867
protected static boolean isRemote(IWorkbenchPart part, ISelection selection) {
873-
if (selection instanceof IStructuredSelection) {
874-
IStructuredSelection ss = (IStructuredSelection) selection;
868+
if (selection instanceof IStructuredSelection ss) {
875869
Object element = ss.getFirstElement();
876-
if(element instanceof IMember) {
877-
IMember member = (IMember) element;
870+
if(element instanceof IMember member) {
878871
return !member.getJavaProject().getProject().exists();
879872
}
880873
}
@@ -916,8 +909,7 @@ protected static IMethod[] getMethods(IStructuredSelection selection, boolean is
916909
while (iterator.hasNext()) {
917910
Object thing = iterator.next();
918911
try {
919-
if (thing instanceof IMethod) {
920-
IMethod method = (IMethod) thing;
912+
if (thing instanceof IMethod method) {
921913
if(isInterace){
922914
if (Flags.isDefaultMethod(method.getFlags()) || Flags.isStatic(method.getFlags())) {
923915
methods.add(method);
@@ -947,8 +939,7 @@ protected static IMethod[] getInterfaceMethods(IStructuredSelection selection) {
947939
while (iterator.hasNext()) {
948940
Object thing = iterator.next();
949941
try {
950-
if (thing instanceof IMethod) {
951-
IMethod method = (IMethod) thing;
942+
if (thing instanceof IMethod method) {
952943
if (Flags.isDefaultMethod(method.getFlags())) {
953944
methods.add(method);
954945
}
@@ -1044,8 +1035,7 @@ private static boolean isInterface(ISelection selection, IWorkbenchPart part) {
10441035
IType type = member.getDeclaringType();
10451036
return type != null && type.isInterface();
10461037
}
1047-
else if(obj instanceof IJavaFieldVariable) {
1048-
IJavaFieldVariable var = (IJavaFieldVariable) obj;
1038+
else if(obj instanceof IJavaFieldVariable var) {
10491039
IType type = JavaDebugUtils.resolveType(var.getDeclaringType());
10501040
return type != null && type.isInterface();
10511041
}
@@ -1099,8 +1089,7 @@ private static boolean isFields(IStructuredSelection selection) {
10991089
if (thing instanceof IField) {
11001090
int flags = ((IField) thing).getFlags();
11011091
return !Flags.isFinal(flags);
1102-
} else if (thing instanceof IJavaFieldVariable) {
1103-
IJavaFieldVariable fv = (IJavaFieldVariable) thing;
1092+
} else if (thing instanceof IJavaFieldVariable fv) {
11041093
return !fv.isFinal();
11051094
}
11061095
}
@@ -1167,8 +1156,7 @@ static IStatus doToggleWatchpoints(IWorkbenchPart part, ISelection finalSelectio
11671156
fin = javaField.getConstant() != null; // watch point is allowed if no constant value
11681157
}
11691158
allowed = !fin;
1170-
} else if (element instanceof IJavaFieldVariable) {
1171-
IJavaFieldVariable var = (IJavaFieldVariable) element;
1159+
} else if (element instanceof IJavaFieldVariable var) {
11721160
typeName = var.getDeclaringType().getName();
11731161
fieldName = var.getName();
11741162
boolean fin = var.isFinal();
@@ -1233,8 +1221,7 @@ private static IJavaWatchpoint getWatchpoint(String typeName, String fieldName)
12331221
IBreakpoint[] breakpoints = breakpointManager.getBreakpoints(JDIDebugModel.getPluginIdentifier());
12341222
for (int i = 0; i < breakpoints.length; i++) {
12351223
IBreakpoint breakpoint = breakpoints[i];
1236-
if (breakpoint instanceof IJavaWatchpoint) {
1237-
IJavaWatchpoint watchpoint = (IJavaWatchpoint) breakpoint;
1224+
if (breakpoint instanceof IJavaWatchpoint watchpoint) {
12381225
if (typeName.equals(watchpoint.getTypeName()) && fieldName.equals(watchpoint.getFieldName())) {
12391226
return watchpoint;
12401227
}
@@ -1448,8 +1435,7 @@ public boolean canToggleWatchpoints(IWorkbenchPart part, ISelection selection) {
14481435
if (isRemote(part, selection)) {
14491436
return false;
14501437
}
1451-
if (selection instanceof IStructuredSelection) {
1452-
IStructuredSelection ss = (IStructuredSelection) selection;
1438+
if (selection instanceof IStructuredSelection ss) {
14531439
return isFields(ss);
14541440
}
14551441
return (selection instanceof ITextSelection) && isField((ITextSelection) selection, part);
@@ -1466,10 +1452,9 @@ public boolean canToggleWatchpoints(IWorkbenchPart part, ISelection selection) {
14661452
*/
14671453
protected static ISelection translateToMembers(IWorkbenchPart part, ISelection selection) throws CoreException {
14681454
ITextEditor textEditor = getTextEditor(part);
1469-
if (textEditor == null || !(selection instanceof ITextSelection)) {
1455+
if (textEditor == null || !(selection instanceof ITextSelection textSelection)) {
14701456
return selection;
14711457
}
1472-
ITextSelection textSelection = (ITextSelection) selection;
14731458
IEditorInput editorInput = textEditor.getEditorInput();
14741459
IDocumentProvider documentProvider = textEditor.getDocumentProvider();
14751460
if (documentProvider == null) {
@@ -1489,8 +1474,7 @@ protected static ISelection translateToMembers(IWorkbenchPart part, ISelection s
14891474
}
14901475
IMember m = null;
14911476
ITypeRoot root = getTypeRoot(editorInput);
1492-
if (root instanceof ICompilationUnit) {
1493-
ICompilationUnit unit = (ICompilationUnit) root;
1477+
if (root instanceof ICompilationUnit unit) {
14941478
synchronized (unit) {
14951479
unit.reconcile(ICompilationUnit.NO_AST, false, null, null);
14961480
}
@@ -1502,8 +1486,7 @@ protected static ISelection translateToMembers(IWorkbenchPart part, ISelection s
15021486
}
15031487
// Class breakpoint should be created if the offset was at the record component in the record definition
15041488
if (m != null && m.getParent() instanceof IType && ((IType) m.getParent()).isRecord()) {
1505-
if (m instanceof IField) {
1506-
IField field = (IField)m;
1489+
if (m instanceof IField field) {
15071490
if (field.isRecordComponent()) {
15081491
m = (IMember) field.getParent();
15091492
}
@@ -1597,10 +1580,9 @@ public static IJavaLineBreakpoint findExistingBreakpoint(ITextEditor editor, ITe
15971580
Iterator<Annotation> iterator = annotationModel.getAnnotationIterator();
15981581
while (iterator.hasNext()) {
15991582
Object object = iterator.next();
1600-
if (!(object instanceof SimpleMarkerAnnotation)) {
1583+
if (!(object instanceof SimpleMarkerAnnotation markerAnnotation)) {
16011584
continue;
16021585
}
1603-
SimpleMarkerAnnotation markerAnnotation = (SimpleMarkerAnnotation) object;
16041586
IMarker marker = markerAnnotation.getMarker();
16051587
try {
16061588
if (marker.isSubtypeOf(IBreakpoint.BREAKPOINT_MARKER)) {
@@ -1623,10 +1605,9 @@ public static IJavaLineBreakpoint findExistingBreakpoint(ITextEditor editor, ITe
16231605
}
16241606

16251607
private void toggleFieldOrMethodBreakpoints(IWorkbenchPart part, ISelection selection) throws CoreException {
1626-
if (!(selection instanceof ITextSelection)) {
1608+
if (!(selection instanceof ITextSelection ts)) {
16271609
return;
16281610
}
1629-
ITextSelection ts = (ITextSelection) selection;
16301611
ITextEditor editor = getTextEditor(part);
16311612
if (editor == null) {
16321613
return;

org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/breakpoints/MethodBreakpointEditor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ protected void setBreakpoint(IJavaBreakpoint breakpoint) throws CoreException {
8181
public void doSave() throws CoreException {
8282
super.doSave();
8383
IJavaBreakpoint breakpoint = getBreakpoint();
84-
if (breakpoint instanceof IJavaMethodBreakpoint) {
85-
IJavaMethodBreakpoint watchpoint = (IJavaMethodBreakpoint) breakpoint;
84+
if (breakpoint instanceof IJavaMethodBreakpoint watchpoint) {
8685
watchpoint.setEntry(fEntry.getSelection());
8786
watchpoint.setExit(fExit.getSelection());
8887
}

org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/breakpoints/WatchpointEditor.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ public Control createControl(Composite parent) {
5757
@Override
5858
protected void setBreakpoint(IJavaBreakpoint breakpoint) throws CoreException {
5959
super.setBreakpoint(breakpoint);
60-
if (breakpoint instanceof IJavaWatchpoint) {
61-
IJavaWatchpoint watchpoint = (IJavaWatchpoint) breakpoint;
60+
if (breakpoint instanceof IJavaWatchpoint watchpoint) {
6261
fAccess.setEnabled(true);
6362
fModification.setEnabled(true);
6463
fAccess.setSelection(watchpoint.isAccess());
@@ -78,8 +77,7 @@ protected void setBreakpoint(IJavaBreakpoint breakpoint) throws CoreException {
7877
public void doSave() throws CoreException {
7978
super.doSave();
8079
IJavaBreakpoint breakpoint = getBreakpoint();
81-
if (breakpoint instanceof IJavaWatchpoint) {
82-
IJavaWatchpoint watchpoint = (IJavaWatchpoint) breakpoint;
80+
if (breakpoint instanceof IJavaWatchpoint watchpoint) {
8381
watchpoint.setAccess(fAccess.getSelection());
8482
watchpoint.setModification(fModification.getSelection());
8583
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,7 @@ else if (segs >= 2) {
266266
private void detectJavadocLocation() {
267267
if (fAutoDetectAttributes) {
268268
IVMInstallType type = fVM.getVMInstallType();
269-
if (type instanceof AbstractVMInstallType) {
270-
AbstractVMInstallType atype = (AbstractVMInstallType)type;
269+
if (type instanceof AbstractVMInstallType atype) {
271270
fJavadocLocation = atype.getDefaultJavadocLocation(getInstallLocation());
272271
String args = atype.getDefaultVMArguments(getInstallLocation());
273272
if (args != null) {

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,7 @@ private void edit(IStructuredSelection selection, int type) {
337337
if(obj instanceof LibraryStandin) {
338338
standin = (LibraryStandin) obj;
339339
}
340-
else if(obj instanceof SubElement){
341-
SubElement sub = (SubElement)obj;
340+
else if(obj instanceof SubElement sub){
342341
standin = sub.getParent();
343342
}
344343
if(standin != null) {
@@ -419,9 +418,8 @@ private void updateButtons() {
419418
for (Iterator<?> iter= selection.iterator(); iter.hasNext();) {
420419
Object element= iter.next();
421420
Object lib;
422-
if (element instanceof SubElement) {
421+
if (element instanceof SubElement subElement) {
423422
allRoots = false;
424-
SubElement subElement= (SubElement)element;
425423
lib = (subElement).getParent().toLibraryLocation();
426424
switch (subElement.getType()) {
427425
case SubElement.JAVADOC_URL:

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ private class TypeLabelProvider extends LabelProvider {
6868
*/
6969
@Override
7070
public String getText(Object element) {
71-
if (element instanceof IVMInstallType) {
72-
IVMInstallType type = (IVMInstallType) element;
71+
if (element instanceof IVMInstallType type) {
7372
return type.getName();
7473
}
7574
return super.getText(element);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ protected void createMainTypeExtensions(Composite parent) {
111111
protected void initializeMainTypeAndName(IJavaElement javaElement, ILaunchConfigurationWorkingCopy config) {
112112
String name = null;
113113
String moduleName = EMPTY_STRING;
114-
if (javaElement instanceof IMember) {
115-
IMember member = (IMember)javaElement;
114+
if (javaElement instanceof IMember member) {
116115
if (member.isBinary()) {
117116
javaElement = member.getClassFile();
118117
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,9 @@ public class SourceElementLabelProvider extends LabelProvider implements ILabelP
3838
public String getText(Object element) {
3939
if (element instanceof IJavaElement) {
4040
return ((IJavaElement)element).getElementName();
41-
} else if (element instanceof ZipEntryStorage) {
42-
ZipEntryStorage storage = (ZipEntryStorage)element;
41+
} else if (element instanceof ZipEntryStorage storage) {
4342
return storage.getZipEntry().getName();
44-
} else if (element instanceof LocalFileStorage) {
45-
LocalFileStorage storage = (LocalFileStorage)element;
43+
} else if (element instanceof LocalFileStorage storage) {
4644
return storage.getName();
4745
}
4846
return super.getText(element);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ public String getText(Object element) {
5858
}
5959
// internal
6060
return res.getName();
61-
} else if (element instanceof LocalFileStorage) {
62-
LocalFileStorage storage = (LocalFileStorage)element;
61+
} else if (element instanceof LocalFileStorage storage) {
6362
File extFile = storage.getFile();
6463
return extFile.getParent();
6564
}

org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/monitors/ThreadMonitorManager.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ public void handleDebugEvents(DebugEvent[] events) {
8484
Object eventSource= debugEvent.getSource();
8585
int eventKind= debugEvent.getKind();
8686
IJavaThread javaThread = null;
87-
if (eventSource instanceof IAdaptable) {
88-
IAdaptable adaptable = (IAdaptable)eventSource;
87+
if (eventSource instanceof IAdaptable adaptable) {
8988
javaThread = adaptable.getAdapter(IJavaThread.class);
9089
if (javaThread != null) {
9190
switch (eventKind) {

0 commit comments

Comments
 (0)