8355335: Avoid pattern matching switches in core ClassFile API

Reviewed-by: asotona
This commit is contained in:
Chen Liang 2025-04-23 21:39:55 +00:00
parent 88e0b00a46
commit ffe6a4f9e1
6 changed files with 28 additions and 31 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -72,11 +72,10 @@ public final class BlockCodeBuilderImpl
}
private int topLocal(CodeBuilder parent) {
return switch (parent) {
case BlockCodeBuilderImpl b -> b.topLocal;
case ChainedCodeBuilder b -> b.terminal.curTopLocal();
case TerminalCodeBuilder b -> b.curTopLocal();
};
if (parent instanceof BlockCodeBuilderImpl bcb) {
return bcb.topLocal;
}
return findTerminal(parent).curTopLocal();
}
@Override

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -39,10 +39,8 @@ public final class ChainedClassBuilder
public ChainedClassBuilder(ClassBuilder downstream,
Consumer<ClassElement> consumer) {
this.consumer = consumer;
this.terminal = switch (downstream) {
case ChainedClassBuilder cb -> cb.terminal;
case DirectClassBuilder db -> db;
};
this.terminal = downstream instanceof ChainedClassBuilder ccb ?
ccb.terminal : (DirectClassBuilder) downstream;
}
@Override

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -38,10 +38,8 @@ public final class ChainedFieldBuilder implements FieldBuilder {
public ChainedFieldBuilder(FieldBuilder downstream,
Consumer<FieldElement> consumer) {
this.consumer = consumer;
this.terminal = switch (downstream) {
case ChainedFieldBuilder cb -> cb.terminal;
case TerminalFieldBuilder tb -> tb;
};
this.terminal = downstream instanceof ChainedFieldBuilder cfb ?
cfb.terminal : (TerminalFieldBuilder) downstream;
}
@Override

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -41,10 +41,8 @@ public final class ChainedMethodBuilder implements MethodBuilder {
public ChainedMethodBuilder(MethodBuilder downstream,
Consumer<MethodElement> consumer) {
this.consumer = consumer;
this.terminal = switch (downstream) {
case ChainedMethodBuilder cb -> cb.terminal;
case TerminalMethodBuilder tb -> tb;
};
this.terminal = downstream instanceof ChainedMethodBuilder cmb ?
cmb.terminal : (TerminalMethodBuilder) downstream;
}
@Override

View File

@ -264,14 +264,16 @@ public final class CodeImpl
//fallback to jump targets inflation without StackMapTableAttribute
for (int pos=codeStart; pos<codeEnd; ) {
var i = bcToInstruction(classReader.readU1(pos), pos);
switch (i) {
case BranchInstruction br -> br.target();
case DiscontinuedInstruction.JsrInstruction jsr -> jsr.target();
case LookupSwitchInstruction ls -> {
switch (i.opcode().kind()) {
case BRANCH -> ((BranchInstruction) i).target();
case DISCONTINUED_JSR -> ((DiscontinuedInstruction.JsrInstruction) i).target();
case LOOKUP_SWITCH -> {
var ls = (LookupSwitchInstruction) i;
ls.defaultTarget();
ls.cases();
}
case TableSwitchInstruction ts -> {
case TABLE_SWITCH -> {
var ts = (TableSwitchInstruction) i;
ts.defaultTarget();
ts.cases();
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -35,10 +35,12 @@ public abstract sealed class NonterminalCodeBuilder implements CodeBuilder
public NonterminalCodeBuilder(CodeBuilder parent) {
this.parent = parent;
this.terminal = switch (parent) {
case NonterminalCodeBuilder cb -> cb.terminal;
case TerminalCodeBuilder cb -> cb;
};
this.terminal = findTerminal(parent);
}
static TerminalCodeBuilder findTerminal(CodeBuilder cob) {
return cob instanceof NonterminalCodeBuilder ncb ?
ncb.terminal : (TerminalCodeBuilder) cob;
}
@Override