modified: src/main/java/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java

Bug in generate BC fuer if(Boolean) gefixt

	modified:   src/main/java/de/dhbwstuttgart/bytecode/signature/Signature.java

	new file:   src/main/java/de/dhbwstuttgart/bytecode/utilities/ConstraintsFinder.java
	Fasst alle Constraints mit der gleichen Linke-Seite in einer Liste zusammen

	new file:   src/main/java/de/dhbwstuttgart/bytecode/utilities/NameReplacer.java
	Ersetzt die gleiche Type Variables durch einen neuen eindeutigen Namen

	modified:   src/main/java/de/dhbwstuttgart/bytecode/utilities/Simplify.java
	Algorithmus angepasst

	modified:   src/test/java/bytecode/FieldTphMMethTest.java
	Test funktioniert
	new file:   src/test/java/bytecode/InfTest.java
	Infimum Test funktioniert

	new file:   src/test/java/bytecode/simplifyalgo/FinderTest.java
	Tests fuer die HilfsMethoden
This commit is contained in:
Fayez Abu Alia 2019-02-14 11:37:15 +01:00
parent fd72ed340d
commit d2581b02ab
10 changed files with 398 additions and 118 deletions

View File

@ -744,6 +744,13 @@ public class BytecodeGenMethod implements StatementVisitor {
statement = new IfStatement(ifStmt.expr, ifStmt.then_block, ifStmt.else_block);
isBinaryExp = statement.isExprBinary();
ifStmt.expr.accept(this);
if(!(ifStmt.expr instanceof BinaryExpr)) {
doUnboxing(getResolvedType(ifStmt.expr.getType()));
Label branchLabel = new Label();
Label endLabel = new Label();
mv.visitJumpInsn(Opcodes.IFEQ, branchLabel);
statement.genBCForRelOp(mv, branchLabel, endLabel, this);
}
statement = null;
}
@ -758,86 +765,90 @@ public class BytecodeGenMethod implements StatementVisitor {
System.out.println("In MethodCall = " + methodCall.name);
String receiverName = getResolvedType(methodCall.receiver.getType());
System.out.println("Methods of " + receiverName + " ");
ClassLoader cLoader = ClassLoader.getSystemClassLoader();
// This will be used if the class is not standard class (not in API)
ClassLoader cLoader2;
java.lang.reflect.Method methodRefl = null;
String clazz = receiverName.replace("/", ".");
String methCallType = resultSet.resolveType(methodCall.getType()).resolvedType.acceptTV(new TypeToDescriptor());
String[] typesOfParams = getTypes(methodCall.arglist.getArguments());
try {
if(receiverName.contains("<")) {
clazz = clazz.substring(0, receiverName.indexOf("<"));
}
if(!receiverName.equals(className)) {
ClassLoader cLoader = ClassLoader.getSystemClassLoader();
// This will be used if the class is not standard class (not in API)
ClassLoader cLoader2;
java.lang.reflect.Method[] methods = cLoader.loadClass(clazz).getMethods();
System.out.println("Methods of " + receiverName + " ");
methodRefl = getMethod(methodCall.name,methodCall.arglist.getArguments().size(),methods);
String methCallType = resultSet.resolveType(methodCall.getType()).resolvedType.acceptTV(new TypeToDescriptor());
String[] typesOfParams = getTypes(methodCall.arglist.getArguments());
try {
if(receiverName.contains("<")) {
clazz = clazz.substring(0, receiverName.indexOf("<"));
}
} catch (Exception e) {
// try {
// cLoader2 = new URLClassLoader(new URL[] {new URL("file://"+path)});
// java.lang.reflect.Method[] methods = cLoader2.loadClass(clazz).getMethods();
// System.out.println("Methods of " + receiverName + " ");
// for(int i = 0; i<methods.length; i++) {
// System.out.println(methods[i]);
// }
// methodRefl = getMethod(methodCall.name,methodCall.arglist.getArguments().size(),methCallType, typesOfParams,methods);
// }catch (Exception e2) {
String superClass = "";
// TODO: Test SubMatrix.jav
while(true) {
for(ClassOrInterface cl : sf.getClasses()) {
if(receiverName.equals(cl.getClassName().toString())) {
superClass = cl.getSuperClass().getName().toString();
break;
}
}
System.out.println(superClass);
java.lang.reflect.Method[] methods = cLoader.loadClass(clazz).getMethods();
System.out.println("Methods of " + receiverName + " ");
methodRefl = getMethod(methodCall.name,methodCall.arglist.getArguments().size(),methods);
if(superClass.equals(""))
break;
try {
String superClazz = superClass.replace("/", ".");
if(superClass.contains("<")) {
superClazz = superClazz.substring(0, superClass.indexOf("<"));
}
java.lang.reflect.Method[] methods = cLoader.loadClass(superClazz).getMethods();
System.out.println("Methods of " + superClass + " ");
for(java.lang.reflect.Method m : methods) {
if(methodCall.name.equals(m.getName())) {
methodRefl = m;
} catch (Exception e) {
// try {
// cLoader2 = new URLClassLoader(new URL[] {new URL("file://"+path)});
// java.lang.reflect.Method[] methods = cLoader2.loadClass(clazz).getMethods();
// System.out.println("Methods of " + receiverName + " ");
// for(int i = 0; i<methods.length; i++) {
// System.out.println(methods[i]);
// }
// methodRefl = getMethod(methodCall.name,methodCall.arglist.getArguments().size(),methCallType, typesOfParams,methods);
// }catch (Exception e2) {
String superClass = "";
// TODO: Test SubMatrix.jav
while(true) {
for(ClassOrInterface cl : sf.getClasses()) {
if(receiverName.equals(cl.getClassName().toString())) {
superClass = cl.getSuperClass().getName().toString();
break;
}
}
System.out.println(superClass);
break;
} catch (Exception e3) {
receiverName = superClass;
continue;
if(superClass.equals(""))
break;
try {
String superClazz = superClass.replace("/", ".");
if(superClass.contains("<")) {
superClazz = superClazz.substring(0, superClass.indexOf("<"));
}
java.lang.reflect.Method[] methods = cLoader.loadClass(superClazz).getMethods();
System.out.println("Methods of " + superClass + " ");
for(java.lang.reflect.Method m : methods) {
if(methodCall.name.equals(m.getName())) {
methodRefl = m;
break;
}
}
break;
} catch (Exception e3) {
receiverName = superClass;
continue;
}
}
}
// }
// }
}
if(methodRefl == null) {
try {
cLoader2 = new URLClassLoader(new URL[] {new URL("file://"+path)});
java.lang.reflect.Method[] methods = cLoader2.loadClass(clazz).getMethods();
System.out.println("Methods of " + receiverName + " ");
for(int i = 0; i<methods.length; i++) {
System.out.println(methods[i]);
}
methodRefl = getMethod(methodCall.name,methodCall.arglist.getArguments().size(),methCallType, typesOfParams,methods);
}
catch (Exception e2) {
System.out.println("");
//do nothing
if(methodRefl == null) {
try {
cLoader2 = new URLClassLoader(new URL[] {new URL("file://"+path)});
java.lang.reflect.Method[] methods = cLoader2.loadClass(clazz).getMethods();
System.out.println("Methods of " + receiverName + " ");
for(int i = 0; i<methods.length; i++) {
System.out.println(methods[i]);
}
methodRefl = getMethod(methodCall.name,methodCall.arglist.getArguments().size(),methCallType, typesOfParams,methods);
}
catch (Exception e2) {
System.out.println("");
//do nothing
}
}
}
methodCall.receiver.accept(this);
System.out.println("Methodcall type : " + resultSet.resolveType(methodCall.getType()).resolvedType.acceptTV(new TypeToDescriptor()));
@ -1197,6 +1208,7 @@ public class BytecodeGenMethod implements StatementVisitor {
break;
case "java/lang/Boolean":
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z", false);
break;
case "java/lang/Byte":
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Byte", "byteValue", "()B", false);

View File

@ -391,7 +391,7 @@ public class Signature {
}
for(TPHConstraint cons : consClass) {
if(!types.contains(cons.getRight())) {
if(!types.contains(cons.getRight()) && !genericsAndBounds.keySet().contains(cons.getRight()+"$")) {
String t = cons.getRight()+"$";
String bound = Type.getInternalName(Object.class);
sw.visitFormalTypeParameter(t);

View File

@ -0,0 +1,48 @@
package de.dhbwstuttgart.bytecode.utilities;
import java.util.ArrayList;
import java.util.List;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
public class ConstraintsFinder {
private List<TPHConstraint> allConstaints;
public ConstraintsFinder(List<TPHConstraint> allConstaints) {
super();
this.allConstaints = allConstaints;
}
public List<List<TPHConstraint>> findConstraints() {
List<List<TPHConstraint>> result = new ArrayList<>();
List<TPHConstraint> visitedCons = new ArrayList<>();
for(TPHConstraint c : allConstaints) {
// get constraints with the same left side
List<TPHConstraint> cons = getConstraints(c,visitedCons);
if(cons.size()>1)
result.add(cons);
}
return result;
}
private List<TPHConstraint> getConstraints(TPHConstraint c, List<TPHConstraint> visitedCons) {
List<TPHConstraint> res = new ArrayList<>();
for(TPHConstraint cons : allConstaints) {
if(!isVisited(cons,visitedCons) && cons.getLeft().equals(c.getLeft())) {
res.add(cons);
visitedCons.add(cons);
}
}
return res;
}
private boolean isVisited(TPHConstraint cons, List<TPHConstraint> visitedCons) {
for(TPHConstraint c : visitedCons) {
if(c.getLeft().equals(cons.getLeft()) && c.getRight().equals(cons.getRight()))
return true;
}
return false;
}
}

View File

@ -0,0 +1,44 @@
package de.dhbwstuttgart.bytecode.utilities;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
import de.dhbwstuttgart.syntaxtree.factory.NameGenerator;
public class NameReplacer {
private List<TPHConstraint> constraints;
private List<TPHConstraint> allConstraints;
private List<String> tphs;
public NameReplacer(List<TPHConstraint> constraints, List<TPHConstraint> allConstraints,List<String> tphs) {
super();
this.constraints = constraints;
this.allConstraints = allConstraints;
this.tphs = tphs;
}
public Map<String, List<String>> replaceNames() {
String newName = NameGenerator.makeNewName();
ArrayList<String> names = new ArrayList<>();
for(TPHConstraint cons : constraints) {
names.add(cons.getRight());
cons.setRight(newName);
}
for(TPHConstraint cons : allConstraints) {
if(names.contains(cons.getLeft()))
cons.setLeft(newName);
if(names.contains(cons.getRight()))
cons.setRight(newName);
}
tphs.removeAll(names);
tphs.add(newName);
HashMap<String, List<String>> res = new HashMap<>();
res.put(newName, names);
return res;
}
}

View File

@ -4,6 +4,8 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.objectweb.asm.Type;
@ -177,7 +179,8 @@ public class Simplify {
TPHConstraint cons = allCons.get(0);
if(!result.containsKey(cons)) {
result.put(cons, null);
result.put(new ExtendsConstraint(cons.getRight(), Type.getInternalName(Object.class), Relation.EXTENDS), null);
if(!cons.getRight().equals(Type.getInternalName(Object.class)))
result.put(new ExtendsConstraint(cons.getRight(), Type.getInternalName(Object.class), Relation.EXTENDS), null);
}
return result;
@ -198,50 +201,79 @@ public class Simplify {
break;
}
}
ConstraintsFinder finder = new ConstraintsFinder(allCons);
List<List<TPHConstraint>> foundCons = finder.findConstraints();
ArrayList<TPHConstraint> eqCons = new ArrayList<>();
for(List<TPHConstraint> list : foundCons) {
NameReplacer replacer = new NameReplacer(list,allCons,methodTphs);
Map<String, List<String>> replRes = replacer.replaceNames();
String key = replRes.keySet().iterator().next();
for(String val : replRes.values().iterator().next()) {
EqualConstraint ec = new EqualConstraint(val, key, Relation.EQUAL);
eqCons.add(ec);
}
updateEqualCons(replRes,eqCons);
TPHConstraint c = list.get(0);
allCons.removeAll(list);
allCons.add(c);
}
// check if there are multiple constraint with the same left side.
// if yes => check if the super type in the method, if not
// then ignore it.
// HashMap<String, String> subAndSuper = new HashMap<>();
// ArrayList<TPHConstraint> eqCons = new ArrayList<>();
// for(TPHConstraint c : allCons) {
// if(subAndSuper.containsKey(c.getLeft())) {
// LinkedList<String> all = new LinkedList<>();
// all.add(c.getLeft());
// String sup =c.getRight();
// all.add(sup);
// HashMap<String, String> ss = new HashMap<>();
// for(TPHConstraint constr : allCons) {
// ss.put(constr.getLeft(), constr.getRight());
// }
// while(ss.containsKey(sup)) {
// sup = ss.get(sup);
// all.add(sup);
// }
// if(!containTPH(methodTphs, all.getLast()))
// continue;
// }
// if(subAndSuper.containsKey(c.getLeft())) {
// System.out.println(c.getLeft());
// String r = c.getRight();
// String r2 = subAndSuper.get(c.getLeft());
// EqualConstraint eq = new EqualConstraint(r2, r, Relation.EQUAL);
// eqCons.add(eq);
// substituteInMap(subAndSuper,eqCons,allCons,r,r2);
// }
// subAndSuper.put(c.getLeft(), c.getRight());
// }
//
// System.out.println("SAME LEFT SIDE: ");
// subAndSuper.forEach((c,hs)->{
// if(c!=null) {
// System.out.print(c+ " -> " + hs);
//
// }
//
//
// System.out.println();
// });
// System.out.println("----------------");
HashMap<String, String> subAndSuper = new HashMap<>();
ArrayList<TPHConstraint> eqCons = new ArrayList<>();
for(TPHConstraint c : allCons) {
if(subAndSuper.containsKey(c.getLeft())) {
LinkedList<String> all = new LinkedList<>();
all.add(c.getLeft());
String sup =c.getRight();
all.add(sup);
HashMap<String, String> ss = new HashMap<>();
for(TPHConstraint constr : allCons) {
ss.put(constr.getLeft(), constr.getRight());
}
while(ss.containsKey(sup)) {
sup = ss.get(sup);
all.add(sup);
}
if(!containTPH(methodTphs, all.getLast()))
continue;
}
if(subAndSuper.containsKey(c.getLeft())) {
System.out.println(c.getLeft());
String r = c.getRight();
String r2 = subAndSuper.get(c.getLeft());
EqualConstraint eq = new EqualConstraint(r2, r, Relation.EQUAL);
eqCons.add(eq);
substituteInMap(subAndSuper,eqCons,allCons,r,r2);
}
subAndSuper.put(c.getLeft(), c.getRight());
}
System.out.println("SAME LEFT SIDE: ");
subAndSuper.forEach((c,hs)->{
if(c!=null) {
System.out.print(c+ " -> " + hs);
}
System.out.println();
});
System.out.println("----------------");
int numOfVisitedPairs = 0;
for(String sub : subAndSuper.keySet()) {
if(isTPHInConstraint(result,sub))
@ -348,6 +380,19 @@ public class Simplify {
}
private static void updateEqualCons(Map<String, List<String>> replRes, ArrayList<TPHConstraint> eqCons) {
List<String> oldNames = replRes.values().iterator().next();
String newName = replRes.keySet().iterator().next();
for(TPHConstraint c : eqCons) {
// if(oldNames.contains(c.getLeft()))
// c.setLeft(newName);
if(oldNames.contains(c.getRight()))
c.setRight(newName);
}
}
public static HashMap<TPHConstraint, HashSet<String>> simplifyConstraintsClass(TPHExtractor tphExtractor, ArrayList<TypePlaceholder> tphsClass) {
// all constraints that will be simplified
ArrayList<TPHConstraint> allCons = tphExtractor.allCons;

View File

@ -33,20 +33,33 @@ public class FieldTphMMethTest {
pathToClassFile = System.getProperty("user.dir")+"/src/test/resources/testBytecode/generatedBC/";
loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
classToTest = loader.loadClass("FieldTphMMeth");
instanceOfClass = classToTest.getConstructor(Object.class,Object.class,Boolean.class).newInstance("C",42,true);
instanceOfClass2 = classToTest.getConstructor(Object.class,Object.class,Boolean.class).newInstance("C",42,false);
}
@Test
public void test() throws Exception {
instanceOfClass = classToTest.getConstructor(Object.class).newInstance("C",42,true);
instanceOfClass2 = classToTest.getConstructor(Object.class).newInstance("C",42,false);
public void testM() throws Exception {
Method m = classToTest.getDeclaredMethod("m", Object.class,Object.class,Boolean.class);
Object result = m.invoke(instanceOfClass, "C",42,false);
assertEquals(42,result);
}
@Test
public void testWithTrue() throws Exception {
Field a = classToTest.getDeclaredField("a");
a.setAccessible(true);
Method m = classToTest.getDeclaredMethod("m", Object.class);
Object result = m.invoke(instanceOfClass, 42);
assertEquals(42,result);
assertEquals("C", a.get(instanceOfClass));
}
@Test
public void testWithFalse() throws Exception {
Field a = classToTest.getDeclaredField("a");
a.setAccessible(true);
assertEquals(42, a.get(instanceOfClass2));
}

View 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 InfTest {
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/Inf.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("Inf");
instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
}
}

View File

@ -0,0 +1,64 @@
package bytecode.simplifyalgo;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import org.junit.BeforeClass;
import org.junit.Test;
import org.objectweb.asm.Type;
import de.dhbwstuttgart.bytecode.TPHExtractor;
import de.dhbwstuttgart.bytecode.constraint.ExtendsConstraint;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation;
import de.dhbwstuttgart.bytecode.utilities.ConstraintsFinder;
import de.dhbwstuttgart.bytecode.utilities.MethodAndTPH;
import de.dhbwstuttgart.bytecode.utilities.Simplify;
import de.dhbwstuttgart.typedeployment.TypeInsertPlacer;
/**
*
* @author Fayez Abu Alia
*
*/
public class FinderTest {
@Test
public void testM1() {
List<TPHConstraint> allCons = new ArrayList<>();
// L < M
TPHConstraint c1 = new ExtendsConstraint("L", "M", Relation.EXTENDS);
// L < N
TPHConstraint c2 = new ExtendsConstraint("L", "N", Relation.EXTENDS);
// M < O
TPHConstraint c3 = new ExtendsConstraint("M", "O", Relation.EXTENDS);
// M < P
TPHConstraint c4 = new ExtendsConstraint("M", "P", Relation.EXTENDS);
allCons.add(c1);
allCons.add(c2);
allCons.add(c3);
allCons.add(c4);
List<List<TPHConstraint>> res = new ArrayList<>();
List<TPHConstraint> l1 = new ArrayList<>();
List<TPHConstraint> l2 = new ArrayList<>();
l1.add(c1);
l1.add(c2);
l2.add(c3);
l2.add(c4);
res.add(l1);
res.add(l2);
ConstraintsFinder finder = new ConstraintsFinder(allCons);
assertEquals(finder.findConstraints(), res);
}
}

View File

@ -1,14 +1,17 @@
import java.lang.Boolean;
public class FieldTphMMeth {
a;
public FieldTphConsMeth(c,d,e) {
public FieldTphMMeth(c,d,e) {
a = m(c,d,e);
}
m(b,d,e) {
if(e) {
return b;
return m3(b);
} else{
m2(d);
return m3(d);
}
}

View File

@ -0,0 +1,14 @@
public class Inf {
m(x,y,a){
var z;
var v;
var w;
var b;
y=x;
z=x;
v=y;
w=y;
y=a;
b=a;
}
}