TypeExpr für New sowie die Generierung der Interfaces steht an

This commit is contained in:
sebastian 2017-04-21 03:13:45 +02:00
parent 3bb14b82a0
commit bd8cf2959f
13 changed files with 699 additions and 512 deletions

778
.idea/workspace.xml generated

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,98 @@
package de.dhbwstuttgart.strucTypes5.algo;
/**
* Created by sebastian on 21.04.17.
*/
import com.google.common.collect.Constraint;
import de.dhbwstuttgart.strucTypes5.constraints.ConstraintAbstract;
import de.dhbwstuttgart.strucTypes5.constraints.ConstraintMethod;
import de.dhbwstuttgart.strucTypes5.typeVars.TypeVar;
import de.dhbwstuttgart.strucTypes5.typeVars.TypeVarStore;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.statement.Receiver;
import java.util.*;
/**
* Takes a Typled Class and a Constraint Set for construct the Interfaces
* => typed class = cl + typVarStore + List with Constraints
*/
public class Construct {
List<ClassOrInterface> interfaceList = new ArrayList<>();
List<ConstraintAbstract> remainingConstraints = new ArrayList<>();
public Construct(ClassOrInterface classOrInterface, TypeVarStore typeVarStore , List<ConstraintAbstract> constraints) {
// Generate Interfaces for Field und MethodConstraints;
// First of all The Interfaces must be sortet
// Other Constraints must be remain in the class
// Sort the Constriants
Map<TypeVar, Set<ConstraintAbstract>> constraintSet = sortConstriants(constraints);
// Generate a Interface for every StructuralType
for (TypeVar typeVar : constraintSet.keySet()) {
interfaceList.add(generateInterface(constraintSet.get(typeVar)));
}
}
/**
*
* @param constraints
* @return
*
* Method sortet Constraints for ReceiverType for the Structural Type.
* Other Constraints as the Field or MethodsConstraints are copied to the global List remainungConstraints
*
*/
private Map<TypeVar,Set<ConstraintAbstract>> sortConstriants(List<ConstraintAbstract> constraints) {
Map<TypeVar, Set<ConstraintAbstract>> sortetConstraints = new HashMap<>();
for (ConstraintAbstract constraint : constraints) {
if (constraint instanceof ConstraintMethod) {
ConstraintMethod constraintMethod = (ConstraintMethod) constraint;
if (sortetConstraints.containsKey(constraintMethod.getReceiver())) {
sortetConstraints.get(constraintMethod.getReceiver()).add(constraintMethod);
}
else {
Set<ConstraintAbstract> constraintSet = new HashSet<>();
sortetConstraints.put(constraintMethod.getReceiver() , constraintSet);
}
}
else {
remainingConstraints.add(constraint);
}
}
return sortetConstraints;
}
/**
* Method Generates Interfaces for the Structural Type.
* The Method also adds the new Constraints to the remainung Constraints List.
*
*/
private ClassOrInterface generateInterface(Set<ConstraintAbstract> constraintSet) {
}
}

View File

@ -1,5 +1,9 @@
package de.dhbwstuttgart.strucTypes5.algo;
import de.dhbwstuttgart.strucTypes5.assumptions.AssumptionMap;
import de.dhbwstuttgart.strucTypes5.typeVars.TypeVarStore;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
/**
* Created by sebastian on 20.04.17.
*/
@ -7,11 +11,23 @@ public class TI {
// Start TypeInference Algorithm, need Assumptions and a Class
public TI() {
public TI(AssumptionMap ass , ClassOrInterface cl) {
TypeVarStore typeVarStore = new TypeVarStore();
Type type = new Type(ass , cl, typeVarStore);
// Construct soll die Interfaces erstellen
// Construct
// Solve
}
}

View File

@ -1,7 +1,10 @@
package de.dhbwstuttgart.strucTypes5.algo;
import de.dhbwstuttgart.strucTypes5.assumptions.AssumptionAbstract;
import de.dhbwstuttgart.strucTypes5.assumptions.AssumptionMakerLocal;
import de.dhbwstuttgart.strucTypes5.assumptions.AssumptionMap;
import de.dhbwstuttgart.strucTypes5.constraints.ConstraintAbstract;
import de.dhbwstuttgart.strucTypes5.typeVars.TypeVarStore;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.Method;
@ -14,25 +17,17 @@ import java.util.List;
*/
public class Type {
List<ConstraintAbstract> constraintList = new ArrayList<>();
// Start Type Algorithm, need Assumption and class , and Typeinformations
public Type(List<AssumptionAbstract> assumptions, ClassOrInterface cl , TypeVarStore typeVarStore) {
public Type(AssumptionMap assumptions, ClassOrInterface cl , TypeVarStore typeVarStore) {
for (Method m : cl.getMethods()) {
// Erstelle Lokale Assumptions
AssumptionMakerLocal assumptionMakerLocal = new AssumptionMakerLocal(m, typeVarStore);
List<AssumptionAbstract> newAssumptions = new ArrayList<>();
newAssumptions.addAll(assumptions);
newAssumptions.addAll(assumptionMakerLocal.getResultAssumptionList());
// Führe TypeExpression aus
//TypeExpr typeExpr = new TypeExpr();
assumptions.addAll(assumptionMakerLocal.getResultAssumptionList());
TypeExpr typeExpr = new TypeExpr(assumptions , m, typeVarStore);
constraintList.addAll(typeExpr.getResultConstraints());
}
}
}

View File

@ -6,10 +6,13 @@ import de.dhbwstuttgart.strucTypes5.assumptions.AssumptionMakerLocal;
import de.dhbwstuttgart.strucTypes5.assumptions.AssumptionMap;
import de.dhbwstuttgart.strucTypes5.constraints.ConstraintAbstract;
import de.dhbwstuttgart.strucTypes5.constraints.ConstraintFactory;
import de.dhbwstuttgart.strucTypes5.typeVars.TypeVar;
import de.dhbwstuttgart.strucTypes5.typeVars.TypeVarAbstract;
import de.dhbwstuttgart.strucTypes5.typeVars.TypeVarFactory;
import de.dhbwstuttgart.strucTypes5.typeVars.TypeVarStore;
import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.statement.*;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import java.util.ArrayList;
@ -25,7 +28,7 @@ public class TypeExpr {
private List<ConstraintAbstract> resultConstraints;
public TypeExpr(List<AssumptionAbstract> assumptionAbstractList , Method method, TypeVarStore typeVarStore) {
public TypeExpr(AssumptionMap assumptionAbstractList , Method method, TypeVarStore typeVarStore) {
this.typeVarStore = typeVarStore;
this.assumptionMap = makeAllAssumptions(assumptionAbstractList,method);
@ -40,11 +43,11 @@ public class TypeExpr {
}
private AssumptionMap makeAllAssumptions(List<AssumptionAbstract> assumptionAbstractList , Method method) {
private AssumptionMap makeAllAssumptions(AssumptionMap assumptionsAll , Method method) {
// Globale Assumptions
AssumptionMap assumptionMap = new AssumptionMap();
assumptionMap.addAll(assumptionAbstractList);
AssumptionMap assumptionMap = assumptionsAll;
//Lokale Assumptions
AssumptionMakerLocal assumptionMakerLocal = new AssumptionMakerLocal(method,typeVarStore);
@ -73,21 +76,55 @@ public class TypeExpr {
}
else if (expression instanceof MethodCall) {
MethodCall methodCall = (MethodCall) expression;
List<ConstraintAbstract> result = typeExpression(methodCall.get_Receiver());
// TypeExpr für die Argumente
for (Expression arg : methodCall.get_ArgList().getArguments()) {
result.addAll(typeExpression(arg));
// Call of non-Abstract Field do not add constraints
if (methodCall.get_Receiver().getType() instanceof RefType) {
List<ConstraintAbstract> result = typeExpression(methodCall.get_Receiver());
typeVarStore.storeTPH(methodCall.getType());
// TypeExpr für die Argumente
for (Expression arg : methodCall.get_ArgList().getArguments()) {
result.addAll(typeExpression(arg));
}
return result;
}
else {
List<ConstraintAbstract> result = typeExpression(methodCall.get_Receiver());
// TypeExpr für die Argumente
for (Expression arg : methodCall.get_ArgList().getArguments()) {
result.addAll(typeExpression(arg));
}
result.addAll(ConstraintFactory.generateConstraintMethod(methodCall, typeVarStore));
return result;
}
result.addAll(ConstraintFactory.generateConstraintMethod(methodCall,typeVarStore));
return result;
}
else if (expression instanceof Receiver) {
Receiver receiver = (Receiver) expression;
List<ConstraintAbstract> result = typeExpression(receiver.get_Expression());
return result;
}
else if (expression instanceof NewClass) {
NewClass newClass = (NewClass) expression;
// Argumente
List<ConstraintAbstract> result = new ArrayList<>();
// TypeExpr für die Argumente
for (Expression arg : newClass.getArgumentList().getArguments()) {
result.addAll(typeExpression(arg));
}
//typeVarStore.addTuple(TypeVarFactory.makeTypeVar() , newClass.getType());
//
return result;
}
else {
System.err.println("Fehler unbekannt");
System.out.println(expression);

View File

@ -0,0 +1,74 @@
package de.dhbwstuttgart.strucTypes5.assumptions;
import de.dhbwstuttgart.strucTypes2.Assumptions_Map;
import de.dhbwstuttgart.strucTypes3.ConstraintAbstract;
import de.dhbwstuttgart.strucTypes5.typeVars.TypeVarStore;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import java.util.List;
/**
* Created by sebastian on 21.04.17.
*/
public class AssumptionClass extends AssumptionAbstract {
// Die Angaben entspreichen einer clT Klasse
private ClassOrInterface cl;
private TypeVarStore typeVarStore;
private Assumptions_Map assumptions_map;
private List<ConstraintAbstract> constraints;
private List<ClassOrInterface> generatedinterfaces;
public AssumptionClass(ClassOrInterface cl, TypeVarStore typeVarStore, Assumptions_Map assumptions_map, List<ConstraintAbstract> constraints, List<ClassOrInterface> generatedinterfaces) {
this.cl = cl;
this.typeVarStore = typeVarStore;
this.assumptions_map = assumptions_map;
this.constraints = constraints;
this.generatedinterfaces = generatedinterfaces;
}
public AssumptionClass () {
}
public ClassOrInterface getCl() {
return cl;
}
public void setCl(ClassOrInterface cl) {
this.cl = cl;
}
public TypeVarStore getTypeVarStore() {
return typeVarStore;
}
public void setTypeVarStore(TypeVarStore typeVarStore) {
this.typeVarStore = typeVarStore;
}
public Assumptions_Map getAssumptions_map() {
return assumptions_map;
}
public void setAssumptions_map(Assumptions_Map assumptions_map) {
this.assumptions_map = assumptions_map;
}
public List<ConstraintAbstract> getConstraints() {
return constraints;
}
public void setConstraints(List<ConstraintAbstract> constraints) {
this.constraints = constraints;
}
public List<ClassOrInterface> getGeneratedinterfaces() {
return generatedinterfaces;
}
public void setGeneratedinterfaces(List<ClassOrInterface> generatedinterfaces) {
this.generatedinterfaces = generatedinterfaces;
}
}

View File

@ -14,11 +14,15 @@ import java.util.List;
public class AssumptionMakerGlobal {
List<AssumptionAbstract> assumptionAbstractList = new ArrayList<>();
AssumptionMap assumptionMapGlobal = new AssumptionMap();
public AssumptionMakerGlobal(ClassOrInterface classOrInterface, TypeVarStore typeVarStore) {
makeFieldAssumptions(classOrInterface.getFieldDecl() , typeVarStore);
makeMethodAssumptions(classOrInterface.getMethods() , typeVarStore);
makeClassAssumption(classOrInterface,typeVarStore);
}
@ -38,6 +42,16 @@ public class AssumptionMakerGlobal {
}
}
// Erstelle ClassAssumption für diese Klasse
private void makeClassAssumption(ClassOrInterface cl , TypeVarStore tvs) {
AssumptionClass assumptionClass = new AssumptionClass();
assumptionClass.setCl(cl);
assumptionClass.setTypeVarStore(tvs);
assumptionAbstractList.add(assumptionClass);
}
public List<AssumptionAbstract> getAssumptionAbstractList() {
return assumptionAbstractList;
}

View File

@ -16,6 +16,7 @@ public class AssumptionMap {
private Map<String, AssumptionMethod> mAss = new HashMap<>();
private Map<String, AssumptionField> fass = new HashMap<>();
private Map<String, AssumptionArgument> argAss = new HashMap<>();
private Map<String, AssumptionClass> classAss = new HashMap<>();
public void addAssumption(AssumptionAbstract ass) {
@ -32,6 +33,10 @@ public class AssumptionMap {
AssumptionArgument assumptionArgument = (AssumptionArgument) ass;
argAss.put(assumptionArgument.getNameOfArgument(), assumptionArgument);
}
else if (ass instanceof AssumptionClass) {
AssumptionClass assumptionClass = (AssumptionClass) ass;
classAss.put(assumptionClass.getCl().getClassName().toString() , assumptionClass);
}
else {
System.err.println("Fehler Ass nicht bekannt");
}
@ -70,9 +75,15 @@ public class AssumptionMap {
return fass.get(name).getTypeVar();
}
public AssumptionClass getClassAssumption(String className) {
return classAss.get(className);
}
@Override
public String toString() {
return String.format("AssMethod \n AssField: %s \n AssArgument %s " , mAss.toString() , fass.toString() , argAss.toString());
return String.format("AssMethod \n AssField: %s \n AssArgument %s \n AssClass %s " , mAss.toString() , fass.toString() , argAss.toString() , classAss.toString());
}
}

View File

@ -6,8 +6,10 @@ import de.dhbwstuttgart.syntaxtree.Field;
import de.dhbwstuttgart.syntaxtree.FormalParameter;
import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.statement.*;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import java.sql.Ref;
import java.util.List;
/**
@ -137,7 +139,6 @@ public class Class2String {
for (Expression arg : methodCall.get_ArgList().getArguments()) {
String expArg = generateExpressionString(arg);
arguments = arguments.concat(expArg + ",");
}
return String.format( "[%s.%s(%s) : %s ] " , receiverString, methodCall.get_Name() , arguments , tS(methodCall.getType()));
}
@ -145,6 +146,16 @@ public class Class2String {
Receiver receiver = (Receiver) expression;
return generateExpressionString(receiver.get_Expression());
}
else if (expression instanceof NewClass) {
NewClass newClass = (NewClass) expression;
String arguments = "";
for (Expression arg : newClass.getArgumentList().getArguments()) {
String expArg = generateExpressionString(arg);
arguments = arguments.concat(expArg + ",");
}
return String.format("[new %s( %s ): %s ]" , newClass.getType() , arguments , newClass.getType() );
}
else {
return "Expression nicht bekannt";
}
@ -159,6 +170,14 @@ public class Class2String {
resultString = ref.toString();
}
else if (aktMode.equals(WITH_TYPEVARS)) {
// Typ Ist Bekannt
if (ref instanceof RefType) {
return ((RefType) ref).getName().toString();
}
System.out.println(ref);
// Typ ist nicht bekannt
resultString = typeVarStore.getTypeVarByTPH(ref).toString();
if (resultString == null) {
System.err.println("Fehler bei " +ref);

View File

@ -6,6 +6,7 @@ import de.dhbwstuttgart.strucTypes5.algo.TypeExpr;
import de.dhbwstuttgart.strucTypes5.assumptions.AssumptionAbstract;
import de.dhbwstuttgart.strucTypes5.assumptions.AssumptionMakerGlobal;
import de.dhbwstuttgart.strucTypes5.assumptions.AssumptionMakerLocal;
import de.dhbwstuttgart.strucTypes5.assumptions.AssumptionMap;
import de.dhbwstuttgart.strucTypes5.ausgabe.Class2String;
import de.dhbwstuttgart.strucTypes5.typeVars.TypeVarStore;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
@ -74,7 +75,9 @@ public class Class2StringTest {
ArrayList<AssumptionAbstract> assAll = new ArrayList<>();
assAll.addAll(assumptionMakerLocal.getResultAssumptionList());
assAll.addAll(makerGlobal.getAssumptionAbstractList());
TypeExpr typeExpr = new TypeExpr(assAll , sf.getClasses().get(0).getMethods().get(0) , typeVarStore );
AssumptionMap assumptionMap = new AssumptionMap();
assumptionMap.addAll(assAll);
TypeExpr typeExpr = new TypeExpr(assumptionMap , sf.getClasses().get(0).getMethods().get(0) , typeVarStore );
// Teste TypeVars Problem Typevars werden benötigt
Class2String class2String3 = new Class2String();

View File

@ -0,0 +1,13 @@
class Main {
main () { return new A().mt(new Main() , new A() , new A() ); }
}
class A {
}

View File

@ -0,0 +1,86 @@
package strucTypes5;
import de.dhbwstuttgart.parser.JavaTXParser;
import de.dhbwstuttgart.strucTypes5.algo.TypeExpr;
import de.dhbwstuttgart.strucTypes5.assumptions.AssumptionAbstract;
import de.dhbwstuttgart.strucTypes5.assumptions.AssumptionMakerGlobal;
import de.dhbwstuttgart.strucTypes5.assumptions.AssumptionMakerLocal;
import de.dhbwstuttgart.strucTypes5.assumptions.AssumptionMap;
import de.dhbwstuttgart.strucTypes5.ausgabe.Class2String;
import de.dhbwstuttgart.strucTypes5.typeVars.TypeVarStore;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import org.junit.Test;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Dieser Test pr�ft nur, ob .java-Dateien fehlerfrei geparst werden. Der
* dabei erstellte Syntaxbaum wird nicht kontrolliert.
*
* @author janulrich
*
*/
public class NewOperatorTest {
private static final String rootDirectory = System.getProperty("user.dir") + "/test/strucTypes5/";
@Test
public void run() {
List<String> filenames = new ArrayList<String>();
filenames.add("NewOperatorTest.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;
Class2String class2String = new Class2String();
String s = class2String.generateStringTPH(sf.getClasses().get(0));
System.out.println(s);
ClassOrInterface classMain = sf.getClasses().get(0);
TypeVarStore tvs = new TypeVarStore();
AssumptionMakerGlobal assumptionMakerGlobal = new AssumptionMakerGlobal(classMain,tvs);
AssumptionMakerLocal assumptionMakerLocal = new AssumptionMakerLocal(classMain.getMethods().get(0), tvs);
List<AssumptionAbstract> assumptionAbstracts = new ArrayList<>();
AssumptionMap assumptionMap = new AssumptionMap();
assumptionMap.addAll(assumptionAbstracts);
TypeExpr typeExpr = new TypeExpr(assumptionMap,classMain.getMethods().get(0) ,tvs);
System.out.println(typeExpr.getResultConstraints());
Class2String class1String = new Class2String();
String s2 = class1String.generateStringTypeVars(sf.getClasses().get(0), tvs);
System.out.println(s2);
}
} catch (Exception exc) {
exc.printStackTrace();
fail();
}
assertTrue("Tests durchlaufen", filenames.size() > 0);
}
}

View File

@ -5,6 +5,7 @@ import de.dhbwstuttgart.strucTypes5.algo.TypeExpr;
import de.dhbwstuttgart.strucTypes5.assumptions.AssumptionAbstract;
import de.dhbwstuttgart.strucTypes5.assumptions.AssumptionMakerGlobal;
import de.dhbwstuttgart.strucTypes5.assumptions.AssumptionMakerLocal;
import de.dhbwstuttgart.strucTypes5.assumptions.AssumptionMap;
import de.dhbwstuttgart.strucTypes5.typeVars.TypeVarStore;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import org.junit.Test;
@ -57,16 +58,14 @@ public class TypeExprTest {
List<AssumptionAbstract> assAll = new ArrayList<>();
assAll.addAll(assumptionMakerLocal.getResultAssumptionList());
assAll.addAll(makerGlobal.getAssumptionAbstractList());
AssumptionMap assumptionMap = new AssumptionMap();
assumptionMap.addAll(assAll);
TypeExpr typeExpr = new TypeExpr(assAll , sf.getClasses().get(0).getMethods().get(0) , typeVarStore );
TypeExpr typeExpr = new TypeExpr(assumptionMap , sf.getClasses().get(0).getMethods().get(0) , typeVarStore );
System.out.println(typeExpr.getResultConstraints());
System.out.println(typeExpr.getTypeVarStore());
}
} catch (Exception exc) {