forked from JavaTX/JavaCompilerCore
modified: ../../../../main/java/de/dhbwstuttgart/bytecode/TPHExtractor.java
modified: ../../../../main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GeneratedGenericsFinder.java modified: ../../../../main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java modified: ../../../../main/java/de/dhbwstuttgart/bytecode/utilities/MethodAndTPH.java modified: ../../../java/constraintSimplify/FamilyOfGenerics.java modified: ../../../java/insertGenerics/TestTPHsAndGenerics.java
This commit is contained in:
parent
f270686b3c
commit
5980f1c7b8
@ -9,6 +9,7 @@ import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import de.dhbwstuttgart.bytecode.constraint.ExtendsConstraint;
|
||||
@ -26,10 +27,16 @@ import de.dhbwstuttgart.syntaxtree.Method;
|
||||
import de.dhbwstuttgart.syntaxtree.ParameterList;
|
||||
import de.dhbwstuttgart.syntaxtree.SourceFile;
|
||||
import de.dhbwstuttgart.syntaxtree.statement.Assign;
|
||||
import de.dhbwstuttgart.syntaxtree.statement.AssignToField;
|
||||
import de.dhbwstuttgart.syntaxtree.statement.Expression;
|
||||
import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression;
|
||||
import de.dhbwstuttgart.syntaxtree.statement.ExpressionReceiver;
|
||||
import de.dhbwstuttgart.syntaxtree.statement.FieldVar;
|
||||
import de.dhbwstuttgart.syntaxtree.statement.LocalVar;
|
||||
import de.dhbwstuttgart.syntaxtree.statement.LocalVarDecl;
|
||||
import de.dhbwstuttgart.syntaxtree.statement.MethodCall;
|
||||
import de.dhbwstuttgart.syntaxtree.statement.Return;
|
||||
import de.dhbwstuttgart.syntaxtree.statement.Statement;
|
||||
import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
|
||||
import de.dhbwstuttgart.syntaxtree.type.RefType;
|
||||
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
|
||||
@ -45,13 +52,17 @@ import de.dhbwstuttgart.typeinference.result.ResultSet;
|
||||
public class TPHExtractor extends AbstractASTWalker {
|
||||
// Alle TPHs der Felder werden iKopf der Klasse definiert
|
||||
// alle TPHs der Klasse: (TPH, is in Method?)
|
||||
public final SourceFile sf;
|
||||
|
||||
/*
|
||||
* ordnet den Methodennamen ihre jeweiligen Methodenrespraesentationen in der abstrakte Syntax zu
|
||||
* ordnet den Methodennamen ihre jeweiligen Methodenrespraesentationen der abstrakte Syntax in der jeweiligen aktuellen Klasse zu
|
||||
*/
|
||||
public final HashMap<String, Method> nameToMeth;
|
||||
public HashMap<String, Method> nameToMeth;
|
||||
|
||||
|
||||
/*
|
||||
* ordnet den Methodennamen ihre jeweiligen Methodenrespraesentationen der abstrakte Syntax in der jeweiligen aktuellen Klasse zu
|
||||
*/
|
||||
public HashMap<String,Expression> nameToField;
|
||||
public final HashMap<String, Boolean> allTPHS = new HashMap<>();
|
||||
public final List<String> tphsClass = new ArrayList<>();
|
||||
MethodAndTPH methodAndTph;
|
||||
@ -65,12 +76,8 @@ public class TPHExtractor extends AbstractASTWalker {
|
||||
private ResultSet resultSet;
|
||||
private Resolver resolver;
|
||||
|
||||
public TPHExtractor(SourceFile sf) {
|
||||
this.sf = sf;
|
||||
this.nameToMeth = new HashMap<>();
|
||||
for(Method m : sf.getAllMethods()) {
|
||||
this.nameToMeth.put(m.getName(), m);
|
||||
}
|
||||
public TPHExtractor() {
|
||||
|
||||
}
|
||||
|
||||
public void setResultSet(ResultSet resultSet) {
|
||||
@ -214,6 +221,28 @@ public class TPHExtractor extends AbstractASTWalker {
|
||||
ListOfMethodsAndTph.add(methodAndTph);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(ClassOrInterface classOrInterface) {
|
||||
Optional<Constructor> optInitialConstructor = classOrInterface.getfieldInitializations();
|
||||
this.nameToField = new HashMap<>();
|
||||
if (optInitialConstructor.isPresent()) {
|
||||
classOrInterface.getfieldInitializations()
|
||||
.get()
|
||||
.block
|
||||
.statements
|
||||
.stream()
|
||||
.filter(s -> s instanceof Assign)
|
||||
.forEach(as ->
|
||||
this.nameToField.put(((AssignToField)(((Assign)as).lefSide)).field.fieldVarName, ((Assign)as).rightSide));
|
||||
}
|
||||
|
||||
this.nameToMeth = new HashMap<>();
|
||||
for(Method m : classOrInterface.getMethods()) {
|
||||
this.nameToMeth.put(m.getName(), m);
|
||||
}
|
||||
super.visit(classOrInterface);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(Constructor cons) {
|
||||
inMethod = false;
|
||||
@ -252,26 +281,37 @@ public class TPHExtractor extends AbstractASTWalker {
|
||||
|
||||
@Override
|
||||
public void visit(MethodCall methodCall) {
|
||||
String fieldOrMethName;
|
||||
super.visit(methodCall);
|
||||
if (nameToMeth.keySet().contains(methodCall.name)) {//Method-call of a method in the actuial Sourcefile
|
||||
Iterator<FormalParameter> paraIt = nameToMeth.get(methodCall.name).getParameterList().iterator();
|
||||
methodCall.getArgumentList()
|
||||
.getArguments()
|
||||
.forEach(x -> {
|
||||
RefTypeOrTPHOrWildcardOrGeneric left = resultSet.resolveType(x.getType()).resolvedType;
|
||||
RefTypeOrTPHOrWildcardOrGeneric right = resultSet.resolveType(paraIt.next().getType()).resolvedType;
|
||||
if (left instanceof TypePlaceholder && methodAndTph.getTphs().contains(((TypePlaceholder)left).getName())) {
|
||||
if (right instanceof TypePlaceholder) {
|
||||
methodAndTph.addPair(allPairs.stream().filter(
|
||||
if (nameToMeth.keySet().contains(fieldOrMethName = methodCall.name) //Method-call of a method in the actuial Sourcefile
|
||||
|| (methodCall.name.equals("apply") &&
|
||||
(methodCall.receiver instanceof ExpressionReceiver) &&
|
||||
(((ExpressionReceiver)methodCall.receiver).expr instanceof FieldVar) &&
|
||||
nameToField.keySet().contains(fieldOrMethName=((FieldVar)((ExpressionReceiver)methodCall.receiver).expr).fieldVarName)
|
||||
)
|
||||
)
|
||||
{
|
||||
Iterator<FormalParameter> paraIt =
|
||||
methodCall.name.equals("apply")
|
||||
? ((LambdaExpression)nameToField.get(fieldOrMethName)).params.iterator()
|
||||
: nameToMeth.get(methodCall.name).getParameterList().iterator();
|
||||
methodCall.getArgumentList()
|
||||
.getArguments()
|
||||
.forEach(x -> {
|
||||
RefTypeOrTPHOrWildcardOrGeneric left = resultSet.resolveType(x.getType()).resolvedType;
|
||||
RefTypeOrTPHOrWildcardOrGeneric right = resultSet.resolveType(paraIt.next().getType()).resolvedType;
|
||||
if (left instanceof TypePlaceholder && methodAndTph.getTphs().contains(((TypePlaceholder)left).getName())) {
|
||||
if (right instanceof TypePlaceholder) {
|
||||
methodAndTph.addPair(allPairs.stream().filter(
|
||||
p -> (p.getLeft() instanceof TypePlaceholder &&
|
||||
p.getRight() instanceof TypePlaceholder) &&
|
||||
((TypePlaceholder)p.getLeft()).getName().equals(((TypePlaceholder)left).getName()) &&
|
||||
((TypePlaceholder)p.getRight()).getName().equals(((TypePlaceholder)right).getName())
|
||||
).findFirst()
|
||||
.get());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -86,7 +86,7 @@ public class GeneratedGenericsFinder implements ASTVisitor {
|
||||
public GeneratedGenericsFinder(SourceFile sf, Collection<ResultSet> listOfResultSets) {
|
||||
this.sf = sf;
|
||||
this.listOfResultSets = listOfResultSets;
|
||||
this.tphExtractor = new TPHExtractor(sf);
|
||||
this.tphExtractor = new TPHExtractor();
|
||||
}
|
||||
|
||||
public GenericGenratorResultForSourceFile findGeneratedGenerics() {
|
||||
|
@ -4,10 +4,14 @@ import de.dhbwstuttgart.bytecode.TPHExtractor;
|
||||
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
|
||||
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation;
|
||||
import de.dhbwstuttgart.bytecode.utilities.MethodAndTPH;
|
||||
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
|
||||
import de.dhbwstuttgart.typeinference.result.ResultPair;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.objectweb.asm.Type;
|
||||
|
||||
public class FamilyOfGeneratedGenerics {
|
||||
@ -15,7 +19,7 @@ public class FamilyOfGeneratedGenerics {
|
||||
// HashMap speichert ob TPH in einer Methode oder in der Klasse ist; und wenn es in der Methode ist, in welcher Methode
|
||||
public HashMap<String, List<PairTphMethod<PositionFinder.Position, String>>> posOfTPHs = new HashMap<>();
|
||||
public List<ClassConstraint> classConstraints = new ArrayList<>();
|
||||
public List<MethodConstraint> methodConstraints = new ArrayList<>();
|
||||
//public List<MethodConstraint> methodConstraints = new ArrayList<>();
|
||||
public HashMap<String, List<MethodConstraint>> methodConstraintsWithPosition = new HashMap<>();
|
||||
|
||||
private static final String objectType = Type.getInternalName(Object.class);
|
||||
@ -24,7 +28,7 @@ public class FamilyOfGeneratedGenerics {
|
||||
this.allConstraints = tphExtractor.allCons;
|
||||
this.posOfTPHs = positionConverter(tphExtractor.allTPHS, tphExtractor.ListOfMethodsAndTph);
|
||||
this.classConstraints = getClassConstraints(allConstraints,posOfTPHs);
|
||||
this.methodConstraints = getMethodConstraints(allConstraints,classConstraints,posOfTPHs, tphExtractor.ListOfMethodsAndTph);
|
||||
//this.methodConstraints = getMethodConstraints(allConstraints,classConstraints,posOfTPHs, null, tphExtractor.ListOfMethodsAndTph);
|
||||
this.methodConstraintsWithPosition = getMethodConstraintsWithPosition(allConstraints,classConstraints,posOfTPHs, tphExtractor.ListOfMethodsAndTph);
|
||||
}
|
||||
|
||||
@ -59,10 +63,19 @@ public class FamilyOfGeneratedGenerics {
|
||||
return cs_cl;
|
||||
}
|
||||
|
||||
public static List<MethodConstraint> getMethodConstraints(List<TPHConstraint> cs, List<ClassConstraint> cs_cl, HashMap<String, List<PairTphMethod<PositionFinder.Position, String>>> posOfTphs, List<MethodAndTPH> listOfMethodsAndTph) {
|
||||
public static List<MethodConstraint> getMethodConstraints(List<TPHConstraint> cs,
|
||||
List<ClassConstraint> cs_cl,
|
||||
HashMap<String, List<PairTphMethod<PositionFinder.Position, String>>> posOfTphs,
|
||||
ArrayList<ResultPair<TypePlaceholder, TypePlaceholder>> methodConstraints1Pairs,
|
||||
List<MethodAndTPH> listOfMethodsAndTph) {
|
||||
List<MethodConstraint> cs_m = new ArrayList<>();
|
||||
List<MethodConstraint> methodConstraints1 = typeOfTheMethodInClSigma(cs, posOfTphs);
|
||||
//List<MethodConstraint> methodConstraints1 = listOfMethodsAndTph.
|
||||
|
||||
List<MethodConstraint> methodConstraints1prime =
|
||||
methodConstraints1Pairs.stream()
|
||||
.map(p -> new MethodConstraint(p.getLeft().getName(), p.getRight().getName(), Relation.EXTENDS))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
for (MethodConstraint cons: methodConstraints1) {
|
||||
if (!checkForDuplicates(cons, cs_m)) {
|
||||
cs_m.add(cons);
|
||||
@ -123,7 +136,7 @@ public class FamilyOfGeneratedGenerics {
|
||||
}
|
||||
}
|
||||
}
|
||||
listOfThisMethod = getMethodConstraints(cs,cs_cl,posOfTPHsForThisMethod,listOfMethodsAndTph);
|
||||
listOfThisMethod = getMethodConstraints(cs,cs_cl,posOfTPHsForThisMethod, method.getBasePairs(),listOfMethodsAndTph);
|
||||
tempMethodConstraintsWithPosition.put(currentMethod, listOfThisMethod);
|
||||
}
|
||||
}
|
||||
|
@ -53,5 +53,9 @@ public class MethodAndTPH {
|
||||
public TypePlaceholder getReturnType() {
|
||||
return returnType;
|
||||
}
|
||||
|
||||
public ArrayList<ResultPair<TypePlaceholder, TypePlaceholder>> getBasePairs() {
|
||||
return basePairs;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ public class FamilyOfGenerics {
|
||||
public void generateBC() throws Exception {
|
||||
SourceFile sf = generateAST();
|
||||
PositionFinder.getPositionOfTPH(sf, null);
|
||||
TPHExtractor tphExtractor = new TPHExtractor(sf);
|
||||
TPHExtractor tphExtractor = new TPHExtractor();
|
||||
List<ResultSet> results = new ArrayList<ResultSet>();
|
||||
GeneratedGenericsFinder generatedGenericsFinder = new GeneratedGenericsFinder(sf, results);
|
||||
}
|
||||
|
@ -1,72 +1,58 @@
|
||||
package insertGenerics;
|
||||
|
||||
import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile;
|
||||
import de.dhbwstuttgart.core.JavaTXCompiler;
|
||||
import de.dhbwstuttgart.syntaxtree.SourceFile;
|
||||
import de.dhbwstuttgart.syntaxtree.visual.ASTPrinter;
|
||||
import de.dhbwstuttgart.syntaxtree.visual.ASTTypePrinter;
|
||||
import de.dhbwstuttgart.typedeployment.TypeInsert;
|
||||
import de.dhbwstuttgart.typedeployment.TypeInsertFactory;
|
||||
import de.dhbwstuttgart.typeinference.result.ResultSet;
|
||||
import org.junit.Test;
|
||||
import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile;
|
||||
import de.dhbwstuttgart.core.JavaTXCompiler;
|
||||
import de.dhbwstuttgart.syntaxtree.SourceFile;
|
||||
import de.dhbwstuttgart.syntaxtree.visual.ASTPrinter;
|
||||
import de.dhbwstuttgart.syntaxtree.visual.ASTTypePrinter;
|
||||
import de.dhbwstuttgart.typedeployment.TypeInsert;
|
||||
import de.dhbwstuttgart.typedeployment.TypeInsertFactory;
|
||||
import de.dhbwstuttgart.typeinference.result.ResultSet;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class TestTPHsAndGenerics {
|
||||
|
||||
public static final String rootDirectory = System.getProperty("user.dir")+"/src/test/resources/insertGenericsJav/";
|
||||
private static final String rootDirectory = System.getProperty("user.dir")+"/src/test/resources/insertGenericsJav/";
|
||||
private String pathToClassFile = System.getProperty("user.dir")+"/src/test/resources/testBytecode/generatedBC/";
|
||||
private static ClassLoader loader;
|
||||
private static Class<?> classToTest;
|
||||
private static Object instanceOfClass;
|
||||
private static String className = "TestTPHsAndGenerics";
|
||||
|
||||
@Test
|
||||
public void ggFinder() throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
|
||||
execute(new File(rootDirectory+className+".jav"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ggFinder() throws IOException, ClassNotFoundException {
|
||||
execute(new File(rootDirectory+"TestTPHSAndGenerics.jav"));
|
||||
}
|
||||
private static class TestResultSet{
|
||||
|
||||
private static class TestResultSet{
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public TestResultSet execute(File fileToTest) throws IOException, ClassNotFoundException {
|
||||
JavaTXCompiler compiler = new JavaTXCompiler(fileToTest);
|
||||
for(File f : compiler.sourceFiles.keySet()){
|
||||
SourceFile sf = compiler.sourceFiles.get(f);
|
||||
}
|
||||
List<ResultSet> results = compiler.typeInference();
|
||||
List<GenericGenratorResultForSourceFile> simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(results);
|
||||
//compiler.generateBytecode(rootDirectory+"xxx.class", results, simplifyResultsForAllSourceFiles);
|
||||
for(File f : compiler.sourceFiles.keySet()){
|
||||
SourceFile sf = compiler.sourceFiles.get(f);
|
||||
System.out.println(ASTTypePrinter.print(sf));
|
||||
System.out.println(ASTPrinter.print(sf));
|
||||
//List<ResultSet> results = compiler.typeInference(); PL 2017-10-03 vor die For-Schleife gezogen
|
||||
assert results.size()>0;
|
||||
Set<String> insertedTypes = new HashSet<>();
|
||||
for(ResultSet resultSet : results){
|
||||
Set<TypeInsert> result = TypeInsertFactory.createTypeInsertPoints(sf, resultSet, results, simplifyResultsForAllSourceFiles);
|
||||
assert result.size()>0;
|
||||
String content = readFile(f.getPath(), StandardCharsets.UTF_8);
|
||||
for(TypeInsert tip : result){
|
||||
insertedTypes.add(tip.insert(content));
|
||||
}
|
||||
}
|
||||
for(String s : insertedTypes){
|
||||
System.out.println(s);
|
||||
}
|
||||
}
|
||||
return new TestResultSet();
|
||||
}
|
||||
|
||||
static String readFile(String path, Charset encoding)
|
||||
throws IOException
|
||||
{
|
||||
byte[] encoded = Files.readAllBytes(Paths.get(path));
|
||||
return new String(encoded, encoding);
|
||||
}
|
||||
}
|
||||
public TestResultSet execute(File fileToTest) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
|
||||
JavaTXCompiler compiler = new JavaTXCompiler(fileToTest);
|
||||
List<ResultSet> results = compiler.typeInference();
|
||||
List<GenericGenratorResultForSourceFile> simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(results);
|
||||
/*
|
||||
compiler.generateBytecode(new File(pathToClassFile), results, simplifyResultsForAllSourceFiles);
|
||||
loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
|
||||
classToTest = loader.loadClass(className);
|
||||
instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
|
||||
*/
|
||||
return new TestResultSet();
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user