From 632c2ca508649c7c418ff6a3671476770c2e6859 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrico=20Schr=C3=B6dter?= Date: Tue, 20 Oct 2015 15:58:39 +0200 Subject: [PATCH] =?UTF-8?q?Genericklassen=20f=C3=BCr=20Superklassen,=20Fie?= =?UTF-8?q?ldtypes=20und=20Methodenparameter=20werden=20generiert?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/de/dhbwstuttgart/syntaxtree/Class.java | 118 ++++++++---------- .../syntaxtree/type/RefType.java | 26 +--- test/bytecode/types/OverloadingTest.java | 2 + 3 files changed, 60 insertions(+), 86 deletions(-) diff --git a/src/de/dhbwstuttgart/syntaxtree/Class.java b/src/de/dhbwstuttgart/syntaxtree/Class.java index 5c66bc2b..2313f22c 100755 --- a/src/de/dhbwstuttgart/syntaxtree/Class.java +++ b/src/de/dhbwstuttgart/syntaxtree/Class.java @@ -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 getGenericClasses(ClassGenerator _cg) { + private Collection getGenericClasses() { Collection 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 fieldDeclarations = new Menge<>(); - Menge 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 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 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 diff --git a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java index cb04e0f2..56caea6c 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java @@ -820,21 +820,6 @@ public class RefType extends ObjectType implements IMatchable @Override public String getBytecodeSignature(ClassGenerator cg) { /* - //Bsp.: Ljava/util/Vector; - 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; 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(); diff --git a/test/bytecode/types/OverloadingTest.java b/test/bytecode/types/OverloadingTest.java index 7c8ba2ce..9e6b3a22 100644 --- a/test/bytecode/types/OverloadingTest.java +++ b/test/bytecode/types/OverloadingTest.java @@ -16,6 +16,8 @@ public class OverloadingTest extends BytecodeTest{ rootDirectory = System.getProperty("user.dir")+"/test/bytecode/types/"; } + + @Test public void testString() { try{