minor reshuffling

This commit is contained in:
Sander Hautvast 2020-11-10 09:22:44 +01:00
parent 8eda63261b
commit d17883701d
4 changed files with 58 additions and 59 deletions

View file

@ -28,14 +28,14 @@ public class CompiledClass {
return thisClass.getIndex(); return thisClass.getIndex();
} }
public ClassEntry superClass() { public ClassEntry addSuperClass() {
if (superClass == null) { if (superClass == null) {
superClass = createClassEntry(beeClass.getSuperClass().getName()); superClass = createClassEntry(beeClass.getSuperClass().getName());
} }
return superClass; return superClass;
} }
public ClassEntry thisClass() { public ClassEntry addThisClass() {
if (thisClass == null) { if (thisClass == null) {
thisClass = createClassEntry(internalName(beeClass.getName())); thisClass = createClassEntry(internalName(beeClass.getName()));
} }
@ -58,11 +58,7 @@ public class CompiledClass {
constantTree.add(interfaceEntry); constantTree.add(interfaceEntry);
} }
public void setThisClass() { public void addInterfaces() {
constantTree.add(thisClass());
}
public void setInterfaces() {
beeClass.getInterfaces().forEach(interfase -> { beeClass.getInterfaces().forEach(interfase -> {
ClassEntry interfaceEntry = new ClassEntry(new Utf8Entry(internalName(interfase.getName()))); ClassEntry interfaceEntry = new ClassEntry(new Utf8Entry(internalName(interfase.getName())));
addInterface(interfaceEntry); addInterface(interfaceEntry);
@ -70,6 +66,12 @@ public class CompiledClass {
}); });
} }
public void addFields() {
beeClass.getFields().forEach(field -> {
// TODO
});
}
private ClassEntry createClassEntry(String name) { private ClassEntry createClassEntry(String name) {
return new ClassEntry(new Utf8Entry(internalName(name))); return new ClassEntry(new Utf8Entry(internalName(name)));
} }

View file

@ -27,11 +27,12 @@ public class Compiler {
public CompiledClass compile(BeeClass beeClass) { public CompiledClass compile(BeeClass beeClass) {
compiledClass = new CompiledClass(beeClass); compiledClass = new CompiledClass(beeClass);
beeClass.getConstructors().forEach(this::updateConstantPool); beeClass.getConstructors().forEach(this::updateConstantPool);
// TODO update constantTree for fields ? // TODO update constant pool for fields ?
// TODO update constantTree for methods // TODO update constant pool for methods
compiledClass.setThisClass(); compiledClass.addThisClass();
compiledClass.setInterfaces(); compiledClass.addInterfaces();
compiledClass.addFields();
return compiledClass; return compiledClass;
} }
@ -46,19 +47,19 @@ public class Compiler {
*/ */
private void updateConstantPool(CodeLine codeline) { private void updateConstantPool(CodeLine codeline) {
if (codeline.hasMethod()) { if (codeline.hasMethod()) {
addMethod(codeline); addMethodToConstantPool(codeline);
} }
if (codeline.hasField()) { if (codeline.hasField()) {
addField(codeline); addFieldToConstantPool(codeline);
} }
} }
private void addMethod(CodeLine codeline) { private void addMethodToConstantPool(CodeLine codeline) {
compiledClass.addConstantPoolEntry(new MethodRefEntry(getOrCreateClassEntry(codeline), createMethodNameAndType(codeline))); compiledClass.addConstantPoolEntry(new MethodRefEntry(getOrCreateClassEntry(codeline), createMethodNameAndType(codeline)));
} }
private void addField(CodeLine codeline) { private void addFieldToConstantPool(CodeLine codeline) {
compiledClass.addConstantPoolEntry(new FieldRefEntry(getOrCreateClassEntry(codeline), createFieldNameAndType(codeline))); compiledClass.addConstantPoolEntry(new FieldRefEntry(getOrCreateClassEntry(codeline), createFieldNameAndType(codeline)));
} }
@ -72,9 +73,10 @@ public class Compiler {
private ClassEntry getOrCreateClassEntry(CodeLine codeline) { private ClassEntry getOrCreateClassEntry(CodeLine codeline) {
if (codeline.getRef() == Ref.SUPER) { if (codeline.getRef() == Ref.SUPER) {
return compiledClass.superClass(); return compiledClass.addSuperClass();
} else if (codeline.getRef() == Ref.THIS) { } else if (codeline.getRef() == Ref.THIS) {
return compiledClass.thisClass(); return compiledClass.addThisClass();
} }
//TODO other cases //TODO other cases
throw new RuntimeException("shouldn't be here"); throw new RuntimeException("shouldn't be here");

View file

@ -62,38 +62,4 @@ public class CompilerTests {
Utf8Entry type = (Utf8Entry) child5; Utf8Entry type = (Utf8Entry) child5;
assertEquals("()V", type.getUtf8()); assertEquals("()V", type.getUtf8());
} }
private BeeClass createClassWithIntField() {
BeeField intField = BeeField.builder()
.withAccessFlags(FieldAccessFlag.PRIVATE)
.withType(int.class)
.withName("intField")
.build();
BeeParameter intValueParameter = BeeParameter.create(int.class, "intValue");
BeeConstructor constructor = BeeConstructor.builder()
.withAccessFlags(MethodAccessFlag.PUBLIC)
.withFormalParameters(intValueParameter)
.withCode(
line(0, LOAD, Ref.THIS),
line(1, INVOKE, Ref.SUPER, "<init>", "()"),
line(2, LOAD, Ref.THIS),
line(3, LOAD, intValueParameter),
line(4, PUT, intField),
line(5, RETURN))
.build();
return BeeClass.builder()
.withClassFileVersion(Version.V14)
.withPackage("nl.sander.beejava.test")
.withAccessFlags(PUBLIC)
.withSimpleName("IntBean")
.withSuperClass(Object.class)
.withFields(intField)
.withConstructors(constructor)
.build();
}
} }

View file

@ -1,9 +1,7 @@
package nl.sander.beejava; package nl.sander.beejava;
import nl.sander.beejava.api.BeeClass; import nl.sander.beejava.api.*;
import nl.sander.beejava.api.BeeConstructor; import nl.sander.beejava.flags.FieldAccessFlag;
import nl.sander.beejava.api.Ref;
import nl.sander.beejava.api.Version;
import nl.sander.beejava.flags.MethodAccessFlag; import nl.sander.beejava.flags.MethodAccessFlag;
import java.io.Serializable; import java.io.Serializable;
@ -20,7 +18,7 @@ public class TestData {
.withAccessFlags(PUBLIC) .withAccessFlags(PUBLIC)
.withSimpleName("EmptyBean") .withSimpleName("EmptyBean")
.withSuperClass(Object.class) // Not mandatory, like in java sourcecode .withSuperClass(Object.class) // Not mandatory, like in java sourcecode
.withConstructors(getBeeConstructor()) // There's no default constructor in beejava. The user must always add them .withConstructors(createConstructor()) // There's no default constructor in beejava. The user must always add them
.build(); .build();
} }
@ -32,18 +30,49 @@ public class TestData {
.withSimpleName("EmptyBean") .withSimpleName("EmptyBean")
.withSuperClass(Object.class) // Not mandatory, like in java sourcecode .withSuperClass(Object.class) // Not mandatory, like in java sourcecode
.withInterfaces(Serializable.class) .withInterfaces(Serializable.class)
.withConstructors(getBeeConstructor()) // There's no default constructor in beejava. The user must always add them .withConstructors(createConstructor()) // There's no default constructor in beejava. The user must always add them
.build(); .build();
} }
private static BeeConstructor getBeeConstructor() { public static BeeClass createClassWithIntField() {
BeeField intField = BeeField.builder()
.withAccessFlags(FieldAccessFlag.PRIVATE)
.withType(int.class)
.withName("intField")
.build();
BeeParameter intValueParameter = BeeParameter.create(int.class, "intValue");
BeeConstructor constructor = BeeConstructor.builder() BeeConstructor constructor = BeeConstructor.builder()
.withAccessFlags(MethodAccessFlag.PUBLIC)
.withFormalParameters(intValueParameter)
.withCode(
line(0, LOAD, Ref.THIS),
line(1, INVOKE, Ref.SUPER, "<init>", "()"),
line(2, LOAD, Ref.THIS),
line(3, LOAD, intValueParameter),
line(4, PUT, intField),
line(5, RETURN))
.build();
return BeeClass.builder()
.withClassFileVersion(Version.V14)
.withPackage("nl.sander.beejava.test")
.withAccessFlags(PUBLIC)
.withSimpleName("IntBean")
.withSuperClass(Object.class)
.withFields(intField)
.withConstructors(constructor)
.build();
}
private static BeeConstructor createConstructor() {
return BeeConstructor.builder()
.withAccessFlags(MethodAccessFlag.PUBLIC) .withAccessFlags(MethodAccessFlag.PUBLIC)
.withCode( .withCode(
line(0, LOAD, Ref.THIS), line(0, LOAD, Ref.THIS),
line(1, INVOKE, Ref.SUPER, "<init>", "()"), line(1, INVOKE, Ref.SUPER, "<init>", "()"),
line(5, RETURN)) line(5, RETURN))
.build(); .build();
return constructor;
} }
} }