From 416d68bcb0da87c6a70297d7e9ea129443e2d683 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Pl=C3=BCmicke?= Date: Mon, 26 Nov 2018 10:59:06 +0100 Subject: [PATCH] =?UTF-8?q?Aenderugen=20siehe=20http://bugzilla.ba-horb.de?= =?UTF-8?q?/show=5Fbug.cgi=3Fid=3D124=20=09modified:=20=20=20../../src/de/?= =?UTF-8?q?dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.ja?= =?UTF-8?q?va=20Zus=C3=A4ztlich=20Ein=20Fehler=20in=20der=20Trennung=20von?= =?UTF-8?q?=20Konstruktoren=20und=20Methoden=20gefixt.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit modified: ../../src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java modified: ../../src/de/dhbwstuttgart/syntaxtree/Constructor.java modified: ../../src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java modified: ../../src/de/dhbwstuttgart/typeinference/assumptions/FunNClass.java modified: ../../src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java modified: ../../test/bytecode/MatrixOpTest.java --- .../SyntaxTreeGenerator.java | 31 ++++++++++++++----- .../syntaxtree/ClassOrInterface.java | 11 ++++++- .../dhbwstuttgart/syntaxtree/Constructor.java | 8 ++--- .../syntaxtree/factory/ASTFactory.java | 5 +-- .../typeinference/assumptions/FunNClass.java | 3 +- .../typeinference/typeAlgo/TYPE.java | 4 +++ test/bytecode/MatrixOpTest.java | 2 +- 7 files changed, 47 insertions(+), 17 deletions(-) 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 fielddecl = convertFields(ctx.classBody(), generics); //fieldInitializations = generateFieldInitializations(ctx.classBody(), generics); - List methods = convertMethods(ctx.classBody(), name, superClass, generics); + List methodsAndConstructors = convertMethods(ctx.classBody(), name, superClass, generics); + List methods = new ArrayList<>(); List konstruktoren = new ArrayList<>(); - for(int i = 0; i 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 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 fields = new ArrayList<>(); + private Optional fieldInitializations; //PL 2018-11-24: Noetig, um Bytecode fuer initializators nur einmal zu erzeugen private List methods = new ArrayList<>(); private GenericDeclarationList genericClassParameters; private RefType superClass; @@ -31,13 +34,14 @@ public class ClassOrInterface extends SyntaxTreeNode implements TypeScope{ private List implementedInterfaces; private List constructors; - public ClassOrInterface(int modifiers, JavaClassName name, List fielddecl, List methods, List constructors, GenericDeclarationList genericClassParameters, + public ClassOrInterface(int modifiers, JavaClassName name, List fielddecl, Optional fieldInitializations, List methods, List constructors, GenericDeclarationList genericClassParameters, RefType superClass, Boolean isInterface, List 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 getFieldDecl(){ return this.fields; } + + public Optional getfieldInitializations(){ + return this.fieldInitializations; + } + public List 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 fieldInitializations) { - super(modifier, name, returnType, parameterList, prepareBlock(codeInsideConstructor,fieldInitializations), gtvDeclarations, offset); + GenericDeclarationList gtvDeclarations, Token offset /*, List 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 fieldInitializations){ + protected static Block prepareBlock(Block constructorBlock /*, List fieldInitializations new ArrayList<>() geloescht PL 2018-11-24 */){ List 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 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"); /*