modified: ../../../../main/java/de/dhbwstuttgart/bytecode/TPHExtractor.java

SourceFile und nameToMeth ergaenzt

	modified:   ../../../../main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GeneratedGenericsFinder.java
	modified:   ../../../../main/java/de/dhbwstuttgart/syntaxtree/SourceFile.java
	  List<Method> getAllMethods() ergaenzty

	modified:   ../../../../main/java/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java
           variance wird uebernommen

	modified:   ../../../../main/java/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java
	   setVariance gefuegt.

	modified:   ../../../java/insertGenerics/TestTwoArgs.java
	modified:   ../../../java/insertGenerics/TestTwoArgs2.java
	modified:   ../../insertGenericsJav/TestTwoArgs.jav
	modified:   ../../insertGenericsJav/TestTwoArgs2.jav
This commit is contained in:
pl@gohorb.ba-horb.de 2021-02-03 16:55:12 +01:00
parent ef723e4103
commit 92bc3d626c
9 changed files with 79 additions and 88 deletions

View File

@ -24,8 +24,10 @@ 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.ParameterList; import de.dhbwstuttgart.syntaxtree.ParameterList;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.statement.LocalVar; import de.dhbwstuttgart.syntaxtree.statement.LocalVar;
import de.dhbwstuttgart.syntaxtree.statement.LocalVarDecl; import de.dhbwstuttgart.syntaxtree.statement.LocalVarDecl;
import de.dhbwstuttgart.syntaxtree.statement.MethodCall;
import de.dhbwstuttgart.syntaxtree.type.GenericRefType; import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
@ -41,6 +43,8 @@ import de.dhbwstuttgart.typeinference.result.ResultSet;
public class TPHExtractor extends AbstractASTWalker { public class TPHExtractor extends AbstractASTWalker {
// Alle TPHs der Felder werden iKopf der Klasse definiert // Alle TPHs der Felder werden iKopf der Klasse definiert
// alle TPHs der Klasse: (TPH, is in Method?) // alle TPHs der Klasse: (TPH, is in Method?)
public final SourceFile sf;
public final HashMap<String, Method> nameToMeth;
public final HashMap<String, Boolean> allTPHS = new HashMap<>(); public final HashMap<String, Boolean> allTPHS = new HashMap<>();
public final List<String> tphsClass = new ArrayList<>(); public final List<String> tphsClass = new ArrayList<>();
MethodAndTPH methodAndTph; MethodAndTPH methodAndTph;
@ -54,8 +58,12 @@ public class TPHExtractor extends AbstractASTWalker {
private ResultSet resultSet; private ResultSet resultSet;
private Resolver resolver; private Resolver resolver;
public TPHExtractor() { public TPHExtractor(SourceFile sf) {
this.sf = sf;
this.nameToMeth = new HashMap<>();
for(Method m : sf.getAllMethods()) {
this.nameToMeth.put(m.getName(), m);
}
} }
public void setResultSet(ResultSet resultSet) { public void setResultSet(ResultSet resultSet) {
@ -216,6 +224,24 @@ public class TPHExtractor extends AbstractASTWalker {
// inLocalOrParamOrReturn = false; // inLocalOrParamOrReturn = false;
} }
@Override
public void visit(MethodCall methodCall) {
super.visit(methodCall);
Iterator<FormalParameter> paraIt = nameToMeth.get(methodCall.name).getParameterList().iterator();
methodCall.getArgumentList()
.getArguments()
.forEach(x -> {
RefTypeOrTPHOrWildcardOrGeneric left = null;
RefTypeOrTPHOrWildcardOrGeneric right = null;
if ((left = x.getType()) instanceof TypePlaceholder) {
if ((right = paraIt.next().getType()) instanceof TypePlaceholder) {
methodAndTph.addPair(((TypePlaceholder)left).getName(),
((TypePlaceholder)left).getName());
}
}
});
}
@Override @Override
public void visit(LocalVar localVar) { public void visit(LocalVar localVar) {
// inLocalOrParamOrReturn = inMethod; // inLocalOrParamOrReturn = inMethod;

View File

@ -66,7 +66,7 @@ import de.dhbwstuttgart.typeinference.result.ResultSet;
* *
*/ */
public class GeneratedGenericsFinder implements ASTVisitor { public class GeneratedGenericsFinder implements ASTVisitor {
private final TPHExtractor tphExtractor = new TPHExtractor(); private final TPHExtractor tphExtractor;
private Collection<ResultSet> listOfResultSets; private Collection<ResultSet> listOfResultSets;
private SourceFile sf; private SourceFile sf;
private List<String> tphsClass; private List<String> tphsClass;
@ -86,6 +86,7 @@ public class GeneratedGenericsFinder implements ASTVisitor {
public GeneratedGenericsFinder(SourceFile sf, Collection<ResultSet> listOfResultSets) { public GeneratedGenericsFinder(SourceFile sf, Collection<ResultSet> listOfResultSets) {
this.sf = sf; this.sf = sf;
this.listOfResultSets = listOfResultSets; this.listOfResultSets = listOfResultSets;
this.tphExtractor = new TPHExtractor(sf);
} }
public GenericGenratorResultForSourceFile findGeneratedGenerics() { public GenericGenratorResultForSourceFile findGeneratedGenerics() {

View File

@ -45,6 +45,12 @@ public class SourceFile extends SyntaxTreeNode{
public List<ClassOrInterface> getClasses() { public List<ClassOrInterface> getClasses() {
return KlassenVektor; return KlassenVektor;
} }
public List<Method> getAllMethods() {
List<Method> ret = new ArrayList<>();
getClasses().forEach(cl -> ret.addAll(cl.getMethods()));
return ret;
}
@Override @Override
public void accept(ASTVisitor visitor) { public void accept(ASTVisitor visitor) {

View File

@ -258,6 +258,7 @@ public class UnifyTypeFactory {
ret = TypePlaceholder.fresh(new NullToken()); ret = TypePlaceholder.fresh(new NullToken());
tphs.put(t.getName(), ret); tphs.put(t.getName(), ret);
} }
ret.setVariance(t.getVariance());
return ret; return ret;
} }

View File

@ -20,10 +20,9 @@ public class TypePlaceholder extends RefTypeOrTPHOrWildcardOrGeneric
private final String name; private final String name;
/** /**
* wird bisher nicht genutzt * wird im Generate Generics Teil nach der Rueckumwandlung nach dem Unify genutzt
* setVariance muss ggf. auskommentiert werden.
*/ */
int variance = 0; private int variance = 0;
/** /**
@ -78,11 +77,9 @@ public class TypePlaceholder extends RefTypeOrTPHOrWildcardOrGeneric
return name; return name;
} }
/* wird bisher nicht genutzt
public void setVariance(int variance) { public void setVariance(int variance) {
this.variance= variance; this.variance= variance;
} }
*/
public int getVariance() { public int getVariance() {
return this.variance; return this.variance;

View File

@ -12,6 +12,9 @@ import org.junit.Test;
import java.io.File; import java.io.File;
import java.io.IOException; 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.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
@ -22,51 +25,30 @@ import java.util.Set;
public class TestTwoArgs { public class TestTwoArgs {
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/";
@Test private static ClassLoader loader;
public void ggFinder() throws IOException, ClassNotFoundException { private static Class<?> classToTest;
execute(new File(rootDirectory+"TestTwoArgs.jav")); private static Object instanceOfClass;
private static String className = "TestTwoArgs";
@Test
public void ggFinder() throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
execute(new File(rootDirectory+className+".jav"));
} }
private static class TestResultSet{ private static class TestResultSet{
} }
public TestResultSet execute(File fileToTest) throws IOException, ClassNotFoundException { public TestResultSet execute(File fileToTest) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
JavaTXCompiler compiler = new JavaTXCompiler(fileToTest); JavaTXCompiler compiler = new JavaTXCompiler(fileToTest);
for(File f : compiler.sourceFiles.keySet()){
SourceFile sf = compiler.sourceFiles.get(f);
}
List<ResultSet> results = compiler.typeInference(); List<ResultSet> results = compiler.typeInference();
List<GenericGenratorResultForSourceFile> simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(results); List<GenericGenratorResultForSourceFile> simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(results);
//compiler.generateBytecode(rootDirectory+"xxx.class", results, simplifyResultsForAllSourceFiles); compiler.generateBytecode(new File(pathToClassFile), results, simplifyResultsForAllSourceFiles);
for(File f : compiler.sourceFiles.keySet()){ loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
SourceFile sf = compiler.sourceFiles.get(f); classToTest = loader.loadClass(className);
System.out.println(ASTTypePrinter.print(sf)); instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
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(); return new TestResultSet();
} }
static String readFile(String path, Charset encoding)
throws IOException
{
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
} }

View File

@ -12,6 +12,9 @@ import org.junit.Test;
import java.io.File; import java.io.File;
import java.io.IOException; 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.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
@ -22,51 +25,30 @@ import java.util.Set;
public class TestTwoArgs2 { public class TestTwoArgs2 {
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/";
@Test private static ClassLoader loader;
public void ggFinder() throws IOException, ClassNotFoundException { private static Class<?> classToTest;
execute(new File(rootDirectory+"TestTwoArgs2.jav")); private static Object instanceOfClass;
private static String className = "TestTwoArgs2";
@Test
public void ggFinder() throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
execute(new File(rootDirectory+className+".jav"));
} }
private static class TestResultSet{ private static class TestResultSet{
} }
public TestResultSet execute(File fileToTest) throws IOException, ClassNotFoundException { public TestResultSet execute(File fileToTest) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
JavaTXCompiler compiler = new JavaTXCompiler(fileToTest); JavaTXCompiler compiler = new JavaTXCompiler(fileToTest);
for(File f : compiler.sourceFiles.keySet()){
SourceFile sf = compiler.sourceFiles.get(f);
}
List<ResultSet> results = compiler.typeInference(); List<ResultSet> results = compiler.typeInference();
List<GenericGenratorResultForSourceFile> simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(results); List<GenericGenratorResultForSourceFile> simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(results);
//compiler.generateBytecode(rootDirectory+"xxx.class", results, simplifyResultsForAllSourceFiles); compiler.generateBytecode(new File(pathToClassFile), results, simplifyResultsForAllSourceFiles);
for(File f : compiler.sourceFiles.keySet()){ loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
SourceFile sf = compiler.sourceFiles.get(f); classToTest = loader.loadClass(className);
System.out.println(ASTTypePrinter.print(sf)); instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
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(); return new TestResultSet();
} }
static String readFile(String path, Charset encoding)
throws IOException
{
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
} }

View File

@ -1,4 +1,4 @@
class TestTwoArgs { public class TestTwoArgs {
a; a;
id(b) { id(b) {
@ -13,6 +13,7 @@ class TestTwoArgs {
m(x,y) { m(x,y) {
x = id(y); x = id(y);
return x;
} }
main(x,y) { main(x,y) {

View File

@ -1,4 +1,4 @@
class TestTwoArgs { public class TestTwoArgs2 {
a; a;
id(b) { id(b) {
@ -6,17 +6,12 @@ class TestTwoArgs {
return c; return c;
} }
setA(x) {
a = x;
return a;
}
m(x,y) { m(x,y) {
x = id(y); x = id(y);
return x; return x;
} }
main(x,y) { main(x,y) {
return m(id(x),setA(y)); return m(id(x),m(x,y));
} }
} }