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);
results.add(code);
results.addAll(getGenericClasses(_cg));
results.addAll(getGenericClasses());
return results;
}
@ -1032,75 +1032,61 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I
return false;
}
private Collection<? extends ByteCodeResult> getGenericClasses(ClassGenerator _cg) {
private Collection<? extends ByteCodeResult> getGenericClasses() {
Collection<ByteCodeResult> results = new Menge<>();
//Super Klasse
String name = getType().getCombinedType(_cg);
//Class superClass = new Class("java/util/Vector",-1);
//Type superClassType = superClass.getType();
//_cg = new ClassGenerator(name, superClassType, name + ".java", Constants.ACC_PUBLIC , new String[] { }, new TypeinferenceResultSet(null, null, null));
//Class generatedClass = new Class(name, 0);
//Block konstruktorBlock = new Block();
//konstruktorBlock.statements.add(new SuperCall(konstruktorBlock));
//Constructor standardKonstruktor = new Constructor(Method.createEmptyMethod(konstruktorBlock, name, superClass), superClass);
//generatedClass.addField(standardKonstruktor);
//results.addAll(generatedClass.genByteCode(new TypeinferenceResultSet(generatedClass, new Menge<>(), new ResultSet())));
/*
//Die Felder in Methoden Felder und Konstruktoren aufteilen:
Menge<FieldDeclaration> fieldDeclarations = new Menge<>();
Menge<Constructor> constructors = new Menge<>();
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);
for(Field field : this.fielddecl){
Type type = field.getType();
//Der Type des Feldes
if(type instanceof RefType){
RefType refType = (RefType) type;
if(!refType.getCombinedType(null).equals(refType.get_Name().replace(".", "%"))){
results.addAll(generateGenericClass(refType.getCombinedType(null), new Class("java/util/Vector",-1)));
}
}
if(field instanceof Method){
Method method = (Method) field;
ParameterList parameterList = method.getParameterList();
//Die Typen der Methodenparameter
for(FormalParameter parameter: parameterList){
Type parameterType = parameter.getType();
if(parameterType instanceof RefType){
RefType refType = (RefType) parameterType;
if(!refType.getCombinedType(null).equals(refType.get_Name().replace(".", "%"))){
results.addAll(generateGenericClass(refType.getCombinedType(null), new Class("java/util/Vector",-1)));
}
}
}
}
}
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

View File

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

View File

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