Merge remote-tracking branch 'origin/bytecode2' into bytecode2
This commit is contained in:
commit
86768153c0
@ -5,6 +5,7 @@ package de.dhbwstuttgart.bytecode;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
import de.dhbwstuttgart.bytecode.constraint.ExtendsConstraint;
|
import de.dhbwstuttgart.bytecode.constraint.ExtendsConstraint;
|
||||||
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
|
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
|
||||||
@ -12,7 +13,11 @@ import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation;
|
|||||||
import de.dhbwstuttgart.bytecode.utilities.MethodAndTPH;
|
import de.dhbwstuttgart.bytecode.utilities.MethodAndTPH;
|
||||||
import de.dhbwstuttgart.syntaxtree.AbstractASTWalker;
|
import de.dhbwstuttgart.syntaxtree.AbstractASTWalker;
|
||||||
import de.dhbwstuttgart.syntaxtree.Constructor;
|
import de.dhbwstuttgart.syntaxtree.Constructor;
|
||||||
|
import de.dhbwstuttgart.syntaxtree.FormalParameter;
|
||||||
import de.dhbwstuttgart.syntaxtree.Method;
|
import de.dhbwstuttgart.syntaxtree.Method;
|
||||||
|
import de.dhbwstuttgart.syntaxtree.ParameterList;
|
||||||
|
import de.dhbwstuttgart.syntaxtree.statement.LocalVar;
|
||||||
|
import de.dhbwstuttgart.syntaxtree.statement.LocalVarDecl;
|
||||||
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
|
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
|
||||||
import de.dhbwstuttgart.typeinference.result.GenericInsertPair;
|
import de.dhbwstuttgart.typeinference.result.GenericInsertPair;
|
||||||
import de.dhbwstuttgart.typeinference.result.ResultSet;
|
import de.dhbwstuttgart.typeinference.result.ResultSet;
|
||||||
@ -21,12 +26,15 @@ import de.dhbwstuttgart.typeinference.result.ResultSet;
|
|||||||
* @author Fayez Abu Alia
|
* @author Fayez Abu Alia
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
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?)
|
||||||
final HashMap<TypePlaceholder,Boolean> allTPHS = new HashMap<>();
|
final HashMap<TypePlaceholder, Boolean> allTPHS = new HashMap<>();
|
||||||
MethodAndTPH methodAndTph;
|
MethodAndTPH methodAndTph;
|
||||||
|
|
||||||
Boolean inMethod = false;
|
Boolean inMethod = false;
|
||||||
|
boolean inLocalOrParam = false;
|
||||||
|
|
||||||
public final ArrayList<MethodAndTPH> ListOfMethodsAndTph = new ArrayList<>();
|
public final ArrayList<MethodAndTPH> ListOfMethodsAndTph = new ArrayList<>();
|
||||||
final ArrayList<GenericInsertPair> allPairs = new ArrayList<>();
|
final ArrayList<GenericInsertPair> allPairs = new ArrayList<>();
|
||||||
public final ArrayList<TPHConstraint> allCons = new ArrayList<>();
|
public final ArrayList<TPHConstraint> allCons = new ArrayList<>();
|
||||||
@ -42,15 +50,18 @@ public class TPHExtractor extends AbstractASTWalker{
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(TypePlaceholder tph) {
|
public void visit(TypePlaceholder tph) {
|
||||||
if(resultSet.resolveType(tph).resolvedType instanceof TypePlaceholder) {
|
if (resultSet.resolveType(tph).resolvedType instanceof TypePlaceholder) {
|
||||||
TypePlaceholder resolvedTPH = (TypePlaceholder) resultSet.resolveType(tph).resolvedType;
|
TypePlaceholder resolvedTPH = (TypePlaceholder) resultSet.resolveType(tph).resolvedType;
|
||||||
if(inMethod)
|
if (inMethod) {
|
||||||
methodAndTph.getTphs().add(resolvedTPH.getName());
|
methodAndTph.getTphs().add(resolvedTPH.getName());
|
||||||
|
if (inLocalOrParam)
|
||||||
|
methodAndTph.getLocalTphs().add(resolvedTPH.getName());
|
||||||
|
}
|
||||||
|
|
||||||
allTPHS.put(resolvedTPH,inMethod);
|
allTPHS.put(resolvedTPH, inMethod);
|
||||||
resultSet.resolveType(tph).additionalGenerics.forEach(ag ->{
|
resultSet.resolveType(tph).additionalGenerics.forEach(ag -> {
|
||||||
if(ag.contains(resolvedTPH)&&ag.TA1.equals(resolvedTPH)&&!contains(allPairs,ag)) {
|
if (ag.contains(resolvedTPH) && ag.TA1.equals(resolvedTPH) && !contains(allPairs, ag)) {
|
||||||
if(inMethod)
|
if (inMethod)
|
||||||
methodAndTph.getPairs().add(ag);
|
methodAndTph.getPairs().add(ag);
|
||||||
allPairs.add(ag);
|
allPairs.add(ag);
|
||||||
TPHConstraint con = new ExtendsConstraint(ag.TA1.getName(), ag.TA2.getName(), Relation.EXTENDS);
|
TPHConstraint con = new ExtendsConstraint(ag.TA1.getName(), ag.TA2.getName(), Relation.EXTENDS);
|
||||||
@ -59,14 +70,16 @@ public class TPHExtractor extends AbstractASTWalker{
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean contains(ArrayList<GenericInsertPair> pairs, GenericInsertPair genPair) {
|
private boolean contains(ArrayList<GenericInsertPair> pairs, GenericInsertPair genPair) {
|
||||||
for(int i=0; i<pairs.size();++i) {
|
for (int i = 0; i < pairs.size(); ++i) {
|
||||||
GenericInsertPair p = pairs.get(i);
|
GenericInsertPair p = pairs.get(i);
|
||||||
if(p.TA1.equals(genPair.TA1) && p.TA2.equals(genPair.TA2))
|
if (p.TA1.equals(genPair.TA1) && p.TA2.equals(genPair.TA2))
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(Method method) {
|
public void visit(Method method) {
|
||||||
inMethod = true;
|
inMethod = true;
|
||||||
@ -83,4 +96,25 @@ public class TPHExtractor extends AbstractASTWalker{
|
|||||||
inMethod = true;
|
inMethod = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(LocalVarDecl localVarDecl) {
|
||||||
|
inLocalOrParam = true;
|
||||||
|
super.visit(localVarDecl);
|
||||||
|
inLocalOrParam = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(LocalVar localVar) {
|
||||||
|
inLocalOrParam = true;
|
||||||
|
super.visit(localVar);
|
||||||
|
inLocalOrParam = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(ParameterList formalParameters) {
|
||||||
|
inLocalOrParam = true;
|
||||||
|
super.visit(formalParameters);
|
||||||
|
inLocalOrParam = false;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -4,6 +4,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
|
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
|
||||||
|
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation;
|
||||||
|
|
||||||
public class ConstraintsFinder {
|
public class ConstraintsFinder {
|
||||||
private List<TPHConstraint> allConstaints;
|
private List<TPHConstraint> allConstaints;
|
||||||
@ -18,10 +19,12 @@ public class ConstraintsFinder {
|
|||||||
|
|
||||||
List<TPHConstraint> visitedCons = new ArrayList<>();
|
List<TPHConstraint> visitedCons = new ArrayList<>();
|
||||||
for(TPHConstraint c : allConstaints) {
|
for(TPHConstraint c : allConstaints) {
|
||||||
// get constraints with the same left side
|
if(c.getRel() == Relation.EXTENDS) {
|
||||||
List<TPHConstraint> cons = getConstraints(c,visitedCons);
|
// get constraints with the same left side
|
||||||
if(cons.size()>1)
|
List<TPHConstraint> cons = getConstraints(c,visitedCons);
|
||||||
result.add(cons);
|
if(cons.size()>1)
|
||||||
|
result.add(cons);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
@ -10,6 +10,8 @@ public class MethodAndTPH {
|
|||||||
private String name;
|
private String name;
|
||||||
private final ArrayList<String> tphs = new ArrayList<>();
|
private final ArrayList<String> tphs = new ArrayList<>();
|
||||||
private final ArrayList<GenericInsertPair> pairs = new ArrayList<>();
|
private final ArrayList<GenericInsertPair> pairs = new ArrayList<>();
|
||||||
|
// tphs of local variables and parameters
|
||||||
|
private final ArrayList<String> localTphs = new ArrayList<>();
|
||||||
|
|
||||||
public MethodAndTPH(String name) {
|
public MethodAndTPH(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
@ -26,4 +28,9 @@ public class MethodAndTPH {
|
|||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ArrayList<String> getLocalTphs() {
|
||||||
|
return localTphs;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -12,12 +12,14 @@ public class NameReplacer {
|
|||||||
private List<TPHConstraint> constraints;
|
private List<TPHConstraint> constraints;
|
||||||
private List<TPHConstraint> allConstraints;
|
private List<TPHConstraint> allConstraints;
|
||||||
private List<String> tphs;
|
private List<String> tphs;
|
||||||
|
private List<String> localTphs;
|
||||||
|
|
||||||
public NameReplacer(List<TPHConstraint> constraints, List<TPHConstraint> allConstraints,List<String> tphs) {
|
public NameReplacer(List<TPHConstraint> constraints, List<TPHConstraint> allConstraints,List<String> tphs, ArrayList<String> localTphs) {
|
||||||
super();
|
super();
|
||||||
this.constraints = constraints;
|
this.constraints = constraints;
|
||||||
this.allConstraints = allConstraints;
|
this.allConstraints = allConstraints;
|
||||||
this.tphs = tphs;
|
this.tphs = tphs;
|
||||||
|
this.localTphs = localTphs;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, List<String>> replaceNames() {
|
public Map<String, List<String>> replaceNames() {
|
||||||
@ -34,8 +36,11 @@ public class NameReplacer {
|
|||||||
if(names.contains(cons.getRight()))
|
if(names.contains(cons.getRight()))
|
||||||
cons.setRight(newName);
|
cons.setRight(newName);
|
||||||
}
|
}
|
||||||
|
|
||||||
tphs.removeAll(names);
|
tphs.removeAll(names);
|
||||||
tphs.add(newName);
|
tphs.add(newName);
|
||||||
|
localTphs.removeAll(names);
|
||||||
|
localTphs.add(newName);
|
||||||
|
|
||||||
HashMap<String, List<String>> res = new HashMap<>();
|
HashMap<String, List<String>> res = new HashMap<>();
|
||||||
res.put(newName, names);
|
res.put(newName, names);
|
||||||
|
File diff suppressed because it is too large
Load Diff
37
src/test/java/bytecode/KompTphTest.java
Normal file
37
src/test/java/bytecode/KompTphTest.java
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
package bytecode;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.URLClassLoader;
|
||||||
|
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import de.dhbwstuttgart.core.JavaTXCompiler;
|
||||||
|
|
||||||
|
public class KompTphTest {
|
||||||
|
|
||||||
|
private static String path;
|
||||||
|
private static File fileToTest;
|
||||||
|
private static JavaTXCompiler compiler;
|
||||||
|
private static ClassLoader loader;
|
||||||
|
private static Class<?> classToTest;
|
||||||
|
private static String pathToClassFile;
|
||||||
|
private static Object instanceOfClass;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void generateBC() throws Exception {
|
||||||
|
path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/KompTph.jav";
|
||||||
|
fileToTest = new File(path);
|
||||||
|
compiler = new JavaTXCompiler(fileToTest);
|
||||||
|
pathToClassFile = System.getProperty("user.dir")+"/src/test/resources/testBytecode/generatedBC/";
|
||||||
|
compiler.generateBytecode(pathToClassFile);
|
||||||
|
loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
|
||||||
|
classToTest = loader.loadClass("KompTph");
|
||||||
|
instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
13
src/test/resources/bytecode/javFiles/KompTph.jav
Normal file
13
src/test/resources/bytecode/javFiles/KompTph.jav
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
public class KompTph {
|
||||||
|
public m(a, b, c) {
|
||||||
|
var d = a;
|
||||||
|
var e = a;
|
||||||
|
a = b;
|
||||||
|
c = b;
|
||||||
|
m2(a,c);
|
||||||
|
}
|
||||||
|
|
||||||
|
public m2(a,b){
|
||||||
|
m(a,a,b);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user