diff --git a/pom.xml b/pom.xml
index ca4bae64..8fe3f636 100644
--- a/pom.xml
+++ b/pom.xml
@@ -17,10 +17,11 @@ http://maven.apache.org/maven-v4_0_0.xsd">
4.11
test
+
org.antlr
antlr4
- 4.8-1
+ 4.11.1
commons-io
@@ -60,7 +61,7 @@ http://maven.apache.org/maven-v4_0_0.xsd">
org.antlr
antlr4-maven-plugin
- 4.8-1
+ 4.11.1
antlr
diff --git a/resources/AllgemeinTest/VectorNotObject.jav b/resources/AllgemeinTest/VectorNotObject.jav
new file mode 100644
index 00000000..d8ea900b
--- /dev/null
+++ b/resources/AllgemeinTest/VectorNotObject.jav
@@ -0,0 +1,9 @@
+import java.util.Vector;
+
+class VectorNotObject {
+
+ vectorAddAll(v1, v2) {
+ v1.addAll(v2);
+ return v1;
+ }
+ }
diff --git a/resources/AllgemeinTest/WildcardCaptureConversionTest.jav b/resources/AllgemeinTest/WildcardCaptureConversionTest.jav
new file mode 100644
index 00000000..59016f7a
--- /dev/null
+++ b/resources/AllgemeinTest/WildcardCaptureConversionTest.jav
@@ -0,0 +1,16 @@
+import java.util.Vector;
+import java.lang.Integer;
+import java.lang.String;
+
+class WildcardCaptureConversionTest {
+
+ m(x, y ,z) {
+ x = new Vector();
+ y = new Vector();
+ x.add(1);
+ y.add("2");
+ //Integer i = x.elementAt(0);
+ //String s = y.elementAt(0);
+ return z.vectorAddAll(x, y);
+ }
+}
\ No newline at end of file
diff --git a/resources/bytecode/javFiles/Box.jav b/resources/bytecode/javFiles/Box.jav
new file mode 100644
index 00000000..4859d611
--- /dev/null
+++ b/resources/bytecode/javFiles/Box.jav
@@ -0,0 +1,7 @@
+class B { }
+class Box_Main extends B {
+ m(b) {
+ b.m(new Box_Main());
+ b.m(new B());
+ }
+}
diff --git a/resources/bytecode/javFiles/Box.java b/resources/bytecode/javFiles/Box.java
new file mode 100644
index 00000000..581e7256
--- /dev/null
+++ b/resources/bytecode/javFiles/Box.java
@@ -0,0 +1,3 @@
+class Box {
+ void m(A a) { }
+}
\ No newline at end of file
diff --git a/resources/bytecode/javFiles/Generics2.jav b/resources/bytecode/javFiles/Generics2.jav
index 52d5caa2..762890bd 100644
--- a/resources/bytecode/javFiles/Generics2.jav
+++ b/resources/bytecode/javFiles/Generics2.jav
@@ -1,3 +1,6 @@
+import java.lang.String;
+import java.lang.Integer;
+
class Generics2{
B m1(B b){
return b;
diff --git a/resources/bytecode/javFiles/Generics3.jav b/resources/bytecode/javFiles/Generics3.jav
new file mode 100644
index 00000000..fb354d43
--- /dev/null
+++ b/resources/bytecode/javFiles/Generics3.jav
@@ -0,0 +1,7 @@
+import java.lang.String;
+import java.lang.Integer;
+import java.util.List;
+
+class Generics3> {
+
+}
\ No newline at end of file
diff --git a/resources/bytecode/javFiles/Generics4.jav b/resources/bytecode/javFiles/Generics4.jav
new file mode 100644
index 00000000..50b0b3e2
--- /dev/null
+++ b/resources/bytecode/javFiles/Generics4.jav
@@ -0,0 +1,12 @@
+import java.lang.String;
+import java.lang.Integer;
+
+class Generics4 {
+ C m1(C b){
+ return b;
+ }
+
+ m2(x) {
+ return m1(x);
+ }
+}
\ No newline at end of file
diff --git a/resources/bytecode/javFiles/Inf.jav b/resources/bytecode/javFiles/Inf.jav
index 788fca8d..60029949 100644
--- a/resources/bytecode/javFiles/Inf.jav
+++ b/resources/bytecode/javFiles/Inf.jav
@@ -10,6 +10,10 @@ public class Inf {
w=y;
y=a;
b=a;
+ var c;
+ var d;
+ c = v;
+ d = v;
}
}
@@ -25,21 +29,27 @@ TPH M m(TPH N x, TPH O y, TPH P a)({
(w)::TPH S = (y)::TPH O;
(y)::TPH O = (a)::TPH P;
(b)::TPH T = (a)::TPH P;
+ TPH U c;
+ TPH V d;
+ (c)::TPH U = (v)::TPH R;
+ (d)::TPH V = (v)::TPH R;
return;
- })::TPH U
+})::TPH W
Inf()({
super(());
- })::TPH X
+ })::TPH Z
}
-// v::R w::S
+// c::U d::V
+// \ /
+// v::R w::S
// \ /
// z::Q y::O b::T
// \ / \ /
// x::N a::P
-RESULT Final: [[(TPH O < TPH S), (TPH P < TPH O), (TPH O < TPH R), (TPH P < TPH T), (TPH M = void), (TPH N < TPH O), (TPH N < TPH Q)]]
+RESULT Final: [[(TPH N < TPH O), (TPH R < TPH V), (TPH N < TPH Q), (TPH P < TPH O), (TPH R < TPH U), (TPH M = void), (TPH O < TPH S), (TPH O < TPH R), (TPH P < TPH T)]]
Simplified constraints: [(TPH O < TPH S), (TPH P < TPH O), (TPH O < TPH R), (TPH P < TPH T), (TPH N < TPH O), (TPH N < TPH Q)]
m: [(TPH DDV = java.lang.Object), (TPH DDX = java.lang.Object), (TPH DDX < TPH DDV), (TPH N < TPH DDX), (TPH P < TPH DDX)]
Class Inf: []
diff --git a/resources/bytecode/javFiles/SimpleCycle.jav b/resources/bytecode/javFiles/SimpleCycle.jav
index 2f45aa18..92f505b5 100644
--- a/resources/bytecode/javFiles/SimpleCycle.jav
+++ b/resources/bytecode/javFiles/SimpleCycle.jav
@@ -1,6 +1,6 @@
public class SimpleCycle {
- m(a,b,d){
+ m(){
var g;
var h;
g = h;
diff --git a/resources/bytecode/javFiles/VectorAdd.jav b/resources/bytecode/javFiles/VectorAdd.jav
index d5e50157..2ad73aa9 100644
--- a/resources/bytecode/javFiles/VectorAdd.jav
+++ b/resources/bytecode/javFiles/VectorAdd.jav
@@ -1,6 +1,6 @@
import java.util.Vector;
import java.lang.Integer;
-
+import java.lang.String;
public class VectorAdd {
vectorAdd(v1, v2) {
@@ -13,4 +13,14 @@ public class VectorAdd {
}
return erg;
}
+
+ m(x, y, z) {
+ x = new Vector();
+ y = new Vector();
+ x.add(1);
+ y.add("2");
+ //Integer i = x.elementAt(0);
+ //String s = y.elementAt(0);
+ return z.addAll(x);
+ }
}
diff --git a/resources/insertGenericsJav/TestTwoArgs.jav b/resources/insertGenericsJav/TestTwoArgs.jav
index 84631151..b4754fe4 100644
--- a/resources/insertGenericsJav/TestTwoArgs.jav
+++ b/resources/insertGenericsJav/TestTwoArgs.jav
@@ -1,22 +1,29 @@
-public class TestTwoArgs {
+public class TestTwoArgs //
+{
+ //AS
a;
+ //AN -> AN
id(b) {
var c = b;
return c;
}
+ //T -> AS
setA(x) {
a = x;
return a;
}
-
+
+ //(AG,Z) -> AG
m(x,y) {
x = id(y);
return x;
}
+ // (AI, AH) -> AG
main(x,y) {
return m(id(x),setA(y));
}
-}
\ No newline at end of file
+}
+
diff --git a/src/main/java/de/dhbwstuttgart/bytecode/AStatement.java b/src/main/java/de/dhbwstuttgart/bytecode/AStatement.java
deleted file mode 100644
index 5c28bf43..00000000
--- a/src/main/java/de/dhbwstuttgart/bytecode/AStatement.java
+++ /dev/null
@@ -1,30 +0,0 @@
-package de.dhbwstuttgart.bytecode;
-
-import org.objectweb.asm.Label;
-import org.objectweb.asm.MethodVisitor;
-import org.objectweb.asm.Opcodes;
-
-import de.dhbwstuttgart.syntaxtree.statement.BinaryExpr;
-import de.dhbwstuttgart.syntaxtree.statement.Expression;
-
-public abstract class AStatement implements IStatement {
- protected Expression expr;
-
- public AStatement(Expression expr) {
- this.expr = expr;
- }
-
- @Override
- public boolean isExprBinary() {
- return (expr instanceof BinaryExpr);
- }
-
- @Override
- public void genBCForRelOp(MethodVisitor mv,Label branchLabel, Label endLabel, BytecodeGenMethod bytecodeGenMethod) {
- mv.visitInsn(Opcodes.ICONST_1);
- mv.visitJumpInsn(Opcodes.GOTO, endLabel);
- mv.visitLabel(branchLabel);
- mv.visitInsn(Opcodes.ICONST_0);
- mv.visitLabel(endLabel);
- }
-}
diff --git a/src/main/java/de/dhbwstuttgart/bytecode/ArgumentExpr.java b/src/main/java/de/dhbwstuttgart/bytecode/ArgumentExpr.java
deleted file mode 100644
index b396e8d1..00000000
--- a/src/main/java/de/dhbwstuttgart/bytecode/ArgumentExpr.java
+++ /dev/null
@@ -1,11 +0,0 @@
-package de.dhbwstuttgart.bytecode;
-
-import de.dhbwstuttgart.syntaxtree.statement.Expression;
-
-public class ArgumentExpr extends AStatement {
-
- public ArgumentExpr(Expression expr) {
- super(expr);
- }
-
-}
diff --git a/src/main/java/de/dhbwstuttgart/bytecode/ArgumentVisitor.java b/src/main/java/de/dhbwstuttgart/bytecode/ArgumentVisitor.java
deleted file mode 100644
index 268e53b5..00000000
--- a/src/main/java/de/dhbwstuttgart/bytecode/ArgumentVisitor.java
+++ /dev/null
@@ -1,258 +0,0 @@
-package de.dhbwstuttgart.bytecode;
-
-import java.util.List;
-
-import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal;
-import de.dhbwstuttgart.syntaxtree.StatementVisitor;
-import de.dhbwstuttgart.syntaxtree.statement.ArgumentList;
-import de.dhbwstuttgart.syntaxtree.statement.Assign;
-import de.dhbwstuttgart.syntaxtree.statement.AssignToField;
-import de.dhbwstuttgart.syntaxtree.statement.BinaryExpr;
-import de.dhbwstuttgart.syntaxtree.statement.Block;
-import de.dhbwstuttgart.syntaxtree.statement.CastExpr;
-import de.dhbwstuttgart.syntaxtree.statement.DoStmt;
-import de.dhbwstuttgart.syntaxtree.statement.EmptyStmt;
-import de.dhbwstuttgart.syntaxtree.statement.ExpressionReceiver;
-import de.dhbwstuttgart.syntaxtree.statement.FieldVar;
-import de.dhbwstuttgart.syntaxtree.statement.ForStmt;
-import de.dhbwstuttgart.syntaxtree.statement.IfStmt;
-import de.dhbwstuttgart.syntaxtree.statement.InstanceOf;
-import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression;
-import de.dhbwstuttgart.syntaxtree.statement.Literal;
-import de.dhbwstuttgart.syntaxtree.statement.LocalVar;
-import de.dhbwstuttgart.syntaxtree.statement.LocalVarDecl;
-import de.dhbwstuttgart.syntaxtree.statement.MethodCall;
-import de.dhbwstuttgart.syntaxtree.statement.NewArray;
-import de.dhbwstuttgart.syntaxtree.statement.NewClass;
-import de.dhbwstuttgart.syntaxtree.statement.Return;
-import de.dhbwstuttgart.syntaxtree.statement.ReturnVoid;
-import de.dhbwstuttgart.syntaxtree.statement.StaticClassName;
-import de.dhbwstuttgart.syntaxtree.statement.Super;
-import de.dhbwstuttgart.syntaxtree.statement.SuperCall;
-import de.dhbwstuttgart.syntaxtree.statement.This;
-import de.dhbwstuttgart.syntaxtree.statement.UnaryExpr;
-import de.dhbwstuttgart.syntaxtree.statement.WhileStmt;
-
-public class ArgumentVisitor implements StatementVisitor {
- private List argListMethCall;
- private BytecodeGenMethod bytecodeGenMethod;
-
- public ArgumentVisitor(List argListMethCall, BytecodeGenMethod bytecodeGenMethod) {
- this.argListMethCall = argListMethCall;
- this.bytecodeGenMethod = bytecodeGenMethod;
- }
-
- @Override
- public void visit(ArgumentList argumentList) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(LambdaExpression lambdaExpression) {
- lambdaExpression.accept(bytecodeGenMethod);
- // Zieltype des Lambas ist Funktionale Interface
- // kann nie primitiv sein => un-/boxing wird hier nicht gebraucht
- argListMethCall.remove(0);
- }
-
- @Override
- public void visit(Assign assign) {
- assign.accept(bytecodeGenMethod);
-
- if(argListMethCall.get(0))
- bytecodeGenMethod.doUnboxing(bytecodeGenMethod.getResolver().getResolvedType(assign.getType()));
- argListMethCall.remove(0);
- }
-
- @Override
- public void visit(BinaryExpr binary) {
- binary.accept(bytecodeGenMethod);
-
- if(argListMethCall.get(0)) {
- bytecodeGenMethod.doUnboxing(bytecodeGenMethod.getResolver().getResolvedType(binary.getType()));
- } else {
- bytecodeGenMethod.doBoxing(bytecodeGenMethod.getResolver().getResolvedType(binary.getType()));
- }
- argListMethCall.remove(0);
- }
-
- @Override
- public void visit(Block block) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(CastExpr castExpr) {
- castExpr.accept(bytecodeGenMethod);
-
- if(argListMethCall.get(0))
- bytecodeGenMethod.doUnboxing(bytecodeGenMethod.getResolver().getResolvedType(castExpr.getType()));
- argListMethCall.remove(0);
- }
-
- @Override
- public void visit(EmptyStmt emptyStmt) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(FieldVar fieldVar) {
- fieldVar.accept(bytecodeGenMethod);
-
- if(argListMethCall.get(0))
- bytecodeGenMethod.doUnboxing(bytecodeGenMethod.getResolver().getResolvedType(fieldVar.getType()));
- argListMethCall.remove(0);
- }
-
- @Override
- public void visit(ForStmt forStmt) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(IfStmt ifStmt) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(InstanceOf instanceOf) {
- instanceOf.accept(bytecodeGenMethod);
-
- if(argListMethCall.get(0))
- bytecodeGenMethod.doUnboxing(bytecodeGenMethod.getResolver().getResolvedType(instanceOf.getType()));
- argListMethCall.remove(0);
- }
-
- @Override
- public void visit(LocalVar localVar) {
- localVar.accept(bytecodeGenMethod);
- if(!bytecodeGenMethod.isBinaryExp) {
- if(argListMethCall.get(0))
- bytecodeGenMethod.doUnboxing(bytecodeGenMethod.getResolver().getResolvedType(localVar.getType()));
- }
- argListMethCall.remove(0);
- }
-
- @Override
- public void visit(LocalVarDecl localVarDecl) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(MethodCall methodCall) {
- methodCall.accept(bytecodeGenMethod);
-
- if(argListMethCall.get(0))
- bytecodeGenMethod.doUnboxing(bytecodeGenMethod.getResolver().getResolvedType(methodCall.getType()));
- argListMethCall.remove(0);
- }
-
- @Override
- public void visit(NewClass methodCall) {
- methodCall.accept(bytecodeGenMethod);
-
- if(argListMethCall.get(0))
- bytecodeGenMethod.doUnboxing(bytecodeGenMethod.getResolver().getResolvedType(methodCall.getType()));
- argListMethCall.remove(0);
- }
-
- @Override
- public void visit(NewArray newArray) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(Return aReturn) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(ReturnVoid aReturn) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(StaticClassName staticClassName) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(Super aSuper) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(This aThis) {
- aThis.accept(bytecodeGenMethod);
-
- if(argListMethCall.get(0))
- bytecodeGenMethod.doUnboxing(bytecodeGenMethod.getResolver().getResolvedType(aThis.getType()));
- argListMethCall.remove(0);
- }
-
- @Override
- public void visit(WhileStmt whileStmt) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(DoStmt whileStmt) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(AssignToField assignLeftSide) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(AssignToLocal assignLeftSide) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(SuperCall superCall) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(ExpressionReceiver expressionReceiver) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(UnaryExpr unaryExpr) {
- unaryExpr.accept(bytecodeGenMethod);
-
- if(argListMethCall.get(0))
- bytecodeGenMethod.doUnboxing(bytecodeGenMethod.getResolver().getResolvedType(unaryExpr.getType()));
- argListMethCall.remove(0);
- }
-
- @Override
- public void visit(Literal literal) {
- literal.accept(bytecodeGenMethod);
-
- if(argListMethCall.get(0))
- bytecodeGenMethod.doUnboxing(bytecodeGenMethod.getResolver().getResolvedType(literal.getType()));
- argListMethCall.remove(0);
- }
-
-}
diff --git a/src/main/java/de/dhbwstuttgart/bytecode/AssignStmt.java b/src/main/java/de/dhbwstuttgart/bytecode/AssignStmt.java
deleted file mode 100644
index 366c450a..00000000
--- a/src/main/java/de/dhbwstuttgart/bytecode/AssignStmt.java
+++ /dev/null
@@ -1,11 +0,0 @@
-package de.dhbwstuttgart.bytecode;
-
-import de.dhbwstuttgart.syntaxtree.statement.Expression;
-
-public class AssignStmt extends AStatement {
-
- public AssignStmt(Expression rightSide) {
- super(rightSide);
- }
-
-}
diff --git a/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGen.java b/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGen.java
deleted file mode 100644
index 04f408af..00000000
--- a/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGen.java
+++ /dev/null
@@ -1,630 +0,0 @@
-package de.dhbwstuttgart.bytecode;
-
-import java.io.File;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Optional;
-
-import de.dhbwstuttgart.parser.scope.JavaClassName;
-import org.objectweb.asm.ClassWriter;
-import org.objectweb.asm.FieldVisitor;
-import org.objectweb.asm.MethodVisitor;
-import org.objectweb.asm.Opcodes;
-import org.objectweb.asm.Type;
-
-import de.dhbwstuttgart.bytecode.Exception.BytecodeGeneratorError;
-import de.dhbwstuttgart.bytecode.descriptor.DescriptorToString;
-import de.dhbwstuttgart.bytecode.descriptor.TypeToDescriptor;
-import de.dhbwstuttgart.bytecode.signature.Signature;
-import de.dhbwstuttgart.bytecode.signature.TypeToSignature;
-import de.dhbwstuttgart.bytecode.signature.TypeToString;
-import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile;
-import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericsGeneratorResult;
-import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericsGeneratorResultForClass;
-import de.dhbwstuttgart.bytecode.utilities.MethodUtility;
-import de.dhbwstuttgart.bytecode.utilities.NormalConstructor;
-import de.dhbwstuttgart.bytecode.utilities.NormalMethod;
-import de.dhbwstuttgart.bytecode.utilities.Resolver;
-import de.dhbwstuttgart.exceptions.NotImplementedException;
-import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal;
-import de.dhbwstuttgart.syntaxtree.ASTVisitor;
-import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
-import de.dhbwstuttgart.syntaxtree.Constructor;
-import de.dhbwstuttgart.syntaxtree.Field;
-import de.dhbwstuttgart.syntaxtree.FormalParameter;
-import de.dhbwstuttgart.syntaxtree.GenericDeclarationList;
-import de.dhbwstuttgart.syntaxtree.GenericTypeVar;
-import de.dhbwstuttgart.syntaxtree.Method;
-import de.dhbwstuttgart.syntaxtree.ParameterList;
-import de.dhbwstuttgart.syntaxtree.SourceFile;
-import de.dhbwstuttgart.syntaxtree.statement.ArgumentList;
-import de.dhbwstuttgart.syntaxtree.statement.Assign;
-import de.dhbwstuttgart.syntaxtree.statement.AssignToField;
-import de.dhbwstuttgart.syntaxtree.statement.BinaryExpr;
-import de.dhbwstuttgart.syntaxtree.statement.Block;
-import de.dhbwstuttgart.syntaxtree.statement.CastExpr;
-import de.dhbwstuttgart.syntaxtree.statement.DoStmt;
-import de.dhbwstuttgart.syntaxtree.statement.EmptyStmt;
-import de.dhbwstuttgart.syntaxtree.statement.ExpressionReceiver;
-import de.dhbwstuttgart.syntaxtree.statement.FieldVar;
-import de.dhbwstuttgart.syntaxtree.statement.ForStmt;
-import de.dhbwstuttgart.syntaxtree.statement.IfStmt;
-import de.dhbwstuttgart.syntaxtree.statement.InstanceOf;
-import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression;
-import de.dhbwstuttgart.syntaxtree.statement.Literal;
-import de.dhbwstuttgart.syntaxtree.statement.LocalVar;
-import de.dhbwstuttgart.syntaxtree.statement.LocalVarDecl;
-import de.dhbwstuttgart.syntaxtree.statement.MethodCall;
-import de.dhbwstuttgart.syntaxtree.statement.NewArray;
-import de.dhbwstuttgart.syntaxtree.statement.NewClass;
-import de.dhbwstuttgart.syntaxtree.statement.Return;
-import de.dhbwstuttgart.syntaxtree.statement.ReturnVoid;
-import de.dhbwstuttgart.syntaxtree.statement.StaticClassName;
-import de.dhbwstuttgart.syntaxtree.statement.Super;
-import de.dhbwstuttgart.syntaxtree.statement.SuperCall;
-import de.dhbwstuttgart.syntaxtree.statement.This;
-import de.dhbwstuttgart.syntaxtree.statement.UnaryExpr;
-import de.dhbwstuttgart.syntaxtree.statement.WhileStmt;
-import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType;
-import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
-import de.dhbwstuttgart.syntaxtree.type.RefType;
-import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
-import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType;
-import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
-import de.dhbwstuttgart.typeinference.result.ResultSet;
-
-public class BytecodeGen implements ASTVisitor {
-
- ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
-
- String type;
-
- public static RefTypeOrTPHOrWildcardOrGeneric THISTYPE = null;
- private JavaClassName className;
- private String pkgName;
- private boolean isInterface;
- private Collection listOfResultSets;
- private ResultSet resultSet;
- private SourceFile sf;
- private File path;
-
- private Optional fieldInitializations;
-
- private int indexOfFirstParam = 0;
-
- private String superClass;
-
- private List tphsClass;
-
- // stores parameter, local vars and the next index on the local variable table,
- // which use for aload_i, astore_i,...
- HashMap paramsAndLocals = new HashMap<>();
- // stores generics and their bounds of class
- HashMap genericsAndBounds = new HashMap<>();
-
- private int constructorPos = 0;
-
- HashMap methodParamsAndTypes = new HashMap<>();
- byte[] bytecode;
- HashMap classFiles;
-
- private final ArrayList methodNameAndParamsT = new ArrayList<>();
- private final ArrayList fieldNameAndParamsT = new ArrayList<>();
-
- private final ArrayList fieldNameSignature = new ArrayList<>();
-
- private List simplifyResultsForAllSourceFiles;
- private GenericsGeneratorResultForClass generatedGenerics;
-
- private Resolver resolver;
- private final ClassLoader classLoader;
-
- public BytecodeGen(HashMap classFiles, Collection listOfResultSets, List simplifyResultsForAllSourceFiles, SourceFile sf,
- File path, ClassLoader classLoader) {
- this.classFiles = classFiles;
- this.listOfResultSets = listOfResultSets;
- this.simplifyResultsForAllSourceFiles = simplifyResultsForAllSourceFiles;
- this.sf = sf;
- this.path = path;
- this.pkgName = sf.getPkgName();
- this.classLoader = classLoader;
- }
-
- @Override
- public void visit(SourceFile sourceFile) {
- for (ClassOrInterface cl : sourceFile.getClasses()) {
- System.out.println("in Class: " + cl.getClassName().toString());
- BytecodeGen classGen = new BytecodeGen(classFiles, listOfResultSets, simplifyResultsForAllSourceFiles, sf, path, classLoader);
- cl.accept(classGen);
- classGen.writeClass(cl.getClassName());
- }
- }
-
- /**
- * Associates the bytecode of the class that was build with the classWriter
- * {@link #cw} with the class name in the map {@link #classFiles}
- *
- * @param name name of the class with which the bytecode is to be associated
- */
- private void writeClass(JavaClassName name) {
- bytecode = cw.toByteArray();
- classFiles.put(name, bytecode);
-
- }
-
- public HashMap getClassFiles() {
- return classFiles;
- }
-
- @Override
- public void visit(ClassOrInterface classOrInterface) {
-
- className = classOrInterface.getClassName();
-
- cw.visitSource(className.getClassName() + ".jav", null);
-
- isInterface = (classOrInterface.getModifiers() & 512) == 512;
-
- int acc = isInterface ? classOrInterface.getModifiers() + Opcodes.ACC_ABSTRACT
- : classOrInterface.getModifiers() + Opcodes.ACC_SUPER;
-
- fieldInitializations = classOrInterface.getfieldInitializations();
-
-// resultSet = listOfResultSets.get(0);
- boolean isVisited = false;
- List listOfResultSetsList = new ArrayList<>(listOfResultSets);
- generatedGenerics = simplifyResultsForAllSourceFiles.stream().map(sr->sr.getSimplifyResultsByName(className)).findFirst().get();
- for (int i = 0; i < listOfResultSetsList.size(); i++) {
- //for (ResultSet rs : listOfResultSets) {
- superClass = classOrInterface.getSuperClass().acceptTV(new TypeToDescriptor());
- resultSet = listOfResultSetsList.get(i);
- resolver = new Resolver(resultSet);
-// tphExtractor.setResultSet(resultSet);
-
-
- // Nur einmal ausführen!!
- if (!isVisited) {
-
- String sig = null;
- /*
- * if class has generics then creates signature Signature looks like:
- * Superclass
- */
- if (classOrInterface.getGenerics().iterator().hasNext() || classOrInterface.getSuperClass().acceptTV(new TypeToSignature()).contains("<")
- || !generatedGenerics.getClassConstraints().isEmpty()) {
-
- List consClass = generatedGenerics.getClassConstraints();
-//
- Signature signature = new Signature(classOrInterface, genericsAndBounds, consClass);
- sig = signature.createSignatureForClassOrInterface();
- System.out.println("Signature: => " + sig);
- }
-
- cw.visit(Opcodes.V1_8, acc, classOrInterface.getClassName().toString().replace(".", "/"), sig,
- classOrInterface.getSuperClass().acceptTV(new TypeToDescriptor()), null);
-
- isVisited = true;
- }
-
- for (Field f : classOrInterface.getFieldDecl()) {
- f.accept(this);
- }
-
- for (Constructor c : classOrInterface.getConstructors()) {
- c.accept(this);
- }
-
- for (Method m : classOrInterface.getMethods()) {
- m.accept(this);
- }
-
- }
-
- }
-
- @Override
- public void visit(Constructor field) {
- // stores generics and their bounds of method
- HashMap genericsAndBoundsMethod = new HashMap<>();
-
- field.getParameterList().accept(this);
-
- String id = MethodUtility.createID(resolver, field);
-
- if (methodNameAndParamsT.contains(id)) {
- return;
- }
- methodNameAndParamsT.add(id);
- System.out.println("Constructor: " + field.name + " , paramsType: " + id);
-
- String desc = null;
- boolean hasGen = false;
-
- for (String paramName : methodParamsAndTypes.keySet()) {
- String typeOfParam = methodParamsAndTypes.get(paramName).acceptTV(new TypeToSignature());
- System.out.println(typeOfParam);
- if (genericsAndBounds.containsKey(typeOfParam) || typeOfParam.contains("$") || typeOfParam.contains("<")) {
- hasGen = true;
- break;
- }
- }
- String sig = null;
- if (hasGen) {
- List constraints = generatedGenerics.getClassConstraints();
- Signature signature = new Signature(genericsAndBounds,
- methodParamsAndTypes, resultSet, constraints);
-
- sig = signature.createSignatureForConstructor(field);
- }
- if (field.getParameterList().iterator().hasNext())
- System.out.println(field.getParameterList().iterator().next().getType().acceptTV(new TypeToDescriptor()));
-
- NormalConstructor constructor = new NormalConstructor(field, genericsAndBounds, hasGen);
- desc = constructor.accept(new DescriptorToString(resultSet));
- System.out.println("Constructor: " + field.getName() + " Sig: " + sig + " Desc: " + desc);
- MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "", desc, sig, null);
- mv.visitCode();
-
- Block block = fieldInitializations.get().block;
-
- constructorPos += 1;
-
- BytecodeGenMethod gen = new BytecodeGenMethod(className, superClass, resultSet, field, mv, paramsAndLocals, cw,
- genericsAndBoundsMethod, genericsAndBounds, isInterface, classFiles, sf, path, block, constructorPos, classLoader);
- if (!field.getParameterList().iterator().hasNext()
- && !(field.block.statements.get(field.block.statements.size() - 1) instanceof ReturnVoid)) {
- mv.visitInsn(Opcodes.RETURN);
- }
- mv.visitMaxs(0, 0);
- mv.visitEnd();
- }
-
- @Override
- public void visit(Method method) {
- // TODO: check if the method is static => if static then the first param will be
- // stored in pos 0
- // else it will be stored in pos 1 and this will be stored in pos 0
- String retType = resultSet.resolveType(method.getReturnType()).resolvedType.acceptTV(new TypeToDescriptor());
-
- String id = MethodUtility.createID(resolver, method);
-
- if (methodNameAndParamsT.contains(id)) {
- return;
- }
- methodNameAndParamsT.add(id);
- System.out.println("Method: " + method.name + " , paramsType: " + id);
- // stores generics and their bounds of method
- HashMap genericsAndBoundsMethod = new HashMap<>();
-
- method.getParameterList().accept(this);
-
- String methDesc = null;
-
- // Method getModifiers() ?
- int acc = isInterface ? Opcodes.ACC_ABSTRACT : method.modifier;
- System.out.println(acc);
-
- /* Prüfe, ob die Rückgabe-Type der Methode eine Type-Variable ist */
- boolean hasGenInParameterList = genericsAndBounds.containsKey(retType) || retType.contains("TPH ")
- || resultSet.resolveType(method.getReturnType()).resolvedType.acceptTV(new TypeToSignature())
- .contains("<");
- /*
- * Wenn die Rückgabe-Type eine Typ-variable ist, erzeuge direkt die Signature,
- * wenn nicht, prüfe, ob einer der Parameter Typ-Variable als Typ hat
- */
- if (!hasGenInParameterList) {
- for (String paramName : methodParamsAndTypes.keySet()) {
- String typeOfParam = methodParamsAndTypes.get(paramName).acceptTV(new TypeToDescriptor());
- String sigOfParam = methodParamsAndTypes.get(paramName).acceptTV(new TypeToSignature());
- if (genericsAndBounds.containsKey(typeOfParam) || typeOfParam.contains("TPH ")
- || sigOfParam.contains("<")) {
- hasGenInParameterList = true;
- break;
- }
- }
- }
- // TODO: Test if the return-type or any of the parameter is a parameterized
- // type. (VP)
- // then create the descriptor with the new syntax.
-
- String sig = null;
- /*
- * method.getGenerics: <....> RT method(..)
- */
- boolean hasGen = method.getGenerics().iterator().hasNext() || hasGenInParameterList;
- /* if method has generics or return type is TPH, create signature */
- // zwite operand muss weggelassen werden
- if (hasGen || resultSet.resolveType(method.getReturnType()).resolvedType.acceptTV(new TypeToString())
- .equals("TPH")) {
-
- List constraints = generatedGenerics.getMethodConstraintsByID(id);
- List classConstraints = generatedGenerics.getClassConstraints();
- Signature signature = new Signature(genericsAndBoundsMethod, genericsAndBounds,
- methodParamsAndTypes, resultSet, constraints,classConstraints);
- sig = signature.createSignatureForMethod(method);
- }
- System.out.println(method.getName() + " ==> " + sig);
- NormalMethod meth = new NormalMethod(method, genericsAndBounds, genericsAndBoundsMethod, hasGen);
- methDesc = meth.accept(new DescriptorToString(resultSet));
-
- MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC + acc, method.getName(), methDesc, sig, null);
-
- mv.visitCode();
- BytecodeGenMethod gen = new BytecodeGenMethod(className, superClass, resultSet, method, mv, paramsAndLocals, cw,
- genericsAndBoundsMethod, genericsAndBounds, isInterface, classFiles, sf, path, classLoader);
-
- mv.visitMaxs(0, 0);
- mv.visitEnd();
- }
-
- @Override
- public void visit(ParameterList formalParameters) {
- paramsAndLocals = new HashMap<>();
- methodParamsAndTypes = new HashMap<>();
- Iterator itr = formalParameters.iterator();
- int i = 1;
- while (itr.hasNext()) {
- FormalParameter fp = itr.next();
- paramsAndLocals.put(fp.getName(), i);
- methodParamsAndTypes.put(fp.getName(), resultSet.resolveType(fp.getType()).resolvedType);
- fp.accept(this);
- i++;
- }
- }
-
- @Override
- public void visit(FormalParameter formalParameter) {
- formalParameter.getType().accept(this);
- }
-
- @Override
- public void visit(RefType refType) {
- type = "L" + refType.toString() + ";";
- }
-
- @Override
- public void visit(SuperWildcardType superWildcardType) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(TypePlaceholder typePlaceholder) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(ExtendsWildcardType extendsWildcardType) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(GenericRefType genericRefType) {
- // TODO Auto-generated method stub
-
- }
-
- // ??
- @Override
- public void visit(FieldVar fieldVar) {
- System.out.println("In FieldVar ---");
-// cw.newField(fieldVar.receiver.toString(), fieldVar.fieldVarName.toString(), fieldVar.getType().toString());
- FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE, fieldVar.fieldVarName, "L" + fieldVar.getType() + ";",
- null, null);
- fv.visitEnd();
- }
-
- @Override
- public void visit(Field field) {
- System.out.println("In Field ---");
- String des = "L";
- if (resultSet.resolveType(field.getType()).resolvedType instanceof TypePlaceholder) {
- des += Type.getInternalName(Object.class);
- } else {
- des += resultSet.resolveType(field.getType()).resolvedType.acceptTV(new TypeToDescriptor());
- }
- des += ";";
- System.out.println(des);
- String sig = resultSet.resolveType(field.getType()).resolvedType.acceptTV(new TypeToSignature(generatedGenerics.getClassConstraints()));
- System.out.println(sig);
- if (sig.charAt(sig.length() - 1) != (";").charAt(0)) {
- sig += ";";
- }
- String nameAndDesc = field.getName() + "%%" + des;
- String nameAndSig = field.getName() + "%%" + sig;
- if (fieldNameAndParamsT.contains(nameAndDesc)) {
- if (fieldNameSignature.contains(nameAndSig)) {
- return;
- }
- throw new BytecodeGeneratorError("Bytecode generation aborted due to duplicate field name&signature");
- }
- fieldNameAndParamsT.add(nameAndDesc);
- fieldNameSignature.add(nameAndSig);
-
- cw.visitField(field.modifier, field.getName(), des, sig, null);
- }
-
- @Override
- public void visit(LambdaExpression lambdaExpression) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(Assign assign) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(BinaryExpr binary) {
-
- }
-
- @Override
- public void visit(Block block) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(CastExpr castExpr) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(EmptyStmt emptyStmt) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(ForStmt forStmt) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(IfStmt ifStmt) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(InstanceOf instanceOf) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(LocalVar localVar) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(LocalVarDecl localVarDecl) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(MethodCall methodCall) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(NewClass methodCall) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(NewArray newArray) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(Return aReturn) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(ReturnVoid aReturn) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(StaticClassName staticClassName) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(Super aSuper) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(This aThis) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(WhileStmt whileStmt) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(DoStmt whileStmt) {
- // TODO Auto-generated method stub
-
- }
-
- // ???
- @Override
- public void visit(Literal literal) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(ArgumentList argumentList) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(GenericTypeVar genericTypeVar) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(GenericDeclarationList genericTypeVars) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(AssignToField assignLeftSide) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(AssignToLocal assignLeftSide) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(SuperCall superCall) {
-
- }
-
- @Override
- public void visit(ExpressionReceiver expressionReceiver) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(UnaryExpr unaryExpr) {
- throw new NotImplementedException();
- }
-
-}
diff --git a/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java b/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java
deleted file mode 100644
index 5cbcfca9..00000000
--- a/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java
+++ /dev/null
@@ -1,1527 +0,0 @@
-package de.dhbwstuttgart.bytecode;
-
-import java.io.File;
-import java.lang.invoke.CallSite;
-import java.lang.invoke.MethodHandle;
-import java.lang.invoke.MethodHandles;
-import java.lang.invoke.MethodType;
-import java.lang.reflect.Parameter;
-import java.net.URL;
-import java.net.URLClassLoader;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.stream.Collectors;
-
-import de.dhbwstuttgart.bytecode.funN.FunNGenerator;
-import de.dhbwstuttgart.bytecode.funN.FunNUtilities;
-import de.dhbwstuttgart.bytecode.utilities.*;
-import de.dhbwstuttgart.environment.DirectoryClassLoader;
-import de.dhbwstuttgart.exceptions.NotImplementedException;
-import de.dhbwstuttgart.parser.scope.JavaClassName;
-import de.dhbwstuttgart.syntaxtree.statement.*;
-import de.dhbwstuttgart.syntaxtree.statement.BinaryExpr.Operator;
-import de.dhbwstuttgart.syntaxtree.statement.UnaryExpr.Operation;
-
-import org.objectweb.asm.ClassWriter;
-import org.objectweb.asm.Handle;
-import org.objectweb.asm.Label;
-import org.objectweb.asm.MethodVisitor;
-import org.objectweb.asm.Opcodes;
-import org.objectweb.asm.Type;
-
-import de.dhbwstuttgart.bytecode.Exception.NotInCurrentPackageException;
-import de.dhbwstuttgart.bytecode.descriptor.DescriptorToString;
-import de.dhbwstuttgart.bytecode.descriptor.TypeToDescriptor;
-import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal;
-import de.dhbwstuttgart.syntaxtree.FormalParameter;
-import de.dhbwstuttgart.syntaxtree.Method;
-import de.dhbwstuttgart.syntaxtree.SourceFile;
-import de.dhbwstuttgart.syntaxtree.StatementVisitor;
-import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
-import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
-import de.dhbwstuttgart.typeinference.result.ResultSet;
-import javassist.NotFoundException;
-
-public class BytecodeGenMethod implements StatementVisitor {
-
- private Resolver resolver;
- private Method m;
- private MethodVisitor mv;
- private HashMap paramsAndLocals = new HashMap<>();
- private JavaClassName className;
- private int lamCounter;
- private ClassWriter cw;
- private ResultSet resultSet;
- private boolean isInterface;
- private HashMap genericsAndBoundsMethod;
- private HashMap genericsAndBounds;
- public boolean isBinaryExp = false;
- private String superClass;
- private File path;
- private SourceFile sf;
- private IStatement statement = null;
- private boolean isReturnStmt = false;
- private boolean isParentBinary = false;
-
- private boolean needDUP = false;
-
- private Block blockFieldInit = null;
- private boolean isBlockFieldInitVisited = false;
- // for tests **
- private String fieldName;
- private String fieldDesc;
- private Expression rightSideTemp;
-
- private boolean isRightSideALambda = false;
- private KindOfLambda kindOfLambda;
- private HashMap classFiles;
-
- private int constructorPos = 0;
-
- private ArrayList varsFunInterface = new ArrayList<>();;
- private final ClassLoader classLoader;
-
- // generate bytecode for constructor
- public BytecodeGenMethod(JavaClassName className, String superClass, ResultSet resultSet, Method m, MethodVisitor mv,
- HashMap paramsAndLocals, ClassWriter cw, HashMap genericsAndBoundsMethod,
- HashMap genericsAndBounds, boolean isInterface, HashMap classFiles,
- SourceFile sf, File path, Block block, int constructorPos, ClassLoader classLoader) {
-
- this.className = className;
- this.superClass = superClass;
- this.resultSet = resultSet;
- this.resolver = new Resolver(resultSet);
- this.m = m;
- this.mv = mv;
- this.paramsAndLocals = paramsAndLocals;
- this.cw = cw;
- this.genericsAndBoundsMethod = genericsAndBoundsMethod;
- this.genericsAndBounds = genericsAndBounds;
- this.isInterface = isInterface;
- this.classFiles = classFiles;
- this.sf = sf;
- this.path = path;
- this.lamCounter = -1;
- this.constructorPos = constructorPos;
- this.classLoader = classLoader;
- if(block != null)
- this.blockFieldInit = block;
- this.m.block.accept(this);
- }
-
- public BytecodeGenMethod(JavaClassName className, String superClass,ResultSet resultSet, Method m, MethodVisitor mv,
- HashMap paramsAndLocals, ClassWriter cw, HashMap genericsAndBoundsMethod,
- HashMap genericsAndBounds, boolean isInterface, HashMap classFiles, SourceFile sf,File path, ClassLoader classLoader) {
-
- this.className = className;
- this.superClass = superClass;
- this.resultSet = resultSet;
- this.resolver = new Resolver(resultSet);
- this.m = m;
- this.mv = mv;
- this.paramsAndLocals = paramsAndLocals;
- this.cw = cw;
- this.genericsAndBoundsMethod = genericsAndBoundsMethod;
- this.genericsAndBounds = genericsAndBounds;
- this.isInterface = isInterface;
- this.classFiles = classFiles;
- this.sf = sf;
- this.path = path;
- this.lamCounter = -1;
- this.classLoader = classLoader;
- if (!isInterface)
- this.m.block.accept(this);
- }
-
- public BytecodeGenMethod(JavaClassName className, ClassWriter cw, LambdaExpression lambdaExpression, ArrayList usedVars, ResultSet resultSet, MethodVisitor mv,
- int indexOfFirstParamLam, boolean isInterface, HashMap classFiles, File path, int lamCounter, SourceFile sf,HashMap genericsAndBoundsMethod,
- HashMap genericsAndBounds, ClassLoader classLoader) {
- this.className = className;
- this.cw = cw;
- this.resultSet = resultSet;
- this.resolver = new Resolver(resultSet);
- this.mv = mv;
- this.isInterface = isInterface;
- this.classFiles = classFiles;
- this.path = path;
- this.lamCounter = lamCounter;
- this.sf = sf;
- this.genericsAndBoundsMethod = genericsAndBoundsMethod;
- this.genericsAndBounds = genericsAndBounds;
- this.classLoader = classLoader;
-
- Iterator itr = lambdaExpression.params.iterator();
- int i = indexOfFirstParamLam;
-
- for(String var : usedVars) {
- this.paramsAndLocals.put(var, i);
- i++;
- }
-
- while (itr.hasNext()) {
- FormalParameter fp = itr.next();
- this.paramsAndLocals.put(fp.getName(), i);
- i++;
- }
- lambdaExpression.methodBody.accept(this);
- }
-
- public Resolver getResolver() {
- return resolver;
- }
-
- public void isBinary(boolean isBinary) {
- this.isBinaryExp =isBinary;
- }
-
-
- @Override
- public void visit(Block block) {
- HashMap paramsAndLocalsOld = new HashMap<>(paramsAndLocals);
- for (Statement stmt : block.getStatements()) {
- stmt.accept(this);
- if(stmt instanceof MethodCall) {
- String ret = resolver.getResolvedType(((MethodCall) stmt).getType());
- if(!ret.equals(CONSTANTS.VOID))
- mv.visitInsn(Opcodes.POP);
- }
- }
- paramsAndLocals = paramsAndLocalsOld;
- }
-
- @Override
- public void visit(SuperCall superCall) {
- superCall.receiver.accept(this);
- superCall.arglist.accept(this);
- mv.visitMethodInsn(Opcodes.INVOKESPECIAL, this.superClass, superCall.name, "()V",
- isInterface);
-
- if(blockFieldInit!=null && !isBlockFieldInitVisited) {
- isBlockFieldInitVisited =true;
- //blockFieldInit.accept(this);
- for(Statement stmt : blockFieldInit.statements) {
- if(stmt instanceof SuperCall)
- continue;
-
- stmt.accept(this);
- }
- }
- }
-
- // ??
- @Override
- public void visit(LocalVar localVar) {
- // wenn String + String zuerst wird ein StringBuilder initialisiert dann
- // wird die lokale Var geladen. Sonst wird zuerst die lokale Var geladen.
- System.out.println(localVar.name);
- mv.visitVarInsn(Opcodes.ALOAD, paramsAndLocals.get(localVar.name));
-
- if (isBinaryExp) {
- doUnboxing(resolver.getResolvedType(localVar.getType()));
- }
- }
-
- // ??
- @Override
- public void visit(LocalVarDecl localVarDecl) {
- paramsAndLocals.put(localVarDecl.getName(), paramsAndLocals.size()+1);
- }
-
- @Override
- public void visit(Assign assign) {
- statement = new AssignStmt(assign.rightSide);
- // if the right side is a lambda => the left side must be a functional interface
- if (assign.rightSide instanceof LambdaExpression) {
- isRightSideALambda = true;
- } else {
- isRightSideALambda = false;
- }
-
- isBinaryExp = statement.isExprBinary();
-
- if (assign.lefSide instanceof AssignToField) {
- // load_0, ldc or .. then putfield
- this.rightSideTemp = assign.rightSide;
- } else {
- assign.rightSide.accept(this);
- }
-
- statement = new AssignStmt(assign.rightSide);
- isBinaryExp = statement.isExprBinary();
-
- if (isBinaryExp) {
- BinaryExpr binary = (BinaryExpr) assign.rightSide;
- String binaryType = resolver.getResolvedType(binary.getType());
- doBoxing(binaryType);
- isBinaryExp = false;
- }
-
- System.out.println("ASSIGN TYPE R: " + resolver.getResolvedType(assign.rightSide.getType()));
- String typeOfRightSide = resolver.getResolvedType(assign.rightSide.getType());
- if(typeOfRightSide.contains(CONSTANTS.ANGLEBRACKET)) {
- mv.visitTypeInsn(Opcodes.CHECKCAST, typeOfRightSide.substring(0, typeOfRightSide.indexOf('<')));
- }
- assign.lefSide.accept(this);
-
- statement = null;
- }
-
- @Override
- public void visit(BinaryExpr binary) {
- isParentBinary = true;
- String lexpType = resolver.getResolvedType(binary.lexpr.getType());
- String rexpType = resolver.getResolvedType(binary.rexpr.getType());
-
- String largerType = getLargerType(lexpType, rexpType);
- String typeOfBinary = resolver.getResolvedType(binary.getType());
-
- if (typeOfBinary.equals(Type.getInternalName(String.class))) {
- mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(StringBuilder.class));
- mv.visitInsn(Opcodes.DUP);
- mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(StringBuilder.class), "", "()V",
- false);
- }
-
- Label endLabel = new Label();
- // this case for while loops and If statements
- if (statement instanceof LoopStmt)
- mv.visitLabel(endLabel);
-
- if(binary.lexpr instanceof UnaryExpr)
- needDUP = true;
-
- binary.lexpr.accept(this);
-
- if(lexpType.equals(Type.getInternalName(String.class))) {
- mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(StringBuilder.class), "append",
- "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
- }
-
- if (!lexpType.equals(rexpType) && !lexpType.equals(largerType))
- doCast(lexpType, largerType);
-
- if(binary.rexpr instanceof UnaryExpr)
- needDUP = true;
-
- binary.rexpr.accept(this);
- if(rexpType.equals(Type.getInternalName(String.class))) {
- mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(StringBuilder.class), "append",
- "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
- }
- isParentBinary = false;
-
- if (!lexpType.equals(rexpType) && !rexpType.equals(largerType))
- doCast(rexpType, largerType);
-
- Operator op = binary.operation;
-
- switch (op) {
- case ADD:
- doVisitAddOpInsn(largerType);
- break;
-
- case SUB:
- doVisitSubOpInsn(typeOfBinary);
- break;
-
- case MUL:
- doVisitMulOpInsn(typeOfBinary);
- break;
-
- case DIV:
- doVisitDivOpInsn(typeOfBinary);
- break;
-
- case MOD:
- doVisitModOpInsn(typeOfBinary);
- break;
-
- case LESSTHAN:
- case LESSEQUAL:
- case BIGGERTHAN:
- case BIGGEREQUAL:
- case EQUAL:
- case NOTEQUAL:
- Label branchLabel = new Label();
- doVisitRelOpInsn(op, largerType, branchLabel, endLabel);
- break;
-
- default:
- break;
- }
- }
-
- private String getLargerType(String lexpType, String rexpType) {
- if (lexpType.equals(Type.getInternalName(String.class))
- || rexpType.equals(Type.getInternalName(String.class))) {
- return Type.getInternalName(String.class);
- } else if (lexpType.equals(Type.getInternalName(Double.class))
- || rexpType.equals(Type.getInternalName(Double.class))) {
- return Type.getInternalName(Double.class);
- } else if (lexpType.equals(Type.getInternalName(Float.class))
- || rexpType.equals(Type.getInternalName(Float.class))) {
- return Type.getInternalName(Float.class);
- } else if (lexpType.equals(Type.getInternalName(Long.class))
- || rexpType.equals(Type.getInternalName(Long.class))) {
- return Type.getInternalName(Long.class);
- } else {
- return Type.getInternalName(Integer.class);
- }
- }
-
- private void doCast(String sourceType, String dest) {
- switch (dest) {
- case CONSTANTS.REFTYPE_LONG:
- mv.visitInsn(Opcodes.I2L);
- break;
-
- case CONSTANTS.REFTYPE_DOUBLE:
- if (sourceType.equals(Type.getInternalName(Long.class))) {
- mv.visitInsn(Opcodes.L2D);
- } else if (sourceType.equals(Type.getInternalName(Float.class))) {
- mv.visitInsn(Opcodes.F2D);
- } else {
- mv.visitInsn(Opcodes.I2D);
- }
- break;
-
- case CONSTANTS.REFTYPE_FLOAT:
- if (sourceType.equals(Type.getInternalName(Long.class))) {
- mv.visitInsn(Opcodes.L2F);
- } else {
- mv.visitInsn(Opcodes.I2F);
- }
- break;
- // braucht man eigentlich nicht, muss getestet werden
- case CONSTANTS.REFTYPE_STRING:
- if (sourceType.equals(Type.getInternalName(Double.class))) {
- mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(String.class), "valueOf",
- "(D)Ljava/lang/String;", false);
- } else if (sourceType.equals(Type.getInternalName(Long.class))) {
- mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(String.class), "valueOf",
- "(J)Ljava/lang/String;", false);
- } else if (sourceType.equals(Type.getInternalName(Float.class))) {
- mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(String.class), "valueOf",
- "(F)Ljava/lang/String;", false);
- } else {
- mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(String.class), "valueOf",
- "(I)Ljava/lang/String;", false);
- }
- break;
- default:
- break;
- }
- }
-
- private void doVisitRelOpInsn(Operator op, String typeOfBinary, Label branchLabel, Label endLabel) {
- System.out.println("TypeOfBinary: " + typeOfBinary);
- switch (typeOfBinary) {
- case CONSTANTS.REFTYPE_LONG:
- mv.visitInsn(Opcodes.LCMP);
- doVisitIfInRelOp(op, branchLabel, endLabel);
- break;
- case CONSTANTS.REFTYPE_DOUBLE:
- mv.visitInsn(Opcodes.DCMPG);
- doVisitIfInRelOp(op, branchLabel, endLabel);
- break;
- case CONSTANTS.REFTYPE_FLOAT:
- mv.visitInsn(Opcodes.FCMPG);
- doVisitIfInRelOp(op, branchLabel, endLabel);
- break;
- default:
- switch (op) {
- case LESSTHAN:
- mv.visitJumpInsn(Opcodes.IF_ICMPGE, branchLabel);
- break;
- case LESSEQUAL:
- mv.visitJumpInsn(Opcodes.IF_ICMPGT, branchLabel);
- break;
- case BIGGERTHAN:
- mv.visitJumpInsn(Opcodes.IF_ICMPLE, branchLabel);
- break;
- case BIGGEREQUAL:
- mv.visitJumpInsn(Opcodes.IF_ICMPLT, branchLabel);
- break;
- case EQUAL:
- mv.visitJumpInsn(Opcodes.IF_ICMPNE, branchLabel);
- break;
- case NOTEQUAL:
- mv.visitJumpInsn(Opcodes.IFEQ, branchLabel);
- break;
- default:
- break;
- }
-
- statement.genBCForRelOp(mv, branchLabel, endLabel, this);
-
- break;
- }
- }
-
- private void doVisitIfInRelOp(Operator op, Label branchLabel, Label endLabel) {
-
- switch (op) {
- case LESSTHAN:
- mv.visitJumpInsn(Opcodes.IFGE, branchLabel);
- break;
- case LESSEQUAL:
- mv.visitJumpInsn(Opcodes.IFGT, branchLabel);
- break;
- case BIGGERTHAN:
- mv.visitJumpInsn(Opcodes.IFLE, branchLabel);
- break;
- case BIGGEREQUAL:
- mv.visitJumpInsn(Opcodes.IFLT, branchLabel);
- break;
- case EQUAL:
- mv.visitJumpInsn(Opcodes.IFNE, branchLabel);
- break;
- case NOTEQUAL:
- mv.visitJumpInsn(Opcodes.IFEQ, branchLabel);
- break;
- default:
- break;
- }
-
- statement.genBCForRelOp(mv, branchLabel, endLabel, this);
- }
-
- private void doVisitModOpInsn(String typeOfBinary) {
- switch (typeOfBinary) {
- case CONSTANTS.REFTYPE_LONG:
- mv.visitInsn(Opcodes.LREM);
- break;
- case CONSTANTS.REFTYPE_DOUBLE:
- mv.visitInsn(Opcodes.DREM);
- break;
- case CONSTANTS.REFTYPE_FLOAT:
- mv.visitInsn(Opcodes.FREM);
- break;
- default:
- mv.visitInsn(Opcodes.IREM);
- break;
- }
- }
-
- private void doVisitDivOpInsn(String typeOfBinary) {
- switch (typeOfBinary) {
- case CONSTANTS.REFTYPE_LONG:
- mv.visitInsn(Opcodes.LDIV);
- break;
- case CONSTANTS.REFTYPE_DOUBLE:
- mv.visitInsn(Opcodes.DDIV);
- break;
- case CONSTANTS.REFTYPE_FLOAT:
- mv.visitInsn(Opcodes.FDIV);
- break;
- default:
- mv.visitInsn(Opcodes.IDIV);
- break;
- }
- }
-
- private void doVisitMulOpInsn(String typeOfBinary) {
- switch (typeOfBinary) {
- case CONSTANTS.REFTYPE_LONG:
- mv.visitInsn(Opcodes.LMUL);
- break;
- case CONSTANTS.REFTYPE_DOUBLE:
- mv.visitInsn(Opcodes.DMUL);
- break;
- case CONSTANTS.REFTYPE_FLOAT:
- mv.visitInsn(Opcodes.FMUL);
- break;
- default:
- mv.visitInsn(Opcodes.IMUL);
- break;
- }
- }
-
- private void doVisitSubOpInsn(String typeOfBinary) {
- switch (typeOfBinary) {
- case CONSTANTS.REFTYPE_LONG:
- mv.visitInsn(Opcodes.LSUB);
- break;
- case CONSTANTS.REFTYPE_DOUBLE:
- mv.visitInsn(Opcodes.DSUB);
- break;
- case CONSTANTS.REFTYPE_FLOAT:
- mv.visitInsn(Opcodes.FSUB);
- break;
- default:
- mv.visitInsn(Opcodes.ISUB);
- break;
- }
-
- }
-
- private void doVisitAddOpInsn(String typeOfBinary) {
- switch (typeOfBinary) {
- case CONSTANTS.REFTYPE_BYTE:
- mv.visitInsn(Opcodes.IADD);
- break;
- case CONSTANTS.REFTYPE_SHORT:
- mv.visitInsn(Opcodes.IADD);
- break;
- case CONSTANTS.REFTYPE_INTEGER:
- mv.visitInsn(Opcodes.IADD);
- break;
- case CONSTANTS.REFTYPE_LONG:
- mv.visitInsn(Opcodes.LADD);
- break;
- case CONSTANTS.REFTYPE_DOUBLE:
- mv.visitInsn(Opcodes.DADD);
- break;
- case CONSTANTS.REFTYPE_FLOAT:
- mv.visitInsn(Opcodes.FADD);
- break;
- case CONSTANTS.REFTYPE_STRING:
- mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", CONSTANTS.TO_STRING, "()Ljava/lang/String;",
- false);
- break;
- default:
- break;
- }
- }
-
- @Override
- public void visit(LambdaExpression lambdaExpression) {
- this.lamCounter++;
-
- String typeErasure = createDescriptorWithTypeErasure(lambdaExpression);
- //ToDo Etienne: Double Check
- RefTypeOrTPHOrWildcardOrGeneric returnType = resolver.resolve(lambdaExpression.getReturnType());
- List argumentTypes = lambdaExpression
- .params
- .getFormalparalist()
- .stream()
- .map(FormalParameter::getType)
- .map(resolver::resolve)
- .collect(Collectors.toList());
- FunNUtilities funNUtilities = FunNGenerator.getInstance();
- FunNUtilities.writeClassFile(funNUtilities.getSuperClassName(argumentTypes.size()),
- funNUtilities.generateSuperBytecode(argumentTypes.size()), path);
- FunNUtilities.writeClassFile(funNUtilities.getSpecializedClassName(argumentTypes, returnType),
- funNUtilities.generateSpecializedBytecode(argumentTypes, returnType), path);
-
- Lambda lam = new Lambda(lambdaExpression);
- String lamDesc = lam.accept(new DescriptorToString(resultSet));
- // Call site, which, when invoked, returns an instance of the functional
- // interface to which
- // the lambda is being converted
- MethodType mt = MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class,
- MethodType.class, MethodType.class, MethodHandle.class, MethodType.class);
-
- Handle bootstrap = new Handle(Opcodes.H_INVOKESTATIC, "java/lang/invoke/LambdaMetafactory", "metafactory",
- mt.toMethodDescriptorString(), false);
- String desugaredMethodName = CONSTANTS.DESUGAREDMETHODNAME + this.lamCounter;
-
- // Für die Parameter-Typen und Return-Typ braucht man die Bounds (für die
- // Typlöschung)
-
- // Type erasure
- Type arg1 = Type.getMethodType(typeErasure);
- // Type arg1 = Type.getMethodType(lamDesc);
- // real Type
- Type arg3 = Type.getMethodType(lamDesc);
-
- int staticOrSpecial, staticOrInstance = 0, indexOfFirstParamLam = 0;
-
- this.kindOfLambda = new KindOfLambda(lambdaExpression);
-
- if (kindOfLambda.isInstanceCapturingLambda()) {
- mv.visitVarInsn(Opcodes.ALOAD, 0);
- loadUsedVarsInLambda();
- staticOrSpecial = Opcodes.H_INVOKESPECIAL;
- indexOfFirstParamLam = 1;
- } else {
- staticOrSpecial = Opcodes.H_INVOKESTATIC;
- staticOrInstance = Opcodes.ACC_STATIC;
- }
- String newDesc = addUsedVarsToDesugaredMethodDescriptor(lamDesc);
- // first check if capturing lambda then invokestatic or invokespecial
- Handle arg2 = new Handle(staticOrSpecial, this.className.getClassName(), desugaredMethodName, newDesc, false);
- // Descriptor of functional interface methode
- SamMethod samMethod = new SamMethod(kindOfLambda.getArgumentList(), lambdaExpression.getType());
- // Desc: (this/nothing)TargetType
- String fiMethodDesc = samMethod.accept(new DescriptorToString(resultSet));
- mv.visitInvokeDynamicInsn("apply", fiMethodDesc, bootstrap, arg1, arg2, arg3);
- if(constructorPos<2) {
- MethodVisitor mvLambdaBody = cw.visitMethod(Opcodes.ACC_PRIVATE + staticOrInstance + Opcodes.ACC_SYNTHETIC,
- desugaredMethodName, newDesc, null, null);
-
- ArrayList usedVars = kindOfLambda.getUsedVars();
-
- new BytecodeGenMethod(className, cw,lambdaExpression, usedVars,this.resultSet, mvLambdaBody, indexOfFirstParamLam, isInterface,
- classFiles,this.path, lamCounter, sf, genericsAndBoundsMethod,
- genericsAndBounds, classLoader);
-
- mvLambdaBody.visitMaxs(0, 0);
- mvLambdaBody.visitEnd();
- cw.visitInnerClass("java/lang/invoke/MethodHandles$Lookup", "java/lang/invoke/MethodHandles", "Lookup",
- Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL);
- }
-// generateBCForFunN(lambdaExpression, typeErasure);
- }
-
- private String addUsedVarsToDesugaredMethodDescriptor(String lamDesc) {
- String newDesc = "(";
- int pos = 0;
- if(kindOfLambda.isThisUsed()) {
- pos = 1;
- }
-
- for(int i=pos;i itr = lambdaExpression.params.iterator();
- while (itr.hasNext()) {
- itr.next();
- typeErasure += "L" + Type.getInternalName(Object.class) + ";";
- }
-
- typeErasure += ")L" + Type.getInternalName(Object.class) + ";";
- return typeErasure;
- }
-
-
-
- @Override
- public void visit(CastExpr castExpr) {
-
- }
-
- @Override
- public void visit(EmptyStmt emptyStmt) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(FieldVar fieldVar) {
- fieldName = fieldVar.fieldVarName;
- fieldDesc = "L";
- if(resultSet.resolveType(fieldVar.getType()).resolvedType instanceof TypePlaceholder) {
- fieldDesc += Type.getInternalName(Object.class);
- } else {
- fieldDesc += resultSet.resolveType(fieldVar.getType()).resolvedType.acceptTV(new TypeToDescriptor());
- }
- fieldDesc +=";";
-
- fieldVar.receiver.accept(this);
- // test (if)
- if (!fieldVar.receiver.getClass().equals(StaticClassName.class)) {
- mv.visitFieldInsn(Opcodes.GETFIELD, resolver.getResolvedType(fieldVar.receiver.getType()), fieldName, fieldDesc);
- }
-
- if (isBinaryExp) {
- doUnboxing(resolver.getResolvedType(fieldVar.getType()));
- }
- // mv.visitFieldInsn(Opcodes.GETSTATIC,
- // fieldVar.receiver.getType().toString().replace(".", "/"),
- // fieldVar.fieldVarName, fieldVar.getType().toString());
- }
-
- @Override
- public void visit(ForStmt forStmt) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(IfStmt ifStmt) {
- statement = new IfStatement(ifStmt.expr, ifStmt.then_block, ifStmt.else_block);
- isBinaryExp = statement.isExprBinary();
- ifStmt.expr.accept(this);
- if(!(ifStmt.expr instanceof BinaryExpr)) {
- doUnboxing(resolver.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;
- }
-
- @Override
- public void visit(InstanceOf instanceOf) {
-
- }
-
- @Override
- public void visit(MethodCall methodCall) {
- boolean parentBinary = isParentBinary;
- System.out.println("In MethodCall = " + methodCall.name);
- String receiverName = resolver.getResolvedType(methodCall.receiver.getType());
- System.out.println("Methods of " + receiverName + " ");
- java.lang.reflect.Method methodRefl = null;
- String clazz = receiverName.replace("/", ".");
-
- String mDesc = "";
-
- MethodCallHelper helper = new MethodCallHelper(methodCall, sf, resultSet, path);
-
- ClassLoader cLoader = this.classLoader;
- // This will be used if the class is not standard class (not in API)
- ClassLoader cLoader2;
-
- String methCallType = resultSet.resolveType(methodCall.getType()).resolvedType.acceptTV(new TypeToDescriptor());
- String[] typesOfParams = getTypes(methodCall.arglist.getArguments());
- try {
- clazz = getRawClassName(receiverName, clazz);
-
- java.lang.reflect.Method[] methods = cLoader.loadClass(clazz).getMethods();
- System.out.println("Methods of " + receiverName + " ");
- methodRefl = getMethod(methodCall.name, methodCall.arglist.getArguments().size(),methCallType, typesOfParams, methods);
-
- } catch (Exception e) {
- String superClass = "";
- while(true) {
- try {
- superClass = helper.getSuperClass(receiverName.replace("/", "."));
-
- try {
- String superClazz = superClass.replace("/", ".");
- superClazz = getRawClassName(superClass, superClazz);
- java.lang.reflect.Method[] methods = cLoader.loadClass(superClazz).getMethods();
- System.out.println("Methods of " + superClass + " ");
-
- methodRefl = getMethod(methodCall.name, methodCall.arglist.getArguments().size(), methCallType, typesOfParams, methods);
-
- break;
- } catch (Exception e3) {
- receiverName = superClass;
- continue;
- }
- } catch (NotInCurrentPackageException e2) {
- break;
- }
-
- }
-
- }
-
- if(methodRefl == null) {
- boolean toCreate = !receiverName.equals(className.getClassName()) && helper.isInCurrPkg(clazz);
- if(toCreate) {
- try {
- mDesc = helper.getDesc(clazz);
- } catch (NotInCurrentPackageException | NotFoundException e) {
- e.printStackTrace();
- }
- } else if(!helper.isInCurrPkg(clazz)){
- if(clazz.contains(CONSTANTS.$$)) {
- mDesc = helper.getDescriptorOfApplyMethod(methCallType);
- //ToDo Etienne: Double Check
- RefTypeOrTPHOrWildcardOrGeneric returnType = resolver.resolve(methodCall.getType());
- List argumentTypes = methodCall
- .arglist
- .getArguments()
- .stream()
- .map(TypableStatement::getType)
- .map(resolver::resolve)
- .collect(Collectors.toList());
- FunNUtilities funNUtilities = FunNGenerator.getInstance();
- FunNUtilities.writeClassFile(funNUtilities.getSuperClassName(argumentTypes.size()),
- funNUtilities.generateSuperBytecode(argumentTypes.size()), path);
- FunNUtilities.writeClassFile(funNUtilities.getSpecializedClassName(argumentTypes, returnType),
- funNUtilities.generateSpecializedBytecode(argumentTypes, returnType), path);
- }else {
- try {
- cLoader2 = new DirectoryClassLoader(path, classLoader);
- java.lang.reflect.Method[] methods = cLoader2.loadClass(clazz).getMethods();
- System.out.println("Methods of " + receiverName + " ");
- for(int i = 0; i argListMethCall = new LinkedList<>();
- String receiverRefl="";
- if(methodRefl == null && receiverName.equals(className.getClassName())) {
- MethodFromMethodCall method = new MethodFromMethodCall(methodCall.arglist, methodCall.getType(),
- receiverName, genericsAndBoundsMethod, genericsAndBounds);
- mDesc = method.accept(new DescriptorToString(resultSet));
- methodCall.arglist.accept(this);
- } else if(methodRefl != null) {
- System.out.println(methodCall.name + " -> Refl != null");
- receiverRefl = methodRefl.getAnnotatedReceiverType().getType().toString();
- getBoolListOfType(methodRefl, argListMethCall);
- System.out.println("Receiver = " + methodRefl.getAnnotatedReceiverType().getType().toString());
- mDesc = getMethodDesc(methodRefl);
- visitArgumentListOfMethodCallFromStandardAPI(methodCall, argListMethCall);
- } else {
- methodCall.arglist.accept(this);
- }
-
- System.out.println("Methodcall ("+ methodCall.name +") Desc : " + mDesc);
-
-
-// methodCall.arglist.accept(this);
- visitInvokeInsn(methodCall, receiverName, methodRefl, clazz, mDesc, receiverRefl);
-
-
- if(methodRefl != null && !methodRefl.getReturnType().isPrimitive()) {
- if(methodRefl.getReturnType().equals(Object.class)) {
- helper.createCheckCast(methodCall,mv);
- }
- /*if(isBinaryExp) {
- doUnboxing(resolver.getResolvedType(methodCall.getType()));
- }*/
- if(parentBinary || isBinaryExp) {
- doUnboxing(resolver.getResolvedType(methodCall.getType()));
- }
- } else if(receiverName.contains(CONSTANTS.$$) && !methCallType.equals(Type.getInternalName(Object.class))) {
- helper.createCheckCast(methodCall,mv);
- }
- System.out.println("ISParent Binary = "+isParentBinary +" -> " + parentBinary);
- if(methodRefl == null && (parentBinary || !isReturnStmt)) {
- if(isBinaryExp)
- doUnboxing(resolver.getResolvedType(methodCall.getType()));
- }
-
- }
-
- private void getBoolListOfType(java.lang.reflect.Method methodRefl, List argListMethCall) {
- for(Parameter p:methodRefl.getParameters()) {
- System.out.println(p.getName() + " und is Primitive = " + p.getType().isPrimitive());
- argListMethCall.add(p.getType().isPrimitive());
- }
- }
-
- private void visitArgumentListOfMethodCallFromStandardAPI(MethodCall methodCall, List argListMethCall) {
- for (Expression al : methodCall.arglist.getArguments()) {
- statement = new ArgumentExpr(al);
- ArgumentVisitor argV = new ArgumentVisitor(argListMethCall,this);
- al.accept(argV);
- statement = null;
- }
- }
-
- private void visitInvokeInsn(MethodCall methodCall, String receiverName, java.lang.reflect.Method methodRefl, String clazz, String mDesc, String receiverRefl) {
- // is methodCall.receiver functional Interface)?
- if (varsFunInterface.contains(methodCall.receiver.getType()) || (methodRefl!= null && receiverRefl.contains("interface")) ||
- receiverName.contains(CONSTANTS.$$)) {
- mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, clazz.replace(".", "/"), methodCall.name,
- mDesc, true);
- } else {
- mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, clazz.replace(".", "/"), methodCall.name,
- mDesc, isInterface);
- }
- }
-
- private String getRawClassName(String receiverName, String clazz) {
- if (receiverName.contains(CONSTANTS.ANGLEBRACKET)) {
- clazz = clazz.substring(0, receiverName.indexOf(CONSTANTS.ANGLEBRACKET));
- }
- return clazz;
- }
-
- private String[] getTypes(List arguments) {
- String[] types = new String[arguments.size()];
- for(int i = 0; i[] pTypes = m.getParameterTypes();
- for(int j = 0; j", d, isInterface);
- }
-
- private String createDescriptorForInitMethod(NewClass methodCall) {
- String d = "(";
- for (Expression e : methodCall.arglist.getArguments()) {
- String type = resolver.getResolvedType(e.getType());
- if(type.contains("TPH ")){
- type = Type.getInternalName(Object.class);
- }
- d = d + "L" + type + ";";
- }
- d += ")V";
- return d;
- }
-
- @Override
- public void visit(NewArray newArray) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void visit(ExpressionReceiver receiver) {
- receiver.expr.accept(this);
- }
-
- @Override
- public void visit(UnaryExpr unaryExpr) {
- unaryExpr.expr.accept(this);
- Operation op = unaryExpr.operation;
-
- String typeOfUnary = resolver.getResolvedType(unaryExpr.getType());
-
- boolean isIncOrDec = false;
-
- switch (op) {
- case POSTDECREMENT:
- case POSTINCREMENT:
- if (statement instanceof AssignStmt)
- mv.visitInsn(Opcodes.DUP);
- genBCForIncAndDec(op, typeOfUnary);
- doBoxing(typeOfUnary);
- isIncOrDec = true;
- break;
- case PREDECREMENT:
- case PREINCREMENT:
- genBCForIncAndDec(op, typeOfUnary);
- doBoxing(typeOfUnary);
- if (statement instanceof AssignStmt)
- mv.visitInsn(Opcodes.DUP);
- isIncOrDec = true;
- break;
-
- case MINUS:
- doVisitNegIns(typeOfUnary);
- break;
- default:
- break;
- }
-
- // Für Byte und Short muss noch einen Cast geben i2b, i2s
- // das wird später gemacht, da bytecode für cast noch nicht erzeugt wird
-
- if (isIncOrDec && (unaryExpr.expr instanceof LocalVar)) {
- if(needDUP) {
- mv.visitInsn(Opcodes.DUP);
- needDUP = false;
- }
- LocalVar local = (LocalVar) unaryExpr.expr;
- mv.visitVarInsn(Opcodes.ASTORE, paramsAndLocals.get(local.name));
- }
- }
-
- private void doVisitNegIns(String typeOfUnary) {
- switch (typeOfUnary) {
- case CONSTANTS.REFTYPE_LONG:
- mv.visitInsn(Opcodes.LNEG);
- break;
- case CONSTANTS.REFTYPE_DOUBLE:
- mv.visitInsn(Opcodes.DNEG);
- break;
- case CONSTANTS.REFTYPE_FLOAT:
- mv.visitInsn(Opcodes.FNEG);
- break;
- default:
- mv.visitInsn(Opcodes.INEG);
- break;
- }
-
- }
-
- private void genBCForIncAndDec(Operation op, String typeOfUnary) {
-
- doUnboxing(typeOfUnary);
- loadValue(typeOfUnary, 1.0, true);
-
- switch (op) {
- case POSTDECREMENT:
- case PREDECREMENT:
- doVisitSubOpInsn(typeOfUnary);
- break;
-
- case POSTINCREMENT:
- case PREINCREMENT:
- doVisitAddOpInsn(typeOfUnary);
- break;
-
- default:
- break;
- }
- }
-
- @Override
- public void visit(Return aReturn) {
- statement = new ReturnStmt(aReturn.retexpr);
- isReturnStmt = true;
- isBinaryExp = statement.isExprBinary();
- boolean isBinary = isBinaryExp;
- if(aReturn.retexpr instanceof UnaryExpr)
- needDUP = true;
-
- aReturn.retexpr.accept(this);
-
- if (isBinary) {
- BinaryExpr binary = (BinaryExpr) aReturn.retexpr;
- doBoxing(resolver.getResolvedType(binary.getType()));
-// isBinaryExp = false;
- }
-
- mv.visitInsn(Opcodes.ARETURN);
- isReturnStmt = false;
- statement = null;
- }
-
- @Override
- public void visit(ReturnVoid aReturn) {
- mv.visitInsn(Opcodes.RETURN);
- }
-
- @Override
- public void visit(StaticClassName staticClassName) {
- // mv.visitMethodInsn(Opcodes.INVOKESTATIC,
- // staticClassName.getType().toString().replace(".", "/"),
- // staticClassName.toString(), staticClassName.getType().toString(), false);
- // mv.visitFieldInsn(Opcodes.GETSTATIC,
- // getResolvedType(staticClassName.getType()), fieldName, fieldDesc);
- throw new NotImplementedException("Static noch nicht implementiert!");
- }
-
- @Override
- public void visit(Super aSuper) {
-
- }
-
- @Override
- public void visit(This aThis) {
-
- if(BytecodeGen.THISTYPE == null)
- BytecodeGen.THISTYPE = aThis.getType();
-
- mv.visitVarInsn(Opcodes.ALOAD, 0);
- }
-
- @Override
- public void visit(WhileStmt whileStmt) {
- statement = new LoopStmt(whileStmt.expr, whileStmt.loopBlock);
- isBinaryExp = statement.isExprBinary();
- whileStmt.expr.accept(this);
-// isBinaryExp = false;
- statement = null;
- }
-
- @Override
- public void visit(DoStmt whileStmt) {
- whileStmt.expr.accept(this);
- // TODO:
- }
-
- @Override
- public void visit(Literal literal) {
- Object value = literal.value;
- String typeOfLiteral = resolver.getResolvedType(literal.getType());
- System.out.println("typeOfLiteral :=> "+ typeOfLiteral);
- // Der Wert des Literals wird auf den Stack geladen und
- // geboxt, wenn es nötig ist.
- loadValue(typeOfLiteral, value, false);
- }
-
- // Unboxing: RefType -> prim
- public void doUnboxing(String type) {
- switch (type) {
-// case "java/lang/String":
-// mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(StringBuilder.class), "append",
-// "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
-//
-// break;
- case "java/lang/Boolean":
- mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z", false);
- break;
- case CONSTANTS.REFTYPE_BYTE:
- mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, CONSTANTS.REFTYPE_BYTE, "byteValue", "()B", false);
- break;
- case CONSTANTS.REFTYPE_SHORT:
- mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, CONSTANTS.REFTYPE_SHORT, "shortValue", "()S", false);
- break;
- case "java/lang/Integer":
- mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Integer", "intValue", "()I", false);
- break;
- case CONSTANTS.REFTYPE_LONG:
- mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, CONSTANTS.REFTYPE_LONG, "longValue", "()J", false);
- break;
- case CONSTANTS.REFTYPE_FLOAT:
- mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, CONSTANTS.REFTYPE_FLOAT, "floatValue", "()F", false);
- break;
- case CONSTANTS.REFTYPE_DOUBLE:
- mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, CONSTANTS.REFTYPE_DOUBLE, "doubleValue", "()D", false);
- break;
- case "java/lang/Character":
- break;
- default:
- // mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Integer", "intValue",
- // "()I", false);
- break;
- }
- }
-
- private void loadValue(String type, Object value, boolean isOperator) {
- switch (type) {
- case CONSTANTS.REFTYPE_STRING:
- mv.visitLdcInsn(String.valueOf(value));
- break;
- case "java/lang/Boolean":
- visitBooleanLiteral((Boolean) value);
- break;
- case CONSTANTS.REFTYPE_BYTE:
- if(value instanceof Double)
- visitByteLiteral(((Double) value).byteValue(), false);
- if(value instanceof Integer)
- visitByteLiteral(((Integer) value).byteValue(), false);
- break;
- case CONSTANTS.REFTYPE_SHORT:
- if(value instanceof Double)
- visitShortLiteral(((Double) value).shortValue(), false);
- if(value instanceof Integer)
- visitShortLiteral(((Integer) value).shortValue(), false);
- break;
- case "java/lang/Integer":
- // zweite Argument isLong
-// visitIntegerLiteral(((Double) value).intValue(), false);
- if(value instanceof Double)
- visitIntegerLiteral(((Double) value).intValue(), false);
- if(value instanceof Integer)
- visitIntegerLiteral(((Integer) value).intValue(), false);
- break;
- case CONSTANTS.REFTYPE_LONG:
- if(value instanceof Double)
- visitLongLiteral(((Double) value).longValue(), true);
- if(value instanceof Integer)
- visitLongLiteral(((Integer) value).longValue(), true);
- break;
- case CONSTANTS.REFTYPE_FLOAT:
- if(value instanceof Double)
- visitFloatLiteral(((Double) value).floatValue());
- if(value instanceof Integer)
- visitFloatLiteral(((Integer) value).floatValue());
- break;
- case CONSTANTS.REFTYPE_DOUBLE:
- if(value instanceof Double)
- visitDoubleLiteral((Double) value);
- if(value instanceof Integer)
- visitDoubleLiteral(((Integer) value).doubleValue());
- break;
- case "java/lang/Character":
- visitCharLiteral((Character) value);
- break;
- default:
- // wenn die Typ des Literals = Number ist, wird integer-value
- // verwendet
-// visitIntegerLiteral(((Double) value).intValue(), false);
- break;
- }
- // Boxing
- if (!type.equals(CONSTANTS.REFTYPE_STRING) && !type.equals("java/lang/Boolean")) {
- if (!this.isBinaryExp && !isOperator)
- doBoxing(type);
- }
-
- }
-
- // Boxing: prim -> RefType
- public void doBoxing(String type) {
- switch (type) {
-// case "java/lang/String":
-// mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;",
-// false);
-// break;
- case "java/lang/Boolean":
- mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;", false);
- break;
- case CONSTANTS.REFTYPE_BYTE:
- mv.visitMethodInsn(Opcodes.INVOKESTATIC, CONSTANTS.REFTYPE_BYTE, "valueOf", "(B)Ljava/lang/Byte;", false);
- break;
- case CONSTANTS.REFTYPE_SHORT:
- mv.visitMethodInsn(Opcodes.INVOKESTATIC, CONSTANTS.REFTYPE_SHORT, "valueOf", "(S)Ljava/lang/Short;", false);
- break;
- case "java/lang/Integer":
- mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;", false);
- break;
- case CONSTANTS.REFTYPE_LONG:
- mv.visitMethodInsn(Opcodes.INVOKESTATIC, CONSTANTS.REFTYPE_LONG, "valueOf", "(J)Ljava/lang/Long;", false);
- break;
- case CONSTANTS.REFTYPE_FLOAT:
- mv.visitMethodInsn(Opcodes.INVOKESTATIC, CONSTANTS.REFTYPE_FLOAT, "valueOf", "(F)Ljava/lang/Float;", false);
- break;
- case CONSTANTS.REFTYPE_DOUBLE:
- mv.visitMethodInsn(Opcodes.INVOKESTATIC, CONSTANTS.REFTYPE_DOUBLE, "valueOf", "(D)Ljava/lang/Double;", false);
- break;
- case "java/lang/Character":
- mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Character", "valueOf", "(C)Ljava/lang/Character;",
- false);
- break;
- default:
- break;
- }
- }
-
- private void visitCharLiteral(Character value) {
- mv.visitIntInsn(Opcodes.BIPUSH, (int) value);
-
- }
-
- private void visitDoubleLiteral(Double value) {
- if (value == 0) {
- mv.visitInsn(Opcodes.DCONST_0);
- } else if (value == 1) {
- mv.visitInsn(Opcodes.DCONST_1);
- } else {
- mv.visitLdcInsn(value);
- }
- }
-
- private void visitFloatLiteral(Float value) {
- if (value.intValue() > -1 && value.intValue() < 3) {
- // Opcodes.FCONST_0 = 11, Opcodes.FCONST_1 = 12, usw
- mv.visitInsn(value.intValue() + 11);
- } else {
- mv.visitLdcInsn(value);
- }
- }
-
- private void visitLongLiteral(Long value, boolean isLong) {
- if (value == 0) {
- mv.visitInsn(Opcodes.LCONST_0);
- } else if (value == 1) {
- mv.visitInsn(Opcodes.LCONST_1);
- } else {
- mv.visitLdcInsn(value);
- }
- }
-
- private void visitShortLiteral(Short value, boolean isLong) {
- if (value < 128 || (value > -129 && value < -1)) {
- visitByteLiteral(value.byteValue(), isLong);
- } else if (value < Math.pow(2, 15) || (value >= -Math.pow(2, 15)) && value < -128) {
- mv.visitIntInsn(Opcodes.SIPUSH, value);
- }
- }
-
- private void visitByteLiteral(Byte value, boolean isLong) {
-
- if (!isLong && value < 6 && value > -1) {
- // Opcodes.ICONST_0 = 3, Opcodes.ICONST_1 = 4, usw
- mv.visitInsn(value + 3);
- } else if (isLong && value > -1 && value < 2) {
- // Opcodes.LCONST_0 = 9, Opcodes.LCONST_1 = 10
- mv.visitInsn(value + 9);
- } else {
- mv.visitIntInsn(Opcodes.BIPUSH, value);
- }
-
- }
-
- private void visitIntegerLiteral(Integer value, boolean isLong) {
-
- if (value < Math.pow(2, 15) || (value >= -Math.pow(2, 15)) && value < -128) {
- visitShortLiteral(value.shortValue(), isLong);
- } else {
- mv.visitLdcInsn(value);
- }
- }
-
- private void visitBooleanLiteral(Boolean b) {
- if (b) {
- mv.visitInsn(Opcodes.ICONST_1);
- } else {
- mv.visitInsn(Opcodes.ICONST_0);
- }
- // muss noch getestet werden.
- mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;", false);
- }
-
- @Override
- public void visit(ArgumentList argumentList) {
- for (Expression al : argumentList.getArguments()) {
- statement = new ArgumentExpr(al);
- isBinaryExp = statement.isExprBinary();
- if(al instanceof UnaryExpr)
- needDUP = true;
- al.accept(this);
- //TODO: teste, ob man das für unary braucht
- if (isBinaryExp) {
- BinaryExpr binary = (BinaryExpr) al;
- String binaryType = resolver.getResolvedType(binary.getType());
- doBoxing(binaryType);
- }
- statement = null;
- }
- }
-
- @Override
- public void visit(AssignToField assignLeftSide) {
- // temporäre Lösung für testen, bis ich weiss wie man funktionale
- // interfaces erkennt
- if (isRightSideALambda)
- varsFunInterface.add(assignLeftSide.field.getType());
- // Loads the an object reference from the local variable
- // array slot onto the top of the operand stack.
- assignLeftSide.field.receiver.accept(this);
- this.rightSideTemp.accept(this);
- String fDesc = "L";
- if(resultSet.resolveType(assignLeftSide.field.getType()).resolvedType instanceof TypePlaceholder) {
- fDesc += Type.getInternalName(Object.class);
- } else {
- fDesc += resultSet.resolveType(assignLeftSide.field.getType()).resolvedType.acceptTV(new TypeToDescriptor());
- }
- fDesc +=";";
-
-
- System.out.println("Receiver = " + resolver.getResolvedType(assignLeftSide.field.receiver.getType()));
- mv.visitFieldInsn(Opcodes.PUTFIELD, resolver.getResolvedType(assignLeftSide.field.receiver.getType()),
- assignLeftSide.field.fieldVarName, fDesc);
- }
-
- @Override
- public void visit(AssignToLocal assignLeftSide) {
- if (isRightSideALambda)
- varsFunInterface.add(assignLeftSide.localVar.getType());
- int index = paramsAndLocals.size();
- String var = assignLeftSide.localVar.name;
- if (!paramsAndLocals.containsKey(var)) {
- paramsAndLocals.put(var, index + 1);
- }
-
- mv.visitVarInsn(Opcodes.ASTORE, paramsAndLocals.get(var));
- }
-
-}
diff --git a/src/main/java/de/dhbwstuttgart/target/bytecode/CodeGenException.java b/src/main/java/de/dhbwstuttgart/bytecode/CodeGenException.java
similarity index 75%
rename from src/main/java/de/dhbwstuttgart/target/bytecode/CodeGenException.java
rename to src/main/java/de/dhbwstuttgart/bytecode/CodeGenException.java
index f517e767..633e0a10 100644
--- a/src/main/java/de/dhbwstuttgart/target/bytecode/CodeGenException.java
+++ b/src/main/java/de/dhbwstuttgart/bytecode/CodeGenException.java
@@ -1,4 +1,4 @@
-package de.dhbwstuttgart.target.bytecode;
+package de.dhbwstuttgart.bytecode;
public class CodeGenException extends RuntimeException {
public CodeGenException(String cause) {
diff --git a/src/main/java/de/dhbwstuttgart/target/bytecode/Codegen.java b/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java
old mode 100755
new mode 100644
similarity index 96%
rename from src/main/java/de/dhbwstuttgart/target/bytecode/Codegen.java
rename to src/main/java/de/dhbwstuttgart/bytecode/Codegen.java
index 04bee492..d32aa851
--- a/src/main/java/de/dhbwstuttgart/target/bytecode/Codegen.java
+++ b/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java
@@ -1,6 +1,5 @@
-package de.dhbwstuttgart.target.bytecode;
+package de.dhbwstuttgart.bytecode;
-import de.dhbwstuttgart.syntaxtree.statement.Block;
import de.dhbwstuttgart.target.tree.*;
import de.dhbwstuttgart.target.tree.expression.*;
import de.dhbwstuttgart.target.tree.type.*;
@@ -59,8 +58,10 @@ public class Codegen {
Scope scope = new Scope(null);
int localCounter;
MethodVisitor mv;
+ TargetType returnType;
- State(MethodVisitor mv, int localCounter) {
+ State(TargetType returnType, MethodVisitor mv, int localCounter) {
+ this.returnType = returnType;
this.mv = mv;
this.localCounter = localCounter;
}
@@ -236,7 +237,7 @@ public class Codegen {
mv.visitInsn(I2F);
else if (dest.equals(TargetType.Double))
mv.visitInsn(I2D);
- } else {
+ } else if (!(dest instanceof TargetGenericType)) {
boxPrimitive(state, source);
mv.visitTypeInsn(CHECKCAST, dest.getInternalName());
unboxPrimitive(state, dest);
@@ -751,7 +752,7 @@ public class Codegen {
private void generate(State state, TargetExpression expr) {
var mv = state.mv;
switch (expr) {
- case TargetClassName className:
+ case TargetClassName ignored:
break; // NOP
case TargetBlock block: {
var localCounter = state.localCounter;
@@ -810,6 +811,9 @@ public class Codegen {
generate(state, varDecl.value());
boxPrimitive(state, varDecl.varType());
mv.visitVarInsn(ASTORE, local.index());
+ } else {
+ mv.visitInsn(ACONST_NULL);
+ mv.visitVarInsn(ASTORE, local.index());
}
break;
}
@@ -830,13 +834,15 @@ public class Codegen {
break;
}
case TargetFieldVar dot: {
+ var fieldType = dot.type();
generate(state, dot.left());
generate(state, assign.right());
- boxPrimitive(state, assign.right().type());
+ convertTo(state, assign.right().type(), fieldType);
+ boxPrimitive(state, fieldType);
if (dot.isStatic())
mv.visitInsn(DUP);
else mv.visitInsn(DUP_X1);
- mv.visitFieldInsn(dot.isStatic() ? PUTSTATIC : PUTFIELD, dot.owner().getInternalName(), dot.right(), dot.type().toSignature());
+ mv.visitFieldInsn(dot.isStatic() ? PUTSTATIC : PUTFIELD, dot.owner().getInternalName(), dot.right(), fieldType.toSignature());
break;
}
default:
@@ -867,6 +873,7 @@ public class Codegen {
mv.visitLabel(start);
if (_for.termination() != null)
generate(state, _for.termination());
+ else mv.visitInsn(ICONST_1);
mv.visitJumpInsn(IFEQ, end);
generate(state, _for.body());
if (_for.increment() != null) {
@@ -907,9 +914,10 @@ public class Codegen {
break;
}
case TargetReturn ret: {
- if (ret.expression() != null) {
+ if (ret.expression() != null && state.returnType != null) {
generate(state, ret.expression());
- boxPrimitive(state, ret.expression().type());
+ convertTo(state, ret.expression().type(), state.returnType);
+ boxPrimitive(state, state.returnType);
mv.visitInsn(ARETURN);
} else mv.visitInsn(RETURN);
break;
@@ -941,7 +949,7 @@ public class Codegen {
mv.visitMethodInsn(call.isInterface() ? INVOKEINTERFACE : call.isStatic() ? INVOKESTATIC: call.name().equals("") ? INVOKESPECIAL : INVOKEVIRTUAL,
call.owner().getInternalName(), call.name(), descriptor, call.isInterface());
- if (call.returnType() != null && !(call.returnType() instanceof TargetPrimitiveType)) {
+ if (call.type() != null && call.returnType() != null && !(call.returnType() instanceof TargetPrimitiveType)) {
if (!call.returnType().equals(call.type()) && !(call.type() instanceof TargetGenericType))
mv.visitTypeInsn(CHECKCAST, call.type().getInternalName());
unboxPrimitive(state, call.type());
@@ -967,13 +975,13 @@ public class Codegen {
}
private void generateField(TargetField field) {
- cw.visitField(field.access() | ACC_PUBLIC, field.name(), field.type().toSignature(), field.type().toGenericSignature(), null);
+ cw.visitField(field.access() | ACC_PUBLIC, field.name(), field.type().toSignature(), field.type().toDescriptor(), null);
}
private void generateConstructor(TargetConstructor constructor) {
MethodVisitor mv = cw.visitMethod(constructor.access() | ACC_PUBLIC, "", constructor.getDescriptor(), constructor.getSignature(), null);
mv.visitCode();
- var state = new State(mv, 1);
+ var state = new State(null, mv, 1);
for (var param: constructor.parameters())
state.createVariable(param.name(), param.type());
@@ -997,7 +1005,7 @@ public class Codegen {
// TODO The older codegen has set ACC_PUBLIC for all methods, good for testing but bad for everything else
MethodVisitor mv = cw.visitMethod(method.access() | ACC_PUBLIC, method.name(), method.getDescriptor(), method.getSignature(), null);
mv.visitCode();
- var state = new State(mv, method.isStatic() ? 0 : 1);
+ var state = new State(method.returnType(), mv, method.isStatic() ? 0 : 1);
for (var param: method.parameters())
state.createVariable(param.name(), param.type());
generate(state, method.block());
@@ -1010,16 +1018,16 @@ public class Codegen {
private static String generateSignature(TargetClass clazz) {
String ret = "<";
for (var generic : clazz.generics()) {
- ret += generic.name() + ":" + generic.bound().toGenericSignature();
+ ret += generic.name() + ":" + generic.bound().toDescriptor();
}
ret += ">";
- ret += clazz.superType().toGenericSignature();
+ ret += clazz.superType().toDescriptor();
return ret;
}
public byte[] generate() {
- cw.visit(V1_8, clazz.modifiers() | ACC_PUBLIC, clazz.qualifiedName(),
+ cw.visit(V1_8, clazz.modifiers() | ACC_PUBLIC | ACC_SUPER, clazz.qualifiedName(),
generateSignature(clazz), clazz.superType() != null ? clazz.superType().getInternalName(): "java/lang/Object",
clazz.implementingInterfaces().stream().map(TargetType::toSignature).toArray(String[]::new)
);
diff --git a/src/main/java/de/dhbwstuttgart/bytecode/Exception/BytecodeGeneratorError.java b/src/main/java/de/dhbwstuttgart/bytecode/Exception/BytecodeGeneratorError.java
deleted file mode 100644
index 7d26f66f..00000000
--- a/src/main/java/de/dhbwstuttgart/bytecode/Exception/BytecodeGeneratorError.java
+++ /dev/null
@@ -1,19 +0,0 @@
-/**
- *
- */
-package de.dhbwstuttgart.bytecode.Exception;
-
-/**
- * @author fayez
- *
- */
-public class BytecodeGeneratorError extends RuntimeException {
-
- /**
- * @param message
- */
- public BytecodeGeneratorError(String message) {
- super(message);
- }
-
-}
diff --git a/src/main/java/de/dhbwstuttgart/bytecode/Exception/NotFoundException.java b/src/main/java/de/dhbwstuttgart/bytecode/Exception/NotFoundException.java
deleted file mode 100644
index 829d08a9..00000000
--- a/src/main/java/de/dhbwstuttgart/bytecode/Exception/NotFoundException.java
+++ /dev/null
@@ -1,25 +0,0 @@
-/**
- *
- */
-package de.dhbwstuttgart.bytecode.Exception;
-
-/**
- * @author fayez
- *
- */
-public class NotFoundException extends Exception {
-
- /**
- *
- */
- private static final long serialVersionUID = 1L;
-
- /**
- * @param message
- */
- public NotFoundException(String message) {
- super(message);
- }
-
-
-}
diff --git a/src/main/java/de/dhbwstuttgart/bytecode/Exception/NotInCurrentPackageException.java b/src/main/java/de/dhbwstuttgart/bytecode/Exception/NotInCurrentPackageException.java
deleted file mode 100644
index 4c3293a6..00000000
--- a/src/main/java/de/dhbwstuttgart/bytecode/Exception/NotInCurrentPackageException.java
+++ /dev/null
@@ -1,26 +0,0 @@
-/**
- *
- */
-package de.dhbwstuttgart.bytecode.Exception;
-
-/**
- * @author fayez
- *
- */
-public class NotInCurrentPackageException extends Exception {
-
- /**
- *
- */
- private static final long serialVersionUID = 1L;
-
- /**
- * @param message
- */
- public NotInCurrentPackageException(String message) {
- super(message);
- }
-
-
-
-}
diff --git a/src/main/java/de/dhbwstuttgart/bytecode/FunNGenerator.java b/src/main/java/de/dhbwstuttgart/bytecode/FunNGenerator.java
new file mode 100644
index 00000000..c5ae16f7
--- /dev/null
+++ b/src/main/java/de/dhbwstuttgart/bytecode/FunNGenerator.java
@@ -0,0 +1,121 @@
+package de.dhbwstuttgart.bytecode;
+
+import de.dhbwstuttgart.target.tree.type.TargetGenericType;
+import de.dhbwstuttgart.target.tree.type.TargetRefType;
+import de.dhbwstuttgart.target.tree.type.TargetType;
+import org.objectweb.asm.ClassWriter;
+import org.objectweb.asm.MethodVisitor;
+import org.objectweb.asm.Type;
+
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import static org.objectweb.asm.Opcodes.*;
+
+/**
+ * //ToDo beschreiben
+ *
+ * @since Studienarbeit Type Erasure
+ * @author etiennezink
+ */
+public class FunNGenerator {
+
+ private static final String argumentGenericBase = "T";
+ private static final String returnGeneric = "R";
+ private static final String methodName = "apply";
+ private static final int bytecodeVersion = V1_8;
+
+ private static final String objectSuperType = Type.getInternalName(Object.class).replace('.','/');
+ private static final String objectSignature = applySignature(TargetType.Object);
+
+ private static String applyDescriptor(TargetType a) { return a.toDescriptor(); }
+ private static String applySignature(TargetType a) { return a.toSignature(); }
+ private static String applyNameDescriptor(TargetType a){ return a instanceof TargetGenericType ? "LTPH;" : String.format("L%s;", applySignature(a)); }
+
+ public static byte[] generateSuperBytecode(int numberArguments) {
+ StringBuilder superFunNClassSignature = new StringBuilder("<");
+ StringBuilder superFunNMethodSignature = new StringBuilder("(");
+ StringBuilder superFunNMethodDescriptor = new StringBuilder("(");
+
+ for (int currentParameter = 1; currentParameter <= numberArguments; currentParameter++){
+ superFunNClassSignature.append(String.format("%s%d:%s",argumentGenericBase, currentParameter, objectSignature));
+ superFunNMethodSignature.append(String.format("T%s;", applySignature(new TargetRefType(argumentGenericBase + currentParameter))));
+ superFunNMethodDescriptor.append(objectSignature);
+ }
+ superFunNClassSignature.append(String.format("%s:%s>%s", returnGeneric, objectSignature, objectSignature));
+ superFunNMethodSignature.append(String.format(")T%s;", applySignature(new TargetRefType(returnGeneric))));
+ superFunNMethodDescriptor.append(String.format(")%s", objectSignature));
+
+ ClassWriter classWriter = new ClassWriter(0);
+ MethodVisitor methodVisitor;
+ classWriter.visit(bytecodeVersion, ACC_PUBLIC | ACC_ABSTRACT | ACC_INTERFACE, getSuperClassName(numberArguments), superFunNClassSignature.toString(), objectSuperType, null);
+ methodVisitor = classWriter.visitMethod(ACC_PUBLIC | ACC_ABSTRACT, methodName, superFunNMethodDescriptor.toString(), superFunNMethodSignature.toString(), null);
+ methodVisitor.visitEnd();
+ classWriter.visitEnd();
+ return classWriter.toByteArray();
+ }
+
+ public static String getSuperClassName(int numberArguments) {
+ return String.format("Fun%d$$", numberArguments);
+ }
+
+ public static byte[] generateSpecializedBytecode(List argumentTypes, TargetType returnType) {
+ List parameters = Stream
+ .concat(argumentTypes.stream(), Stream.of(returnType))
+ .collect(Collectors.toList());
+ StringBuilder funNClassSignature = new StringBuilder(objectSignature + applySignature(new TargetRefType(getSuperClassName(argumentTypes.size()), parameters)));
+ boolean containsGeneric = false;
+
+ String genericSignature = "<";
+ for (TargetType typeArgument : parameters) {
+ //ToDo Etienne: Refactor
+ if (typeArgument instanceof TargetGenericType generic){
+ //if(genericSignature.contains(generic.name())) continue;
+ genericSignature += String.format("%s:%s", generic.name(), applyDescriptor(generic));
+ containsGeneric = true;
+ }
+ }
+ genericSignature += ">";
+ if (containsGeneric) funNClassSignature.insert(0, genericSignature);
+ System.out.println(funNClassSignature.toString());
+
+ ClassWriter classWriter = new ClassWriter(0);
+ classWriter.visit(bytecodeVersion, ACC_PUBLIC | ACC_ABSTRACT | ACC_INTERFACE, getSpecializedClassName(argumentTypes, returnType), funNClassSignature.toString(), objectSuperType, new String[]{getSuperClassName(argumentTypes.size())});
+ classWriter.visitEnd();
+ return classWriter.toByteArray();
+ }
+
+ public static String getSpecializedClassName(List argumentTypes, TargetType returnType) {
+ return String.format("Fun%d$$%s%s",
+ argumentTypes.size(),
+ argumentTypes
+ .stream()
+ .map(FunNGenerator::applyNameDescriptor)
+ .collect(Collectors.joining()),
+ applyNameDescriptor(returnType))
+ .replace('/', '$')
+ .replace(";", "$_$");
+ }
+
+ public static String getSpecializedDescriptor(List argumentTypes, TargetType returnType) {
+ return applyDescriptor(new TargetRefType(getSpecializedClassName(argumentTypes, returnType)));
+ }
+
+ public static String getSpecializedSignature(List argumentTypes, TargetType returnType) {
+ return applySignature(new TargetRefType(getSpecializedClassName(argumentTypes, returnType)));
+ }
+
+ public static List getArguments(List list) {
+ return list
+ .stream()
+ .limit(Math.max(0, list.size() - 1))
+ .collect(Collectors.toList());
+ }
+
+ public static TargetType getReturnType(List list) {
+ if(list.size() == 0)
+ throw new IndexOutOfBoundsException();
+ return list.get(list.size() - 1);
+ }
+}
diff --git a/src/main/java/de/dhbwstuttgart/bytecode/IStatement.java b/src/main/java/de/dhbwstuttgart/bytecode/IStatement.java
deleted file mode 100644
index f8d61097..00000000
--- a/src/main/java/de/dhbwstuttgart/bytecode/IStatement.java
+++ /dev/null
@@ -1,9 +0,0 @@
-package de.dhbwstuttgart.bytecode;
-
-import org.objectweb.asm.Label;
-import org.objectweb.asm.MethodVisitor;
-
-public interface IStatement {
- public boolean isExprBinary();
- public void genBCForRelOp(MethodVisitor mv, Label branchLabel, Label endLabel, BytecodeGenMethod bytecodeGenMethod);
-}
diff --git a/src/main/java/de/dhbwstuttgart/bytecode/IfStatement.java b/src/main/java/de/dhbwstuttgart/bytecode/IfStatement.java
deleted file mode 100644
index dc548f01..00000000
--- a/src/main/java/de/dhbwstuttgart/bytecode/IfStatement.java
+++ /dev/null
@@ -1,29 +0,0 @@
-package de.dhbwstuttgart.bytecode;
-
-import org.objectweb.asm.Label;
-import org.objectweb.asm.MethodVisitor;
-import org.objectweb.asm.Opcodes;
-
-import de.dhbwstuttgart.syntaxtree.statement.Expression;
-import de.dhbwstuttgart.syntaxtree.statement.Statement;
-
-public class IfStatement extends AStatement{
-
- private Statement then_block;
- private Statement else_block;
-
- public IfStatement(Expression expr, Statement then_block, Statement else_block) {
- super(expr);
- this.then_block = then_block;
- this.else_block = else_block;
- }
-
- @Override
- public void genBCForRelOp(MethodVisitor mv,Label branchLabel, Label endLabel, BytecodeGenMethod bytecodeGenMethod) {
- bytecodeGenMethod.isBinary(false);
- this.then_block.accept(bytecodeGenMethod);
-
- mv.visitLabel(branchLabel);
- this.else_block.accept(bytecodeGenMethod);
- }
-}
diff --git a/src/main/java/de/dhbwstuttgart/bytecode/JavaTXSignatureAttribute.java b/src/main/java/de/dhbwstuttgart/bytecode/JavaTXSignatureAttribute.java
new file mode 100644
index 00000000..f185494c
--- /dev/null
+++ b/src/main/java/de/dhbwstuttgart/bytecode/JavaTXSignatureAttribute.java
@@ -0,0 +1,24 @@
+package de.dhbwstuttgart.bytecode;
+
+import org.objectweb.asm.*;
+
+public class JavaTXSignatureAttribute extends Attribute {
+ final String signature;
+
+ protected JavaTXSignatureAttribute(String signature) {
+ super("JavaTXSignature");
+ this.signature = signature;
+ }
+
+ @Override
+ protected Attribute read(ClassReader classReader, int offset, int length, char[] charBuffer, int codeAttributeOffset, Label[] labels) {
+ return super.read(classReader, offset, length, charBuffer, codeAttributeOffset, labels);
+ }
+
+ @Override
+ protected ByteVector write(ClassWriter classWriter, byte[] code, int codeLength, int maxStack, int maxLocals) {
+ var data = new ByteVector();
+ data.putUTF8(this.signature);
+ return data;
+ }
+}
diff --git a/src/main/java/de/dhbwstuttgart/bytecode/LoopStmt.java b/src/main/java/de/dhbwstuttgart/bytecode/LoopStmt.java
deleted file mode 100644
index 0f7788d4..00000000
--- a/src/main/java/de/dhbwstuttgart/bytecode/LoopStmt.java
+++ /dev/null
@@ -1,26 +0,0 @@
-package de.dhbwstuttgart.bytecode;
-
-import org.objectweb.asm.Label;
-import org.objectweb.asm.MethodVisitor;
-import org.objectweb.asm.Opcodes;
-
-import de.dhbwstuttgart.syntaxtree.statement.Expression;
-import de.dhbwstuttgart.syntaxtree.statement.Statement;
-
-public class LoopStmt extends AStatement {
-
- private Statement loopBlock;
-
- public LoopStmt(Expression expr, Statement loopBlock) {
- super(expr);
- this.loopBlock = loopBlock;
- }
-
- @Override
- public void genBCForRelOp(MethodVisitor mv,Label branchLabel, Label endLabel, BytecodeGenMethod bytecodeGenMethod) {
- bytecodeGenMethod.isBinary(false);
- this.loopBlock.accept(bytecodeGenMethod);
- mv.visitJumpInsn(Opcodes.GOTO, endLabel);
- mv.visitLabel(branchLabel);
- }
-}
diff --git a/src/main/java/de/dhbwstuttgart/bytecode/ReturnStmt.java b/src/main/java/de/dhbwstuttgart/bytecode/ReturnStmt.java
deleted file mode 100644
index bd747720..00000000
--- a/src/main/java/de/dhbwstuttgart/bytecode/ReturnStmt.java
+++ /dev/null
@@ -1,14 +0,0 @@
-package de.dhbwstuttgart.bytecode;
-
-import org.objectweb.asm.MethodVisitor;
-
-import de.dhbwstuttgart.syntaxtree.statement.BinaryExpr;
-import de.dhbwstuttgart.syntaxtree.statement.Expression;
-
-public class ReturnStmt extends AStatement {
-
- public ReturnStmt(Expression retexpr) {
- super(retexpr);
- }
-
-}
diff --git a/src/main/java/de/dhbwstuttgart/bytecode/TPHExtractor.java b/src/main/java/de/dhbwstuttgart/bytecode/TPHExtractor.java
deleted file mode 100644
index 6f443a47..00000000
--- a/src/main/java/de/dhbwstuttgart/bytecode/TPHExtractor.java
+++ /dev/null
@@ -1,269 +0,0 @@
-/**
- *
- */
-package de.dhbwstuttgart.bytecode;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.function.Function;
-import java.util.function.Predicate;
-import java.util.stream.Collectors;
-
-import de.dhbwstuttgart.bytecode.constraint.ExtendsConstraint;
-import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
-import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation;
-import de.dhbwstuttgart.bytecode.utilities.MethodAndTPH;
-import de.dhbwstuttgart.bytecode.utilities.MethodUtility;
-import de.dhbwstuttgart.bytecode.utilities.Resolver;
-import de.dhbwstuttgart.syntaxtree.AbstractASTWalker;
-import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
-import de.dhbwstuttgart.syntaxtree.Constructor;
-import de.dhbwstuttgart.syntaxtree.Field;
-import de.dhbwstuttgart.syntaxtree.FormalParameter;
-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.GenericRefType;
-import de.dhbwstuttgart.syntaxtree.type.RefType;
-import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
-import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
-import de.dhbwstuttgart.typeinference.constraints.Constraint;
-import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
-import de.dhbwstuttgart.typeinference.constraints.Pair;
-import de.dhbwstuttgart.typeinference.result.GenericInsertPair;
-import de.dhbwstuttgart.typeinference.result.ResultPair;
-import de.dhbwstuttgart.typeinference.result.ResultSet;
-
-/**
- * @author Fayez Abu Alia
- *
- */
-public class TPHExtractor extends AbstractASTWalker {
- // Alle TPHs der Felder werden iKopf der Klasse definiert
- // alle TPHs der Klasse: (TPH, is in Method?)
- public final HashMap allTPHS = new HashMap<>();
- public final List tphsClass = new ArrayList<>();
- MethodAndTPH methodAndTph;
-
- Boolean inMethod = false;
- boolean inLocalOrParamOrReturn = false;
-
- public final ArrayList ListOfMethodsAndTph = new ArrayList<>();
-
- // Alle Constraints in einer Menge (Und- & Oder-Constraints)
- public Set oldConstraints = new HashSet<>();
-
- final ArrayList> allPairs = new ArrayList<>();
- public final ArrayList allCons = new ArrayList<>();
- private ResultSet resultSet;
- private Resolver resolver;
-
- public TPHExtractor() {
-
- }
-
- public void setResultSet(ResultSet resultSet) {
- this.resultSet = resultSet;
- this.resolver = new Resolver(resultSet);
- }
-
- public Resolver getResolver() {
- return resolver;
- }
-
-// @Override
-// public void visit(ClassOrInterface classOrInterface) {
-// inMethod = false;
-// classOrInterface.getfieldInitializations().ifPresent(c->c.block.accept(this));
-// super.visit(classOrInterface);
-// inMethod = true;
-// }
- @Override
- public void visit(TypePlaceholder tph) {
- if (resultSet.resolveType(tph).resolvedType instanceof TypePlaceholder) {
- TypePlaceholder resolvedTPH = (TypePlaceholder) resultSet.resolveType(tph).resolvedType;
- String tphName = resolvedTPH.getName();
- if (inMethod && !tphsClass.contains(tphName)) {
- ArrayList tphs = null;
- if (!(tphs = methodAndTph.getTphs()).contains(tphName)) {
- methodAndTph.addTph(tphName);
- }
- if (inLocalOrParamOrReturn) {
- if (!(tphs = methodAndTph.getLocalTphs()).contains(tphName)) {
- tphs.add(tphName);
- }
- }
- } else {
- if (!tphsClass.contains(tphName)) {
- tphsClass.add(tphName);
- }
- }
- if (!allTPHS.containsKey(tphName)) {
- allTPHS.put(tphName, inMethod);
- }
-// final List cons = new ArrayList<>();
-// resultSet.resolveType(tph).additionalGenerics.forEach(ag -> {
-// TPHConstraint con = new ExtendsConstraint(ag.TA1.getName(), ag.TA2.getName(), Relation.EXTENDS);
-// cons.add(con);
-// });
-//
-// Map visitMap = new HashMap<>();
-// for(TPHConstraint cc : cons) {
-// visitMap.put(cc, false);
-// }
-//
-// String left = resolvedTPH.getName();
-// for (TPHConstraint cc : visitMap.keySet()) {
-//
-// if(visitMap.get(cc))
-// continue;
-//
-// if (cc.getLeft().equals(left)) {
-// allCons.add(cc);
-// List toVisit = getToVisitCons(cons,cc.getRight(), visitMap);
-// }
-// }
-
- //resultSet.resolveType(tph).getAdditionalGenerics().forEach(ag -> {
- resultSet.genIns.forEach(ag -> {
-
-// if (ag.contains(resolvedTPH) /* && ag.TA1.equals(resolvedTPH) */ && !contains(allPairs, ag)) {
- if (inMethod)
- methodAndTph.getPairs().add(ag);
- allPairs.add(ag);
- TPHConstraint con = new ExtendsConstraint(ag.getLeft().getName(), ag.getRight().getName());
- if(!containsConstraint(allCons,con))
- allCons.add(con);
-// }
- });
- } else if (resultSet.resolveType(tph).resolvedType instanceof RefType) {
- RefType rt = (RefType) resultSet.resolveType(tph).resolvedType;
- rt.accept(this);
- }
- }
-
- private static boolean containsConstraint(ArrayList allCons, TPHConstraint c) {
- for(TPHConstraint con:allCons) {
- if(c.getLeft().equals(con.getLeft()) && c.getRight().equals(con.getRight())) {
- return true;
- }
- }
- return false;
- }
-
- private List getToVisitCons(List cons, String right, Map visitMap) {
- List res = new ArrayList<>();
- for(TPHConstraint cc : cons) {
- if(cc.getLeft().equals(right)) {
- res.add(cc);
- if(visitMap.get(cc))
- visitMap.replace(cc, false);
- }
- }
- return res;
- }
-
- @Override
- public void visit(GenericRefType genericRefType) {
- String name = genericRefType.getParsedName();
- if (inMethod) {
- methodAndTph.addTph(name);
- if (inLocalOrParamOrReturn)
- methodAndTph.getLocalTphs().add(name);
- }else {
- tphsClass.add(name);
- }
- allTPHS.put(name, inMethod);
- }
-
- private boolean contains(ArrayList pairs, GenericInsertPair genPair) {
- for (int i = 0; i < pairs.size(); ++i) {
- GenericInsertPair p = pairs.get(i);
- if (p.TA1.equals(genPair.TA1) && p.TA2.equals(genPair.TA2))
- return true;
- }
- return false;
- }
-
- @Override
- public void visit(Method method) {
- inMethod = true;
- String id = MethodUtility.createID(resolver,method);
- Predicate filterUndConstraints = cs -> ((cs.TA1 instanceof TypePlaceholder) && (cs.TA2 instanceof TypePlaceholder) &&
- (resultSet.resolveType((TypePlaceholder)(cs.TA1)).resolvedType instanceof TypePlaceholder) &&
- (resultSet.resolveType((TypePlaceholder)(cs.TA2)).resolvedType instanceof TypePlaceholder));
-
- Function, ConstraintSet> filterConstrRemainingTVar =
- csS -> {
- ConstraintSet ret = new ConstraintSet<>();
- ret.addAllUndConstraint(
- csS.getUndConstraints()
- .stream().filter(filterUndConstraints)
- .collect(Collectors.toCollection(Constraint::new)));
-
- csS.getOderConstraints()
- .forEach(oConSSet -> { Set> setCons = new HashSet<>();
- oConSSet.forEach(OConS -> { Constraint newConsPair = new Constraint();
- newConsPair.isStatement = OConS.isStatement;
- setCons.add(
- OConS.stream()
- .filter(filterUndConstraints)
- .collect(Collectors.toCollection(() -> newConsPair)) );
- } );
- ret.addOderConstraint(setCons);} );
- return ret;
- };
- ConstraintSet filteredConstraints = filterConstrRemainingTVar.apply(method.getConstraints());
- methodAndTph = new MethodAndTPH(id, filteredConstraints);
- oldConstraints.addAll(filteredConstraints.getAll());
-
- inLocalOrParamOrReturn = true;
- method.getReturnType().accept(this);
- method.getParameterList().accept(this);
- inLocalOrParamOrReturn = false;
-
- if(method.block != null)
- method.block.accept(this);
-
- inMethod = false;
- ListOfMethodsAndTph.add(methodAndTph);
- }
-
- @Override
- public void visit(Constructor cons) {
- inMethod = false;
- //super.visit(cons);
- cons.getParameterList().accept(this);
- if(cons.block != null)
- cons.block.accept(this);
- inMethod = true;
- }
-
- @Override
- public void visit(LocalVarDecl localVarDecl) {
-// inLocalOrParamOrReturn = inMethod;
- super.visit(localVarDecl);
-// inLocalOrParamOrReturn = false;
- }
-
- @Override
- public void visit(LocalVar localVar) {
-// inLocalOrParamOrReturn = inMethod;
- super.visit(localVar);
-// inLocalOrParamOrReturn = false;
- }
-
- @Override
- public void visit(ParameterList formalParameters) {
- inLocalOrParamOrReturn = inMethod;
- super.visit(formalParameters);
- inLocalOrParamOrReturn = false;
- }
-
-}
\ No newline at end of file
diff --git a/src/main/java/de/dhbwstuttgart/bytecode/descriptor/DescriptorToString.java b/src/main/java/de/dhbwstuttgart/bytecode/descriptor/DescriptorToString.java
deleted file mode 100644
index c272c12f..00000000
--- a/src/main/java/de/dhbwstuttgart/bytecode/descriptor/DescriptorToString.java
+++ /dev/null
@@ -1,260 +0,0 @@
-package de.dhbwstuttgart.bytecode.descriptor;
-
-import java.util.HashMap;
-import java.util.Iterator;
-
-import de.dhbwstuttgart.bytecode.utilities.*;
-import de.dhbwstuttgart.syntaxtree.statement.ArgumentList;
-import org.objectweb.asm.Type;
-
-import de.dhbwstuttgart.syntaxtree.FormalParameter;
-import de.dhbwstuttgart.syntaxtree.statement.Expression;
-import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
-import de.dhbwstuttgart.typeinference.result.ResultSet;
-
-public class DescriptorToString implements DescriptorVisitor, CONSTANTS {
- ResultSet resultSet;
-
- public DescriptorToString() {
- }
-
- public DescriptorToString(ResultSet resultSet) {
- this.resultSet = resultSet;
- }
-
- private String addReturnType(String desc, RefTypeOrTPHOrWildcardOrGeneric returnType, ResultSet resultSet) {
- if(resultSet.resolveType(returnType).resolvedType.toString().equals(CONSTANTS.VOID)){
- desc = desc + ")V";
- }else {
- desc = desc + ")" + "L"+resultSet.resolveType(returnType).resolvedType.acceptTV(new TypeToDescriptor())+ ";";
- }
- return desc;
- }
-
- @Override
- public String visit(NormalMethod method) {
-
- String desc = "(";
- Iterator itr = method.getParameterList().iterator();
- while(itr.hasNext()) {
- FormalParameter fp = itr.next();
-// System.out.println(resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToSignature()));
-// System.out.println("Parmetrisierte typ ? "+ ((RefType) fp.getType()).getParaList().size());
- if(method.hasGen()) {
- String fpDesc = fp.getType().acceptTV(new TypeToDescriptor());
- if(method.getGenericsAndBoundsMethod().containsKey(fpDesc)) {
- String bound = getBound(fpDesc, method.getGenericsAndBoundsMethod());
- desc += "L"+bound+ ";";
- }else if(method.getGenericsAndBounds().containsKey(fpDesc)){
- String bound = getBound(fpDesc, method.getGenericsAndBounds());
- desc += "L"+bound+ ";";
- }else {
-// desc += "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";";
- String resType = resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor());
- if(resType.contains(CONSTANTS.TPH)/*resType.subSequence(0, 4).equals("TPH ")*/) {
- // Bound ist immer Object
- desc += "L"+Type.getInternalName(Object.class)+ ";";
- } else {
- // TODO::
- if(method.getGenericsAndBounds().containsKey(resType)) {
- String bound = getBound(resType, method.getGenericsAndBounds());
- desc += "L"+bound+ ";";
- }else if(method.getGenericsAndBoundsMethod().containsKey(resType)) {
- String bound = getBound(resType, method.getGenericsAndBoundsMethod());
- desc += "L"+bound+ ";";
- } else {
- desc += "L"+resType+ ";";
- }
- }
- }
- }
- //TODO: generate a class java%% ... %%
- else if(resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor()).contains(CONSTANTS.ANGLEBRACKET)){
- desc += "L"+resultSet.resolveType(fp.getType()).resolvedType.toString().replace(".", "$$").replace(CONSTANTS.ANGLEBRACKET, "$$$").replace(">", "$$$")+ ";";
- }
- else {
- desc += "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";";
- }
- }
-
- if(resultSet.resolveType(method.getReturnType()).resolvedType.toString().equals(CONSTANTS.VOID)) {
- desc += ")V";
- }else {
- if(method.hasGen()) {
- String ret = method.getReturnType().acceptTV(new TypeToDescriptor());
- if(method.getGenericsAndBoundsMethod().containsKey(ret)) {
- desc += ")L"+method.getGenericsAndBoundsMethod().get(ret)+ ";";
- }else if(method.getGenericsAndBounds().containsKey(ret)){
- desc += ")L"+method.getGenericsAndBounds().get(ret)+ ";";
- }else {
- String resType = resultSet.resolveType(method.getReturnType()).resolvedType.acceptTV(new TypeToDescriptor());
- if(resType.contains(CONSTANTS.TPH)/*resType.subSequence(0, 4).equals("TPH ")*/) {
-// desc += ")" + "L"+method.getGenericsAndBoundsMethod().get(resType.substring(4)+"$")+ ";";
- desc += ")" + "L"+Type.getInternalName(Object.class)+ ";";
- } else {
- // TODO::
- if(method.getGenericsAndBounds().containsKey(resType)) {
- String bound = getBound(resType, method.getGenericsAndBounds());
- desc += ")L"+bound+ ";";
- }else if(method.getGenericsAndBoundsMethod().containsKey(resType)) {
- String bound = getBound(resType, method.getGenericsAndBoundsMethod());
- desc += ")L"+bound+ ";";
- } else {
- desc += ")L"+resType+ ";";
- }
-// desc += ")" + "L"+resType+ ";";
- }
- }
- }else {
- desc += ")" + "L"+resultSet.resolveType(method.getReturnType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";";
- }
- }
-// desc = addReturnType(desc,method.getReturnType(), resultSet);
- return desc;
- }
-
- private String getBound(String fpDesc, HashMap genericsAndBounds) {
- String start = genericsAndBounds.get(fpDesc);
- while(genericsAndBounds.containsKey(start)) {
- start = genericsAndBounds.get(start);
- }
-
- return start;
- }
-
- @Override
- public String visit(NormalConstructor constructor) {
- String desc = "(";
- Iterator itr = constructor.getParameterList().iterator();
- while(itr.hasNext()) {
- FormalParameter fp = itr.next();
- if(constructor.hasGen()) {
-// System.out.println("Cons has Gens");
- String fpDesc = fp.getType().acceptTV(new TypeToDescriptor());
-// System.out.println(fpDesc);
- if(constructor.getGenericsAndBounds().containsKey(fpDesc)){
- desc += "L"+constructor.getGenericsAndBounds().get(fpDesc)+ ";";
- }else {
-// desc += "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";";
- String resType = resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor());
- if(resType.subSequence(0, 4).equals(CONSTANTS.TPH)) {
- // Bound ist immer Object
- desc += "L"+Type.getInternalName(Object.class)+ ";";
- } else {
- desc += "L"+resType+ ";";
- }
- }
- }else {
-// System.out.println("Cons has NO Gens");
- desc += "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";";
- }
- }
- desc = desc + ")V";
- return desc;
- }
-
- //ToDo Etienne: ändern
- @Override
- public String visit(Lambda lambdaExpression) {
- String desc = "(";
- Iterator itr = lambdaExpression.getParams().iterator();
- while(itr.hasNext()) {
- FormalParameter fp = itr.next();
- String d = resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor());
- if(d.contains(CONSTANTS.TPH) ||d.contains(CONSTANTS.ANGLEBRACKET)) {
- desc += "L"+Type.getInternalName(Object.class)+ ";";
- }else {
- desc = desc + "L"+ d + ";";
- }
- }
-
- String retType = resultSet.resolveType(lambdaExpression.getReturnType()).resolvedType.acceptTV(new TypeToDescriptor());
-
- if(retType.contains(CONSTANTS.TPH)|| retType.contains(CONSTANTS.ANGLEBRACKET)){
- desc += ")L"+Type.getInternalName(Object.class)+ ";";
- }else {
- desc = desc + ")"+"L"+retType+";";
- }
- return desc;
- }
-
- @Override
- public String visit(SamMethod samMethod) {
- String desc = "(";
- Iterator itr = samMethod.getArgumentList().iterator();
- while(itr.hasNext()) {
- RefTypeOrTPHOrWildcardOrGeneric rt = itr.next();
- String d = resultSet.resolveType(rt).resolvedType.acceptTV(new TypeToDescriptor());
-
- if(d.contains(CONSTANTS.TPH) ||d.contains(CONSTANTS.ANGLEBRACKET)) {
- desc += "L"+Type.getInternalName(Object.class)+ ";";
- }else {
- desc += "L"+ d + ";";
-
- }
- }
- String retType = resultSet.resolveType(samMethod.getReturnType()).resolvedType.acceptTV(new TypeToDescriptor());
-
- if(retType.contains(CONSTANTS.TPH)|| retType.contains(CONSTANTS.ANGLEBRACKET)){
- desc += ")L"+Type.getInternalName(Object.class)+ ";";
- }else {
- desc = desc + ")"+"L"+retType+";";
- }
- return desc;
- }
-
- @Override
- public String visit(MethodFromMethodCall methodFromMethodCall) {
- String desc = "(";
- for(Expression e : methodFromMethodCall.getArgList().getArguments()) {
- String d = resultSet.resolveType(e.getType()).resolvedType.acceptTV(new TypeToDescriptor());
-
- if(d.contains(CONSTANTS.TPH) ||d.contains(CONSTANTS.ANGLEBRACKET) || methodFromMethodCall.getReceiverName().contains("$$")) {
- desc += "L"+Type.getInternalName(Object.class)+ ";";
- }else {
- if(methodFromMethodCall.getGenericsAndBoundsMethod().containsKey(d)) {
- desc += "L"+methodFromMethodCall.getGenericsAndBoundsMethod().get(d)+ ";";
- }else if(methodFromMethodCall.getGenericsAndBounds().containsKey(d)) {
- desc += "L"+methodFromMethodCall.getGenericsAndBounds().get(d)+ ";";
- }else {
- desc += "L"+resultSet.resolveType(e.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";";
- }
- }
-
- }
- String retType = resultSet.resolveType(methodFromMethodCall.getReturnType()).resolvedType.acceptTV(new TypeToDescriptor());
- System.out.println("DescriptorToString retType = " + retType);
- if(retType.equals(CONSTANTS.VOID)) {
- desc += ")V";
- }else if(retType.contains(CONSTANTS.TPH)|| retType.contains(CONSTANTS.ANGLEBRACKET) || methodFromMethodCall.getReceiverName().contains("$$")){
- desc += ")L"+Type.getInternalName(Object.class)+ ";";
- }else {
- if(methodFromMethodCall.getGenericsAndBoundsMethod().containsKey(retType)) {
- desc += ")L"+methodFromMethodCall.getGenericsAndBoundsMethod().get(retType)+ ";";
- }else if(methodFromMethodCall.getGenericsAndBounds().containsKey(retType)){
- desc += ")L"+methodFromMethodCall.getGenericsAndBounds().get(retType)+ ";";
- }else {
- desc += ")" + "L"+retType+ ";";
- }
- }
-// desc = addReturnType(desc, methodFromMethodCall.getReturnType(), resultSet);
- return desc;
- }
-
- @Override
- public String createDescForFunN(ArgumentList argumentList, String returnType) {
- Iterator itr1 = argumentList.getArguments().iterator();
- String methDesc = "(";
- while(itr1.hasNext()) {
- methDesc += "L" + Type.getInternalName(Object.class) + ";";
- itr1.next();
- }
- if (returnType.equals(CONSTANTS.VOID)){
- methDesc += ")V";
- } else {
- methDesc += ")L" + Type.getInternalName(Object.class) + ";";
- }
- return methDesc;
- }
-
-}
diff --git a/src/main/java/de/dhbwstuttgart/bytecode/descriptor/DescriptorVisitor.java b/src/main/java/de/dhbwstuttgart/bytecode/descriptor/DescriptorVisitor.java
deleted file mode 100644
index d1576c76..00000000
--- a/src/main/java/de/dhbwstuttgart/bytecode/descriptor/DescriptorVisitor.java
+++ /dev/null
@@ -1,17 +0,0 @@
-package de.dhbwstuttgart.bytecode.descriptor;
-
-import de.dhbwstuttgart.bytecode.utilities.Lambda;
-import de.dhbwstuttgart.bytecode.utilities.MethodFromMethodCall;
-import de.dhbwstuttgart.bytecode.utilities.NormalConstructor;
-import de.dhbwstuttgart.bytecode.utilities.NormalMethod;
-import de.dhbwstuttgart.bytecode.utilities.SamMethod;
-import de.dhbwstuttgart.syntaxtree.statement.ArgumentList;
-
-public interface DescriptorVisitor {
- String visit(NormalMethod method);
- String visit(NormalConstructor constructor);
- String visit(Lambda lambdaExpression);
- String visit(SamMethod samMethod);
- String visit(MethodFromMethodCall methodFromMethodCall);
- String createDescForFunN(ArgumentList argumentList, String returnType);
-}
diff --git a/src/main/java/de/dhbwstuttgart/bytecode/descriptor/TypeToDescriptor.java b/src/main/java/de/dhbwstuttgart/bytecode/descriptor/TypeToDescriptor.java
deleted file mode 100644
index f10f8070..00000000
--- a/src/main/java/de/dhbwstuttgart/bytecode/descriptor/TypeToDescriptor.java
+++ /dev/null
@@ -1,52 +0,0 @@
-package de.dhbwstuttgart.bytecode.descriptor;
-
-import de.dhbwstuttgart.bytecode.funN.FunNGenerator;
-import de.dhbwstuttgart.bytecode.funN.FunNUtilities;
-import de.dhbwstuttgart.syntaxtree.type.*;
-
-public class TypeToDescriptor implements TypeVisitor{
-
- private final boolean specializedFunN;
-
- public TypeToDescriptor(){ this(true); }
-
- public TypeToDescriptor(boolean specializedFunN) { this.specializedFunN = specializedFunN; }
-
- @Override
- public String visit(RefType refType) {
- if (refType.getName().toString().matches("Fun\\d+\\$\\$") && specializedFunN) {
- FunNUtilities funNUtilities = FunNGenerator.getInstance();
- return funNUtilities.getSpecializedDescriptor(funNUtilities.getArguments(refType.getParaList()), funNUtilities.getReturnType(refType.getParaList()));
- }
-
- return refType.getName().toString().replace(".", "/");
-// String t = refType.getName().toString().replace(".", "/");
-// return t.equals("Fun1")?(t+"$$"):t;
- }
-
- @Override
- public String visit(SuperWildcardType superWildcardType) {
- System.out.println("\nWILDCARD ="+superWildcardType.getInnerType().toString().replace(".", "/"));
- //return superWildcardType.getInnerType().toString().replace(".", "/");
- return superWildcardType.getInnerType().acceptTV(new TypeToDescriptor());
- //throw new NotImplementedException();
- }
-
- @Override
- public String visit(TypePlaceholder typePlaceholder) {
- return typePlaceholder.toString().replace(".", "/");
- }
-
- @Override
- public String visit(ExtendsWildcardType extendsWildcardType) {
- System.out.println("\nWILDCARD extends ="+extendsWildcardType.getInnerType().toString().replace(".", "/"));
- //return extendsWildcardType.getInnerType().toString().replace(".", "/");
- return extendsWildcardType.getInnerType().acceptTV(new TypeToDescriptor());
- //throw new NotImplementedException();
- }
-
- @Override
- public String visit(GenericRefType genericRefType) {
- return genericRefType.getParsedName().replace(".", "/");
- }
-}
\ No newline at end of file
diff --git a/src/main/java/de/dhbwstuttgart/bytecode/funN/FunNGenerator.java b/src/main/java/de/dhbwstuttgart/bytecode/funN/FunNGenerator.java
deleted file mode 100644
index 3a651c93..00000000
--- a/src/main/java/de/dhbwstuttgart/bytecode/funN/FunNGenerator.java
+++ /dev/null
@@ -1,149 +0,0 @@
-package de.dhbwstuttgart.bytecode.funN;
-
-import de.dhbwstuttgart.bytecode.descriptor.TypeToDescriptor;
-import de.dhbwstuttgart.bytecode.signature.TypeToSignature;
-import de.dhbwstuttgart.bytecode.utilities.CONSTANTS;
-import de.dhbwstuttgart.parser.scope.JavaClassName;
-import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
-import de.dhbwstuttgart.syntaxtree.type.RefType;
-import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
-import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
-import org.objectweb.asm.ClassWriter;
-import org.objectweb.asm.MethodVisitor;
-import org.objectweb.asm.Type;
-
-import java.io.File;
-import java.io.FileOutputStream;
-import java.util.List;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
-
-import static org.objectweb.asm.Opcodes.*;
-
-/**
- * //ToDo beschreiben
- *
- * @since Studienarbeit Type Erasure
- * @author etiennezink
- */
-public class FunNGenerator implements FunNUtilities{
-
- private static FunNGenerator funNGenerator = new FunNGenerator();
-
- public static FunNGenerator getInstance(){
- return funNGenerator;
- }
-
- private final String argumentGenericBase = "T";
- private final String returnGeneric = "R";
- private final String methodName = "apply";
- private final int bytecodeVersion = V1_8;
-
- private final String objectSuperType = Type.getInternalName(Object.class).replace('.','/');
- private final RefType objectRefType = new RefType(new JavaClassName(objectSuperType), null);
- private final String objectSignature = applySignature(objectRefType);
-
- private String applyDescriptor(RefTypeOrTPHOrWildcardOrGeneric a) { return a.acceptTV(new TypeToDescriptor(true)); }
- private String applySignature(RefTypeOrTPHOrWildcardOrGeneric a) { return a.acceptTV(new TypeToSignature(true)); }
- private String applyNameDescriptor(RefTypeOrTPHOrWildcardOrGeneric a){ return a instanceof TypePlaceholder ? "LTPH;" : String.format("L%s;", applyDescriptor(a)); }
-
- @Override
- public byte[] generateSuperBytecode(int numberArguments) {
- StringBuilder superFunNClassSignature = new StringBuilder("<");
- StringBuilder superFunNMethodSignature = new StringBuilder("(");
- StringBuilder superFunNMethodDescriptor = new StringBuilder("(");
-
- for (int currentParameter = 1; currentParameter <= numberArguments; currentParameter++){
- superFunNClassSignature.append(String.format("%s%d:%s",argumentGenericBase, currentParameter, objectSignature));
- superFunNMethodSignature.append(String.format("T%s;", applySignature( new GenericRefType(argumentGenericBase + currentParameter, null))));
- superFunNMethodDescriptor.append(objectSignature);
- }
- superFunNClassSignature.append(String.format("%s:%s>%s", returnGeneric, objectSignature, objectSignature));
- superFunNMethodSignature.append(String.format(")T%s;", applySignature(new GenericRefType(returnGeneric, null))));
- superFunNMethodDescriptor.append(String.format(")%s", objectSignature));
-
- ClassWriter classWriter = new ClassWriter(0);
- MethodVisitor methodVisitor;
- classWriter.visit(bytecodeVersion, ACC_PUBLIC | ACC_ABSTRACT | ACC_INTERFACE, getSuperClassName(numberArguments), superFunNClassSignature.toString(), objectSuperType, null);
- methodVisitor = classWriter.visitMethod(ACC_PUBLIC | ACC_ABSTRACT, methodName, superFunNMethodDescriptor.toString(), superFunNMethodSignature.toString(), null);
- methodVisitor.visitEnd();
- classWriter.visitEnd();
- return classWriter.toByteArray();
- }
-
- @Override
- public String getSuperClassName(int numberArguments) {
- return String.format("Fun%d$$", numberArguments);
- }
-
- @Override
- public byte[] generateSpecializedBytecode(List argumentTypes, RefTypeOrTPHOrWildcardOrGeneric returnType) {
- List parameters = Stream
- .concat(argumentTypes.stream(), Stream.of(returnType))
- .collect(Collectors.toList());
- RefType superFunN = new RefType(new JavaClassName(getSuperClassName(argumentTypes.size())), parameters , null);
- StringBuilder funNClassSignature = new StringBuilder(objectSignature + (superFunN.acceptTV(new TypeToSignature(false))));
- boolean containsGeneric = false;
-
- String genericSignature = "<";
- for (RefTypeOrTPHOrWildcardOrGeneric typeArgument : parameters) {
- //ToDo Etienne: Refactor
- if (typeArgument instanceof GenericRefType){
- GenericRefType generic = (GenericRefType) typeArgument;
- if(genericSignature.contains(generic.getParsedName())) continue;
- genericSignature += String.format("%s:%s", generic.getParsedName(), applyDescriptor(generic));
- containsGeneric = true;
- } else if(typeArgument instanceof TypePlaceholder){
- TypePlaceholder placeholder = (TypePlaceholder) typeArgument;
- if(genericSignature.contains(applySignature(placeholder).substring(1))) continue;
- genericSignature += String.format("%s:%s", applySignature(placeholder).substring(1), objectSignature);
- containsGeneric = true;
- }
- }
- genericSignature += ">";
- if (containsGeneric) funNClassSignature.insert(0, genericSignature);
-
- ClassWriter classWriter = new ClassWriter(0);
- classWriter.visit(bytecodeVersion, ACC_PUBLIC | ACC_ABSTRACT | ACC_INTERFACE, getSpecializedClassName(argumentTypes, returnType), funNClassSignature.toString(), objectSuperType, new String[]{getSuperClassName(argumentTypes.size())});
- classWriter.visitEnd();
- return classWriter.toByteArray();
- }
-
- @Override
- public String getSpecializedClassName(List argumentTypes, RefTypeOrTPHOrWildcardOrGeneric returnType) {
- return String.format("Fun%d$$%s%s",
- argumentTypes.size(),
- argumentTypes
- .stream()
- .map(this::applyNameDescriptor)
- .collect(Collectors.joining()),
- applyNameDescriptor(returnType))
- .replace('/', '$')
- .replace(";", "$_$");
- }
-
- @Override
- public String getSpecializedDescriptor(List argumentTypes, RefTypeOrTPHOrWildcardOrGeneric returnType) {
- return applyDescriptor(new RefType(new JavaClassName(getSpecializedClassName(argumentTypes, returnType)), null));
- }
-
- @Override
- public String getSpecializedSignature(List argumentTypes, RefTypeOrTPHOrWildcardOrGeneric returnType) {
- return applySignature(new RefType(new JavaClassName(getSpecializedClassName(argumentTypes, returnType)), null));
- }
-
- @Override
- public List getArguments(List list) {
- return list
- .stream()
- .limit(Math.max(0, list.size() - 1))
- .collect(Collectors.toList());
- }
-
- @Override
- public RefTypeOrTPHOrWildcardOrGeneric getReturnType(List list) {
- if(list.size() == 0)
- throw new IndexOutOfBoundsException();
- return list.get(list.size() - 1);
- }
-}
diff --git a/src/main/java/de/dhbwstuttgart/bytecode/funN/FunNUtilities.java b/src/main/java/de/dhbwstuttgart/bytecode/funN/FunNUtilities.java
deleted file mode 100644
index 7ae4da4d..00000000
--- a/src/main/java/de/dhbwstuttgart/bytecode/funN/FunNUtilities.java
+++ /dev/null
@@ -1,36 +0,0 @@
-package de.dhbwstuttgart.bytecode.funN;
-
-import de.dhbwstuttgart.bytecode.utilities.CONSTANTS;
-import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
-
-import java.io.File;
-import java.io.FileOutputStream;
-import java.util.List;
-
-public interface FunNUtilities {
-
- byte[] generateSuperBytecode(int numberArguments);
- String getSuperClassName(int numberArguments);
-
- byte[] generateSpecializedBytecode(List argumentTypes, RefTypeOrTPHOrWildcardOrGeneric returnType);
- String getSpecializedClassName(List argumentTypes, RefTypeOrTPHOrWildcardOrGeneric returnType);
- String getSpecializedDescriptor(List argumentTypes, RefTypeOrTPHOrWildcardOrGeneric returnType);
- String getSpecializedSignature(List argumentTypes, RefTypeOrTPHOrWildcardOrGeneric returnType);
-
- List getArguments(List list);
- RefTypeOrTPHOrWildcardOrGeneric getReturnType(List list);
-
- @Deprecated
- public static boolean writeClassFile(String className, byte[] bytecode, File directory) {
- try (FileOutputStream output = new FileOutputStream(new File(directory , className + CONSTANTS.EXTENSIONCLASS))){
- output.write(bytecode);
- output.flush();
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return false;
- }
-
-
-}
diff --git a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/ConstraintsSimplierResult.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/ConstraintsSimplierResult.java
deleted file mode 100644
index 5ed0bd5e..00000000
--- a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/ConstraintsSimplierResult.java
+++ /dev/null
@@ -1,53 +0,0 @@
-/**
- *
- */
-package de.dhbwstuttgart.bytecode.genericsGenerator;
-
-import java.util.List;
-
-import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.NameReplacementResult;
-import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericsGeneratorResult;
-
-/**
- * This class represents the result of the constraints simplifier.
- *
- * @author fayez
- *
- */
-public class ConstraintsSimplierResult {
- private List genericsGenResults;
- private List nameReplacementResults;
- /**
- * @param genericsGenResults
- */
- public ConstraintsSimplierResult(List genericsGenResults,
- List nameReplacementResults) {
- this.genericsGenResults = genericsGenResults;
- this.setNameReplacementResults(nameReplacementResults);
- }
- /**
- * @return the genericsGenResults
- */
- public List getGenericsGenResults() {
- return genericsGenResults;
- }
- /**
- * @param genericsGenResults the genericsGenResults to set
- */
- public void setGenericsGenResults(List genericsGenResults) {
- this.genericsGenResults = genericsGenResults;
- }
- /**
- * @return the nameReplacementResults
- */
- public List getNameReplacementResults() {
- return nameReplacementResults;
- }
- /**
- * @param nameReplacementResults the nameReplacementResults to set
- */
- public void setNameReplacementResults(List nameReplacementResults) {
- this.nameReplacementResults = nameReplacementResults;
- }
-
-}
diff --git a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/ConstraintsSimplifier.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/ConstraintsSimplifier.java
deleted file mode 100644
index b510c71b..00000000
--- a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/ConstraintsSimplifier.java
+++ /dev/null
@@ -1,137 +0,0 @@
-/**
- *
- */
-package de.dhbwstuttgart.bytecode.genericsGenerator;
-
-import java.util.*;
-
-import de.dhbwstuttgart.bytecode.TPHExtractor;
-import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
-import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.*;
-
-/**
- * @author fayez
- *
- */
-public class ConstraintsSimplifier {
- public static ConstraintsSimplierResult simplifyConstraints(final TPHExtractor tphExtractor, final List genericsGeneratorResults, final List tphsClass) {
-
- List allCons = tphExtractor.allCons;
- List equalCons = new ArrayList<>();
-
- /* Step 1 */
- List foundCons = GenericsGeneratorUtility.findConstraintsWithLeftSameLeftSide(allCons);
- if(!foundCons.isEmpty()) {
- List equalCons2 = new ArrayList<>();
- GenericsGeneratorUtility.simplifyConstraintsWithSameLeftSide(foundCons,tphExtractor,tphsClass,equalCons2);
- equalCons.addAll(equalCons2);
- }
-
-// if(!simpleCycles.isEmpty()) {
-// handleSimpleCycles(allCons, simpleCycles,tphExtractor);
-// equalCons.addAll(GenericsGeneratorUtility.getEqualConstraints(allCons));
-// }
-//
- List cycles = findCycles(allCons);
-
- if(!cycles.isEmpty()) {
- handleCycle(genericsGeneratorResults, allCons, cycles,tphExtractor);
- }
- /* The right hand side of equal constraint is the new name in name replacement result */
- List nameReplacementResults = GenericsGeneratorUtility.createNameReplacementResultsFromEqualConstraints(equalCons);
- getEqualsFromNameReplacementResults(genericsGeneratorResults,nameReplacementResults);
- ConstraintsSimplierResult result = new ConstraintsSimplierResult(genericsGeneratorResults, nameReplacementResults);
- return result;
- }
-
- private static void getEqualsFromNameReplacementResults(List genericsGeneratorResults,
- List nameReplacementResults) {
- genericsGeneratorResults.forEach(g->{
- String tph = g.getConstraint().getLeft();
- if (!nameReplacementResults.isEmpty()) {
- collectAllEquals(nameReplacementResults, g, tph);
- }
-
- if (!nameReplacementResults.isEmpty()) {
- collectAllEquals(nameReplacementResults, g);
- }
- });
-
- }
-
- private static void collectAllEquals(List nameReplacementResults, GenericsGeneratorResult g) {
- Set equalsTPHs = g.getEqualsTPHs();
- Set tphsToAdd = getAllEqualTphs(equalsTPHs, nameReplacementResults);
- while (!tphsToAdd.isEmpty()){
- equalsTPHs.addAll(tphsToAdd);
- tphsToAdd = getAllEqualTphs(equalsTPHs, nameReplacementResults);
- }
- }
-
- private static Set getAllEqualTphs(Set equalsTPHs, List nameReplacementResults) {
- Set tphsToAdd = new HashSet<>();
- equalsTPHs.forEach(e->{
- collectAllEquals(nameReplacementResults, tphsToAdd,e);
- });
- return tphsToAdd;
- }
-
- public static void collectAllEquals(List nameReplacementResults, Set tphsToAdd,
- String tph) {
- List toRemoveList = new ArrayList<>();
- Optional nameReplacementResult = nameReplacementResults.stream().filter(n->n.getName().equals(tph)).findFirst();
- nameReplacementResult.ifPresent(toRemove->{
- tphsToAdd.addAll(toRemove.getOldNames());
- toRemoveList.add(toRemove);
- });
- if(!toRemoveList.isEmpty()){
- nameReplacementResults.remove(toRemoveList.get(0));
- toRemoveList.clear();
- }
- }
-
- /**
- * @param nameReplacementResults
- * @param g
- * @param tph
- */
- public static void collectAllEquals(List nameReplacementResults, GenericsGeneratorResult g,
- String tph) {
- List toRemoveList = new ArrayList<>();
- Optional nameReplacementResult = nameReplacementResults.stream().filter(n->n.getName().equals(tph)).findFirst();
- nameReplacementResult.ifPresent(toRemove->{
- g.getEqualsTPHs().addAll(toRemove.getOldNames());
- toRemoveList.add(toRemove);
- });
- if(!toRemoveList.isEmpty()){
- nameReplacementResults.remove(toRemoveList.get(0));
- toRemoveList.clear();
- }
- }
-
- /**
- * @param genericsGeneratorResults
- * @param allCons
- * @param cycles
- * @param tphExtractor
- */
- public static void handleCycle(List genericsGeneratorResults,
- List allCons, List cycles, TPHExtractor tphExtractor) {
- GenericsGeneratorUtility.modifyRelationForConstraintsInCycle(cycles);
- List nameReplacementResults = GenericsGeneratorUtility.substituteTPHSFormCycle(allCons, cycles,tphExtractor);
- GenericsGeneratorUtility.createResults(genericsGeneratorResults,nameReplacementResults);
- GenericsGeneratorUtility.removeEqualConstraints(allCons);
- }
-
- /**
- * @param allCons
- * @return
- */
- public static List findCycles(List allCons) {
- /* find all cycles */
- CyclesFinder cyclesFinder = new CyclesFinder(allCons);
- List cycles = cyclesFinder.findCycles();
- return cycles;
- }
-
-}
diff --git a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/CyclesFinder.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/CyclesFinder.java
deleted file mode 100644
index 9b0867f5..00000000
--- a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/CyclesFinder.java
+++ /dev/null
@@ -1,106 +0,0 @@
-/**
- *
- */
-package de.dhbwstuttgart.bytecode.genericsGenerator;
-
-import java.util.ArrayList;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Optional;
-
-import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
-import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation;
-import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.Cycle;
-
-/**
- * @author fayez
- *
- */
-public class CyclesFinder {
- private final List allCons;
-
- public CyclesFinder(List allCons) {
- this.allCons = allCons;
- }
-
- public List findCycles() {
- List vistedConstraints = new ArrayList<>(allCons.size());
- List cycles = new ArrayList<>();
- for (TPHConstraint constraint : allCons) {
- if(constraint.getRel()==Relation.EQUAL)
- continue;
- if (!vistedConstraints.contains(constraint)) {
- vistedConstraints.add(constraint);
- checkConstraint(constraint, vistedConstraints, cycles);
- }
- }
- return cycles;
- }
-
- private void checkConstraint(TPHConstraint constraint, List vistedConstraints,
- List cycles) {
- List tphsInRelation = new LinkedList<>();
- List maybeCycle = new ArrayList<>();
- maybeCycle.add(constraint);
- tphsInRelation.add(constraint.getLeft());
- tphsInRelation.add(constraint.getRight());
- Optional nextConstraint = getConstraintInRelation(tphsInRelation, maybeCycle);
- while (nextConstraint.isPresent()) {
- if(containsInLongCycle(nextConstraint.get(), cycles)) {
- break;
- }
- if (isCycle(tphsInRelation)) {
- addAllToVisitedConstraints(vistedConstraints, maybeCycle);
- Cycle cycle = new Cycle(maybeCycle);
- cycles.add(cycle);
- return;
- }
- nextConstraint = getConstraintInRelation(tphsInRelation, maybeCycle);
- }
-
-// addAllToVisitedConstraints(vistedConstraints, maybeCycle);
- }
-
- private boolean containsInLongCycle(TPHConstraint c, List cycles) {
- for(Cycle cycle : cycles) {
- if(cycle.containConstraint(c))
- return true;
- }
- return false;
- }
-
- private void addAllToVisitedConstraints(List vistedConstraints, List maybeCycle) {
- for (TPHConstraint con : maybeCycle) {
- if (!vistedConstraints.contains(con))
- vistedConstraints.add(con);
- }
- }
-
- private Optional getConstraintInRelation(List tphsInRelation,
- List maybeCycle) {
- TPHConstraint constraint = getConstraintByLeftSide(tphsInRelation.get(tphsInRelation.size()-1),maybeCycle);
- Optional nextConstraint = Optional.ofNullable(constraint);
- if(nextConstraint.isPresent()) {
- maybeCycle.add(constraint);
- tphsInRelation.add(constraint.getRight());
- }
- return nextConstraint;
- }
-
- private TPHConstraint getConstraintByLeftSide(String left, List maybeCycle) {
- for(TPHConstraint constraint : allCons) {
- if(constraint.getRel()==Relation.EQUAL)
- continue;
- if(maybeCycle.contains(constraint))
- continue;
- if(constraint.getLeft().equals(left))
- return constraint;
- }
- return null;
- }
-
- private boolean isCycle(List tphsInRelation) {
- return tphsInRelation.get(0).equals(tphsInRelation.get(tphsInRelation.size() - 1));
- }
-
-}
diff --git a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GeneratedGenericsFinder.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GeneratedGenericsFinder.java
deleted file mode 100644
index e25958d1..00000000
--- a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GeneratedGenericsFinder.java
+++ /dev/null
@@ -1,732 +0,0 @@
-/**
- *
- */
-package de.dhbwstuttgart.bytecode.genericsGenerator;
-
-import java.util.*;
-
-import de.dhbwstuttgart.bytecode.TPHExtractor;
-import de.dhbwstuttgart.bytecode.constraint.ExtendsConstraint;
-import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
-import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.*;
-import de.dhbwstuttgart.bytecode.insertGenerics.ClassConstraint;
-import de.dhbwstuttgart.bytecode.insertGenerics.FamilyOfGeneratedGenerics;
-import de.dhbwstuttgart.bytecode.utilities.MethodAndTPH;
-import de.dhbwstuttgart.bytecode.utilities.MethodUtility;
-import de.dhbwstuttgart.bytecode.utilities.Resolver;
-import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal;
-import de.dhbwstuttgart.parser.scope.JavaClassName;
-import de.dhbwstuttgart.syntaxtree.ASTVisitor;
-import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
-import de.dhbwstuttgart.syntaxtree.Constructor;
-import de.dhbwstuttgart.syntaxtree.Field;
-import de.dhbwstuttgart.syntaxtree.FormalParameter;
-import de.dhbwstuttgart.syntaxtree.GenericDeclarationList;
-import de.dhbwstuttgart.syntaxtree.GenericTypeVar;
-import de.dhbwstuttgart.syntaxtree.Method;
-import de.dhbwstuttgart.syntaxtree.ParameterList;
-import de.dhbwstuttgart.syntaxtree.SourceFile;
-import de.dhbwstuttgart.syntaxtree.statement.ArgumentList;
-import de.dhbwstuttgart.syntaxtree.statement.Assign;
-import de.dhbwstuttgart.syntaxtree.statement.AssignToField;
-import de.dhbwstuttgart.syntaxtree.statement.BinaryExpr;
-import de.dhbwstuttgart.syntaxtree.statement.Block;
-import de.dhbwstuttgart.syntaxtree.statement.CastExpr;
-import de.dhbwstuttgart.syntaxtree.statement.DoStmt;
-import de.dhbwstuttgart.syntaxtree.statement.EmptyStmt;
-import de.dhbwstuttgart.syntaxtree.statement.ExpressionReceiver;
-import de.dhbwstuttgart.syntaxtree.statement.FieldVar;
-import de.dhbwstuttgart.syntaxtree.statement.ForStmt;
-import de.dhbwstuttgart.syntaxtree.statement.IfStmt;
-import de.dhbwstuttgart.syntaxtree.statement.InstanceOf;
-import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression;
-import de.dhbwstuttgart.syntaxtree.statement.Literal;
-import de.dhbwstuttgart.syntaxtree.statement.LocalVar;
-import de.dhbwstuttgart.syntaxtree.statement.LocalVarDecl;
-import de.dhbwstuttgart.syntaxtree.statement.MethodCall;
-import de.dhbwstuttgart.syntaxtree.statement.NewArray;
-import de.dhbwstuttgart.syntaxtree.statement.NewClass;
-import de.dhbwstuttgart.syntaxtree.statement.Return;
-import de.dhbwstuttgart.syntaxtree.statement.ReturnVoid;
-import de.dhbwstuttgart.syntaxtree.statement.StaticClassName;
-import de.dhbwstuttgart.syntaxtree.statement.Super;
-import de.dhbwstuttgart.syntaxtree.statement.SuperCall;
-import de.dhbwstuttgart.syntaxtree.statement.This;
-import de.dhbwstuttgart.syntaxtree.statement.UnaryExpr;
-import de.dhbwstuttgart.syntaxtree.statement.WhileStmt;
-import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType;
-import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
-import de.dhbwstuttgart.syntaxtree.type.RefType;
-import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType;
-import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
-import de.dhbwstuttgart.typeinference.result.ResultSet;
-
-/**
- * @author fayez
- *
- */
-public class GeneratedGenericsFinder implements ASTVisitor {
- private final TPHExtractor tphExtractor = new TPHExtractor();
- private Collection listOfResultSets;
- private SourceFile sf;
- private List tphsClass;
- private GenericGenratorResultForSourceFile generatedGenericsForSF;//Ergebnis des GGenerics
- private ResultSet resultSet;
- private final List methodNameAndParamsT = new ArrayList<>();
- private FamilyOfGeneratedGenerics fogg;
-
- private String pkgName;
- private JavaClassName className;
- private Resolver resolver;
-
-
- /**
- * @param sf
- * @param listOfResultSets
- */
- public GeneratedGenericsFinder(SourceFile sf, Collection listOfResultSets) {
- this.sf = sf;
- this.listOfResultSets = listOfResultSets;
- }
-
- public GenericGenratorResultForSourceFile findGeneratedGenerics() {
- sf.accept(this);
- return generatedGenericsForSF;
- }
-
- /**
- * returns the family of Generated Generics
- * insbesondere fuer Testzwecke
- */
- public FamilyOfGeneratedGenerics getFogg() {
- return fogg;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * de.dhbwstuttgart.syntaxtree.ASTVisitor#visit(de.dhbwstuttgart.syntaxtree.
- * SourceFile)
- */
- @Override
- public void visit(SourceFile sourceFile) {
- pkgName = sf.getPkgName();
- generatedGenericsForSF = new GenericGenratorResultForSourceFile(pkgName);
- for (ClassOrInterface cl : sourceFile.getClasses()) {
- cl.accept(this);
- }
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * de.dhbwstuttgart.syntaxtree.ASTVisitor#visit(de.dhbwstuttgart.syntaxtree.
- * ClassOrInterface)
- */
- @Override
- public void visit(ClassOrInterface classOrInterface) {
- className = classOrInterface.getClassName();
- List listOfResultSetsList = new ArrayList<>(listOfResultSets);
-
- boolean isVisited = false;
-
- ConstraintsSimplierResult simplifiedConstraints = null;
- GenericsGeneratorResultForClass ggResult = null;
- GenericsGeneratorResultForClass ggResultAlternative = null;
-
- for (int i = 0; i < listOfResultSetsList.size(); i++) {
- resultSet = listOfResultSetsList.get(i);
- tphExtractor.setResultSet(resultSet);
- resolver = new Resolver(resultSet);
- classOrInterface.accept(tphExtractor);
- //PL 2020-10-16: Ab hier GGenerics implementieren durch Ali
- //Rueckgabe an generatedGenericsForSF
- fogg = new FamilyOfGeneratedGenerics(tphExtractor, resultSet);
-
- System.out.println("fogg.allConstraints: "+ fogg.allConstraints);
- System.out.println("fogg.posOfTPHs: "+ fogg.posOfTPHs);
- System.out.println("fogg.classConstraints: "+ fogg.classConstraints);
- System.out.println("fogg.methodConstraintsWithPosition: "+ fogg.methodConstraintsWithPosition);
- System.out.println(fogg);
-
- ///*
- //Fayez Ansatz Anfang
- tphsClass = tphExtractor.tphsClass;
- //PL 2020-01-15
- //Es muss ggResult aus fogg gebildet werden
- simplifiedConstraints = GenericsGenerator.simplifyConstraints(tphExtractor, tphsClass);
- if(!isVisited) {
- ggResult = GenericsGenerator.generateConstraints(className, tphExtractor, tphsClass,simplifiedConstraints);
- isVisited = true;
- }
-
- for(Method m : classOrInterface.getMethods()) {
- addMethodConstraints(simplifiedConstraints, ggResult, m);
-
- }
-
-
- if(ggResult != null) { //Hinzufuegen von Fayez ggResult
- generatedGenericsForSF.addGenericGeneratorResultClass(ggResult);
- }
- // Fayez Ansatz Ende
- //*/
-
- //Ali Ansatz Anfang
- List listOfClassCons = new ArrayList<>();
- for(TPHConstraint clCons: fogg.classConstraints) {
-// ExtendsConstraint ec = new ExtendsConstraint(clCons.getLeft(), clCons.getRight());
- GenericsGeneratorResult ggR = new GenericsGeneratorResult(clCons, new HashSet<>());
- listOfClassCons.add(ggR);
- }
-
- GenericGeneratorResultsForAllMethods ggRfaM = null;
- List listOfMethAndCons = new ArrayList<>();
- for(String methID: fogg.methodConstraintsWithPosition.keySet()) {
- List listOfGGR = new ArrayList<>();
- for(TPHConstraint methCons: fogg.methodConstraintsWithPosition.get(methID)) {
-// ExtendsConstraint ec = new ExtendsConstraint(methCons.getLeft(),methCons.getRight());
- GenericsGeneratorResult ggR = new GenericsGeneratorResult(methCons, new HashSet<>());
- listOfGGR.add(ggR);
- }
- MethodAndConstraints mac = new MethodAndConstraints(methID, listOfGGR);
- listOfMethAndCons.add(mac);
- }
- ggRfaM = new GenericGeneratorResultsForAllMethods(listOfMethAndCons);
- ggResultAlternative = new GenericsGeneratorResultForClass(className, listOfClassCons, ggRfaM);
-
- if(ggResultAlternative != null) {//hinzufuegen von Alis ggResult
- //generatedGenericsForSF.addGenericGeneratorResultClass(ggResultAlternative);
- System.out.println(generatedGenericsForSF);
- }
- System.out.println(ggResultAlternative);
- //Ali Ansatz Ende
-
- }
-
- }
-
- /**
- * @param simplifiedConstraints
- * @param ggResult
- * @param m
- */
- public void addMethodConstraints(ConstraintsSimplierResult simplifiedConstraints,
- GenericsGeneratorResultForClass ggResult, Method m) {
- String id = MethodUtility.createID(resolver, m);
- boolean isInGGResult = (ggResult != null) && ggResult.contains(id);
- if(methodNameAndParamsT.contains(id) || isInGGResult)
- return;
- methodNameAndParamsT.add(id);
- Optional methodAndTPH = GenericsGeneratorUtility.getMethodAndTphs(tphExtractor.ListOfMethodsAndTph,id);
- methodAndTPH.ifPresent(mt->{
- MethodAndConstraints methodConstraints = GenericsGenerator.generateConstraintsForMethod(tphExtractor.allCons, mt , simplifiedConstraints, tphsClass);
- ggResult.getMethodsAndTheirConstraints().getMethodsAndConstraints().add(methodConstraints);
- });
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * de.dhbwstuttgart.syntaxtree.ASTVisitor#visit(de.dhbwstuttgart.syntaxtree.
- * Method)
- */
- @Override
- public void visit(Method method) {
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * de.dhbwstuttgart.syntaxtree.ASTVisitor#visit(de.dhbwstuttgart.syntaxtree.
- * ParameterList)
- */
- @Override
- public void visit(ParameterList formalParameters) {
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * de.dhbwstuttgart.syntaxtree.ASTVisitor#visit(de.dhbwstuttgart.syntaxtree.
- * Constructor)
- */
- @Override
- public void visit(Constructor field) {
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see de.dhbwstuttgart.syntaxtree.StatementVisitor#visit(de.dhbwstuttgart.
- * syntaxtree.statement.ArgumentList)
- */
- @Override
- public void visit(ArgumentList argumentList) {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see de.dhbwstuttgart.syntaxtree.StatementVisitor#visit(de.dhbwstuttgart.
- * syntaxtree.statement.LambdaExpression)
- */
- @Override
- public void visit(LambdaExpression lambdaExpression) {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see de.dhbwstuttgart.syntaxtree.StatementVisitor#visit(de.dhbwstuttgart.
- * syntaxtree.statement.Assign)
- */
- @Override
- public void visit(Assign assign) {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see de.dhbwstuttgart.syntaxtree.StatementVisitor#visit(de.dhbwstuttgart.
- * syntaxtree.statement.BinaryExpr)
- */
- @Override
- public void visit(BinaryExpr binary) {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see de.dhbwstuttgart.syntaxtree.StatementVisitor#visit(de.dhbwstuttgart.
- * syntaxtree.statement.Block)
- */
- @Override
- public void visit(Block block) {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see de.dhbwstuttgart.syntaxtree.StatementVisitor#visit(de.dhbwstuttgart.
- * syntaxtree.statement.CastExpr)
- */
- @Override
- public void visit(CastExpr castExpr) {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see de.dhbwstuttgart.syntaxtree.StatementVisitor#visit(de.dhbwstuttgart.
- * syntaxtree.statement.EmptyStmt)
- */
- @Override
- public void visit(EmptyStmt emptyStmt) {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see de.dhbwstuttgart.syntaxtree.StatementVisitor#visit(de.dhbwstuttgart.
- * syntaxtree.statement.FieldVar)
- */
- @Override
- public void visit(FieldVar fieldVar) {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see de.dhbwstuttgart.syntaxtree.StatementVisitor#visit(de.dhbwstuttgart.
- * syntaxtree.statement.ForStmt)
- */
- @Override
- public void visit(ForStmt forStmt) {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see de.dhbwstuttgart.syntaxtree.StatementVisitor#visit(de.dhbwstuttgart.
- * syntaxtree.statement.IfStmt)
- */
- @Override
- public void visit(IfStmt ifStmt) {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see de.dhbwstuttgart.syntaxtree.StatementVisitor#visit(de.dhbwstuttgart.
- * syntaxtree.statement.InstanceOf)
- */
- @Override
- public void visit(InstanceOf instanceOf) {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see de.dhbwstuttgart.syntaxtree.StatementVisitor#visit(de.dhbwstuttgart.
- * syntaxtree.statement.LocalVar)
- */
- @Override
- public void visit(LocalVar localVar) {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see de.dhbwstuttgart.syntaxtree.StatementVisitor#visit(de.dhbwstuttgart.
- * syntaxtree.statement.LocalVarDecl)
- */
- @Override
- public void visit(LocalVarDecl localVarDecl) {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see de.dhbwstuttgart.syntaxtree.StatementVisitor#visit(de.dhbwstuttgart.
- * syntaxtree.statement.MethodCall)
- */
- @Override
- public void visit(MethodCall methodCall) {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see de.dhbwstuttgart.syntaxtree.StatementVisitor#visit(de.dhbwstuttgart.
- * syntaxtree.statement.NewClass)
- */
- @Override
- public void visit(NewClass methodCall) {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see de.dhbwstuttgart.syntaxtree.StatementVisitor#visit(de.dhbwstuttgart.
- * syntaxtree.statement.NewArray)
- */
- @Override
- public void visit(NewArray newArray) {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see de.dhbwstuttgart.syntaxtree.StatementVisitor#visit(de.dhbwstuttgart.
- * syntaxtree.statement.Return)
- */
- @Override
- public void visit(Return aReturn) {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see de.dhbwstuttgart.syntaxtree.StatementVisitor#visit(de.dhbwstuttgart.
- * syntaxtree.statement.ReturnVoid)
- */
- @Override
- public void visit(ReturnVoid aReturn) {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see de.dhbwstuttgart.syntaxtree.StatementVisitor#visit(de.dhbwstuttgart.
- * syntaxtree.statement.StaticClassName)
- */
- @Override
- public void visit(StaticClassName staticClassName) {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see de.dhbwstuttgart.syntaxtree.StatementVisitor#visit(de.dhbwstuttgart.
- * syntaxtree.statement.Super)
- */
- @Override
- public void visit(Super aSuper) {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see de.dhbwstuttgart.syntaxtree.StatementVisitor#visit(de.dhbwstuttgart.
- * syntaxtree.statement.This)
- */
- @Override
- public void visit(This aThis) {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see de.dhbwstuttgart.syntaxtree.StatementVisitor#visit(de.dhbwstuttgart.
- * syntaxtree.statement.WhileStmt)
- */
- @Override
- public void visit(WhileStmt whileStmt) {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see de.dhbwstuttgart.syntaxtree.StatementVisitor#visit(de.dhbwstuttgart.
- * syntaxtree.statement.DoStmt)
- */
- @Override
- public void visit(DoStmt whileStmt) {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see de.dhbwstuttgart.syntaxtree.StatementVisitor#visit(de.dhbwstuttgart.
- * syntaxtree.statement.AssignToField)
- */
- @Override
- public void visit(AssignToField assignLeftSide) {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * de.dhbwstuttgart.syntaxtree.StatementVisitor#visit(de.dhbwstuttgart.parser.
- * SyntaxTreeGenerator.AssignToLocal)
- */
- @Override
- public void visit(AssignToLocal assignLeftSide) {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see de.dhbwstuttgart.syntaxtree.StatementVisitor#visit(de.dhbwstuttgart.
- * syntaxtree.statement.SuperCall)
- */
- @Override
- public void visit(SuperCall superCall) {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see de.dhbwstuttgart.syntaxtree.StatementVisitor#visit(de.dhbwstuttgart.
- * syntaxtree.statement.ExpressionReceiver)
- */
- @Override
- public void visit(ExpressionReceiver expressionReceiver) {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see de.dhbwstuttgart.syntaxtree.StatementVisitor#visit(de.dhbwstuttgart.
- * syntaxtree.statement.UnaryExpr)
- */
- @Override
- public void visit(UnaryExpr unaryExpr) {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see de.dhbwstuttgart.syntaxtree.StatementVisitor#visit(de.dhbwstuttgart.
- * syntaxtree.statement.Literal)
- */
- @Override
- public void visit(Literal literal) {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * de.dhbwstuttgart.syntaxtree.ASTVisitor#visit(de.dhbwstuttgart.syntaxtree.
- * GenericTypeVar)
- */
- @Override
- public void visit(GenericTypeVar genericTypeVar) {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * de.dhbwstuttgart.syntaxtree.ASTVisitor#visit(de.dhbwstuttgart.syntaxtree.
- * FormalParameter)
- */
- @Override
- public void visit(FormalParameter formalParameter) {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * de.dhbwstuttgart.syntaxtree.ASTVisitor#visit(de.dhbwstuttgart.syntaxtree.
- * GenericDeclarationList)
- */
- @Override
- public void visit(GenericDeclarationList genericTypeVars) {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * de.dhbwstuttgart.syntaxtree.ASTVisitor#visit(de.dhbwstuttgart.syntaxtree.
- * Field)
- */
- @Override
- public void visit(Field field) {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * de.dhbwstuttgart.syntaxtree.ASTVisitor#visit(de.dhbwstuttgart.syntaxtree.type
- * .RefType)
- */
- @Override
- public void visit(RefType refType) {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * de.dhbwstuttgart.syntaxtree.ASTVisitor#visit(de.dhbwstuttgart.syntaxtree.type
- * .SuperWildcardType)
- */
- @Override
- public void visit(SuperWildcardType superWildcardType) {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * de.dhbwstuttgart.syntaxtree.ASTVisitor#visit(de.dhbwstuttgart.syntaxtree.type
- * .TypePlaceholder)
- */
- @Override
- public void visit(TypePlaceholder typePlaceholder) {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * de.dhbwstuttgart.syntaxtree.ASTVisitor#visit(de.dhbwstuttgart.syntaxtree.type
- * .ExtendsWildcardType)
- */
- @Override
- public void visit(ExtendsWildcardType extendsWildcardType) {
- // TODO Auto-generated method stub
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * de.dhbwstuttgart.syntaxtree.ASTVisitor#visit(de.dhbwstuttgart.syntaxtree.type
- * .GenericRefType)
- */
- @Override
- public void visit(GenericRefType genericRefType) {
- // TODO Auto-generated method stub
-
- }
-
-}
diff --git a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GenericsGenerator.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GenericsGenerator.java
deleted file mode 100644
index 8e5bd481..00000000
--- a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GenericsGenerator.java
+++ /dev/null
@@ -1,256 +0,0 @@
-/**
- *
- */
-package de.dhbwstuttgart.bytecode.genericsGenerator;
-
-import java.util.ArrayList;
-import java.util.HashSet;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Optional;
-import java.util.Set;
-import java.util.stream.Collectors;
-
-import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.*;
-import de.dhbwstuttgart.parser.scope.JavaClassName;
-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.utilities.MethodAndTPH;
-
-/**
- * @author fayez
- *
- */
-public class GenericsGenerator {
- /*TODO: When generating generics for a class if we have the following:
- tphClass < tphMeth1 < tphMeth2
- then we have to convert tphMeth1, tphMeth2 to tph class and generate the following
- class constraints: tphClass < tphMeth1, tphMeth1 < tphMeth2, tphMeth2 < Object
- */
-
- public static GenericsGeneratorResultForClass generateConstraints(final JavaClassName className, final TPHExtractor tphExtractor,
- final List tphsClass, final ConstraintsSimplierResult simplifiedConstraints) {
-
- List classConstraints = generateConstraintsForClass(tphExtractor,
- simplifiedConstraints, tphsClass);
-// GenericGeneratorResultsForAllMethods methodsAndConstraints = generateConstraintsForAllMethods(tphExtractor,
-// simplifiedConstraints, tphsClass);
- GenericGeneratorResultsForAllMethods methodsAndConstraints = new GenericGeneratorResultsForAllMethods(new ArrayList<>());
-
- return new GenericsGeneratorResultForClass(className, classConstraints, methodsAndConstraints);
- }
-
-
- /**
- * @param tphExtractor
- * @param tphsClass
- * @return
- */
- public static ConstraintsSimplierResult simplifyConstraints(final TPHExtractor tphExtractor,
- final List tphsClass) {
- final List genericsGeneratorResults = new ArrayList<>();
-
- ConstraintsSimplierResult simplifiedConstraints = ConstraintsSimplifier.simplifyConstraints(tphExtractor,
- genericsGeneratorResults, tphsClass);
- return simplifiedConstraints;
- }
-
-
- private static List generateConstraintsForClass(final TPHExtractor tphExtractor,
- final ConstraintsSimplierResult simplifiedConstraints, final List tphsClass) {
- final List constraints = new ArrayList<>();
-
- if (tphsClass.isEmpty())
- return constraints;
-
- final List allCons = tphExtractor.allCons;
-
- final Set visitedTPHs = new HashSet<>();
-
- final List methodTPHs = tphExtractor.ListOfMethodsAndTph.stream().map(m -> m.getLocalTphs())
- .flatMap(l -> l.stream()).collect(Collectors.toList());
-
- createConstraintsForClassTphs(simplifiedConstraints, tphsClass, constraints, allCons, visitedTPHs, methodTPHs);
-
- GenericsGeneratorUtility.addTPHsToClassTPHs(constraints, tphsClass);
- GenericsGeneratorUtility.removeClassTPHsFromMethodTPHs(tphsClass, tphExtractor);
-
- generateGGConstraintsForTPH(tphsClass, constraints, simplifiedConstraints);
-
- return constraints;
- }
-
- private static void createConstraintsForClassTphs(ConstraintsSimplierResult simplifiedConstraints, List tphsClass, List constraints, List allCons, Set visitedTPHs, List methodTPHs) {
- List classAndMethodTphs = new ArrayList<>(tphsClass);
- classAndMethodTphs.addAll(methodTPHs);
-
- for (String tph : tphsClass) {
-
- if (visitedTPHs.contains(tph))
- continue;
-
- final LinkedList tphsInRel = GenericsGeneratorUtility
- .createLinkedListForTPHsInRelationClass(allCons, tphsClass, methodTPHs, visitedTPHs, tph);
-
- generateConstraintsForClassFromList(tphsInRel,simplifiedConstraints,classAndMethodTphs,constraints);
- }
- }
-
- private static void generateConstraintsForClassFromList(LinkedList tphsInRel, ConstraintsSimplierResult simplifiedConstraints, List classAndMethodTphs, List constraints) {
-
- if(tphsInRel.isEmpty())
- return;
-
- List resultConstraints = new ArrayList<>();
- String subType = tphsInRel.getFirst();
- String superType = GenericsGeneratorUtility.getNextClassTph(classAndMethodTphs, tphsInRel);
-
- int idxOfSuper = tphsInRel.indexOf(superType);
- int size = tphsInRel.size();
- while (size > 2 && idxOfSuper < size-1){
- GenericsGeneratorResult genericsGeneratorResult = getGenericsGeneratorResultForClass(simplifiedConstraints, subType, superType);
- constraints.add(genericsGeneratorResult);
-
- tphsInRel = new LinkedList<>(tphsInRel.subList(idxOfSuper, size));
- subType = tphsInRel.getFirst();
- superType = GenericsGeneratorUtility.getNextClassTph(classAndMethodTphs, tphsInRel);
-
- idxOfSuper = tphsInRel.indexOf(superType);
- size = tphsInRel.size();
- }
- GenericsGeneratorResult genericsGeneratorResult = getGenericsGeneratorResultForClass(simplifiedConstraints, subType, superType);
- constraints.add(genericsGeneratorResult);
- }
-
- private static GenericsGeneratorResult getGenericsGeneratorResultForClass(ConstraintsSimplierResult simplifiedConstraints, String subType, String superType) {
- TPHConstraint constraint = new ExtendsConstraint(subType, superType);
-
- Set equalSet = GenericsGeneratorUtility
- .createEqualSet(simplifiedConstraints.getNameReplacementResults(), subType);
-
- return new GenericsGeneratorResult(constraint, equalSet);
- }
- /* TODO Remove this methoda*/
- private static GenericsGeneratorResult generateGGResultForClass(LinkedList tphsInRel,
- ConstraintsSimplierResult simplifiedConstraints, List tphsClass) {
- String subType = tphsInRel.getFirst();
-
- String superType = GenericsGeneratorUtility.getNextClassTph(tphsClass, tphsInRel);
-
- TPHConstraint constraint = new ExtendsConstraint(subType, superType);
-
- Set equalSet = GenericsGeneratorUtility
- .createEqualSet(simplifiedConstraints.getNameReplacementResults(), subType);
-
- return new GenericsGeneratorResult(constraint, equalSet);
- }
-
- private static GenericGeneratorResultsForAllMethods generateConstraintsForAllMethods(
- final TPHExtractor tphExtractor, ConstraintsSimplierResult simplifiedConstraints, List tphsClass) {
-
- final List methodsAndConstraints = new ArrayList<>();
- final GenericGeneratorResultsForAllMethods results = new GenericGeneratorResultsForAllMethods(
- methodsAndConstraints);
- tphExtractor.ListOfMethodsAndTph.forEach(m -> {
- MethodAndConstraints methAndCons = generateConstraintsForMethod(tphExtractor.allCons, m,
- simplifiedConstraints, tphsClass);
- methodsAndConstraints.add(methAndCons);
- });
-
- return results;
- }
-
- public static MethodAndConstraints generateConstraintsForMethod(final List allCons,
- final MethodAndTPH m, final ConstraintsSimplierResult simplifiedConstraints, List tphsClass) {
- final List localTphs = m.getLocalTphs();
-
- List constraints = new ArrayList<>();
- MethodAndConstraints result = new MethodAndConstraints(m.getId(), constraints);
-
- if (localTphs.isEmpty())
- return result;
-
- if (localTphs.size() == 1 && tphsClass.isEmpty()) {
- generateGGConstraintsForTPH(localTphs, constraints, simplifiedConstraints);
- return result;
- }
-
- final Set visitedTPHs = new HashSet<>();
-
- for (String tph : localTphs) {
-
- if (visitedTPHs.contains(tph))
- continue;
-
- final LinkedList tphsInRel = GenericsGeneratorUtility.createLinkedListForTPHsInRelation(allCons,
- localTphs, tphsClass, visitedTPHs, tph);
- if (!tphsInRel.isEmpty()) {
- GenericsGeneratorUtility.removeNotLocalTphs(tphsInRel, localTphs, tphsClass);
- if (!GenericsGeneratorUtility.isInResult(tphsInRel.getFirst(), constraints)) {
- GenericsGeneratorResult constraint = generateGGResult(tphsInRel, simplifiedConstraints);
- constraints.add(constraint);
- }
- }
- }
-
- generateGGConstraintsForTPH(localTphs, constraints, simplifiedConstraints);
- return result;
- }
-
- private static void generateGGConstraintsForTPH(List localTphs,
- final List constraints, final ConstraintsSimplierResult simplifiedConstraints) {
-
- localTphs.forEach(tph -> {
- boolean isInSimplifiedConstraints = GenericsGeneratorUtility.isTPHInGenericGeneratorResult(simplifiedConstraints.getGenericsGenResults(), tph);
- if (isInSimplifiedConstraints) {
- GenericsGeneratorResult ggResult = GenericsGeneratorUtility.getGenericGeneratorResult(simplifiedConstraints.getGenericsGenResults(), tph).get();
-// GenericsGeneratorUtility.removeGenericGeneratorResult(simplifiedConstraints.getGenericsGenResults(),ggResult);
- constraints.add(ggResult);
- } else {
- boolean isInConstraints = GenericsGeneratorUtility.isTPHInGenericGeneratorResult(constraints, tph);
-
- if (!isInConstraints) {
- GenericsGeneratorResult ggResult = generateGGResult(tph, simplifiedConstraints.getNameReplacementResults());
- constraints.add(ggResult);
- }
- }
- });
-
- }
-
- private static GenericsGeneratorResult generateGGResult(String tph,
- List nameReplacementResults) {
- String superType = Type.getInternalName(Object.class);
- TPHConstraint constraint = new ExtendsConstraint(tph, superType);
- Set equalSet = GenericsGeneratorUtility.createEqualSet(nameReplacementResults, tph);
-
- return new GenericsGeneratorResult(constraint, equalSet);
- }
-
- private static GenericsGeneratorResult generateGGResult(final LinkedList tphsInRel,
- final ConstraintsSimplierResult simplifiedConstraints) {
-
- String subType = tphsInRel.getFirst();
-
- if (tphsInRel.size() == 1) {
- Optional constraintFromSimplifiedCon = simplifiedConstraints
- .getGenericsGenResults().stream().filter(g -> g.getConstraint().getLeft().equals(subType))
- .findFirst();
- if (constraintFromSimplifiedCon.isPresent())
- return constraintFromSimplifiedCon.get();
- }
-
- String superType = tphsInRel.size() > 1 ? tphsInRel.getLast() : Type.getInternalName(Object.class);
-
- TPHConstraint constraint = new ExtendsConstraint(subType, superType);
-
- Set equalSet = GenericsGeneratorUtility
- .createEqualSet(simplifiedConstraints.getNameReplacementResults(), subType);
-
- return new GenericsGeneratorResult(constraint, equalSet);
- }
-
-}
diff --git a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GenericsGeneratorUtility.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GenericsGeneratorUtility.java
deleted file mode 100644
index f909734f..00000000
--- a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GenericsGeneratorUtility.java
+++ /dev/null
@@ -1,571 +0,0 @@
-/**
- *
- */
-package de.dhbwstuttgart.bytecode.genericsGenerator;
-
-import java.util.ArrayList;
-import java.util.HashSet;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Optional;
-import java.util.Set;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
-
-import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.*;
-import org.objectweb.asm.Type;
-
-import de.dhbwstuttgart.bytecode.TPHExtractor;
-import de.dhbwstuttgart.bytecode.constraint.EqualConstraint;
-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.NameReplacer;
-
-/**
- * @author fayez
- *
- */
-public class GenericsGeneratorUtility {
- /**
- * Returns a list of type placeholders names of a specified method
- *
- * @param methodID The id of the method ReturnTypeMethodName(ParameterTypes)
- * @param listOfMethodsAndTph List of all methods
- * @return a list of type placehoders names
- */
- public static List getMethodTPHS(String methodID, ArrayList listOfMethodsAndTph) {
- return listOfMethodsAndTph.stream().filter(mt->mt.getId().equals(methodID)).findAny().get().getTphs();
- }
-
- /**
- * Returns a reverse constraint of a specified constraint which is given by its left and right
- * side
- *
- * @param allCons List of all constraints
- * @param left The left side of the given constraint
- * @param right The right side of the given constraint
- * @return An Optional object which contains the reverse constraint if it is exist
- */
- public static Optional getReverseConstraint(List allCons, String left, String right) {
- return allCons.stream().filter(c->{
- return c.getRight().equals(left) && c.getLeft().equals(right);
- }).findAny();
- }
-
- /**
- * Substitutes the old name by the new name in all constraints
- *
- * @param allCons List of all constraints
- * @param oldName The name to be replaced
- * @param newName The new name
- */
- public static void substituteTPH(List allCons, String oldName, String newName) {
- allCons.stream()
- .filter(c->c.getRel()==Relation.EXTENDS)
- .forEach(c->{
- if(c.getLeft().equals(oldName))
- c.setLeft(newName);
- if(c.getRight().equals(oldName))
- c.setRight(newName);
- });
-
- }
-
- public static List findConstraintsWithLeftSameLeftSide(List allCons) {
- // finder looks for constraints that have the same left hand side
- // and put them in a list
- ConstraintsFinder finder = new ConstraintsFinder(allCons);
- List foundCons = finder.findConstraints();
- return foundCons;
- }
-
- public static void simplifyConstraintsWithSameLeftSide(List consWithSameLeftSide,
- List allCons, List methodTPHS) {
- List eqCons = new ArrayList<>();
-
- for (ConstraintsWithSameLeftSide list : consWithSameLeftSide) {
- NameReplacementResult replRes = modifyNames(allCons, methodTPHS, list);
- createEqualConstraintsForNames(replRes.getName(), replRes.getOldNames(),eqCons);
-
- for (TPHConstraint c : allCons) {
- if (c.getRel() == Relation.EQUAL && replRes.getName().equals(c.getRight())) {
- eqCons.add(c);
- }
- }
- updateEqualCons(replRes, eqCons);
-
- TPHConstraint c = list.getConstraints().get(0);
- allCons.removeAll(list.getConstraints());
- allCons.add(c);
- }
-
- }
-
- public static void simplifyConstraintsWithSameLeftSide(List consWithSameLeftSide,
- TPHExtractor tphExtractor, List tphsClass, List equalCons) {
- List allCons = tphExtractor.allCons;
-
- for (ConstraintsWithSameLeftSide list : consWithSameLeftSide) {
- if(!stillWithSameLeftSide(list))
- continue;
- NameReplacementResult replRes = modifyNames(allCons, list, tphExtractor.ListOfMethodsAndTph,tphsClass);
- createEqualConstraintsForNames(replRes.getName(), replRes.getOldNames(),equalCons);
-
- for (TPHConstraint c : allCons) {
- if (c.getRel() == Relation.EQUAL && replRes.getName().equals(c.getRight())) {
- equalCons.add(c);
- }
- }
- updateEqualCons(replRes, equalCons);
-
- TPHConstraint c = list.getConstraints().get(0);
- allCons.removeAll(list.getConstraints());
- removeConstraintsWithSameLeftAndRightSide(allCons);
- allCons.add(c);
- removeDuplicate(allCons);
- }
- consWithSameLeftSide.clear();
- consWithSameLeftSide = findConstraintsWithLeftSameLeftSide(allCons);
- if(!consWithSameLeftSide.isEmpty())
- simplifyConstraintsWithSameLeftSide(consWithSameLeftSide,tphExtractor,tphsClass,equalCons);
-
- }
-
- private static void removeDuplicate(List allCons) {
- List