Genericklassen für Superklassen, Fieldtypes und Methodenparameter werden generiert

This commit is contained in:
Enrico Schrödter 2015-10-20 15:58:39 +02:00
parent bafffe26ad
commit 632c2ca508
3 changed files with 60 additions and 86 deletions

View File

@ -116,7 +116,7 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I
ByteCodeResult code = new ByteCodeResult(_cg); ByteCodeResult code = new ByteCodeResult(_cg);
results.add(code); results.add(code);
results.addAll(getGenericClasses(_cg)); results.addAll(getGenericClasses());
return results; return results;
} }
@ -1032,75 +1032,61 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I
return false; return false;
} }
private Collection<? extends ByteCodeResult> getGenericClasses(ClassGenerator _cg) { private Collection<? extends ByteCodeResult> getGenericClasses() {
Collection<ByteCodeResult> results = new Menge<>(); Collection<ByteCodeResult> results = new Menge<>();
//Super Klasse for(Field field : this.fielddecl){
String name = getType().getCombinedType(_cg); Type type = field.getType();
//Der Type des Feldes
//Class superClass = new Class("java/util/Vector",-1); if(type instanceof RefType){
//Type superClassType = superClass.getType(); RefType refType = (RefType) type;
//_cg = new ClassGenerator(name, superClassType, name + ".java", Constants.ACC_PUBLIC , new String[] { }, new TypeinferenceResultSet(null, null, null)); if(!refType.getCombinedType(null).equals(refType.get_Name().replace(".", "%"))){
results.addAll(generateGenericClass(refType.getCombinedType(null), new Class("java/util/Vector",-1)));
//Class generatedClass = new Class(name, 0); }
}
//Block konstruktorBlock = new Block();
//konstruktorBlock.statements.add(new SuperCall(konstruktorBlock)); if(field instanceof Method){
//Constructor standardKonstruktor = new Constructor(Method.createEmptyMethod(konstruktorBlock, name, superClass), superClass); Method method = (Method) field;
ParameterList parameterList = method.getParameterList();
//generatedClass.addField(standardKonstruktor);
//Die Typen der Methodenparameter
//results.addAll(generatedClass.genByteCode(new TypeinferenceResultSet(generatedClass, new Menge<>(), new ResultSet()))); for(FormalParameter parameter: parameterList){
Type parameterType = parameter.getType();
/* if(parameterType instanceof RefType){
//Die Felder in Methoden Felder und Konstruktoren aufteilen: RefType refType = (RefType) parameterType;
Menge<FieldDeclaration> fieldDeclarations = new Menge<>();
Menge<Constructor> constructors = new Menge<>(); if(!refType.getCombinedType(null).equals(refType.get_Name().replace(".", "%"))){
results.addAll(generateGenericClass(refType.getCombinedType(null), new Class("java/util/Vector",-1)));
Block konstruktorBlock = new Block(); }
konstruktorBlock.statements.add(new SuperCall(konstruktorBlock)); }
}
Method constructorMethod = Method.createEmptyMethod(konstruktorBlock, name, superClass); }
}
constructorMethod.parameterlist = new ParameterList();
Constructor constructor = new Constructor(constructorMethod, superClass);
constructor.parserPostProcessing(superClass);
constructors.add(constructor);
InstructionList fieldInitializations = new InstructionList();
for(FieldDeclaration f : fieldDeclarations){
fieldInitializations.append(f.genByteCode(_cg));
}
//Die Konstruktoren müssen die Feld initialisierungswerte beinhalten:
for(Constructor c : constructors){
c.genByteCode(_cg, fieldInitializations);
}
*/
/*
Block konstruktorBlock = new Block();
konstruktorBlock.statements.add(new SuperCall(konstruktorBlock));
Constructor standardKonstruktor = new Constructor(Method.createEmptyMethod(konstruktorBlock,name, superClass), superClass);
Menge<Constructor> constructors = new Menge<>();
constructors.add(standardKonstruktor);
//Die Konstruktoren müssen die Feld initialisierungswerte beinhalten:
for(Constructor c : constructors){
c.genByteCode(_cg, new InstructionList());
}
*/
ByteCodeResult code = new ByteCodeResult(_cg);
results.add(code);
return results; return results;
} }
private Menge<ByteCodeResult> generateGenericClass(String name, Class superClass){
//TODO: bytecode -- Generics hinzufügen
//Type superClassType = superClass.getType();
//TODO: bytecode
//ClassGenerator genericClassGenerator = new ClassGenerator(name, superClassType, name + ".java", Constants.ACC_PUBLIC , new String[] { }, new TypeinferenceResultSet(null, null, null));
//TODO: bytecode -- Namen der neuen Klasse
Class generatedClass = new Class(name, 0);
//TODO: bytecode -- alle Konstruktoren generieren
Block konstruktorBlock = new Block();
konstruktorBlock.statements.add(new SuperCall(konstruktorBlock));
Constructor standardKonstruktor = new Constructor(Method.createEmptyMethod(konstruktorBlock, name, superClass), superClass);
standardKonstruktor.parserPostProcessing(generatedClass);
generatedClass.addField(standardKonstruktor);
return generatedClass.genByteCode(new TypeinferenceResultSet(generatedClass, new Menge<>(), new ResultSet()));
}
} }
// ino.end // ino.end

View File

@ -820,21 +820,6 @@ public class RefType extends ObjectType implements IMatchable
@Override @Override
public String getBytecodeSignature(ClassGenerator cg) { public String getBytecodeSignature(ClassGenerator cg) {
/* /*
//Bsp.: Ljava/util/Vector<Ljava/lang/String;>;
StringBuilder sb = new StringBuilder();
sb.append(getTypeName());
if(parameter != null){
for(Type type: parameter){
sb.append(""); //TODO: einen geeignete Delemiter suchen
sb.append(type.getBytecodeSignature(cg));
}
}
return sb.toString();
*/
String paramString = ""; String paramString = "";
if(this.parameter != null && this.parameter.size()>0){ if(this.parameter != null && this.parameter.size()>0){
paramString+="<"; paramString+="<";
@ -842,28 +827,29 @@ public class RefType extends ObjectType implements IMatchable
while(it.hasNext()){ while(it.hasNext()){
Type param = it.next(); Type param = it.next();
paramString+=param.getBytecodeSignature(cg); paramString+=param.getBytecodeSignature(cg);
//if(it.hasNext())
//paramString+=";"; //kein Delimiter zwischen den Typen
} }
paramString+=">"; paramString+=">";
} }
String typeSignature = this.getBytecodeType(cg).getSignature(); String typeSignature = this.getBytecodeType(cg).getSignature();
typeSignature = typeSignature.substring(0, typeSignature.length()-1); typeSignature = typeSignature.substring(0, typeSignature.length()-1);
return typeSignature+paramString+";"; return typeSignature+paramString+";";
*/
return "L"+getCombinedType(cg)+";";
} }
public String getCombinedType(ClassGenerator cg){ public String getCombinedType(ClassGenerator cg){
//Bsp.: Ljava/util/Vector<Ljava/lang/String;>; //Bsp.: Ljava/util/Vector<Ljava/lang/String;>;
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append(this.get_Name().replace(".", "%")); if(parameter != null && parameter.size() > 0){
sb.append(this.get_Name().replace(".", "%"));
if(parameter != null){
sb.append("%%"); sb.append("%%");
for(Type type: parameter){ for(Type type: parameter){
sb.append(((RefType) type).getCombinedType(cg)); sb.append(((RefType) type).getCombinedType(cg));
sb.append("%"); sb.append("%");
} }
}else{
sb.append(this.get_Name());
} }
return sb.toString(); return sb.toString();

View File

@ -16,6 +16,8 @@ public class OverloadingTest extends BytecodeTest{
rootDirectory = System.getProperty("user.dir")+"/test/bytecode/types/"; rootDirectory = System.getProperty("user.dir")+"/test/bytecode/types/";
} }
@Test @Test
public void testString() { public void testString() {
try{ try{