Skip to content

Commit ffe6a4f

Browse files
committed
8355335: Avoid pattern matching switches in core ClassFile API
Reviewed-by: asotona
1 parent 88e0b00 commit ffe6a4f

File tree

6 files changed

+28
-31
lines changed

6 files changed

+28
-31
lines changed

src/java.base/share/classes/jdk/internal/classfile/impl/BlockCodeBuilderImpl.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -72,11 +72,10 @@ public boolean isEmpty() {
7272
}
7373

7474
private int topLocal(CodeBuilder parent) {
75-
return switch (parent) {
76-
case BlockCodeBuilderImpl b -> b.topLocal;
77-
case ChainedCodeBuilder b -> b.terminal.curTopLocal();
78-
case TerminalCodeBuilder b -> b.curTopLocal();
79-
};
75+
if (parent instanceof BlockCodeBuilderImpl bcb) {
76+
return bcb.topLocal;
77+
}
78+
return findTerminal(parent).curTopLocal();
8079
}
8180

8281
@Override

src/java.base/share/classes/jdk/internal/classfile/impl/ChainedClassBuilder.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -39,10 +39,8 @@ public final class ChainedClassBuilder
3939
public ChainedClassBuilder(ClassBuilder downstream,
4040
Consumer<ClassElement> consumer) {
4141
this.consumer = consumer;
42-
this.terminal = switch (downstream) {
43-
case ChainedClassBuilder cb -> cb.terminal;
44-
case DirectClassBuilder db -> db;
45-
};
42+
this.terminal = downstream instanceof ChainedClassBuilder ccb ?
43+
ccb.terminal : (DirectClassBuilder) downstream;
4644
}
4745

4846
@Override

src/java.base/share/classes/jdk/internal/classfile/impl/ChainedFieldBuilder.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -38,10 +38,8 @@ public final class ChainedFieldBuilder implements FieldBuilder {
3838
public ChainedFieldBuilder(FieldBuilder downstream,
3939
Consumer<FieldElement> consumer) {
4040
this.consumer = consumer;
41-
this.terminal = switch (downstream) {
42-
case ChainedFieldBuilder cb -> cb.terminal;
43-
case TerminalFieldBuilder tb -> tb;
44-
};
41+
this.terminal = downstream instanceof ChainedFieldBuilder cfb ?
42+
cfb.terminal : (TerminalFieldBuilder) downstream;
4543
}
4644

4745
@Override

src/java.base/share/classes/jdk/internal/classfile/impl/ChainedMethodBuilder.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -41,10 +41,8 @@ public final class ChainedMethodBuilder implements MethodBuilder {
4141
public ChainedMethodBuilder(MethodBuilder downstream,
4242
Consumer<MethodElement> consumer) {
4343
this.consumer = consumer;
44-
this.terminal = switch (downstream) {
45-
case ChainedMethodBuilder cb -> cb.terminal;
46-
case TerminalMethodBuilder tb -> tb;
47-
};
44+
this.terminal = downstream instanceof ChainedMethodBuilder cmb ?
45+
cmb.terminal : (TerminalMethodBuilder) downstream;
4846
}
4947

5048
@Override

src/java.base/share/classes/jdk/internal/classfile/impl/CodeImpl.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,14 +264,16 @@ private void inflateJumpTargets() {
264264
//fallback to jump targets inflation without StackMapTableAttribute
265265
for (int pos=codeStart; pos<codeEnd; ) {
266266
var i = bcToInstruction(classReader.readU1(pos), pos);
267-
switch (i) {
268-
case BranchInstruction br -> br.target();
269-
case DiscontinuedInstruction.JsrInstruction jsr -> jsr.target();
270-
case LookupSwitchInstruction ls -> {
267+
switch (i.opcode().kind()) {
268+
case BRANCH -> ((BranchInstruction) i).target();
269+
case DISCONTINUED_JSR -> ((DiscontinuedInstruction.JsrInstruction) i).target();
270+
case LOOKUP_SWITCH -> {
271+
var ls = (LookupSwitchInstruction) i;
271272
ls.defaultTarget();
272273
ls.cases();
273274
}
274-
case TableSwitchInstruction ts -> {
275+
case TABLE_SWITCH -> {
276+
var ts = (TableSwitchInstruction) i;
275277
ts.defaultTarget();
276278
ts.cases();
277279
}

src/java.base/share/classes/jdk/internal/classfile/impl/NonterminalCodeBuilder.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -35,10 +35,12 @@ public abstract sealed class NonterminalCodeBuilder implements CodeBuilder
3535

3636
public NonterminalCodeBuilder(CodeBuilder parent) {
3737
this.parent = parent;
38-
this.terminal = switch (parent) {
39-
case NonterminalCodeBuilder cb -> cb.terminal;
40-
case TerminalCodeBuilder cb -> cb;
41-
};
38+
this.terminal = findTerminal(parent);
39+
}
40+
41+
static TerminalCodeBuilder findTerminal(CodeBuilder cob) {
42+
return cob instanceof NonterminalCodeBuilder ncb ?
43+
ncb.terminal : (TerminalCodeBuilder) cob;
4244
}
4345

4446
@Override

0 commit comments

Comments
 (0)