forked from JavaTX/JavaCompilerCore
TypeExpr für methoden geschrieben
This commit is contained in:
parent
b3ffb7bd58
commit
d4e32794ff
64
src/de/dhbwstuttgart/strucTypes3/AssTuple.java
Normal file
64
src/de/dhbwstuttgart/strucTypes3/AssTuple.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
69
src/de/dhbwstuttgart/strucTypes3/AssumptionGenerator.java
Normal file
69
src/de/dhbwstuttgart/strucTypes3/AssumptionGenerator.java
Normal 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));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//TODO : Assumption für die eingene aktuelle Klasse
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return assAll;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
5
src/de/dhbwstuttgart/strucTypes3/ConstraintAbstract.java
Normal file
5
src/de/dhbwstuttgart/strucTypes3/ConstraintAbstract.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package de.dhbwstuttgart.strucTypes3;
|
||||||
|
|
||||||
|
public class ConstraintAbstract {
|
||||||
|
|
||||||
|
}
|
49
src/de/dhbwstuttgart/strucTypes3/FieldConstraint.java
Normal file
49
src/de/dhbwstuttgart/strucTypes3/FieldConstraint.java
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
package de.dhbwstuttgart.strucTypes3;
|
||||||
|
|
||||||
|
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
|
||||||
|
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
45
src/de/dhbwstuttgart/strucTypes3/GleichConstraint.java
Normal file
45
src/de/dhbwstuttgart/strucTypes3/GleichConstraint.java
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
209
src/de/dhbwstuttgart/strucTypes3/HelpMethoden.java
Normal file
209
src/de/dhbwstuttgart/strucTypes3/HelpMethoden.java
Normal file
@ -0,0 +1,209 @@
|
|||||||
|
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<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));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
33
src/de/dhbwstuttgart/strucTypes3/MethodConstraint.java
Normal file
33
src/de/dhbwstuttgart/strucTypes3/MethodConstraint.java
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
package de.dhbwstuttgart.strucTypes3;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
|
||||||
|
|
||||||
|
|
||||||
|
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, argumentTypes.toString() , returnType.toString() , argumentTypeVars.toString() );
|
||||||
|
}
|
||||||
|
}
|
45
src/de/dhbwstuttgart/strucTypes3/SubTypeConstraint.java
Normal file
45
src/de/dhbwstuttgart/strucTypes3/SubTypeConstraint.java
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
63
src/de/dhbwstuttgart/strucTypes3/ToStringGenerator.java
Normal file
63
src/de/dhbwstuttgart/strucTypes3/ToStringGenerator.java
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
package de.dhbwstuttgart.strucTypes3;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
|
||||||
|
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 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -10,7 +10,7 @@ class A {
|
|||||||
|
|
||||||
mt2(x2) { return x2.feld_von_x ; }
|
mt2(x2) { return x2.feld_von_x ; }
|
||||||
|
|
||||||
mt3(x,y,z) { return x.sub(y).add(z); }
|
mt3(xaver,y,z) { return xaver.sub(y).add(z); }
|
||||||
|
|
||||||
mt4(x,y,z) { return x.f.add(y.sub(z,x)); }
|
mt4(x,y,z) { return x.f.add(y.sub(z,x)); }
|
||||||
|
|
||||||
|
@ -9,17 +9,12 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import de.dhbwstuttgart.parser.JavaTXParser;
|
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.ClassOrInterface;
|
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
|
||||||
import de.dhbwstuttgart.syntaxtree.Field;
|
import de.dhbwstuttgart.syntaxtree.Field;
|
||||||
import de.dhbwstuttgart.syntaxtree.FormalParameter;
|
import de.dhbwstuttgart.syntaxtree.FormalParameter;
|
||||||
import de.dhbwstuttgart.syntaxtree.Method;
|
import de.dhbwstuttgart.syntaxtree.Method;
|
||||||
import de.dhbwstuttgart.syntaxtree.SourceFile;
|
import de.dhbwstuttgart.syntaxtree.SourceFile;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.Block;
|
import de.dhbwstuttgart.syntaxtree.statement.Block;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.CastExpr;
|
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.Expression;
|
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;
|
||||||
@ -150,7 +145,6 @@ public class GeneralParserTest2 {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
System.err.println("Problem");
|
System.err.println("Problem");
|
||||||
}
|
}
|
||||||
@ -254,7 +248,7 @@ class HelpMethoden {
|
|||||||
String argumentsStr = "";
|
String argumentsStr = "";
|
||||||
List<Expression> arguments = methodCall.get_ArgList().getArguments();
|
List<Expression> arguments = methodCall.get_ArgList().getArguments();
|
||||||
for (Expression arg : arguments) {
|
for (Expression arg : arguments) {
|
||||||
argumentsStr = argumentsStr + " " + HelpMethoden.expression_typed2String(arg) + arg.getType();
|
argumentsStr = argumentsStr + " " + HelpMethoden.expression_typed2String(arg) ;//+ arg.getType();
|
||||||
}
|
}
|
||||||
|
|
||||||
result = String.format("%s.%s(%s)", result, methodCall.get_Name(), argumentsStr);
|
result = String.format("%s.%s(%s)", result, methodCall.get_Name(), argumentsStr);
|
||||||
@ -438,7 +432,4 @@ class MethodConstraint extends ConstraintAbstract {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
return String.format("MethodConstraint( %s, %s , %s (%s , %s ))" , receiver.toString() , methodName, "arg" , returnType.toString() , argumentTypeVars.toString() );
|
return String.format("MethodConstraint( %s, %s , %s (%s , %s ))" , receiver.toString() , methodName, "arg" , returnType.toString() , argumentTypeVars.toString() );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
100
test/strucTypes3/GeneralParserTest2.java
Normal file
100
test/strucTypes3/GeneralParserTest2.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
20
test/strucTypes3/StructuralTypesSimple3.jav
Normal file
20
test/strucTypes3/StructuralTypesSimple3.jav
Normal 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)); }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
10
test/strucTypes3/StructuralTypesSimpleSkript.jav
Normal file
10
test/strucTypes3/StructuralTypesSimpleSkript.jav
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
|
||||||
|
class A {
|
||||||
|
|
||||||
|
|
||||||
|
mt(x,y,z) { return x.sub(y).add(z); }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user