Add type to TypedClass

This commit is contained in:
ahmad 2024-05-11 11:43:27 +02:00
parent c7d756b083
commit e1a0540a06

View File

@ -25,6 +25,7 @@ public class TypedClass implements TypedNode {
private List<TypedField> typedFields = new ArrayList<>(); private List<TypedField> typedFields = new ArrayList<>();
private List<TypedMethod> typedMethods = new ArrayList<>(); private List<TypedMethod> typedMethods = new ArrayList<>();
private List<TypedConstructor> typedConstructors = new ArrayList<>(); private List<TypedConstructor> typedConstructors = new ArrayList<>();
private Type type;
public TypedClass(Map<String, Type> localVar, Class c) { public TypedClass(Map<String, Type> localVar, Class c) {
convertToTypedClass(localVar, c); convertToTypedClass(localVar, c);
@ -32,6 +33,7 @@ public class TypedClass implements TypedNode {
public void convertToTypedClass(Map<String, Type> localVar, Class c) { public void convertToTypedClass(Map<String, Type> localVar, Class c) {
className = c.classname(); className = c.classname();
type = Type.REFERENCE(className);
for (Declaration field : c.fieldDeclarations()) { for (Declaration field : c.fieldDeclarations()) {
typedFields.add(new TypedField(localVar, this, field)); typedFields.add(new TypedField(localVar, this, field));
@ -46,7 +48,7 @@ public class TypedClass implements TypedNode {
typedMethods.add(new TypedMethod(localVar, this, method)); typedMethods.add(new TypedMethod(localVar, this, method));
typedMethods.get(typedMethods.size() - 1).convertToTypedBlock(localVar, this, method); typedMethods.get(typedMethods.size() - 1).convertToTypedBlock(localVar, this, method);
} }
System.out.println("TypedClass: " + this.toString());
} }
@Override @Override
@ -74,6 +76,61 @@ public class TypedClass implements TypedNode {
return new TypedClass(local, c); return new TypedClass(local, c);
} }
public boolean isParameterWitNameInMethod(String parameterName) {
for (TypedMethod m : typedMethods) {
for (TypedParameter p : m.getTypedParameters()) {
if (p.getParaName().equals(parameterName)) {
return true;
}
}
}
return false;
}
public Type getParameterType(String parameterName) {
for (TypedMethod m : typedMethods) {
for (TypedParameter p : m.getTypedParameters()) {
if (p.getParaName().equals(parameterName)) {
return p.getType();
}
}
}
return null;
}
public boolean isThereField(String fieldName) {
for (TypedField f : typedFields) {
if (f.getVarName().equals(getFieldNameWithOutThis(fieldName))) {
return true;
}
}
return false;
}
private String getFieldNameWithOutThis(String fieldName) {
if(fieldName.startsWith("this.")){
fieldName = fieldName.substring(5);
}
return fieldName;
}
public Type getFieldType(String fieldName) {
for (TypedField f : typedFields) {
if (f.getVarName().equals(getFieldNameWithOutThis(fieldName))) {
return f.getType();
}
}
return null;
}
public Type getMethodType(String methodName) {
for (TypedMethod m : typedMethods) {
if (m.getName().equals(methodName)) {
return m.getReturnType();
}
}
return null;
}
public byte[] codeGen() { public byte[] codeGen() {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, className, null, "java/lang/Object", null); cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, className, null, "java/lang/Object", null);