Skip to content

Commit 504e69c

Browse files
committed
Use pattern matching for the instanceof operator
1 parent 5b6d014 commit 504e69c

File tree

4 files changed

+18
-18
lines changed

4 files changed

+18
-18
lines changed

src/com/jeantessier/classreader/InvokeDynamicPrinter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,10 @@ public void visitInstruction(Instruction helper) {
246246
raiseIndent();
247247
indent().append("pc=").append(helper.getStart()).append(" : ").append(helper.getMnemonic());
248248
var indexedEntry = helper.getIndexedConstantPoolEntry();
249-
if (indexedEntry instanceof Dynamic_info) {
250-
append(" ").append(((Dynamic_info) indexedEntry).getName());
251-
} else if (indexedEntry instanceof InvokeDynamic_info) {
252-
append(" ").append(((InvokeDynamic_info) indexedEntry).getName());
249+
if (indexedEntry instanceof Dynamic_info dynamic_info) {
250+
append(" ").append(dynamic_info.getName());
251+
} else if (indexedEntry instanceof InvokeDynamic_info invokeDynamic_info) {
252+
append(" ").append(invokeDynamic_info.getName());
253253
}
254254
// TODO: Replace with type pattern matching in switch expression in Java 21
255255
// switch (helper.getIndexedConstantPoolEntry()) {

src/com/jeantessier/classreader/MetricsGatherer.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -482,12 +482,12 @@ public void visitSynthetic_attribute(Synthetic_attribute attribute) {
482482

483483
Object owner = attribute.getOwner();
484484

485-
if (owner instanceof Classfile) {
486-
syntheticClasses.add((Classfile) owner);
487-
} else if (owner instanceof Field_info) {
488-
syntheticFields.add((Field_info) owner);
489-
} else if (owner instanceof Method_info) {
490-
syntheticMethods.add((Method_info) owner);
485+
if (owner instanceof Classfile classfile) {
486+
syntheticClasses.add(classfile);
487+
} else if (owner instanceof Field_info field) {
488+
syntheticFields.add(field);
489+
} else if (owner instanceof Method_info method) {
490+
syntheticMethods.add(method);
491491
} else {
492492
Logger.getLogger(getClass()).warn("Synthetic attribute on unknown Visitable: " + owner.getClass().getName());
493493
}

src/com/jeantessier/classreader/TextPrinter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,10 @@ private Printer appendIndexedConstantPoolEntry(Instruction instruction) {
322322
break;
323323
case 0xba: // invokedynamic
324324
var indexedEntry = instruction.getIndexedConstantPoolEntry();
325-
if (indexedEntry instanceof Dynamic_info) {
326-
append(" ").append(((Dynamic_info) indexedEntry).getName());
327-
} else if (indexedEntry instanceof InvokeDynamic_info) {
328-
append(" ").append(((InvokeDynamic_info) indexedEntry).getName());
325+
if (indexedEntry instanceof Dynamic_info dynamic_info) {
326+
append(" ").append(dynamic_info.getName());
327+
} else if (indexedEntry instanceof InvokeDynamic_info invokeDynamic_info) {
328+
append(" ").append(invokeDynamic_info.getName());
329329
}
330330
// TODO: Replace with type pattern matching in switch expression in Java 21
331331
// switch (instruction.getIndexedConstantPoolEntry()) {

src/com/jeantessier/classreader/XMLPrinter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,10 +1044,10 @@ public void visitInstruction(Instruction instruction) {
10441044
append(" index=\"").append(instruction.getIndex()).append("\">");
10451045
append(instruction);
10461046
var indexedEntry = instruction.getIndexedConstantPoolEntry();
1047-
if (indexedEntry instanceof Dynamic_info) {
1048-
append(" ").append(((Dynamic_info) indexedEntry).getName());
1049-
} else if (indexedEntry instanceof InvokeDynamic_info) {
1050-
append(" ").append(((InvokeDynamic_info) indexedEntry).getName());
1047+
if (indexedEntry instanceof Dynamic_info dynamic_info) {
1048+
append(" ").append(dynamic_info.getName());
1049+
} else if (indexedEntry instanceof InvokeDynamic_info invokeDynamic_info) {
1050+
append(" ").append(invokeDynamic_info.getName());
10511051
}
10521052
// TODO: Replace with type pattern matching in switch expression in Java 21
10531053
// switch (instruction.getIndexedConstantPoolEntry()) {

0 commit comments

Comments
 (0)