Merge branch 'strucTypes_dev2' of ssh://gohorb.ba-horb.de/bahome/projekt/git/JavaCompilerCore into strucTypes_dev2

This commit is contained in:
JanUlrich 2017-04-06 12:12:36 +02:00
commit 2be1a17488
47 changed files with 2408 additions and 267 deletions

View File

@ -1,14 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<classpath> <classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="src" path="src"/> <classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="test"/> <classpathentry kind="src" path="test"/>
<classpathentry kind="lib" path="lib/bcel-6.1-SNAPSHOT.jar" sourcepath="lib/bcel-6.1-SNAPSHOT-sources.jar"/> <classpathentry kind="lib" path="lib/bcel-6.1-SNAPSHOT.jar" sourcepath="lib/bcel-6.1-SNAPSHOT-sources.jar"/>
<classpathentry kind="lib" path="lib/guava-10.0.1.jar"/> <classpathentry kind="lib" path="lib/guava-10.0.1.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="lib" path="lib/antlr-complete.jar"/> <classpathentry kind="lib" path="lib/antlr-complete.jar"/>
<classpathentry kind="lib" path="lib/reflections-0.9.10-javadoc.jar"/> <classpathentry kind="lib" path="lib/reflections-0.9.10-javadoc.jar"/>
<classpathentry kind="lib" path="lib/reflections-0.9.10-sources.jar"/> <classpathentry kind="lib" path="lib/reflections-0.9.10-sources.jar"/>
<classpathentry kind="lib" path="lib/reflections-0.9.10.jar"/> <classpathentry kind="lib" path="lib/reflections-0.9.10.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/> <classpathentry kind="output" path="bin"/>
</classpath> </classpath>

View File

@ -210,7 +210,7 @@ public class SyntaxTreeGenerator{
if(ctx.superclass() != null){ if(ctx.superclass() != null){
superClass = convert(ctx.superclass()); superClass = convert(ctx.superclass());
}else{ }else{
superClass = new ASTFactory(reg).createObjectClass().getType(); superClass = new RefType(new JavaClassName("java.lang.Object"), new NullToken());
} }
Boolean isInterface = false; Boolean isInterface = false;
List<RefTypeOrTPHOrWildcardOrGeneric> implementedInterfaces = null; List<RefTypeOrTPHOrWildcardOrGeneric> implementedInterfaces = null;

View File

@ -43,6 +43,25 @@ public class AS_Abstract {
} }
public static AS_Argument find_AS_Argument(FormalParameter f, Method m , List<AS_Abstract> ass ) {
AS_Argument res = null;
for (AS_Abstract as : ass) {
if ( as.getClass().equals(AS_Argument.class) ) {
AS_Argument asa = (AS_Argument) as;
if (f == asa.fp) {
if (asa.tv_alpha.method == m) {
res = asa;
}
}
}
}
return res;
}
public static AS_Argument find_AS_Argument_by_Name(String name, List<AS_Abstract> ass ) { public static AS_Argument find_AS_Argument_by_Name(String name, List<AS_Abstract> ass ) {
// if // if

View File

@ -6,7 +6,9 @@ import java.util.List;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.Field; import de.dhbwstuttgart.syntaxtree.Field;
import de.dhbwstuttgart.syntaxtree.Method; import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.SourceFile;
public class Algo_Type { public class Algo_Type {
@ -61,7 +63,7 @@ public class Algo_Type {
} }
// ASS = AssAll vereinigt mit ass_arguments (wird oben erledigt) // ASS = AssAll vereinigt mit ass_arguments (wird oben erledigt)
//-> assAll.addAll(ass_arguments);
// (Test) Beispielhafte Ausgabe der Assumptions // (Test) Beispielhafte Ausgabe der Assumptions
@ -87,10 +89,6 @@ public class Algo_Type {
} }
@ -121,10 +119,6 @@ public class Algo_Type {
// Gettermethoden // Gettermethoden
public Object getResult_cl_t() { public Object getResult_cl_t() {
return result_cl_t; return result_cl_t;

View File

@ -3,8 +3,6 @@ package de.dhbwstuttgart.strucTypes;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Receiver;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.Field; import de.dhbwstuttgart.syntaxtree.Field;
import de.dhbwstuttgart.syntaxtree.Method; import de.dhbwstuttgart.syntaxtree.Method;
@ -13,13 +11,12 @@ import de.dhbwstuttgart.syntaxtree.statement.Expression;
import de.dhbwstuttgart.syntaxtree.statement.FieldVar; import de.dhbwstuttgart.syntaxtree.statement.FieldVar;
import de.dhbwstuttgart.syntaxtree.statement.LocalVar; import de.dhbwstuttgart.syntaxtree.statement.LocalVar;
import de.dhbwstuttgart.syntaxtree.statement.MethodCall; import de.dhbwstuttgart.syntaxtree.statement.MethodCall;
import de.dhbwstuttgart.syntaxtree.statement.Receiver;
import de.dhbwstuttgart.syntaxtree.statement.Return; import de.dhbwstuttgart.syntaxtree.statement.Return;
import de.dhbwstuttgart.syntaxtree.statement.Statement; import de.dhbwstuttgart.syntaxtree.statement.Statement;
public class Algo_TypeExpr { public class Algo_TypeExpr {
/* /*
* *
* Hier sind die Ergebnisse * Hier sind die Ergebnisse
@ -31,31 +28,21 @@ public class Algo_TypeExpr {
// TODO Hier muss noch ein passendes Datenformat gefunden werden // TODO Hier muss noch ein passendes Datenformat gefunden werden
String res_expression_typed; String res_expression_typed;
public Algo_TypeExpr() { public Algo_TypeExpr() {
} }
// übergabe der assumptions und der Statments der Methode // übergabe der assumptions und der Statments der Methode
public Algo_TypeExpr(List<AS_Abstract> ass, List<Statement> statements) { public Algo_TypeExpr(List<AS_Abstract> ass, List<Statement> statements) {
this.res_constriant_set = new ArrayList<TC_Abstract>(); this.res_constriant_set = new ArrayList<TC_Abstract>();
res_expression_typed = ""; res_expression_typed = "";
// Methode enthält kein Statement // Methode enthält kein Statement
if (statements.size() == 0) { if (statements.size() == 0) {
System.out.println("Not Implemmented"); System.out.println("Not Implemmented");
} }
// Methode enthält nur ein Return Statement das ein feld zurückgibt // Methode enthält nur ein Return Statement das ein feld zurückgibt
if (statements.size() == 1) { if (statements.size() == 1) {
if (statements.get(0).getClass().equals(Return.class)) { if (statements.get(0).getClass().equals(Return.class)) {
@ -78,22 +65,15 @@ public class Algo_TypeExpr {
} }
} }
// Methode nur ein Return statment das ein feld zurückgibt und eine
// Methode nur ein Return statment das ein feld zurückgibt und eine Methode aufruft // Methode aufruft
//-> Es muss so sein dass ich diese Return Statement beurteilen kann
} }
/* /*
* Methode generiert Quellcode aus einer Methode * Methode generiert Quellcode aus einer Methode Letztendlich wird in der
* Letztendlich wird in der Methode der gesamte Datentyp durchwandert und erstellt * Methode der gesamte Datentyp durchwandert und erstellt Nach dem selben
* Nach dem selben Schema muss auch die generierung der Constriants erfolgen: * Schema muss auch die generierung der Constriants erfolgen:
*/ */
public static String expression2code(List<AS_Abstract> ass, List<Statement> statements) { public static String expression2code(List<AS_Abstract> ass, List<Statement> statements) {
String res = ""; String res = "";
@ -107,14 +87,30 @@ public class Algo_TypeExpr {
if (statements.size() == 1) { if (statements.size() == 1) {
if (statements.get(0).getClass() == Return.class) { if (statements.get(0).getClass() == Return.class) {
Return ret = (Return) statements.get(0); Return ret = (Return) statements.get(0);
if (ret.getClass().equals(Statement.class)) {
Statement ret2 = (Statement) ret.get_Expression(); Statement ret2 = (Statement) ret.get_Expression();
res = statement2code(ass, ret2); res = statement2code(ass, ret2);
} else if (ret.getClass().equals(FieldVar.class)) {
// Neu Implementiert - funktioniert es ??
Expression ex = (Expression) ret;
FieldVar fvar = (FieldVar) ex;
String exp_str = fvar.getFieldVarName();
// Statement Cast evtl. problematisch
List<Statement> liste = new ArrayList<Statement>();
liste.add((Statement) fvar.receiver);
String reciever_str = expression2code(ass, liste);
return reciever_str + "." + exp_str;
}
} }
} }
return res; return res;
} }
public static String statement2code(List<AS_Abstract> ass, Statement statement) { public static String statement2code(List<AS_Abstract> ass, Statement statement) {
// TODO: Else if // TODO: Else if
@ -125,11 +121,19 @@ public class Algo_TypeExpr {
return exp_str; return exp_str;
} }
// fieldvar muss ausgelagert werden weil es von Expression ableitett
if (statement.getClass().equals(FieldVar.class)) { if (statement.getClass().equals(FieldVar.class)) {
// FieldVar ist nicht implementiert // Neu Implementiert - funktioniert es ??
} Expression ex = (Expression) statement;
FieldVar fvar = (FieldVar) ex;
String exp_str = fvar.getFieldVarName();
// Statement Cast evtl. problematisch
String reciever_str = statement2code(ass, (Statement) fvar.receiver);
return reciever_str + "." + exp_str;
}
if (statement.getClass().equals(MethodCall.class)) { if (statement.getClass().equals(MethodCall.class)) {
MethodCall mtcall = (MethodCall) statement; MethodCall mtcall = (MethodCall) statement;
@ -148,9 +152,6 @@ public class Algo_TypeExpr {
return "Fehler"; return "Fehler";
} }
public List<TC_Abstract> generate(List<AS_Abstract> ass, List<Statement> exp) { public List<TC_Abstract> generate(List<AS_Abstract> ass, List<Statement> exp) {
// Methoden enhält keine Statements // Methoden enhält keine Statements
@ -159,7 +160,6 @@ public class Algo_TypeExpr {
this.res_expression_typed = null; this.res_expression_typed = null;
} }
// Methoden enthält ein Return Statement // Methoden enthält ein Return Statement
if (exp.size() == 1) { if (exp.size() == 1) {
Statement std = (Statement) exp.get(0); Statement std = (Statement) exp.get(0);
@ -171,24 +171,21 @@ public class Algo_TypeExpr {
return null; return null;
} }
public List<TC_Abstract> generate_return(List<AS_Abstract> ass, Return ret) { public List<TC_Abstract> generate_return(List<AS_Abstract> ass, Return ret) {
Expression exp = ret.get_Expression(); Expression exp = ret.get_Expression();
if (exp.getClass() == LocalVar.class) { if (exp.getClass() == LocalVar.class) {
LocalVar lvar = (LocalVar) exp; LocalVar lvar = (LocalVar) exp;
// TODO wie soll ich hier mit der Language umgehen // TODO wie soll ich hier mit der Language umgehen
return new ArrayList<TC_Abstract>(); return new ArrayList<TC_Abstract>();
} }
if (exp.getClass() == FieldVar.class) { if (exp.getClass() == FieldVar.class) {
// Hier kommt der Constraint für die Field Var hinein.
} }
if (exp.getClass() == MethodCall.class) { if (exp.getClass() == MethodCall.class) {
MethodCall mt_call = (MethodCall) exp; MethodCall mt_call = (MethodCall) exp;
@ -198,26 +195,14 @@ public class Algo_TypeExpr {
return null; return null;
} }
public List<TC_Abstract> getRes_constriant_set() { public List<TC_Abstract> getRes_constriant_set() {
return res_constriant_set; return res_constriant_set;
} }
public String getRes_expression_typed() { public String getRes_expression_typed() {
return res_expression_typed; return res_expression_typed;
} }
// Neuer Versuch mit neuer herangehensweise um den Typen zu berechnen // Neuer Versuch mit neuer herangehensweise um den Typen zu berechnen
/* /*
* Es werden die Typen der Methoden berechnet und als String ausgegeben * Es werden die Typen der Methoden berechnet und als String ausgegeben
@ -227,35 +212,35 @@ public class Algo_TypeExpr {
if (o.getClass().equals(LocalVar.class)) { if (o.getClass().equals(LocalVar.class)) {
LocalVar v = (LocalVar) o; LocalVar v = (LocalVar) o;
return get_rt_Type_of_LocalVar(v, ass); return get_rt_Type_of_LocalVar(v, ass);
} } else if (o.getClass().equals(MethodCall.class)) {
else if (o.getClass().equals(MethodCall.class)) {
MethodCall m_call = (MethodCall) o; MethodCall m_call = (MethodCall) o;
return get_rt_Type_of_MethodCall(m_call, ass); return get_rt_Type_of_MethodCall(m_call, ass);
} } else if (o.getClass().equals(FieldVar.class)) {
else if (o.getClass().equals(FieldVar.class)){ FieldVar fvar = (FieldVar) o;
// FieldVar ist noch nicht implementiert String type_fvar = get_rt_Type_of_FieldVar(fvar, ass);
return null; return new TV_Dummy(type_fvar);
} } else {
else {
return null; return null;
} }
} }
public static TV_Alpha get_rt_Type_of_LocalVar(LocalVar v, List<AS_Abstract> ass) { public static TV_Alpha get_rt_Type_of_LocalVar(LocalVar v, List<AS_Abstract> ass) {
AS_Argument arg = AS_Abstract.find_AS_Argument_by_Name(v.get_expression(), ass); AS_Argument arg = AS_Abstract.find_AS_Argument_by_Name(v.get_expression(), ass);
return arg.tv_alpha; return arg.tv_alpha;
} }
public static TV_ReturnType2 get_rt_Type_of_MethodCall(MethodCall mt_call, List<AS_Abstract> ass) { public static TV_ReturnType2 get_rt_Type_of_MethodCall(MethodCall mt_call, List<AS_Abstract> ass) {
TV_Abstract tv_receiver = get_rt_Type_of(mt_call.get_Receiver().get_Expression(), ass); TV_Abstract tv_receiver = get_rt_Type_of(mt_call.get_Receiver().get_Expression(), ass);
TV_ReturnType2 res = new TV_ReturnType2(mt_call.get_Name(), tv_receiver.toString()); TV_ReturnType2 res = new TV_ReturnType2(mt_call.get_Name(), tv_receiver.toString());
return res; return res;
} }
public static String get_rt_Type_of_FieldVar(FieldVar fvar, List<AS_Abstract> ass) {
TV_Abstract exp = get_rt_Type_of(fvar.getReceiver(), ass);
return "DeltaXXX ( " + fvar.getFieldVarName() + "," + exp.toString() + ")";
}
// Neuer Versuch eine Methode berechneten Typen auszugeben // Neuer Versuch eine Methode berechneten Typen auszugeben
public static String get_Return_typed(Method m, List<AS_Abstract> ass) { public static String get_Return_typed(Method m, List<AS_Abstract> ass) {
@ -264,7 +249,6 @@ public class Algo_TypeExpr {
return get_rt_Type_of(ex, ass).toString(); return get_rt_Type_of(ex, ass).toString();
} }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
// Erstellen der Constraints. // Erstellen der Constraints.
// Auch dieses erstellen muss Rekursiv erfolgen // Auch dieses erstellen muss Rekursiv erfolgen
@ -273,20 +257,14 @@ public class Algo_TypeExpr {
public static List<TC_Abstract> generateConstraint(ClassOrInterface cl, List<AS_Abstract> ass) { public static List<TC_Abstract> generateConstraint(ClassOrInterface cl, List<AS_Abstract> ass) {
List<TC_Abstract> result = new ArrayList<>(); List<TC_Abstract> result = new ArrayList<>();
// 1. Generate Field Constraints // 1. Generate Field Constraints
result.addAll(generateFieldConstraints(cl, ass)); result.addAll(generateFieldConstraints(cl, ass));
// 2. Generate Method Constraints // 2. Generate Method Constraints
result.addAll(generateMethodConstraints(cl, ass)); result.addAll(generateMethodConstraints(cl, ass));
return result; return result;
} }
public static List<TC_Field> generateFieldConstraints(ClassOrInterface cl, List<AS_Abstract> ass) { public static List<TC_Field> generateFieldConstraints(ClassOrInterface cl, List<AS_Abstract> ass) {
List<TC_Field> result = new ArrayList<>(); List<TC_Field> result = new ArrayList<>();
@ -299,53 +277,58 @@ public class Algo_TypeExpr {
} }
/* /*
* Leider hat das erstellen der Constraints für die Methoden nicht mehr funktioniert * Leider hat das erstellen der Constraints für die Methoden nicht mehr
* Aus dies muss Rekursiv erfolgen * funktioniert Aus dies muss Rekursiv erfolgen
*/ */
public static List<TC_Method> generateMethodConstraints(ClassOrInterface cl, List<AS_Abstract> ass) { public static List<TC_Abstract> generateMethodConstraints(ClassOrInterface es, List<AS_Abstract> ass) {
List<TC_Method> result = new ArrayList<>();
List<TC_Abstract> constraints = new ArrayList<TC_Abstract>();
for (Method m : es.getMethods() ) {
Return r = (Return) m.get_Block().statements.get(0);
constraints.addAll(generateMethodConstraints(r.get_Expression(), ass));
}
return constraints;
}
public static List<TC_Abstract> generateMethodConstraints(Expression ex, List<AS_Abstract> ass) {
for(Method m : cl.getMethods()) {
Return r = (Return) m.get_Block().getStatements().get(0);
Expression ex = r.get_Expression();
if (ex.getClass().equals(LocalVar.class)) { if (ex.getClass().equals(LocalVar.class)) {
// Kein Constraint List<TC_Abstract> result = new ArrayList<>();
} return result;
else if (ex.getClass().equals(FieldVar.class)){ } else if (ex.getClass().equals(FieldVar.class)) {
// Noch nicht implementiert FieldVar v = (FieldVar) ex;
List<TC_Abstract> res = generateMethodConstraints(v.getReceiver(), ass);
TV_Abstract rt_type = get_rt_Type_of(v.getReceiver(),ass);
} // NEW TC field TC_Field()
else if (ex.getClass().equals(MethodCall.class)) { res.add(new TC_Dummy(String.format("TC_Field: ( %s , %s , %s )" , rt_type.toString() , v.getFieldVarName() , "TV_Field(" + rt_type.toString() + " , " + v.getFieldVarName() ) ));
return res;
} else if (ex.getClass().equals(MethodCall.class)) {
MethodCall m_call = (MethodCall) ex; MethodCall m_call = (MethodCall) ex;
List<TC_Abstract> res = generateMethodConstraints(m_call.get_Receiver().get_Expression(), ass);
TV_Abstract rt_type = get_rt_Type_of(m_call, ass);
TC_Method tc = new TC_Method(cl,m); res.add(new TC_Dummy("Klasse? " , m_call.get_Name() , "Arguments" , "ReturnType: " + rt_type.toString() , "Typvar: ?" ));
return res;
result.add(tc); } else {
}
else {
// Darf nicht vorkommen // Darf nicht vorkommen
System.out.println("Fehler - Type Expression"); System.out.println("Fehler - Type Expression");
return null;
} }
}
return result;
} }
} }

View File

@ -61,13 +61,13 @@ public class CodeGenerator {
String rt_type = Algo_TypeExpr.get_Return_typed(m, ass); String rt_type = Algo_TypeExpr.get_Return_typed(m, ass);
String code = rt_type +" " + m.getName() + " ( " ; String code = rt_type +" " + m.getName() + " ( " ;
// Generate Formalparameter // Generate Formalparameter
List<FormalParameter> fp = m.getParameterList().getFormalparalist(); List<FormalParameter> fp = m.getParameterList().getFormalparalist();
for (FormalParameter f : fp ) { for (FormalParameter f : fp ) {
AS_Argument arg = AS_Abstract.find_AS_Argument(f, ass); //AS_Argument arg = AS_Abstract.find_AS_Argument(f, ass);
AS_Argument arg = AS_Abstract.find_AS_Argument(f, m, ass);
code = code + arg.get_code() + ", "; code = code + arg.get_code() + ", ";
} }

View File

@ -0,0 +1,23 @@
package de.dhbwstuttgart.strucTypes;
public class TC_Dummy extends TC_Abstract {
private String dummy;
public TC_Dummy(String inhalt) {
this.dummy = inhalt;
}
public TC_Dummy(String type1, String method, String arguments_of_type , String returntype , String typvar) {
this.dummy = String.format("Mue( %s , %s , %s, (%s , %s )) " , type1, method, arguments_of_type, returntype, typvar);
}
public String toString() {
return dummy;
}
}

View File

@ -2,6 +2,7 @@ package de.dhbwstuttgart.strucTypes;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.Method; import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.statement.MethodCall;
/* Formal Idea: /* Formal Idea:
* *
@ -16,19 +17,39 @@ public class TC_Method extends TC_Abstract {
// Not completely implemented in the moment // Not completely implemented in the moment
private ClassOrInterface cl; private ClassOrInterface cl;
private TV_Abstract typvar;
private Method method; private Method method;
private MethodCall method_call;
// variante 1 -> Klasse ist bekannt
public TC_Method(ClassOrInterface cl, Method method ) { public TC_Method(ClassOrInterface cl, Method method ) {
super(); super();
this.cl = cl; this.cl = cl;
this.method = method; this.method = method;
typvar = null;
} }
// variante 2 -> Klasse ist nicht bekannt
public TC_Method(TV_Abstract typvar, MethodCall method_call) {
super();
this.cl = null;
this.method = null;
this.typvar = typvar;
this.method_call = method_call;
}
public String toString() { public String toString() {
if (cl != null) {
return "Mue( " + cl.getClassName().toString() + "," + method.getName() + "...)" ; return "Mue( " + cl.getClassName().toString() + "," + method.getName() + "...)" ;
} }
else {
return String.format("Mue( %s , %s ..." , typvar.toString() , method_call.get_Name() );
}
}

View File

@ -0,0 +1,18 @@
package de.dhbwstuttgart.strucTypes;
public class TV_Dummy extends TV_Abstract {
private String type = "";
public TV_Dummy(String type) {
this.type = type;
}
public String toString() {
return type;
}
}

View File

@ -0,0 +1,5 @@
package de.dhbwstuttgart.strucTypes2;
public class Assumption {
}

View File

@ -0,0 +1,7 @@
package de.dhbwstuttgart.strucTypes2;
public class Assumptions_Map {
private Map<TypePlaceholder, TVar> assumption_Map = new HashMap<>();
}

View File

@ -0,0 +1,36 @@
package de.dhbwstuttgart.strucTypes2;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.SourceFile;
public class CodeGenerator {
public static String generate_code_from_SourceFile(SourceFile sf) {
String res = "";
throw new NotImplementedException();
}
public static String generate_code_from_Class(ClassOrInterface cl) {
throw new NotImplementedException();
}
}

View File

@ -0,0 +1,27 @@
package de.dhbwstuttgart.strucTypes2;
import java.util.HashMap;
import java.util.Map;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
public class Collect_Types {
public static Map<RefTypeOrTPHOrWildcardOrGeneric,String> collect(SourceFile sf) {
Map<RefTypeOrTPHOrWildcardOrGeneric,String> typen = new HashMap<>();
// Sammle Typen aus Klassen
return typen;
}
}

View File

@ -0,0 +1,5 @@
package de.dhbwstuttgart.strucTypes2;
public class TV_Abstract {
}

View File

@ -0,0 +1,5 @@
package de.dhbwstuttgart.strucTypes2;
public class TV_Argument {
}

View File

@ -0,0 +1,42 @@
package de.dhbwstuttgart.strucTypes2;
import de.dhbwstuttgart.syntaxtree.Field;
public class TV_Field extends TV_Abstract {
private TV_Abstract type_of_class;
private Field field;
public TV_Field(TV_Abstract type_of_class, Field field) {
super();
this.type_of_class = type_of_class;
this.field = field;
}
public TV_Abstract getType_of_class() {
return type_of_class;
}
public void setType_of_class(TV_Abstract type_of_class) {
this.type_of_class = type_of_class;
}
public Field getField() {
return field;
}
public void setField(Field field) {
this.field = field;
}
}

View File

@ -0,0 +1,15 @@
package de.dhbwstuttgart.strucTypes2;
import de.dhbwstuttgart.typecheck.JavaClassName;
public class TV_Type extends TV_Abstract {
private JavaClassName name;
}

View File

@ -0,0 +1,64 @@
package de.dhbwstuttgart.strucTypes3;
import java.util.Map;
import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
public class AssTuple {
private Map<String, RefTypeOrTPHOrWildcardOrGeneric> fAss;
private Map<String, RefTypeOrTPHOrWildcardOrGeneric> mAss;
private Map<Method, Map<String, RefTypeOrTPHOrWildcardOrGeneric>> argAss;
public AssTuple(Map<String, RefTypeOrTPHOrWildcardOrGeneric> fAss,
Map<String, RefTypeOrTPHOrWildcardOrGeneric> mAss,
Map<Method, Map<String, RefTypeOrTPHOrWildcardOrGeneric>> argAss) {
super();
this.fAss = fAss;
this.mAss = mAss;
this.argAss = argAss;
}
public RefTypeOrTPHOrWildcardOrGeneric getTypeOfLocalVar(String name , Method method) {
// Schaue als erstes in den Argumenten dann in den Feldvariablen der Klasse
Map<String,RefTypeOrTPHOrWildcardOrGeneric> argAssMethod = argAss.get(method);
if (argAssMethod.containsKey(name)) {
return argAssMethod.get(name);
}
else if (fAss.containsKey(name)) {
return fAss.get(name);
}
else {
System.err.println("Fehler: Typ nicht bekannt");
return null;
}
}
public Map<String, RefTypeOrTPHOrWildcardOrGeneric> getfAss() {
return fAss;
}
public void setfAss(Map<String, RefTypeOrTPHOrWildcardOrGeneric> fAss) {
this.fAss = fAss;
}
public Map<String, RefTypeOrTPHOrWildcardOrGeneric> getmAss() {
return mAss;
}
public void setmAss(Map<String, RefTypeOrTPHOrWildcardOrGeneric> mAss) {
this.mAss = mAss;
}
public Map<Method, Map<String, RefTypeOrTPHOrWildcardOrGeneric>> getArgAss() {
return argAss;
}
public void setArgAss(Map<Method, Map<String, RefTypeOrTPHOrWildcardOrGeneric>> argAss) {
this.argAss = argAss;
}
}

View File

@ -0,0 +1,69 @@
package de.dhbwstuttgart.strucTypes3;
import java.util.HashMap;
import java.util.Map;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.Field;
import de.dhbwstuttgart.syntaxtree.FormalParameter;
import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
public class AssumptionGenerator {
/*
* Generiert die Assumptions einer Klasse
*/
public static AssTuple generateAssumptions(ClassOrInterface cl) {
// Erstellen der Assumptions
// (Assumption für die Argumente der Methode werden gleich
// miterstellt)
// Assumptions
Map<String, RefTypeOrTPHOrWildcardOrGeneric> fAss = new HashMap<>();
Map<String, RefTypeOrTPHOrWildcardOrGeneric> mAss = new HashMap<>();
// Assumptions für die MethodenParameter
Map<Method, Map<String, RefTypeOrTPHOrWildcardOrGeneric>> argAss = new HashMap<>();
AssTuple assAll = new AssTuple(fAss, mAss, argAss);
// Extraktion aller Typen der Methoden
// Assumptions für Felder
for (Field f : cl.getFieldDecl()) {
String fieldname = f.getName();
String type = f.getType().toString();
fAss.put(fieldname, f.getType());
System.out.println(String.format("Feldname: %s Typ: %s", fieldname, type));
}
// Assumptions für Methoden
for (Method m : cl.getMethods()) {
String methodName = m.getName();
String returnType = m.getType().toString();
mAss.put(methodName, m.getType());
String typeArg = "";
Map<String, RefTypeOrTPHOrWildcardOrGeneric> ass = new HashMap<>();
for (FormalParameter p : m.getParameterList().getFormalparalist()) {
String pName = p.getName();
String pType = p.getType().toString();
typeArg = typeArg + String.format("%s :%s ", pName, pType);
ass.put(p.getName(), p.getType());
}
argAss.put(m, ass);
System.out.println(String.format("Methodenname: %s , Arguments: %s -> ReturnType %s ", methodName, typeArg,
returnType));
}
// Assumption für die eingene aktuelle Klasse
fAss.put("this", cl.getType());
return assAll;
}
}

View File

@ -0,0 +1,5 @@
package de.dhbwstuttgart.strucTypes3;
public class ConstraintAbstract {
}

View File

@ -0,0 +1,49 @@
package de.dhbwstuttgart.strucTypes3;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
public class FieldConstraint extends ConstraintAbstract {
private RefTypeOrTPHOrWildcardOrGeneric reveiver;
private String name_of_field;
private RefTypeOrTPHOrWildcardOrGeneric attribut;
public FieldConstraint(RefTypeOrTPHOrWildcardOrGeneric receiver, String name_of_field,
RefTypeOrTPHOrWildcardOrGeneric attribut) {
super();
this.reveiver = receiver;
this.name_of_field = name_of_field;
this.attribut = attribut;
}
public String toString() {
return String.format("FieldConstraint(%s , %s , %s)" , reveiver, name_of_field, attribut );
}
public RefTypeOrTPHOrWildcardOrGeneric getReveiver() {
return reveiver;
}
public void setReveiver(RefTypeOrTPHOrWildcardOrGeneric reveiver) {
this.reveiver = reveiver;
}
public String getName_of_field() {
return name_of_field;
}
public void setName_of_field(String name_of_field) {
this.name_of_field = name_of_field;
}
public RefTypeOrTPHOrWildcardOrGeneric getAttribut() {
return attribut;
}
public void setAttribut(RefTypeOrTPHOrWildcardOrGeneric attribut) {
this.attribut = attribut;
}
}

View File

@ -0,0 +1,45 @@
package de.dhbwstuttgart.strucTypes3;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
public class GleichConstraint extends ConstraintAbstract {
private RefTypeOrTPHOrWildcardOrGeneric a;
private RefTypeOrTPHOrWildcardOrGeneric b;
// Type A ist Type B
public GleichConstraint(RefTypeOrTPHOrWildcardOrGeneric a, RefTypeOrTPHOrWildcardOrGeneric b) {
this.a = a;
this.b = b;
}
public String toString() {
return String.format("GleichConstraint( %s , %s )" , a.toString(), b.toString());
}
public RefTypeOrTPHOrWildcardOrGeneric getA() {
return a;
}
public void setA(RefTypeOrTPHOrWildcardOrGeneric a) {
this.a = a;
}
public RefTypeOrTPHOrWildcardOrGeneric getB() {
return b;
}
public void setB(RefTypeOrTPHOrWildcardOrGeneric b) {
this.b = b;
}
}

View File

@ -0,0 +1,212 @@
package de.dhbwstuttgart.strucTypes3;
import java.util.ArrayList;
import java.util.List;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.Method;
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.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.strucTypes3.AssTuple;
import de.dhbwstuttgart.strucTypes3.ConstraintAbstract;
import de.dhbwstuttgart.strucTypes3.FieldConstraint;
import de.dhbwstuttgart.strucTypes3.GleichConstraint;
import de.dhbwstuttgart.strucTypes3.MethodConstraint;
public class HelpMethoden {
public static String expression2String(Expression expression) {
// LocalVar
if (expression.getClass().equals(LocalVar.class)) {
String resultStr = "";
LocalVar lvar = (LocalVar) expression;
resultStr = resultStr + lvar.get_expression();
return resultStr;
}
// FeldVar
else if (expression.getClass().equals(FieldVar.class)) {
FieldVar fieldVar = (FieldVar) expression;
Expression receiver = fieldVar.getReceiver();
String result = HelpMethoden.expression2String(receiver);
result = result + "." + fieldVar.getFieldVarName();
return result;
}
// MethodCall
else if (expression.getClass().equals(MethodCall.class)) {
MethodCall methodCall = (MethodCall) expression;
// nur bei MethodCall reciever = Typ Reciver mit eigenem TPH
Expression receiver = methodCall.get_Receiver();
String result;
if (receiver.getClass().equals(Receiver.class)) {
Receiver r_cast = (Receiver) receiver;
result = HelpMethoden.expression2String(r_cast.get_Expression());
} else {
result = HelpMethoden.expression2String(receiver);
}
// Jetzt muss noch die ArgumentListe ermittelt werden
// Problem, Diese kann alles enthalten
String argumentsStr = "";
List<Expression> arguments = methodCall.get_ArgList().getArguments();
for (Expression arg : arguments) {
argumentsStr = argumentsStr + " " + HelpMethoden.expression2String(arg);
}
result = String.format("%s.%s(%s)", result, methodCall.get_Name(), argumentsStr);
return result;
} else {
System.err.println("Fehler");
return null;
}
}
public static String expression_typed2String(Expression expression) {
// LocalVar
if (expression.getClass().equals(LocalVar.class)) {
String resultStr = "";
LocalVar lvar = (LocalVar) expression;
resultStr = resultStr + lvar.get_expression() + ":" + lvar.getType().toString();
return resultStr;
}
// FeldVar
else if (expression.getClass().equals(FieldVar.class)) {
FieldVar fieldVar = (FieldVar) expression;
Expression receiver = fieldVar.getReceiver();
String result = HelpMethoden.expression_typed2String(receiver);
result = result + "." + fieldVar.getFieldVarName() + ":" + fieldVar.getType().toString();
return result;
}
// MethodCall
else if (expression.getClass().equals(MethodCall.class)) {
MethodCall methodCall = (MethodCall) expression;
// nur bei MethodCall reciever = Typ Reciver mit eigenem TPH
Expression receiver = methodCall.get_Receiver();
String result;
if (receiver.getClass().equals(Receiver.class)) {
Receiver r_cast = (Receiver) receiver;
result = HelpMethoden.expression_typed2String(r_cast.get_Expression());
} else {
result = HelpMethoden.expression_typed2String(receiver) + ":" + receiver.getType().toString();
}
// Jetzt muss noch die ArgumentListe ermittelt werden
// Problem, Diese kann alles enthalten
String argumentsStr = "";
List<Expression> arguments = methodCall.get_ArgList().getArguments();
for (Expression arg : arguments) {
argumentsStr = argumentsStr + " " + HelpMethoden.expression_typed2String(arg) ;//+ arg.getType();
}
result = String.format("%s.%s(%s)", result, methodCall.get_Name(), argumentsStr);
return result;
} else {
System.err.println("Fehler");
return null;
}
}
// Diese Methode übernimmt die Berechnung der Typen
public static List<ConstraintAbstract> TypeExpr(AssTuple ass, Expression expression, Method method, ClassOrInterface cl) {
// LocalVar (Constraints für den Algo, Constraints für die Interfaces
// usw. )
// LocalVar ------------------------------------------------------------------------------------------
if (expression.getClass().equals(LocalVar.class)) {
LocalVar lvar = (LocalVar) expression;
List<ConstraintAbstract> constraints = new ArrayList<ConstraintAbstract>();
RefTypeOrTPHOrWildcardOrGeneric type = ass.getTypeOfLocalVar(lvar.get_expression(), method);
GleichConstraint constraint = new GleichConstraint(lvar.getType(), type);
constraints.add(constraint);
return constraints;
}
// FeldVar ------------------------------------------------------------------------------------------
else if (expression.getClass().equals(FieldVar.class)) {
FieldVar fieldVar = (FieldVar) expression;
Expression receiver = fieldVar.getReceiver();
List<ConstraintAbstract> constraints = HelpMethoden.TypeExpr(ass, receiver, method, cl);
// Hier muss der Constraint angelegt werden. Der Type receiver X besitzt ein feld mit dem Typen Y
// constriant(c,f,c')
// constraint(TypeKlasse, feldname, typdesfelde)
// constriant(typReveier , besitzt ein feld mit dem typen , typdesfeldes);
// constriant(typReceiver, name des Feldes , TypDesFeldes).
FieldConstraint constraint = new FieldConstraint(receiver.getType(), fieldVar.getFieldVarName() , fieldVar.getType());
constraints.add(constraint);
return constraints;
}
// MethodCall ------------------------------------------------------------------------------------------
else if (expression.getClass().equals(MethodCall.class)) {
MethodCall methodCall = (MethodCall) expression;
// nur bei MethodCall reciever = Typ Reciver mit eigenem TPH
Expression receiver = methodCall.get_Receiver();
List<ConstraintAbstract> constraints;
if (receiver.getClass().equals(Receiver.class)) {
Receiver r_cast = (Receiver) receiver;
constraints = HelpMethoden.TypeExpr(ass, r_cast.get_Expression(), method, cl);
} else {
constraints = HelpMethoden.TypeExpr(ass, receiver, method, cl);
}
// An dieser Stelle wird der Methoden Constraint erstellt
/*
*
*/
List<MyTypeVar> argumentTypesVars = new ArrayList<>();
List<RefTypeOrTPHOrWildcardOrGeneric> argumentTypes = new ArrayList<>();
Integer index_arg = 1;
for (Expression arg : methodCall.get_ArgList().getArguments()) {
argumentTypesVars.add(new MyTypeVar(methodCall.get_Name(), index_arg , receiver.getType()));
index_arg = index_arg +1;
constraints.addAll(HelpMethoden.TypeExpr(ass, arg, method, cl));
argumentTypes.add(arg.getType());
}
/*
// Es wird der Typ des übergabeparameter erstellt
List<SubTypeConstraint> puffer = new ArrayList<>();
for (RefTypeOrTPHOrWildcardOrGeneric type : argumentTypesVars) {
for (ConstraintAbstract c : constraints) {
if (c.getClass().equals(GleichConstraint.class)) {
GleichConstraint gc = (GleichConstraint) c;
if (gc.getA().equals(type)) {
argumentTypes.add(gc.getB());
// subtype
puffer.add(new SubTypeConstraint(gc.getB() , gc.getA() ));
}
}
}
}
constraints.addAll(puffer);
*/
MethodConstraint methodConstraint = new MethodConstraint(receiver.getType(), methodCall.get_Name(), argumentTypes, methodCall.getType(), argumentTypesVars );
constraints.add(methodConstraint);
return constraints;
} else {
System.err.println("Fehler");
return null;
}
}
}

View File

@ -0,0 +1,104 @@
package de.dhbwstuttgart.strucTypes3;
import java.util.List;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
public class MethodConstraint extends ConstraintAbstract {
/* Der MethodConstraint
* constraint( typ_receiver, methodname, arguments_typen ( return_type , typvar von argumenttypen )
*/
private RefTypeOrTPHOrWildcardOrGeneric receiver;
private String methodName;
private List<RefTypeOrTPHOrWildcardOrGeneric> argumentTypes;
private RefTypeOrTPHOrWildcardOrGeneric returnType;
private List<MyTypeVar> argumentTypeVars;
public MethodConstraint(RefTypeOrTPHOrWildcardOrGeneric receiver, String methodName,
List<RefTypeOrTPHOrWildcardOrGeneric> argumentTypes, RefTypeOrTPHOrWildcardOrGeneric returnType,
List<MyTypeVar> argumentTypeVars) {
super();
this.receiver = receiver;
this.methodName = methodName;
this.argumentTypes = argumentTypes;
this.returnType = returnType;
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<RefTypeOrTPHOrWildcardOrGeneric> getArgumentTypes() {
return argumentTypes;
}
public void setArgumentTypes(List<RefTypeOrTPHOrWildcardOrGeneric> argumentTypes) {
this.argumentTypes = argumentTypes;
}
public RefTypeOrTPHOrWildcardOrGeneric getReturnType() {
return returnType;
}
public void setReturnType(RefTypeOrTPHOrWildcardOrGeneric returnType) {
this.returnType = returnType;
}
public List<MyTypeVar> getArgumentTypeVars() {
return argumentTypeVars;
}
public void setArgumentTypeVars(List<MyTypeVar> 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() );
}
}

View File

@ -0,0 +1,53 @@
package de.dhbwstuttgart.strucTypes3;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
public class MyTypeVar {
private String method_name;
private Integer index_Argument;
private RefTypeOrTPHOrWildcardOrGeneric typevar;
public MyTypeVar(String method_name, Integer index_Argument, RefTypeOrTPHOrWildcardOrGeneric typevar) {
super();
this.method_name = method_name;
this.index_Argument = index_Argument;
this.typevar = typevar;
}
public String getMethod_name() {
return method_name;
}
public void setMethod_name(String method_name) {
this.method_name = method_name;
}
public Integer getIndex_Argument() {
return index_Argument;
}
public void setIndex_Argument(Integer index_Argument) {
this.index_Argument = index_Argument;
}
public RefTypeOrTPHOrWildcardOrGeneric getTypevar() {
return typevar;
}
public void setTypevar(RefTypeOrTPHOrWildcardOrGeneric typevar) {
this.typevar = typevar;
}
public String toString() {
return String.format("TypVar?( %s , %s , %s )" , method_name , index_Argument , typevar );
}
}

View File

@ -0,0 +1,45 @@
package de.dhbwstuttgart.strucTypes3;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
public class SubTypeConstraint extends ConstraintAbstract {
private RefTypeOrTPHOrWildcardOrGeneric basisKlasse;
private RefTypeOrTPHOrWildcardOrGeneric subTypeKlasse;
public SubTypeConstraint(RefTypeOrTPHOrWildcardOrGeneric basisKlasse,
RefTypeOrTPHOrWildcardOrGeneric subTypeKlasse) {
super();
this.basisKlasse = basisKlasse;
this.subTypeKlasse = subTypeKlasse;
}
public String toString() {
return String.format("SubType( %s <* %s ) " ,basisKlasse, subTypeKlasse);
}
public RefTypeOrTPHOrWildcardOrGeneric getBasisKlasse() {
return basisKlasse;
}
public void setBasisKlasse(RefTypeOrTPHOrWildcardOrGeneric basisKlasse) {
this.basisKlasse = basisKlasse;
}
public RefTypeOrTPHOrWildcardOrGeneric getSubTypeKlasse() {
return subTypeKlasse;
}
public void setSubTypeKlasse(RefTypeOrTPHOrWildcardOrGeneric subTypeKlasse) {
this.subTypeKlasse = subTypeKlasse;
}
}

View File

@ -0,0 +1,102 @@
package de.dhbwstuttgart.strucTypes3;
import java.util.List;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.Field;
import de.dhbwstuttgart.syntaxtree.FieldDeclaration;
import de.dhbwstuttgart.syntaxtree.FormalParameter;
import de.dhbwstuttgart.syntaxtree.GenericTypeVar;
import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.statement.Block;
import de.dhbwstuttgart.syntaxtree.statement.Expression;
import de.dhbwstuttgart.syntaxtree.statement.Return;
import de.dhbwstuttgart.syntaxtree.statement.Statement;
public class ToStringGenerator {
public static String klasseToString(ClassOrInterface cl, AssTuple assAll) {
// Als nächstes muss die Expression getypt werden
for (Method m : cl.getMethods()) {
Block block = m.get_Block();
/*
* Wahrscheinlich ist der TPH des Blocks der wahre Typ und der Typ
* der methode kann auch kleiner sein
*/
System.out.println("Block" + block.getType().toString());
// ein block hat einen TPH und eine Statement Liste
// davon wird das erste statement genommen welches ein
// Return Statement ist
// TODO: check get 0
Statement statement = block.getStatements().get(0);
if (statement.getClass().equals(Return.class)) {
Return rStatement = (Return) statement;
Expression expression = rStatement.get_Expression();
// An dieser Stelle muss ein Rekursiver Aufruf
// möglich werden
String exp = HelpMethoden.expression2String(expression);
System.out.println("Expression: " + exp);
String exp_t = HelpMethoden.expression_typed2String(expression);
System.out.println("Expression_typed: " + exp_t);
List<ConstraintAbstract> constraints = HelpMethoden.TypeExpr(assAll, expression, m, cl);
for (ConstraintAbstract c : constraints) {
System.out.println(c.toString());
}
} else {
System.err.println("Problem");
}
}
return null;
}
public static String interface2String(ClassOrInterface face) {
// Ausgabe name
String n = face.getClassName().toString();
// Ausgabe Generics
String generics = "";
//for ( GenericTypeVar f : face.getGenericDeclarationList()) {
//generics = generics + " " + f.toString();
//}
// Ausgabe der Felder
String felder = "";
for (Field f : face.getFieldDecl()) {
felder = f.toString();
}
// Ausgabe der Methoden
String methods = "";
for (Method m : face.getMethods()) {
String mName = m.getName() + " ( ";
//m.getParameterList().getFormalparalist().forEach((x) -> { mName.concat(x.getName().toString()); });
for (FormalParameter fp : m.getParameterList().getFormalparalist()) {
mName = mName + " "+ fp.getName();
}
methods = methods + mName + ") " ;
}
return String.format(" interface: %s %s %s %s \n" , n, generics , felder, methods);
}
}

View File

@ -54,6 +54,10 @@ public class ClassOrInterface extends GTVDeclarationContext implements IItemWith
return this.name; return this.name;
} }
public GenericDeclarationList getGenericDeclarationList() {
return this.genericClassParameters;
}
// Get modifiers // Get modifiers
public int getModifiers(){ public int getModifiers(){
return this.modifiers; return this.modifiers;

View File

@ -40,4 +40,19 @@ public class FieldVar extends Expression {
ret.addOderConstraint(oderConstraints); ret.addOderConstraint(oderConstraints);
return ret; return ret;
} }
// @ Sebastian
public String getFieldVarName() {
return fieldVarName;
}
// @ Sebastian
public Expression getReceiver() {
return receiver;
}
} }

View File

@ -5,7 +5,6 @@ import static org.junit.Assert.*;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import de.dhbwstuttgart.parser.JavaTXParser; import de.dhbwstuttgart.parser.JavaTXParser;
import de.dhbwstuttgart.strucTypes.AS_Abstract; import de.dhbwstuttgart.strucTypes.AS_Abstract;
import de.dhbwstuttgart.strucTypes.TC_Abstract; import de.dhbwstuttgart.strucTypes.TC_Abstract;
@ -13,6 +12,7 @@ import de.dhbwstuttgart.strucTypes.Algo_Type;
import de.dhbwstuttgart.strucTypes.Algo_TypeExpr; import de.dhbwstuttgart.strucTypes.Algo_TypeExpr;
import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.statement.CastExpr; import de.dhbwstuttgart.syntaxtree.statement.CastExpr;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import org.junit.Test; import org.junit.Test;
@ -41,8 +41,9 @@ public class GeneralParserTest{
//filenames.add("GenericFieldVarTest.jav"); //filenames.add("GenericFieldVarTest.jav");
//filenames.add("FieldVarTest.jav"); //filenames.add("FieldVarTest.jav");
//filenames.add("StructuralTypesField.jav");
filenames.add("StructuralTypesSimple3.jav"); filenames.add("StructuralTypesSimple3.jav");
//filenames.add("StructuralTypesField.jav");
//filenames.add("Main.jav");
JavaTXParser parser = new JavaTXParser(); JavaTXParser parser = new JavaTXParser();
try{ try{
@ -60,15 +61,17 @@ public class GeneralParserTest{
// Print der Expression: // Print der Expression:
System.out.println(Algo_TypeExpr.expression2code(ass, sf.getClasses().get(0).getMethods().get(0).get_Block().statements)); //System.out.println(Algo_TypeExpr.expression2code(ass, sf.getClasses().get(0).getMethods().get(0).get_Block().statements));
System.out.println(Algo_TypeExpr.expression2code(ass, sf.getClasses().get(0).getMethods().get(2).get_Block().statements)); //System.out.println(Algo_TypeExpr.expression2code(ass, sf.getClasses().get(0).getMethods().get(2).get_Block().statements));
// Erstellen der TypeExpression // Erstellen der TypeExpression
// Wegen java Build path
if (false) {
throw new NotImplementedException();
}
} }

View File

@ -0,0 +1,70 @@
package strucTypes;
import static org.junit.Assert.*;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import de.dhbwstuttgart.parser.JavaTXParser;
import de.dhbwstuttgart.strucTypes.AS_Abstract;
import de.dhbwstuttgart.strucTypes.TC_Abstract;
import de.dhbwstuttgart.strucTypes.Algo_Type;
import de.dhbwstuttgart.strucTypes.Algo_TypeExpr;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.statement.CastExpr;
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 GeneralParserTest_Main{
private static final String rootDirectory = System.getProperty("user.dir")+"/test/strucTypes/";
@Test
public void run(){
/*
* Auflistung aller Files die getestet werden sollen
*/
List<String> filenames = new ArrayList<String>();
//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("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;
}
}catch(Exception exc){
exc.printStackTrace();
fail();
}
assertTrue("Tests durchlaufen",filenames.size()>0);
}
}

6
test/strucTypes/Main.jav Normal file
View File

@ -0,0 +1,6 @@
class Main {
main() { return new A<>().mt(new myInteger(1)); }
}

View File

@ -3,14 +3,18 @@ class A {
A f1; A f1;
A f2; f2;
mt1(x) { return x; } mt1(x1) { return x1; }
mt2(x2) { return x2.feld_von_x ; }
mt3(xaver,y,z) { return xaver.sub(y).add(z); }
mt4(x,y,z) { return x.f.add(y.sub(z,x)); }
//mt2(x) { return x.feld ; }
mt3(x,y,z) { return x.sub(y).add(z); }
} }

View File

@ -0,0 +1,65 @@
package strucTypes2;
import java.util.Map;
import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
class AssTuple {
private Map<String, RefTypeOrTPHOrWildcardOrGeneric> fAss;
private Map<String, RefTypeOrTPHOrWildcardOrGeneric> mAss;
private Map<Method, Map<String, RefTypeOrTPHOrWildcardOrGeneric>> argAss;
public AssTuple(Map<String, RefTypeOrTPHOrWildcardOrGeneric> fAss,
Map<String, RefTypeOrTPHOrWildcardOrGeneric> mAss,
Map<Method, Map<String, RefTypeOrTPHOrWildcardOrGeneric>> argAss) {
super();
this.fAss = fAss;
this.mAss = mAss;
this.argAss = argAss;
}
public RefTypeOrTPHOrWildcardOrGeneric getTypeOfLocalVar(String name , Method method) {
// Schaue als erstes in den Argumenten dann in den Feldvariablen der Klasse
Map<String,RefTypeOrTPHOrWildcardOrGeneric> argAssMethod = argAss.get(method);
if (argAssMethod.containsKey(name)) {
return argAssMethod.get(name);
}
else if (fAss.containsKey(name)) {
return fAss.get(name);
}
else {
System.err.println("Fehler: Typ nicht bekannt");
return null;
}
}
public Map<String, RefTypeOrTPHOrWildcardOrGeneric> getfAss() {
return fAss;
}
public void setfAss(Map<String, RefTypeOrTPHOrWildcardOrGeneric> fAss) {
this.fAss = fAss;
}
public Map<String, RefTypeOrTPHOrWildcardOrGeneric> getmAss() {
return mAss;
}
public void setmAss(Map<String, RefTypeOrTPHOrWildcardOrGeneric> mAss) {
this.mAss = mAss;
}
public Map<Method, Map<String, RefTypeOrTPHOrWildcardOrGeneric>> getArgAss() {
return argAss;
}
public void setArgAss(Map<Method, Map<String, RefTypeOrTPHOrWildcardOrGeneric>> argAss) {
this.argAss = argAss;
}
}

View File

@ -0,0 +1,5 @@
package strucTypes2;
public class Assumption_Abstract {
}

View File

@ -0,0 +1,36 @@
package strucTypes2;
import de.dhbwstuttgart.syntaxtree.Field;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
public class Assumption_Field {
TypePlaceholder tph;
Field field;
public Assumption_Field(RefTypeOrTPHOrWildcardOrGeneric tph, Field field) {
if (tph.getClass().equals(TypePlaceholder.class)) {
this.tph = (TypePlaceholder) tph;
}
else {
System.out.println("Fehler");
}
this.field = field;
}
public String toString() {
return "Assumption" + tph.toString() + " , " + field.getName();
}
}

View File

@ -0,0 +1,31 @@
package strucTypes2;
import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
public class Assumption_Method {
TypePlaceholder tph;
Method m;
public Assumption_Method(RefTypeOrTPHOrWildcardOrGeneric tph, Method m) {
if (tph.getClass().equals(TypePlaceholder.class)) {
this.tph = (TypePlaceholder) tph;
}
else {
System.out.println("Fehler");
}
this.m = m;
}
public String toString() {
return "Assumption Method: " + tph.toString() + " , " + m.getName();
}
}

View File

@ -0,0 +1,42 @@
package strucTypes2;
import java.util.ArrayList;
import java.util.List;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.Field;
import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.SourceFile;
public class FindAssumptions {
public static void generateFieldAssumptions(ClassOrInterface cl) {
List<Assumption_Field> res = new ArrayList<Assumption_Field>();
for (Field f : cl.getFieldDecl()) {
res.add(new Assumption_Field(f.getType(), f));
}
}
public static void generateMethodAssumptions(ClassOrInterface cl) {
List<Assumption_Method> res = new ArrayList<Assumption_Method>();
for (Method m : cl.getMethods() ) {
res.add(new Assumption_Method())
}
}
public static void generateArgumentAssumptions() {
}
}

View File

@ -0,0 +1,74 @@
package strucTypes2;
import static org.junit.Assert.*;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import de.dhbwstuttgart.parser.JavaTXParser;
import de.dhbwstuttgart.strucTypes.AS_Abstract;
import de.dhbwstuttgart.strucTypes.TC_Abstract;
import de.dhbwstuttgart.strucTypes.Algo_Type;
import de.dhbwstuttgart.strucTypes.Algo_TypeExpr;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.statement.CastExpr;
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 GeneralParserTest{
private static final String rootDirectory = System.getProperty("user.dir")+"/test/strucTypes/";
@Test
public void run(){
/*
* Auflistung aller Files die getestet werden sollen
*/
List<String> filenames = new ArrayList<String>();
//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("StructuralTypesSimple3.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;
// Hier soll der CodeGenerator hin.
}
}catch(Exception exc){
exc.printStackTrace();
fail();
}
assertTrue("Tests durchlaufen",filenames.size()>0);
}
}

View File

@ -0,0 +1,435 @@
package strucTypes2;
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.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 GeneralParserTest2 {
private static final String rootDirectory = System.getProperty("user.dir") + "/test/strucTypes/";
@Test
public void run() {
/*
* Auflistung aller Files die getestet werden sollen
*/
List<String> filenames = new ArrayList<String>();
// 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("StructuralTypesSimple3.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 Assumptions
// (Assumption für die Argumente der Methode werden gleich
// miterstellt)
// Assumptions
Map<String, RefTypeOrTPHOrWildcardOrGeneric> fAss = new HashMap<>();
Map<String, RefTypeOrTPHOrWildcardOrGeneric> mAss = new HashMap<>();
// Assumptions für die MethodenParameter
Map<Method, Map<String, RefTypeOrTPHOrWildcardOrGeneric>> argAss = new HashMap<>();
AssTuple assAll = new AssTuple(fAss,mAss,argAss);
// Extraktion aller Typen der Methoden
for (ClassOrInterface cl : sf.getClasses()) {
for (Field f : cl.getFieldDecl()) {
String fieldname = f.getName();
String type = f.getType().toString();
fAss.put(fieldname, f.getType());
System.out.println(String.format("Feldname: %s Typ: %s", fieldname, type));
}
for (Method m : cl.getMethods()) {
String methodName = m.getName();
String returnType = m.getType().toString();
mAss.put(methodName, m.getType());
String typeArg = "";
Map<String, RefTypeOrTPHOrWildcardOrGeneric> ass = new HashMap<>();
for (FormalParameter p : m.getParameterList().getFormalparalist()) {
String pName = p.getName();
String pType = p.getType().toString();
typeArg = typeArg + String.format("%s : %s", pName, pType);
ass.put(p.getName(), p.getType());
}
argAss.put(m, ass);
System.out.println(String.format("Methodenname: %s , Arguments: %s -> ReturnType %s",
methodName, typeArg, returnType));
}
}
// Als nächstes muss die Expression getypt werden
for (ClassOrInterface cl : sf.getClasses()) {
for (Method m : cl.getMethods()) {
Block block = m.get_Block();
/*
* Wahrscheinlich ist der TPH des Blocks der wahre Typ
* und der Typ der methode kann auch kleiner sein
*/
System.out.println("Block" + block.getType().toString());
// ein block hat einen TPH und eine Statement Liste
// davon wird das erste statement genommen welches ein
// Return Statement ist
// TODO: check get 0
Statement statement = block.getStatements().get(0);
if (statement.getClass().equals(Return.class)) {
Return rStatement = (Return) statement;
Expression expression = rStatement.get_Expression();
// An dieser Stelle muss ein Rekursiver Aufruf
// möglich werden
String exp = HelpMethoden.expression2String(expression);
System.out.println("Expression: " + exp);
String exp_t = HelpMethoden.expression_typed2String(expression);
System.out.println("Expression_typed: " + exp_t);
List<ConstraintAbstract> constraints = HelpMethoden.TypeExpr(assAll, expression, m, cl);
for (ConstraintAbstract c : constraints) {
System.out.println(c.toString());
}
} else {
System.err.println("Problem");
}
}
}
}
} catch (Exception exc) {
exc.printStackTrace();
fail();
}
assertTrue("Tests durchlaufen", filenames.size() > 0);
}
}
class HelpMethoden {
public static String expression2String(Expression expression) {
// LocalVar
if (expression.getClass().equals(LocalVar.class)) {
String resultStr = "";
LocalVar lvar = (LocalVar) expression;
resultStr = resultStr + lvar.get_expression();
return resultStr;
}
// FeldVar
else if (expression.getClass().equals(FieldVar.class)) {
FieldVar fieldVar = (FieldVar) expression;
Expression receiver = fieldVar.getReceiver();
String result = HelpMethoden.expression2String(receiver);
result = result + "." + fieldVar.getFieldVarName();
return result;
}
// MethodCall
else if (expression.getClass().equals(MethodCall.class)) {
MethodCall methodCall = (MethodCall) expression;
// nur bei MethodCall reciever = Typ Reciver mit eigenem TPH
Expression receiver = methodCall.get_Receiver();
String result;
if (receiver.getClass().equals(Receiver.class)) {
Receiver r_cast = (Receiver) receiver;
result = HelpMethoden.expression2String(r_cast.get_Expression());
} else {
result = HelpMethoden.expression2String(receiver);
}
// Jetzt muss noch die ArgumentListe ermittelt werden
// Problem, Diese kann alles enthalten
String argumentsStr = "";
List<Expression> arguments = methodCall.get_ArgList().getArguments();
for (Expression arg : arguments) {
argumentsStr = argumentsStr + " " + HelpMethoden.expression2String(arg);
}
result = String.format("%s.%s(%s)", result, methodCall.get_Name(), argumentsStr);
return result;
} else {
System.err.println("Fehler");
return null;
}
}
public static String expression_typed2String(Expression expression) {
// LocalVar
if (expression.getClass().equals(LocalVar.class)) {
String resultStr = "";
LocalVar lvar = (LocalVar) expression;
resultStr = resultStr + lvar.get_expression() + ":" + lvar.getType().toString();
return resultStr;
}
// FeldVar
else if (expression.getClass().equals(FieldVar.class)) {
FieldVar fieldVar = (FieldVar) expression;
Expression receiver = fieldVar.getReceiver();
String result = HelpMethoden.expression_typed2String(receiver);
result = result + "." + fieldVar.getFieldVarName() + ":" + fieldVar.getType().toString();
return result;
}
// MethodCall
else if (expression.getClass().equals(MethodCall.class)) {
MethodCall methodCall = (MethodCall) expression;
// nur bei MethodCall reciever = Typ Reciver mit eigenem TPH
Expression receiver = methodCall.get_Receiver();
String result;
if (receiver.getClass().equals(Receiver.class)) {
Receiver r_cast = (Receiver) receiver;
result = HelpMethoden.expression_typed2String(r_cast.get_Expression());
} else {
result = HelpMethoden.expression_typed2String(receiver) + ":" + receiver.getType().toString();
}
// Jetzt muss noch die ArgumentListe ermittelt werden
// Problem, Diese kann alles enthalten
String argumentsStr = "";
List<Expression> arguments = methodCall.get_ArgList().getArguments();
for (Expression arg : arguments) {
argumentsStr = argumentsStr + " " + HelpMethoden.expression_typed2String(arg) ;//+ arg.getType();
}
result = String.format("%s.%s(%s)", result, methodCall.get_Name(), argumentsStr);
return result;
} else {
System.err.println("Fehler");
return null;
}
}
// Diese Methode übernimmt die Berechnung der Typen
public static List<ConstraintAbstract> TypeExpr(AssTuple ass, Expression expression, Method method, ClassOrInterface cl) {
// LocalVar (Constraints für den Algo, Constraints für die Interfaces
// usw. )
// LocalVar
if (expression.getClass().equals(LocalVar.class)) {
LocalVar lvar = (LocalVar) expression;
List<ConstraintAbstract> constraints = new ArrayList<ConstraintAbstract>();
RefTypeOrTPHOrWildcardOrGeneric type = ass.getTypeOfLocalVar(lvar.get_expression(), method);
GleichConstraint constraint = new GleichConstraint(lvar.getType(), type);
constraints.add(constraint);
return constraints;
}
// FeldVar
else if (expression.getClass().equals(FieldVar.class)) {
FieldVar fieldVar = (FieldVar) expression;
Expression receiver = fieldVar.getReceiver();
List<ConstraintAbstract> constraints = HelpMethoden.TypeExpr(ass, receiver, method, cl);
//TODO Hier muss der Constraint angelegt werden. Der Type receiver X besitzt ein feld mit dem Typen Y
//TODO constriant(c,f,c')
//TODO constraint(TypeKlasse, feldname, typdesfelde)
//TODO constriant(typReveier , besitzt ein feld mit dem typen , typdesfeldes);
//TODO constriant(typReceiver, name des Feldes , TypDesFeldes).
FieldConstraint constraint = new FieldConstraint(receiver.getType(), fieldVar.getFieldVarName() , fieldVar.getType());
constraints.add(constraint);
return constraints;
}
// MethodCall
else if (expression.getClass().equals(MethodCall.class)) {
MethodCall methodCall = (MethodCall) expression;
// nur bei MethodCall reciever = Typ Reciver mit eigenem TPH
Expression receiver = methodCall.get_Receiver();
List<ConstraintAbstract> constraints;
if (receiver.getClass().equals(Receiver.class)) {
Receiver r_cast = (Receiver) receiver;
constraints = HelpMethoden.TypeExpr(ass, r_cast.get_Expression(), method, cl);
} else {
constraints = HelpMethoden.TypeExpr(ass, receiver, method, cl);
}
//TODO An dieser Stelle muss noch der MethodenConstraint erstellt werden
List<RefTypeOrTPHOrWildcardOrGeneric> argumentTypesVars = new ArrayList<>();
List<RefTypeOrTPHOrWildcardOrGeneric> argumentTypes = new ArrayList<>();
for (Expression arg : methodCall.get_ArgList().getArguments()) {
argumentTypesVars.add(arg.getType());
constraints.addAll(HelpMethoden.TypeExpr(ass, arg, method, cl));
}
MethodConstraint methodConstraint = new MethodConstraint(receiver.getType(), methodCall.get_Name(), argumentTypes, methodCall.getType(), argumentTypesVars );
constraints.add(methodConstraint);
return constraints;
} else {
System.err.println("Fehler");
return null;
}
}
}
/****************************************
* Definition der Constraint Klassen
*/
class ConstraintAbstract {
}
class GleichConstraint extends ConstraintAbstract {
private RefTypeOrTPHOrWildcardOrGeneric a;
private RefTypeOrTPHOrWildcardOrGeneric b;
// Type A ist Type B
public GleichConstraint(RefTypeOrTPHOrWildcardOrGeneric a, RefTypeOrTPHOrWildcardOrGeneric b) {
this.a = a;
this.b = b;
}
public String toString() {
return String.format("GleichConstraint( %s , %s )" , a.toString(), b.toString());
}
}
class FieldConstraint extends ConstraintAbstract {
private RefTypeOrTPHOrWildcardOrGeneric reveiver;
private String name_of_field;
private RefTypeOrTPHOrWildcardOrGeneric attribut;
public FieldConstraint(RefTypeOrTPHOrWildcardOrGeneric receiver, String name_of_field,
RefTypeOrTPHOrWildcardOrGeneric attribut) {
super();
this.reveiver = receiver;
this.name_of_field = name_of_field;
this.attribut = attribut;
}
public String toString() {
return String.format("FieldConstraint(%s , %s , %s)" , reveiver, name_of_field, attribut );
}
public RefTypeOrTPHOrWildcardOrGeneric getReveiver() {
return reveiver;
}
public void setReveiver(RefTypeOrTPHOrWildcardOrGeneric reveiver) {
this.reveiver = reveiver;
}
public String getName_of_field() {
return name_of_field;
}
public void setName_of_field(String name_of_field) {
this.name_of_field = name_of_field;
}
public RefTypeOrTPHOrWildcardOrGeneric getAttribut() {
return attribut;
}
public void setAttribut(RefTypeOrTPHOrWildcardOrGeneric attribut) {
this.attribut = attribut;
}
}
class MethodConstraint extends ConstraintAbstract {
/* Der MethodConstraint
* constraint( typ_receiver, methodname, arguments_typen ( return_type , typvar von argumenttypen )
*/
private RefTypeOrTPHOrWildcardOrGeneric receiver;
private String methodName;
private List<RefTypeOrTPHOrWildcardOrGeneric> argumentTypes;
private RefTypeOrTPHOrWildcardOrGeneric returnType;
private List<RefTypeOrTPHOrWildcardOrGeneric> argumentTypeVars;
public MethodConstraint(RefTypeOrTPHOrWildcardOrGeneric receiver, String methodName,
List<RefTypeOrTPHOrWildcardOrGeneric> argumentTypes, RefTypeOrTPHOrWildcardOrGeneric returnType,
List<RefTypeOrTPHOrWildcardOrGeneric> argumentTypeVars) {
super();
this.receiver = receiver;
this.methodName = methodName;
this.argumentTypes = argumentTypes;
this.returnType = returnType;
this.argumentTypeVars = argumentTypeVars;
}
public String toString() {
return String.format("MethodConstraint( %s, %s , %s (%s , %s ))" , receiver.toString() , methodName, "arg" , returnType.toString() , argumentTypeVars.toString() );
}
}

View File

@ -0,0 +1,16 @@
class A {
A f1;
A f2;
mt1(x) { return x; }
mt2(x) { return x.feld_von_x ; }
mt3(x,y,z) { return x.sub(y).add(z); }
}

View File

@ -0,0 +1,109 @@
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<Field> fielddecl = new ArrayList<>();
fielddecl.add(field);
JavaClassName name = new JavaClassName(fieldConstriant.getReveiver().toString());
//Erstellen der Generics
List<GenericTypeVar> genericTypeVarList = new ArrayList<>();
String type_of_field = fieldConstriant.getAttribut().toString();
genericTypeVarList.add(new GenericTypeVar(type_of_field, null, new NullToken(), new NullToken()));
GenericDeclarationList genericDeclaraitonList = new GenericDeclarationList(genericTypeVarList, new NullToken());
// methods
List<Method> methods = new ArrayList<Method>();
ClassOrInterface face = new ClassOrInterface(0, name, fielddecl, methods, genericDeclaraitonList, null, true, null , null);
return face;
}
public static ClassOrInterface constructInterfaceMethod(MethodConstraint methodConstraint) {
System.out.println("Generiere Method-Interface");
// Erstellen der Argumente der Methode
List<FormalParameter> 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<Method> methodlist = new ArrayList<>();
methodlist.add(method);
// Name des neuen Interfaces
JavaClassName name = new JavaClassName(methodConstraint.getReceiver().toString());
//Erstellen der Generics
List<GenericTypeVar> 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;
}
}

View File

@ -0,0 +1,100 @@
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 GeneralParserTest2 {
private static final String rootDirectory = System.getProperty("user.dir") + "/test/strucTypes3/";
@Test
public void run() {
/*
* Auflistung aller Files die getestet werden sollen
*/
List<String> filenames = new ArrayList<String>();
// 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;
ClassOrInterface aktClass = sf.getClasses().get(0);
AssTuple ass = AssumptionGenerator.generateAssumptions(aktClass);
List<ConstraintAbstract> 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); });
}
} catch (Exception exc) {
exc.printStackTrace();
fail();
}
assertTrue("Tests durchlaufen", filenames.size() > 0);
}
}

View File

@ -0,0 +1,124 @@
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.strucTypes3.ToStringGenerator;
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<String> filenames = new ArrayList<String>();
// 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("StructuralTypesMiddle.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<ConstraintAbstract> 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<ClassOrInterface> sf2 = new ArrayList<>();
constraints.forEach( (constraint) -> {sf2.add( Construct.constructInterfaces(constraint)); } );
//sf2.forEach((x) -> {if (x != null ) { String y = ToStringGenerator.interface2String(x); System.out.println(y); }} );
for (int i = 0; i < sf2.size() ; i++) {
if (sf2.get(i) != null ) {
System.out.println(ToStringGenerator.interface2String(sf2.get(i)));
}
}
System.out.println(sf2.size());
}
} catch (Exception exc) {
exc.printStackTrace();
fail();
}
assertTrue("Tests durchlaufen", filenames.size() > 0);
}
}

View File

@ -0,0 +1,27 @@
class A {
//Integer feld1;
//feld2;
//mtNeu() { return new Integer(); }
//mtcast(a) { return (Integer) a; }
mfeld(r) { return r.feld; }
mt1(x,y,z) { return x.sub(y).add(z); }
mt(g,h,i) { return g.div( h , i) ; }
}
class Integer {
}

View File

@ -0,0 +1,20 @@
class A {
A f1;
f2;
mt1(x1) { return x1; }
mt2(x2) { return x2.feld_von_x ; }
mt3(xaver,y,z) { return xaver.sub(y).add(z); }
mt4(x,y,z) { return x.f.add(y.sub(z,x)); }
}

View File

@ -0,0 +1,7 @@
class A {
mt(x,y,z) { return x.sub(y).add(z); }
}