Skip to content

Commit

Permalink
conversion to constant methods
Browse files Browse the repository at this point in the history
  • Loading branch information
asotona committed Dec 8, 2023
1 parent 10c700a commit ca38cd4
Show file tree
Hide file tree
Showing 13 changed files with 246 additions and 401 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public class AbstractBoundLocalVariable
extends AbstractElement {
protected final CodeImpl code;
protected final int offset;
private Utf8Entry nameEntry;
private Utf8Entry secondaryEntry;

public AbstractBoundLocalVariable(CodeImpl code, int offset) {
this.code = code;
Expand All @@ -44,20 +42,16 @@ protected int nameIndex() {
return code.classReader.readU2(offset + 4);
}

public Utf8Entry name() {
if (nameEntry == null)
nameEntry = (Utf8Entry) code.constantPool().entryByIndex(nameIndex());
return nameEntry;
public const Utf8Entry name() {
return (Utf8Entry) code.constantPool().entryByIndex(nameIndex());
}

protected int secondaryIndex() {
return code.classReader.readU2(offset + 6);
}

protected Utf8Entry secondaryEntry() {
if (secondaryEntry == null)
secondaryEntry = (Utf8Entry) code.constantPool().entryByIndex(secondaryIndex());
return secondaryEntry;
protected const Utf8Entry secondaryEntry() {
return (Utf8Entry) code.constantPool().entryByIndex(secondaryIndex());
}

public Label startScope() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,17 +374,13 @@ public String toString() {
public static final class BoundFieldInstruction
extends BoundInstruction implements FieldInstruction {

private FieldRefEntry fieldEntry;

public BoundFieldInstruction(Opcode op, CodeImpl code, int pos) {
super(op, op.sizeIfFixed(), code, pos);
}

@Override
public FieldRefEntry field() {
if (fieldEntry == null)
fieldEntry = code.classReader.readEntry(pos + 1, FieldRefEntry.class);
return fieldEntry;
public const FieldRefEntry field() {
return code.classReader.readEntry(pos + 1, FieldRefEntry.class);
}

@Override
Expand All @@ -404,17 +400,14 @@ public String toString() {

public static final class BoundInvokeInstruction
extends BoundInstruction implements InvokeInstruction {
MemberRefEntry methodEntry;

public BoundInvokeInstruction(Opcode op, CodeImpl code, int pos) {
super(op, op.sizeIfFixed(), code, pos);
}

@Override
public MemberRefEntry method() {
if (methodEntry == null)
methodEntry = code.classReader.readEntry(pos + 1, MemberRefEntry.class);
return methodEntry;
public const MemberRefEntry method() {
return code.classReader.readEntry(pos + 1, MemberRefEntry.class);
}

@Override
Expand Down Expand Up @@ -444,17 +437,14 @@ public String toString() {

public static final class BoundInvokeInterfaceInstruction
extends BoundInstruction implements InvokeInstruction {
InterfaceMethodRefEntry methodEntry;

public BoundInvokeInterfaceInstruction(Opcode op, CodeImpl code, int pos) {
super(op, op.sizeIfFixed(), code, pos);
}

@Override
public MemberRefEntry method() {
if (methodEntry == null)
methodEntry = code.classReader.readEntry(pos + 1, InterfaceMethodRefEntry.class);
return methodEntry;
public const MemberRefEntry method() {
return code.classReader.readEntry(pos + 1, InterfaceMethodRefEntry.class);
}

@Override
Expand Down Expand Up @@ -484,17 +474,14 @@ public String toString() {

public static final class BoundInvokeDynamicInstruction
extends BoundInstruction implements InvokeDynamicInstruction {
InvokeDynamicEntry indyEntry;

BoundInvokeDynamicInstruction(Opcode op, CodeImpl code, int pos) {
super(op, op.sizeIfFixed(), code, pos);
}

@Override
public InvokeDynamicEntry invokedynamic() {
if (indyEntry == null)
indyEntry = code.classReader.readEntry(pos + 1, InvokeDynamicEntry.class);
return indyEntry;
public const InvokeDynamicEntry invokedynamic() {
return code.classReader.readEntry(pos + 1, InvokeDynamicEntry.class);
}

@Override
Expand All @@ -514,17 +501,14 @@ public String toString() {

public static final class BoundNewObjectInstruction
extends BoundInstruction implements NewObjectInstruction {
ClassEntry classEntry;

BoundNewObjectInstruction(CodeImpl code, int pos) {
super(Opcode.NEW, Opcode.NEW.sizeIfFixed(), code, pos);
}

@Override
public ClassEntry className() {
if (classEntry == null)
classEntry = code.classReader.readClassEntry(pos + 1);
return classEntry;
public const ClassEntry className() {
return code.classReader.readClassEntry(pos + 1);
}

@Override
Expand Down Expand Up @@ -621,17 +605,14 @@ public String toString() {

public static final class BoundTypeCheckInstruction
extends BoundInstruction implements TypeCheckInstruction {
ClassEntry typeEntry;

public BoundTypeCheckInstruction(Opcode op, CodeImpl code, int pos) {
super(op, op.sizeIfFixed(), code, pos);
}

@Override
public ClassEntry type() {
if (typeEntry == null)
typeEntry = code.classReader.readClassEntry(pos + 1);
return typeEntry;
public const ClassEntry type() {
return code.classReader.readClassEntry(pos + 1);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public abstract sealed class AbstractUnboundModel<E extends ClassFileElement>
implements CompoundElement<E>, AttributedElement
permits BufferedCodeBuilder.Model, BufferedFieldBuilder.Model, BufferedMethodBuilder.Model {
private final List<E> elements;
private List<Attribute<?>> attributes;

public AbstractUnboundModel(List<E> elements) {
this.elements = elements;
Expand All @@ -60,12 +59,10 @@ public List<E> elementList() {
}

@Override
public List<Attribute<?>> attributes() {
if (attributes == null)
attributes = elements.stream()
.filter(e -> e instanceof Attribute)
.<Attribute<?>>map(e -> (Attribute<?>) e)
.toList();
return attributes;
public const List<Attribute<?>> attributes() {
return elements.stream()
.filter(e -> e instanceof Attribute)
.<Attribute<?>>map(e -> (Attribute<?>) e)
.toList();
}
}
Loading

0 comments on commit ca38cd4

Please sign in to comment.