From 50dda3041fc66c1e3c65d0771ca15b540d7145eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrico=20Schr=C3=B6dter?= Date: Fri, 16 Oct 2015 15:15:30 +0200 Subject: [PATCH] Beginn der Generierung der GenericKlasse, Problem ist der Konstruktor, dieser wird auf Objekt aufgerufen und nicht auf der SUperklasse --- src/de/dhbwstuttgart/syntaxtree/Class.java | 61 ++++++++++++++++++- .../syntaxtree/type/RefType.java | 19 +++++- .../types/ExtendsVectorStringTest.java | 17 ++---- 3 files changed, 82 insertions(+), 15 deletions(-) diff --git a/src/de/dhbwstuttgart/syntaxtree/Class.java b/src/de/dhbwstuttgart/syntaxtree/Class.java index b11ed15f..d7b65693 100755 --- a/src/de/dhbwstuttgart/syntaxtree/Class.java +++ b/src/de/dhbwstuttgart/syntaxtree/Class.java @@ -24,6 +24,7 @@ import de.dhbwstuttgart.bytecode.DHBWInstructionFactory; import de.dhbwstuttgart.core.AClassOrInterface; import de.dhbwstuttgart.core.IItemWithOffset; import de.dhbwstuttgart.parser.JavaClassName; +import de.dhbwstuttgart.syntaxtree.misc.DeclId; import de.dhbwstuttgart.syntaxtree.misc.UsedId; import de.dhbwstuttgart.syntaxtree.modifier.Modifiers; import de.dhbwstuttgart.syntaxtree.statement.Block; @@ -1035,11 +1036,65 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I Collection results = new Menge<>(); //Super Klasse - String name = "java/util/Vectorjava/lang/String"; //getParentClass().getType().getBytecodeSignature(_cg); + String name = getType().getCombinedType(_cg); - Type superClass = new Class("java/util/Vector",-1).getType(); + Class superClass = new Class("java/util/Vector",-1); + Type superClassType = superClass.getType(); - _cg = new ClassGenerator(name, superClass, name + ".java", Constants.ACC_PUBLIC , new String[] { }, new TypeinferenceResultSet(null, null, null)); + //_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); diff --git a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java index 4f2e045d..cb04e0f2 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java @@ -814,7 +814,7 @@ public class RefType extends ObjectType implements IMatchable } public org.apache.commons.bcel6.generic.Type getBytecodeType(ClassGenerator cg) { - return new org.apache.commons.bcel6.generic.ObjectType(this.get_Name()); + return new org.apache.commons.bcel6.generic.ObjectType(getCombinedType(cg)); } @Override @@ -852,6 +852,23 @@ public class RefType extends ObjectType implements IMatchable return typeSignature+paramString+";"; } + public String getCombinedType(ClassGenerator cg){ + //Bsp.: Ljava/util/Vector; + StringBuilder sb = new StringBuilder(); + + sb.append(this.get_Name().replace(".", "%")); + + if(parameter != null){ + sb.append("%%"); + for(Type type: parameter){ + sb.append(((RefType) type).getCombinedType(cg)); + sb.append("%"); + } + } + + return sb.toString(); + } + } // ino.end diff --git a/test/bytecode/types/ExtendsVectorStringTest.java b/test/bytecode/types/ExtendsVectorStringTest.java index 10f4bdfe..8511499b 100644 --- a/test/bytecode/types/ExtendsVectorStringTest.java +++ b/test/bytecode/types/ExtendsVectorStringTest.java @@ -32,17 +32,12 @@ public class ExtendsVectorStringTest extends BytecodeTest{ } @Test - public void testConstruct(){ - try{ - Class cls = getClassToTest(); - - Constructor method = cls.getConstructor(new Class[]{}); - method.newInstance(); - assertTrue(true); - }catch(Exception e){ - e.printStackTrace(); - fail(); - } + public void testConstruct() throws Exception{ + Class cls = getClassToTest(); + + Constructor method = cls.getConstructor(new Class[]{}); + method.newInstance(); + assertTrue(true); } }