diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java index b0331a00..da51d61e 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java @@ -151,7 +151,7 @@ public class SyntaxTreeGenerator{ block = stmtGen.convert(body.block(),true); } if(parentClass.equals(new JavaClassName(name))){ - return new Constructor(modifiers, name, retType, parameterList, block, gtvDeclarations, header.getStart(), fieldInitializations); + return new Constructor(modifiers, name, retType, parameterList, block, gtvDeclarations, header.getStart() /*, fieldInitializations geloescht PL 2018-11-24 */); }else{ return new Method(modifiers, name, retType, parameterList,block, gtvDeclarations, header.getStart()); } @@ -198,14 +198,18 @@ public class SyntaxTreeGenerator{ } List<Field> fielddecl = convertFields(ctx.classBody(), generics); //fieldInitializations = generateFieldInitializations(ctx.classBody(), generics); - List<Method> methods = convertMethods(ctx.classBody(), name, superClass, generics); + List<Method> methodsAndConstructors = convertMethods(ctx.classBody(), name, superClass, generics); + List<Method> methods = new ArrayList<>(); List<Constructor> konstruktoren = new ArrayList<>(); - for(int i = 0; i<methods.size();i++){ - Method m = methods.get(i); + //int noOfMethods = methods.size(); + for(int i = 0; i < methodsAndConstructors.size(); i++){ + Method m = methodsAndConstructors.get(i); if(m instanceof Constructor){ - methods.remove(i); konstruktoren.add((Constructor) m); } + else { + methods.add(m); + } } if(konstruktoren.size()<1){//Standardkonstruktor anfügen: konstruktoren.add( @@ -217,7 +221,10 @@ public class SyntaxTreeGenerator{ Boolean isInterface = false; List<RefType> implementedInterfaces = convert(ctx.superinterfaces(), generics); - return new ClassOrInterface(modifiers, name, fielddecl, methods, konstruktoren, genericClassParameters, superClass, + + return new ClassOrInterface(modifiers, name, fielddecl, + Optional.of(this.generatePseudoConstructor(ctx.Identifier().getText(), name, superClass, genericClassParameters, offset)), + methods, konstruktoren, genericClassParameters, superClass, isInterface, implementedInterfaces, offset); } @@ -267,8 +274,16 @@ public class SyntaxTreeGenerator{ RefType classType = ClassOrInterface.generateTypeOfClass(reg.getName(className), classGenerics, offset); ParameterList params = new ParameterList(new ArrayList<>(), offset); Block block = new Block(new ArrayList<>(), offset); - return new Constructor(Modifier.PUBLIC, className, classType, params, block, classGenerics, offset, fieldInitializations); + return new Constructor(Modifier.PUBLIC, className, classType, params, block, classGenerics, offset /*, fieldInitializations geloescht PL 2018-11-24 */); } + + /* fieldInitializations werden in einem Psedokonstruktor in der abstrakten Syntax gespeichert */ + private Constructor generatePseudoConstructor(String className, JavaClassName parentClass, RefType superClass, GenericDeclarationList classGenerics, Token offset){ + RefType classType = ClassOrInterface.generateTypeOfClass(reg.getName(className), classGenerics, offset); + ParameterList params = new ParameterList(new ArrayList<>(), offset); + Block block = new Block(fieldInitializations, offset); + return new Constructor(Modifier.PUBLIC, className, classType, params, block, classGenerics, offset /*, fieldInitializations geloescht PL 2018-11-24 */); + } private RefType convert(Java8Parser.SuperclassContext superclass) { if(superclass.classType().classOrInterfaceType() != null){ @@ -441,7 +456,7 @@ public class SyntaxTreeGenerator{ List<RefType> extendedInterfaces = convert(ctx.extendsInterfaces(), generics); - return new ClassOrInterface(modifiers, name, fields, methods, new ArrayList<>(), + return new ClassOrInterface(modifiers, name, fields, Optional.empty(), methods, new ArrayList<>(), genericParams, superClass, true, extendedInterfaces, ctx.getStart()); } diff --git a/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java b/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java index d0177ace..bbd184f0 100644 --- a/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java +++ b/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java @@ -3,6 +3,7 @@ package de.dhbwstuttgart.syntaxtree; import de.dhbwstuttgart.core.IItemWithOffset; import de.dhbwstuttgart.exceptions.DebugException; import de.dhbwstuttgart.parser.scope.JavaClassName; +import de.dhbwstuttgart.syntaxtree.statement.Statement; import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.syntaxtree.visual.ASTPrinter; @@ -16,6 +17,7 @@ import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.Optional; /** * Stellt jede Art von Klasse dar. Auch abstrakte Klassen und Interfaces @@ -24,6 +26,7 @@ public class ClassOrInterface extends SyntaxTreeNode implements TypeScope{ protected int modifiers; protected JavaClassName name; private List<Field> fields = new ArrayList<>(); + private Optional<Constructor> fieldInitializations; //PL 2018-11-24: Noetig, um Bytecode fuer initializators nur einmal zu erzeugen private List<Method> methods = new ArrayList<>(); private GenericDeclarationList genericClassParameters; private RefType superClass; @@ -31,13 +34,14 @@ public class ClassOrInterface extends SyntaxTreeNode implements TypeScope{ private List<RefType> implementedInterfaces; private List<Constructor> constructors; - public ClassOrInterface(int modifiers, JavaClassName name, List<Field> fielddecl, List<Method> methods, List<Constructor> constructors, GenericDeclarationList genericClassParameters, + public ClassOrInterface(int modifiers, JavaClassName name, List<Field> fielddecl, Optional<Constructor> fieldInitializations, List<Method> methods, List<Constructor> constructors, GenericDeclarationList genericClassParameters, RefType superClass, Boolean isInterface, List<RefType> implementedInterfaces, Token offset){ super(offset); if(isInterface && !Modifier.isInterface(modifiers))modifiers += Modifier.INTERFACE; this.modifiers = modifiers; this.name = name; this.fields = fielddecl; + this.fieldInitializations= fieldInitializations; this.genericClassParameters = genericClassParameters; this.superClass = superClass; this.isInterface = isInterface; @@ -59,6 +63,11 @@ public class ClassOrInterface extends SyntaxTreeNode implements TypeScope{ public List<Field> getFieldDecl(){ return this.fields; } + + public Optional<Constructor> getfieldInitializations(){ + return this.fieldInitializations; + } + public List<Method> getMethods(){ return this.methods; } diff --git a/src/de/dhbwstuttgart/syntaxtree/Constructor.java b/src/de/dhbwstuttgart/syntaxtree/Constructor.java index 79db5ced..0982bd9b 100644 --- a/src/de/dhbwstuttgart/syntaxtree/Constructor.java +++ b/src/de/dhbwstuttgart/syntaxtree/Constructor.java @@ -15,8 +15,8 @@ public class Constructor extends Method { //TODO: Constructor braucht ein super-Statement public Constructor(int modifier, String name, RefTypeOrTPHOrWildcardOrGeneric returnType, ParameterList parameterList, Block codeInsideConstructor, - GenericDeclarationList gtvDeclarations, Token offset, List<Statement> fieldInitializations) { - super(modifier, name, returnType, parameterList, prepareBlock(codeInsideConstructor,fieldInitializations), gtvDeclarations, offset); + GenericDeclarationList gtvDeclarations, Token offset /*, List<Statement> fieldInitializations geloescht PL 2018-11-24 */) { + super(modifier, name, returnType, parameterList, prepareBlock(codeInsideConstructor /* ,fieldInitializations geloescht PL 2018-11-24 */), gtvDeclarations, offset); } @@ -25,10 +25,10 @@ public class Constructor extends Method { * welche die Felder der zugehörigen Klasse dieses * Konstruktor initialisieren */ - protected static Block prepareBlock(Block constructorBlock, List<Statement> fieldInitializations){ + protected static Block prepareBlock(Block constructorBlock /*, List<Statement> fieldInitializations new ArrayList<>() geloescht PL 2018-11-24 */){ List<Statement> statements = constructorBlock.getStatements(); statements.add(0, new SuperCall(constructorBlock.getOffset())); - statements.addAll(fieldInitializations); + /* statements.addAll(fieldInitializations); geloescht PL 2018-11-24 */ return new Block(statements, constructorBlock.getOffset()); } diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java index d92d0b9a..2fb32dbe 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java @@ -4,6 +4,7 @@ import java.lang.reflect.*; import java.lang.reflect.Constructor; import java.util.ArrayList; import java.util.List; +import java.util.Optional; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.parser.NullToken; @@ -65,7 +66,7 @@ public class ASTFactory { Token offset = new NullToken(); //Braucht keinen Offset, da diese Klasse nicht aus einem Quellcode geparst wurde - return new ClassOrInterface(modifier, name, felder, methoden, konstruktoren, genericDeclarationList, superClass,isInterface, implementedInterfaces, offset); + return new ClassOrInterface(modifier, name, felder, Optional.empty() /* eingefuegt PL 2018-11-24 */,methoden, konstruktoren, genericDeclarationList, superClass,isInterface, implementedInterfaces, offset); } private static Field createField(java.lang.reflect.Field field, JavaClassName jreClass) { @@ -98,7 +99,7 @@ public class ASTFactory { return null; } - return new de.dhbwstuttgart.syntaxtree.Constructor(modifier, name,returnType, parameterList, block, gtvDeclarations, offset, new ArrayList<>()); + return new de.dhbwstuttgart.syntaxtree.Constructor(modifier, name,returnType, parameterList, block, gtvDeclarations, offset /*, new ArrayList<>() geloescht PL 2018-11-24 */); } public static Method createMethod(java.lang.reflect.Method jreMethod, java.lang.Class inClass){ diff --git a/src/de/dhbwstuttgart/typeinference/assumptions/FunNClass.java b/src/de/dhbwstuttgart/typeinference/assumptions/FunNClass.java index 0c8220f1..6067851e 100644 --- a/src/de/dhbwstuttgart/typeinference/assumptions/FunNClass.java +++ b/src/de/dhbwstuttgart/typeinference/assumptions/FunNClass.java @@ -17,10 +17,11 @@ import org.antlr.v4.runtime.Token; import java.util.ArrayList; import java.util.List; +import java.util.Optional; public class FunNClass extends ClassOrInterface { public FunNClass(List<GenericRefType> funNParams) { - super(0, new JavaClassName("Fun"+(funNParams.size())), new ArrayList<>(), + super(0, new JavaClassName("Fun"+(funNParams.size())), new ArrayList<>(), Optional.empty() /* eingefuegt PL 2018-11-24 */, createMethods(funNParams), new ArrayList<>(), createGenerics(funNParams), ASTFactory.createObjectType(), true, new ArrayList<>(), new NullToken()); diff --git a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java index 6a441050..246c3691 100644 --- a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java +++ b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java @@ -38,6 +38,9 @@ public class TYPE { for(Constructor m : cl.getConstructors()){ ret.addAll(getConstraintsConstructor(m,info, cl)); } + if (cl.getfieldInitializations().isPresent()) { + ret.addAll(getConstraintsConstructor(cl.getfieldInitializations().get(), info, cl)); + } return ret; } /* @@ -61,6 +64,7 @@ public class TYPE { return new TypeInferenceInformation(classes); } */ + private ConstraintSet getConstraintsMethod(Method m, TypeInferenceInformation info, ClassOrInterface currentClass) { if(m.block == null)return new ConstraintSet(); //Abstrakte Methoden generieren keine Constraints TypeInferenceBlockInformation blockInfo = new TypeInferenceBlockInformation(info.getAvailableClasses(), currentClass, m); diff --git a/test/bytecode/MatrixOpTest.java b/test/bytecode/MatrixOpTest.java index 8fe538aa..5372f457 100644 --- a/test/bytecode/MatrixOpTest.java +++ b/test/bytecode/MatrixOpTest.java @@ -32,7 +32,7 @@ public class MatrixOpTest { fileToTest = new File(path); compiler = new JavaTXCompiler(fileToTest); pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/"; -// compiler.generateBytecode(pathToClassFile); + compiler.generateBytecode(pathToClassFile); // loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); // classToTest = loader.loadClass("MatrixOP"); /*