forked from JavaTX/JavaCompilerCore
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:
parent
ef723e4103
commit
92bc3d626c
@ -24,8 +24,10 @@ import de.dhbwstuttgart.syntaxtree.Field;
|
||||
import de.dhbwstuttgart.syntaxtree.FormalParameter;
|
||||
import de.dhbwstuttgart.syntaxtree.Method;
|
||||
import de.dhbwstuttgart.syntaxtree.ParameterList;
|
||||
import de.dhbwstuttgart.syntaxtree.SourceFile;
|
||||
import de.dhbwstuttgart.syntaxtree.statement.LocalVar;
|
||||
import de.dhbwstuttgart.syntaxtree.statement.LocalVarDecl;
|
||||
import de.dhbwstuttgart.syntaxtree.statement.MethodCall;
|
||||
import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
|
||||
import de.dhbwstuttgart.syntaxtree.type.RefType;
|
||||
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
|
||||
@ -41,6 +43,8 @@ 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;
|
||||
public final HashMap<String, Method> nameToMeth;
|
||||
public final HashMap<String, Boolean> allTPHS = new HashMap<>();
|
||||
public final List<String> tphsClass = new ArrayList<>();
|
||||
MethodAndTPH methodAndTph;
|
||||
@ -54,8 +58,12 @@ public class TPHExtractor extends AbstractASTWalker {
|
||||
private ResultSet resultSet;
|
||||
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) {
|
||||
@ -216,6 +224,24 @@ public class TPHExtractor extends AbstractASTWalker {
|
||||
// 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
|
||||
public void visit(LocalVar localVar) {
|
||||
// inLocalOrParamOrReturn = inMethod;
|
||||
|
@ -66,7 +66,7 @@ import de.dhbwstuttgart.typeinference.result.ResultSet;
|
||||
*
|
||||
*/
|
||||
public class GeneratedGenericsFinder implements ASTVisitor {
|
||||
private final TPHExtractor tphExtractor = new TPHExtractor();
|
||||
private final TPHExtractor tphExtractor;
|
||||
private Collection<ResultSet> listOfResultSets;
|
||||
private SourceFile sf;
|
||||
private List<String> tphsClass;
|
||||
@ -86,6 +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);
|
||||
}
|
||||
|
||||
public GenericGenratorResultForSourceFile findGeneratedGenerics() {
|
||||
|
@ -46,6 +46,12 @@ public class SourceFile extends SyntaxTreeNode{
|
||||
return KlassenVektor;
|
||||
}
|
||||
|
||||
public List<Method> getAllMethods() {
|
||||
List<Method> ret = new ArrayList<>();
|
||||
getClasses().forEach(cl -> ret.addAll(cl.getMethods()));
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(ASTVisitor visitor) {
|
||||
visitor.visit(this);
|
||||
|
@ -258,6 +258,7 @@ public class UnifyTypeFactory {
|
||||
ret = TypePlaceholder.fresh(new NullToken());
|
||||
tphs.put(t.getName(), ret);
|
||||
}
|
||||
ret.setVariance(t.getVariance());
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -20,10 +20,9 @@ public class TypePlaceholder extends RefTypeOrTPHOrWildcardOrGeneric
|
||||
private final String name;
|
||||
|
||||
/**
|
||||
* wird bisher nicht genutzt
|
||||
* setVariance muss ggf. auskommentiert werden.
|
||||
* wird im Generate Generics Teil nach der Rueckumwandlung nach dem Unify genutzt
|
||||
*/
|
||||
int variance = 0;
|
||||
private int variance = 0;
|
||||
|
||||
|
||||
/**
|
||||
@ -78,11 +77,9 @@ public class TypePlaceholder extends RefTypeOrTPHOrWildcardOrGeneric
|
||||
return name;
|
||||
}
|
||||
|
||||
/* wird bisher nicht genutzt
|
||||
public void setVariance(int variance) {
|
||||
this.variance= variance;
|
||||
}
|
||||
*/
|
||||
|
||||
public int getVariance() {
|
||||
return this.variance;
|
||||
|
@ -12,6 +12,9 @@ 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;
|
||||
@ -22,51 +25,30 @@ import java.util.Set;
|
||||
|
||||
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/";
|
||||
private static ClassLoader loader;
|
||||
private static Class<?> classToTest;
|
||||
private static Object instanceOfClass;
|
||||
private static String className = "TestTwoArgs";
|
||||
|
||||
@Test
|
||||
public void ggFinder() throws IOException, ClassNotFoundException {
|
||||
execute(new File(rootDirectory+"TestTwoArgs.jav"));
|
||||
@Test
|
||||
public void ggFinder() throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
|
||||
execute(new File(rootDirectory+className+".jav"));
|
||||
}
|
||||
|
||||
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);
|
||||
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);
|
||||
}
|
||||
}
|
||||
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();
|
||||
}
|
||||
|
||||
static String readFile(String path, Charset encoding)
|
||||
throws IOException
|
||||
{
|
||||
byte[] encoded = Files.readAllBytes(Paths.get(path));
|
||||
return new String(encoded, encoding);
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,9 @@ 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;
|
||||
@ -22,51 +25,30 @@ import java.util.Set;
|
||||
|
||||
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/";
|
||||
private static ClassLoader loader;
|
||||
private static Class<?> classToTest;
|
||||
private static Object instanceOfClass;
|
||||
private static String className = "TestTwoArgs2";
|
||||
|
||||
@Test
|
||||
public void ggFinder() throws IOException, ClassNotFoundException {
|
||||
execute(new File(rootDirectory+"TestTwoArgs2.jav"));
|
||||
@Test
|
||||
public void ggFinder() throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
|
||||
execute(new File(rootDirectory+className+".jav"));
|
||||
}
|
||||
|
||||
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);
|
||||
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);
|
||||
}
|
||||
}
|
||||
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();
|
||||
}
|
||||
|
||||
static String readFile(String path, Charset encoding)
|
||||
throws IOException
|
||||
{
|
||||
byte[] encoded = Files.readAllBytes(Paths.get(path));
|
||||
return new String(encoded, encoding);
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
class TestTwoArgs {
|
||||
public class TestTwoArgs {
|
||||
a;
|
||||
|
||||
id(b) {
|
||||
@ -13,6 +13,7 @@ class TestTwoArgs {
|
||||
|
||||
m(x,y) {
|
||||
x = id(y);
|
||||
return x;
|
||||
}
|
||||
|
||||
main(x,y) {
|
||||
|
@ -1,4 +1,4 @@
|
||||
class TestTwoArgs {
|
||||
public class TestTwoArgs2 {
|
||||
a;
|
||||
|
||||
id(b) {
|
||||
@ -6,17 +6,12 @@ class TestTwoArgs {
|
||||
return c;
|
||||
}
|
||||
|
||||
setA(x) {
|
||||
a = x;
|
||||
return a;
|
||||
}
|
||||
|
||||
m(x,y) {
|
||||
x = id(y);
|
||||
return x;
|
||||
}
|
||||
|
||||
main(x,y) {
|
||||
return m(id(x),setA(y));
|
||||
return m(id(x),m(x,y));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user