From ce97cf82fd44d4e4d0d3c15dfe7e768961f0d3df Mon Sep 17 00:00:00 2001 From: sebastian Date: Tue, 4 Apr 2017 17:12:01 +0200 Subject: [PATCH] =?UTF-8?q?Generierung=20von=20Interfaces=20f=C3=BCr=20Fel?= =?UTF-8?q?der=20und=20Methode=20=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../strucTypes3/FieldConstraint.java | 2 +- .../strucTypes3/MethodConstraint.java | 73 ++++++++++- test/strucTypes3/Construct.java | 95 ++++++++++++++ .../GeneralParserTestConstruct.java | 116 ++++++++++++++++++ 4 files changed, 284 insertions(+), 2 deletions(-) create mode 100644 test/strucTypes3/Construct.java create mode 100644 test/strucTypes3/GeneralParserTestConstruct.java diff --git a/src/de/dhbwstuttgart/strucTypes3/FieldConstraint.java b/src/de/dhbwstuttgart/strucTypes3/FieldConstraint.java index 67b4feaa..b7ec196f 100644 --- a/src/de/dhbwstuttgart/strucTypes3/FieldConstraint.java +++ b/src/de/dhbwstuttgart/strucTypes3/FieldConstraint.java @@ -3,7 +3,7 @@ package de.dhbwstuttgart.strucTypes3; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; -class FieldConstraint extends ConstraintAbstract { +public class FieldConstraint extends ConstraintAbstract { private RefTypeOrTPHOrWildcardOrGeneric reveiver; private String name_of_field; diff --git a/src/de/dhbwstuttgart/strucTypes3/MethodConstraint.java b/src/de/dhbwstuttgart/strucTypes3/MethodConstraint.java index 8812a7da..d9989da6 100644 --- a/src/de/dhbwstuttgart/strucTypes3/MethodConstraint.java +++ b/src/de/dhbwstuttgart/strucTypes3/MethodConstraint.java @@ -4,7 +4,7 @@ import java.util.List; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; -class MethodConstraint extends ConstraintAbstract { +public class MethodConstraint extends ConstraintAbstract { /* Der MethodConstraint * constraint( typ_receiver, methodname, arguments_typen ( return_type , typvar von argumenttypen ) @@ -27,7 +27,78 @@ class MethodConstraint extends ConstraintAbstract { this.argumentTypeVars = argumentTypeVars; } + + + + public RefTypeOrTPHOrWildcardOrGeneric getReceiver() { + return receiver; + } + + + + + public void setReceiver(RefTypeOrTPHOrWildcardOrGeneric receiver) { + this.receiver = receiver; + } + + + + + public String getMethodName() { + return methodName; + } + + + + + public void setMethodName(String methodName) { + this.methodName = methodName; + } + + + + + public List getArgumentTypes() { + return argumentTypes; + } + + + + + public void setArgumentTypes(List argumentTypes) { + this.argumentTypes = argumentTypes; + } + + + + + public RefTypeOrTPHOrWildcardOrGeneric getReturnType() { + return returnType; + } + + + + + public void setReturnType(RefTypeOrTPHOrWildcardOrGeneric returnType) { + this.returnType = returnType; + } + + + + + public List getArgumentTypeVars() { + return argumentTypeVars; + } + + + + + public void setArgumentTypeVars(List argumentTypeVars) { + this.argumentTypeVars = argumentTypeVars; + } + public String toString() { return String.format("MethodConstraint( %s, %s , %s (%s , %s ))" , receiver.toString() , methodName, argumentTypes.toString() , returnType.toString() , argumentTypeVars.toString() ); + } } \ No newline at end of file diff --git a/test/strucTypes3/Construct.java b/test/strucTypes3/Construct.java new file mode 100644 index 00000000..52997243 --- /dev/null +++ b/test/strucTypes3/Construct.java @@ -0,0 +1,95 @@ +package strucTypes3; + +import java.util.ArrayList; +import java.util.List; + +import com.sun.org.apache.xalan.internal.xsltc.runtime.Parameter; + +import de.dhbwstuttgart.parser.NullToken; +import de.dhbwstuttgart.strucTypes3.ConstraintAbstract; +import de.dhbwstuttgart.strucTypes3.FieldConstraint; +import de.dhbwstuttgart.strucTypes3.MethodConstraint; +import de.dhbwstuttgart.strucTypes3.MyTypeVar; +import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +import de.dhbwstuttgart.syntaxtree.Field; +import de.dhbwstuttgart.syntaxtree.FormalParameter; +import de.dhbwstuttgart.syntaxtree.GenericDeclarationList; +import de.dhbwstuttgart.syntaxtree.GenericTypeVar; +import de.dhbwstuttgart.syntaxtree.Method; +import de.dhbwstuttgart.syntaxtree.ParameterList; +import de.dhbwstuttgart.typecheck.JavaClassName; + +public class Construct { + + + public static ClassOrInterface constructInterfaces(ConstraintAbstract constraint) { + + if (constraint.getClass().equals(FieldConstraint.class)) { + FieldConstraint fieldConstraint = (FieldConstraint) constraint; + return Construct.constructInterfaceField(fieldConstraint); + } + else if (constraint.getClass().equals(MethodConstraint.class)) { + MethodConstraint methodConstriant = (MethodConstraint) constraint; + return Construct.constructInterfaceMethod(methodConstriant); + + } + else { + // Fehlerfall + return null; + } + } + + + public static ClassOrInterface constructInterfaceField(FieldConstraint fieldConstriant) { + + Field field = new Field(fieldConstriant.getName_of_field() , fieldConstriant.getAttribut() , 0 , null); + List fielddecl = new ArrayList<>(); + fielddecl.add(field); + JavaClassName name = new JavaClassName(fieldConstriant.getReveiver().toString()); + ClassOrInterface face = new ClassOrInterface(0, name, fielddecl, null, null, null, null, null , null); + return face; + + } + + + public static ClassOrInterface constructInterfaceMethod(MethodConstraint methodConstraint) { + + System.out.println("Generiere Method-Interface"); + + // Erstellen der Argumente der Methode + List formalParameterList = new ArrayList<>(); + ParameterList params = new ParameterList(formalParameterList, new NullToken()); + for (MyTypeVar typeVar : methodConstraint.getArgumentTypeVars()) { + params.getFormalparalist().add(new FormalParameter(typeVar.toString() , typeVar.getTypevar(), new NullToken() )); + } + + // Erstellen der Methode + Method method = new Method(methodConstraint.getMethodName() ,methodConstraint.getReturnType(), 0, params, null, null, null); + List methodlist = new ArrayList<>(); + methodlist.add(method); + + // Name des neuen Interfaces + JavaClassName name = new JavaClassName(methodConstraint.getReceiver().toString()); + + //Erstellen der Generics + List genericTypeVarList = new ArrayList<>(); + GenericDeclarationList genericDeclaraitonList = new GenericDeclarationList(genericTypeVarList, new NullToken()); + for (MyTypeVar tv : methodConstraint.getArgumentTypeVars()) { + genericTypeVarList.add(new GenericTypeVar(tv.toString(), null, new NullToken(), new NullToken())); + } + + + ClassOrInterface face = new ClassOrInterface(0, name, null, methodlist, genericDeclaraitonList, null, true, null, null); + return face; + } + + + + + + + + + + +} diff --git a/test/strucTypes3/GeneralParserTestConstruct.java b/test/strucTypes3/GeneralParserTestConstruct.java new file mode 100644 index 00000000..a8ab15ff --- /dev/null +++ b/test/strucTypes3/GeneralParserTestConstruct.java @@ -0,0 +1,116 @@ +package strucTypes3; + +import static org.junit.Assert.*; + +import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import de.dhbwstuttgart.parser.JavaTXParser; +import de.dhbwstuttgart.strucTypes3.AssTuple; +import de.dhbwstuttgart.strucTypes3.AssumptionGenerator; +import de.dhbwstuttgart.strucTypes3.ConstraintAbstract; +import de.dhbwstuttgart.strucTypes3.HelpMethoden; +import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +import de.dhbwstuttgart.syntaxtree.Field; +import de.dhbwstuttgart.syntaxtree.FormalParameter; +import de.dhbwstuttgart.syntaxtree.Method; +import de.dhbwstuttgart.syntaxtree.SourceFile; +import de.dhbwstuttgart.syntaxtree.statement.Block; +import de.dhbwstuttgart.syntaxtree.statement.Expression; +import de.dhbwstuttgart.syntaxtree.statement.FieldVar; +import de.dhbwstuttgart.syntaxtree.statement.LocalVar; +import de.dhbwstuttgart.syntaxtree.statement.MethodCall; +import de.dhbwstuttgart.syntaxtree.statement.Receiver; +import de.dhbwstuttgart.syntaxtree.statement.Return; +import de.dhbwstuttgart.syntaxtree.statement.Statement; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; +import sun.reflect.generics.reflectiveObjects.NotImplementedException; + +import org.junit.Test; + +/** + * Dieser Test pr�ft nur, ob .java-Dateien fehlerfrei geparst werden. Der + * dabei erstellte Syntaxbaum wird nicht kontrolliert. + * + * @author janulrich + * + */ +public class GeneralParserTestConstruct { + private static final String rootDirectory = System.getProperty("user.dir") + "/test/strucTypes3/"; + + @Test + public void run() { + + /* + * Auflistung aller Files die getestet werden sollen + */ + List filenames = new ArrayList(); + // filenames.add("FieldInitializationTest.jav"); + // filenames.add("ImportTest.jav"); + // filenames.add("StatementsTest.jav"); + // filenames.add("ImportTestGeneric.jav"); + // filenames.add("BoundedParameter.jav"); + // filenames.add("GenericFieldVarTest.jav"); + // filenames.add("FieldVarTest.jav"); + + filenames.add("StructuralTypesSimpleSkript.jav"); + // filenames.add("StructuralTypesField.jav"); + // filenames.add("Main.jav"); + + JavaTXParser parser = new JavaTXParser(); + try { + for (String filename : filenames) { + + System.out.println("Teste: " + filename); + + SourceFile sf = parser.parse(new File(rootDirectory + filename)); + + SourceFile sfdebug = sf; + + + // Erstellen der Constrinats für die erste Klasse --------------------------------------- + ClassOrInterface aktClass = sf.getClasses().get(0); + AssTuple ass = AssumptionGenerator.generateAssumptions(aktClass); + + + List constraints = new ArrayList<>(); + for (Method m : aktClass.getMethods()) { + Return rt = (Return) m.get_Block().statements.get(0); + Expression ex = (Expression) rt.get_Expression(); + constraints.addAll(HelpMethoden.TypeExpr(ass, ex, m, aktClass)); + } + + constraints.forEach( (x) -> {System.out.println(x); }); + + + // Erstellen der Interfaces für die erste Klasse + List sf2 = new ArrayList<>(); + constraints.forEach( (constraint) -> {sf2.add( Construct.constructInterfaces(constraint)); } ); + + + + System.out.println(sf2.size()); + + + + + + + + + + + + + } + + } catch (Exception exc) { + exc.printStackTrace(); + fail(); + } + assertTrue("Tests durchlaufen", filenames.size() > 0); + } +}