forked from JavaTX/JavaCompilerCore
Merge
This commit is contained in:
commit
cecec91f74
2
.gitignore
vendored
2
.gitignore
vendored
@ -15,6 +15,8 @@ bin
|
|||||||
.classpath
|
.classpath
|
||||||
*.iml
|
*.iml
|
||||||
.idea/
|
.idea/
|
||||||
|
/target/
|
||||||
.DS_Store
|
.DS_Store
|
||||||
.project
|
.project
|
||||||
.settings/
|
.settings/
|
||||||
|
|
||||||
|
BIN
src/de/dhbwstuttgart/.DS_Store
vendored
Normal file
BIN
src/de/dhbwstuttgart/.DS_Store
vendored
Normal file
Binary file not shown.
227
src/de/dhbwstuttgart/bytecode/BytecodeGenLambda.java
Normal file
227
src/de/dhbwstuttgart/bytecode/BytecodeGenLambda.java
Normal file
@ -0,0 +1,227 @@
|
|||||||
|
package de.dhbwstuttgart.bytecode;
|
||||||
|
|
||||||
|
import org.objectweb.asm.MethodVisitor;
|
||||||
|
|
||||||
|
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.Binary;
|
||||||
|
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.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.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.Receiver;
|
||||||
|
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.UnaryPlus;
|
||||||
|
import de.dhbwstuttgart.syntaxtree.statement.WhileStmt;
|
||||||
|
import de.dhbwstuttgart.syntaxtree.statement.literal.Literal;
|
||||||
|
import de.dhbwstuttgart.syntaxtree.statement.literal.Null;
|
||||||
|
|
||||||
|
public class BytecodeGenLambda implements StatementVisitor{
|
||||||
|
private LambdaExpression lambdaExpression;
|
||||||
|
private MethodVisitor mv;
|
||||||
|
|
||||||
|
public BytecodeGenLambda(LambdaExpression lambdaExpression, MethodVisitor mv) {
|
||||||
|
this.lambdaExpression = lambdaExpression;
|
||||||
|
this.mv = mv;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(ArgumentList argumentList) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@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(Binary binary) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@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(FieldVar fieldVar) {
|
||||||
|
// 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(Receiver receiver) {
|
||||||
|
// 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(UnaryPlus unaryPlus) {
|
||||||
|
// 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(Null aNull) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(Literal literal) {
|
||||||
|
// 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
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
14
src/de/dhbwstuttgart/bytecode/ClassFile.java
Normal file
14
src/de/dhbwstuttgart/bytecode/ClassFile.java
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package de.dhbwstuttgart.bytecode;
|
||||||
|
|
||||||
|
public class ClassFile {
|
||||||
|
|
||||||
|
String name;
|
||||||
|
byte[] bytecode;
|
||||||
|
|
||||||
|
public ClassFile(String name, byte[] bytecode) {
|
||||||
|
this.name = name;
|
||||||
|
this.bytecode = bytecode;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -5,8 +5,10 @@ import java.io.FileNotFoundException;
|
|||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.invoke.CallSite;
|
import java.lang.invoke.CallSite;
|
||||||
|
import java.lang.invoke.MethodHandle;
|
||||||
import java.lang.invoke.MethodHandles;
|
import java.lang.invoke.MethodHandles;
|
||||||
import java.lang.invoke.MethodType;
|
import java.lang.invoke.MethodType;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLClassLoader;
|
import java.net.URLClassLoader;
|
||||||
|
|
||||||
@ -18,8 +20,10 @@ import org.objectweb.asm.MethodVisitor;
|
|||||||
import org.objectweb.asm.Opcodes;
|
import org.objectweb.asm.Opcodes;
|
||||||
import org.objectweb.asm.Type;
|
import org.objectweb.asm.Type;
|
||||||
|
|
||||||
|
|
||||||
public class Test {
|
public class Test {
|
||||||
private static final String rootDirectory = System.getProperty("user.dir") + "/bin/de/dhbwstuttgart/bytecode/";
|
private static final String rootDirectory = System.getProperty("user.dir") + "/bin/de/dhbwstuttgart/bytecode/";
|
||||||
|
|
||||||
protected static ClassLoader getClassLoader() throws Exception {
|
protected static ClassLoader getClassLoader() throws Exception {
|
||||||
File file = new File(rootDirectory);
|
File file = new File(rootDirectory);
|
||||||
URL url = file.toURI().toURL();
|
URL url = file.toURI().toURL();
|
||||||
@ -33,83 +37,100 @@ public class Test {
|
|||||||
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
|
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
|
||||||
cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, "TestClass", null, "java/lang/Object", null);
|
cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, "TestClass", null, "java/lang/Object", null);
|
||||||
|
|
||||||
|
cw.visitSource("TestClass.java", null);
|
||||||
|
|
||||||
// Create Constructor
|
// Create Constructor
|
||||||
MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "(Ljava/lang/Boolean;)V", null, null);
|
MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
|
||||||
mv.visitVarInsn(Opcodes.ALOAD, 0);
|
mv.visitVarInsn(Opcodes.ALOAD, 0);
|
||||||
|
|
||||||
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
|
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
|
||||||
|
|
||||||
// mv.visitMethodInsn(INVOKEDYNAMIC, "#0", "run", "()Ljava/lang/Runnable");
|
// mv.visitMethodInsn(INVOKEDYNAMIC, "#0", "run", "()Ljava/lang/Runnable");
|
||||||
|
//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);
|
||||||
|
|
||||||
MethodType mt = MethodType.methodType(CallSite.class, MethodHandles.Lookup.class,
|
Handle bootstrap = new Handle(Opcodes.H_INVOKESTATIC, "java/lang/invoke/LambdaMetafactory", "metafactory",
|
||||||
String.class, MethodType.class);
|
mt.toMethodDescriptorString());
|
||||||
|
Handle arg2 = new Handle(Opcodes.H_INVOKESTATIC, "TestClass", "lambda$0", "()V");
|
||||||
|
|
||||||
Handle bootstrap = new Handle(Opcodes.H_INVOKESTATIC, "java/lang/invoke/LambdaMetafactory",
|
mv.visitInvokeDynamicInsn("run", "()Ljava/lang/Runnable;", bootstrap,
|
||||||
"metafactory", mt.toMethodDescriptorString());
|
Type.getMethodType("()V"), arg2,
|
||||||
|
Type.getMethodType("()V"));
|
||||||
mv.visitInvokeDynamicInsn("run", "()Ljava/lang/Runnable;", bootstrap);
|
|
||||||
|
|
||||||
mv.visitVarInsn(Opcodes.ASTORE, 1);
|
mv.visitVarInsn(Opcodes.ASTORE, 1);
|
||||||
mv.visitVarInsn(Opcodes.ALOAD, 1);
|
mv.visitVarInsn(Opcodes.ALOAD, 1);
|
||||||
mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/lang/Runnable", "run", "()V");
|
mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/lang/Runnable", "run", "()V");
|
||||||
mv.visitInsn(Opcodes.RETURN);
|
mv.visitInsn(Opcodes.RETURN);
|
||||||
|
|
||||||
|
// creates bridge method, contains lambdas body
|
||||||
|
MethodVisitor mvl = cw.visitMethod(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC + Opcodes.ACC_SYNTHETIC, "lambda$0",
|
||||||
|
"()V", null, null);
|
||||||
|
mvl.visitCode();
|
||||||
|
mvl.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
|
||||||
|
mvl.visitLdcInsn("lambda");
|
||||||
|
mvl.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
|
||||||
|
mvl.visitInsn(Opcodes.RETURN);
|
||||||
|
mvl.visitMaxs(2, 0);
|
||||||
|
mvl.visitEnd();
|
||||||
|
|
||||||
mv.visitMaxs(1, 2);
|
mv.visitMaxs(1, 2);
|
||||||
mv.visitEnd();
|
mv.visitEnd();
|
||||||
|
|
||||||
|
|
||||||
|
cw.visitInnerClass("java/lang/invoke/MethodHandles$Lookup", "java/lang/invoke/MethodHandles", "Lookup",
|
||||||
|
Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL);
|
||||||
cw.visitEnd();
|
cw.visitEnd();
|
||||||
|
|
||||||
byte[] b = cw.toByteArray();
|
byte[] b = cw.toByteArray();
|
||||||
|
|
||||||
|
|
||||||
// Test if statement
|
// Test if statement
|
||||||
/* ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES|ClassWriter.COMPUTE_MAXS);
|
/*
|
||||||
|
* ClassWriter cw = new
|
||||||
cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC+Opcodes.ACC_SUPER, "TestIf", null, "java/lang/Object", null);
|
* ClassWriter(ClassWriter.COMPUTE_FRAMES|ClassWriter.COMPUTE_MAXS);
|
||||||
MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "(Ljava/lang/Boolean;)V", null, null);
|
*
|
||||||
mv.visitCode();
|
* cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC+Opcodes.ACC_SUPER, "TestIf", null,
|
||||||
|
* "java/lang/Object", null); MethodVisitor mv =
|
||||||
// Label l0 = new Label();
|
* cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "(Ljava/lang/Boolean;)V", null,
|
||||||
// mv.visitLabel(l0);
|
* null); mv.visitCode();
|
||||||
|
*
|
||||||
mv.visitVarInsn(Opcodes.ALOAD, 0);
|
* // Label l0 = new Label(); // mv.visitLabel(l0);
|
||||||
|
*
|
||||||
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
|
* mv.visitVarInsn(Opcodes.ALOAD, 0);
|
||||||
|
*
|
||||||
// Label l1 = new Label();
|
* mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>",
|
||||||
// mv.visitLabel(l1);
|
* "()V");
|
||||||
mv.visitVarInsn(Opcodes.ALOAD, 1);
|
*
|
||||||
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z");
|
* // Label l1 = new Label(); // mv.visitLabel(l1);
|
||||||
|
* mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
|
||||||
Label label = new Label();
|
* "java/lang/Boolean", "booleanValue", "()Z");
|
||||||
mv.visitJumpInsn(Opcodes.IFEQ, label);
|
*
|
||||||
|
* Label label = new Label(); mv.visitJumpInsn(Opcodes.IFEQ, label);
|
||||||
mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
|
*
|
||||||
mv.visitLdcInsn("1");
|
* mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out",
|
||||||
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
|
* "Ljava/io/PrintStream;"); mv.visitLdcInsn("1");
|
||||||
|
* mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println",
|
||||||
Label endLabel = new Label();
|
* "(Ljava/lang/String;)V");
|
||||||
mv.visitJumpInsn(Opcodes.GOTO, endLabel);
|
*
|
||||||
|
* Label endLabel = new Label(); mv.visitJumpInsn(Opcodes.GOTO, endLabel);
|
||||||
mv.visitLabel(label);
|
*
|
||||||
mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
|
* mv.visitLabel(label); mv.visitFieldInsn(Opcodes.GETSTATIC,
|
||||||
mv.visitLdcInsn("0");
|
* "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitLdcInsn("0");
|
||||||
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
|
* mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println",
|
||||||
|
* "(Ljava/lang/String;)V");
|
||||||
|
*
|
||||||
|
*
|
||||||
mv.visitLabel(endLabel);
|
*
|
||||||
mv.visitInsn(Opcodes.RETURN);
|
* mv.visitLabel(endLabel); mv.visitInsn(Opcodes.RETURN);
|
||||||
|
*
|
||||||
// Label l2 = new Label();
|
* // Label l2 = new Label(); // mv.visitLabel(l2);
|
||||||
// mv.visitLabel(l2);
|
*
|
||||||
|
* // mv.visitLocalVariable("this", "LTestIf;", null, l0, l2, 0); //
|
||||||
// mv.visitLocalVariable("this", "LTestIf;", null, l0, l2, 0);
|
* mv.visitLocalVariable("b", "Ljava/lang/Boolean;", null, l0, l2, 1);
|
||||||
// mv.visitLocalVariable("b", "Ljava/lang/Boolean;", null, l0, l2, 1);
|
* mv.visitMaxs(2, 2); mv.visitEnd();
|
||||||
mv.visitMaxs(2, 2);
|
*
|
||||||
mv.visitEnd();
|
* cw.visitEnd(); byte[] b = cw.toByteArray();
|
||||||
|
|
||||||
cw.visitEnd();
|
|
||||||
byte[] b = cw.toByteArray();
|
|
||||||
*/
|
*/
|
||||||
FileOutputStream output;
|
FileOutputStream output;
|
||||||
|
|
||||||
|
59
src/de/dhbwstuttgart/bytecode/TestFields.java
Normal file
59
src/de/dhbwstuttgart/bytecode/TestFields.java
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
package de.dhbwstuttgart.bytecode;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
public class TestFields {
|
||||||
|
private static final String rootDirectory = System.getProperty("user.dir") + "/bin/de/dhbwstuttgart/bytecode/";
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES|ClassWriter.COMPUTE_MAXS);
|
||||||
|
cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, "TetsF", null, "java/lang/Object", null);
|
||||||
|
|
||||||
|
cw.visitSource("TetsF.java", null);
|
||||||
|
|
||||||
|
FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE, "z", Type.INT_TYPE.getDescriptor(), null, null);
|
||||||
|
fv.visitEnd();
|
||||||
|
|
||||||
|
FieldVisitor fvS = cw.visitField(Opcodes.ACC_PUBLIC, "s", "Ljava/lang/String;", null, null);
|
||||||
|
fvS.visitEnd();
|
||||||
|
// Create Constructor
|
||||||
|
MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
|
||||||
|
mv.visitCode();
|
||||||
|
|
||||||
|
mv.visitVarInsn(Opcodes.ALOAD, 0);
|
||||||
|
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
|
||||||
|
mv.visitVarInsn(Opcodes.ALOAD, 0);
|
||||||
|
mv.visitLdcInsn("");
|
||||||
|
mv.visitFieldInsn(Opcodes.PUTFIELD, "TetsF", "s", "Ljava/lang/String;");
|
||||||
|
mv.visitInsn(Opcodes.RETURN);
|
||||||
|
mv.visitMaxs(2, 1);
|
||||||
|
mv.visitEnd();
|
||||||
|
|
||||||
|
byte[] b = cw.toByteArray();
|
||||||
|
|
||||||
|
FileOutputStream output;
|
||||||
|
|
||||||
|
try {
|
||||||
|
output = new FileOutputStream(new File(System.getProperty("user.dir") + "/testBytecode/TetsF.class"));
|
||||||
|
output.write(b);
|
||||||
|
output.close();
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IOException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
18
src/de/dhbwstuttgart/bytecode/TestMeth.java
Normal file
18
src/de/dhbwstuttgart/bytecode/TestMeth.java
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package de.dhbwstuttgart.bytecode;
|
||||||
|
|
||||||
|
public class TestMeth {
|
||||||
|
private int z;
|
||||||
|
public String s;
|
||||||
|
public TestMeth(int temp) {
|
||||||
|
this.z = temp;
|
||||||
|
}
|
||||||
|
public void m1(int a, int b) {
|
||||||
|
int y = m2(1,2,3,4);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int m2(int a, int b, int x, int y) {
|
||||||
|
Integer c = 55;
|
||||||
|
Integer g;
|
||||||
|
return a+b+y+c;
|
||||||
|
}
|
||||||
|
}
|
6
src/de/dhbwstuttgart/bytecode/TetsF.java
Normal file
6
src/de/dhbwstuttgart/bytecode/TetsF.java
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package de.dhbwstuttgart.bytecode;
|
||||||
|
|
||||||
|
public class TetsF {
|
||||||
|
private int z;
|
||||||
|
public String s = "";
|
||||||
|
}
|
@ -1,7 +1,7 @@
|
|||||||
package de.dhbwstuttgart.core;
|
package de.dhbwstuttgart.core;
|
||||||
|
|
||||||
|
|
||||||
import de.dhbwstuttgart.environment.CompilationEnvironment;
|
import de.dhbwstuttgart.environment.CompilationEnvironment;
|
||||||
import de.dhbwstuttgart.environment.PackageCrawler;
|
|
||||||
import de.dhbwstuttgart.parser.JavaTXParser;
|
import de.dhbwstuttgart.parser.JavaTXParser;
|
||||||
import de.dhbwstuttgart.parser.scope.GenericsRegistry;
|
import de.dhbwstuttgart.parser.scope.GenericsRegistry;
|
||||||
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.SyntaxTreeGenerator;
|
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.SyntaxTreeGenerator;
|
||||||
|
@ -21,6 +21,7 @@ public class ArgumentList extends SyntaxTreeNode
|
|||||||
return expr;
|
return expr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void accept(ASTVisitor visitor) {
|
public void accept(ASTVisitor visitor) {
|
||||||
visitor.visit(this);
|
visitor.visit(this);
|
||||||
|
@ -2,7 +2,6 @@ package bytecode;
|
|||||||
|
|
||||||
import de.dhbwstuttgart.bytecode.BytecodeGen;
|
import de.dhbwstuttgart.bytecode.BytecodeGen;
|
||||||
import de.dhbwstuttgart.core.JavaTXCompiler;
|
import de.dhbwstuttgart.core.JavaTXCompiler;
|
||||||
import de.dhbwstuttgart.exceptions.DebugException;
|
|
||||||
import de.dhbwstuttgart.syntaxtree.SourceFile;
|
import de.dhbwstuttgart.syntaxtree.SourceFile;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
@ -17,7 +16,6 @@ import java.nio.file.Paths;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
@ -28,8 +26,10 @@ public class JavaTXCompilerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test() throws IOException, java.lang.ClassNotFoundException {
|
public void test() throws IOException, java.lang.ClassNotFoundException {
|
||||||
|
System.out.println(rootDirectory);
|
||||||
filesToTest.add(new File(rootDirectory+"EmptyClass.jav"));
|
filesToTest.add(new File(rootDirectory+"EmptyClass.jav"));
|
||||||
JavaTXCompiler compiler = new JavaTXCompiler(filesToTest);
|
JavaTXCompiler compiler = new JavaTXCompiler(filesToTest);
|
||||||
|
System.out.println("test");
|
||||||
for(File f : filesToTest){
|
for(File f : filesToTest){
|
||||||
String content = readFile(f.getPath(), StandardCharsets.UTF_8);
|
String content = readFile(f.getPath(), StandardCharsets.UTF_8);
|
||||||
HashMap<String,byte[]> bytecode = this.getBytecode(compiler.sourceFiles.get(f));
|
HashMap<String,byte[]> bytecode = this.getBytecode(compiler.sourceFiles.get(f));
|
||||||
|
@ -0,0 +1,3 @@
|
|||||||
|
public class EmptyClass{
|
||||||
|
|
||||||
|
}
|
37
test/javFiles/EmptyMethod.jav
Normal file
37
test/javFiles/EmptyMethod.jav
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
public class EmptyMethod{
|
||||||
|
static String s1 ="";
|
||||||
|
String s2;
|
||||||
|
public void m1(){
|
||||||
|
//String s = "";
|
||||||
|
System.out.println("test");
|
||||||
|
//Integer ab = Math.abs(1);
|
||||||
|
//Math.abs(1);
|
||||||
|
//String lV = "local";
|
||||||
|
//s1 = "1";
|
||||||
|
//s1.concat("2");
|
||||||
|
s2 = s1;
|
||||||
|
//m2();
|
||||||
|
Clazz i = new Clazz();
|
||||||
|
Integer i = new Integer(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void m2(){}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Clazz{}
|
||||||
|
/*
|
||||||
|
public class EmptyMethod2{
|
||||||
|
public static test = "5";
|
||||||
|
public void m1(Integer i, String j, Boolean b){
|
||||||
|
//String s = "";
|
||||||
|
EmptyMethod em = new EmptyMethod();
|
||||||
|
em.m1();
|
||||||
|
em.s1 = "";
|
||||||
|
//Integer ab = Math.abs(1);
|
||||||
|
//Math.abs(1);
|
||||||
|
//String lV = "local";
|
||||||
|
//s1 = "1";
|
||||||
|
//s1.concat("2");
|
||||||
|
//s2 = s1;
|
||||||
|
}
|
||||||
|
}*/
|
11
test/javFiles/Op1.jav
Normal file
11
test/javFiles/Op1.jav
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
public class Op1{
|
||||||
|
public Op1() {
|
||||||
|
|
||||||
|
Runnable lam = () -> {
|
||||||
|
String test = "";
|
||||||
|
String b = "b";
|
||||||
|
test = b;
|
||||||
|
System.out.println(test);};
|
||||||
|
//lam.run();
|
||||||
|
}
|
||||||
|
}
|
6
test/javFiles/Subclass.jav
Normal file
6
test/javFiles/Subclass.jav
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
public class Subclass extends Superclass {
|
||||||
|
|
||||||
|
public void printMethod() {
|
||||||
|
super.printMethod();
|
||||||
|
}
|
||||||
|
}
|
6
test/javFiles/Superclass.jav
Normal file
6
test/javFiles/Superclass.jav
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
public class Superclass {
|
||||||
|
|
||||||
|
public void printMethod() {
|
||||||
|
System.out.println("Printed in Superclass.");
|
||||||
|
}
|
||||||
|
}
|
@ -36,6 +36,7 @@ public class JavaTXCompilerTest {
|
|||||||
//filesToTest.add(new File(rootDirectory+"Faculty.jav"));
|
//filesToTest.add(new File(rootDirectory+"Faculty.jav"));
|
||||||
//filesToTest.add(new File(rootDirectory+"mathStruc.jav"));
|
//filesToTest.add(new File(rootDirectory+"mathStruc.jav"));
|
||||||
//filesToTest.add(new File(rootDirectory+"test.jav"));
|
//filesToTest.add(new File(rootDirectory+"test.jav"));
|
||||||
|
filesToTest.add(new File(rootDirectory+"EmptyMethod.jav"));
|
||||||
//filesToTest.add(new File(rootDirectory+"Lambda.jav"));
|
//filesToTest.add(new File(rootDirectory+"Lambda.jav"));
|
||||||
//filesToTest.add(new File(rootDirectory+"Lambda2.jav"));
|
//filesToTest.add(new File(rootDirectory+"Lambda2.jav"));
|
||||||
//filesToTest.add(new File(rootDirectory+"Lambda3.jav"));
|
//filesToTest.add(new File(rootDirectory+"Lambda3.jav"));
|
||||||
|
6
testBytecode/Field.java
Normal file
6
testBytecode/Field.java
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
public class Field{
|
||||||
|
public void m(){
|
||||||
|
MethFieldVar mF = new MethFieldVar();
|
||||||
|
mF.s1 = "Field S1";
|
||||||
|
}
|
||||||
|
}
|
8
testBytecode/Import.java
Normal file
8
testBytecode/Import.java
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
import java.util.Vector;
|
||||||
|
|
||||||
|
class Import {
|
||||||
|
void methode(){
|
||||||
|
Vector v = new Vector();
|
||||||
|
v.add(v);
|
||||||
|
}
|
||||||
|
}
|
10
testBytecode/Lam1.java
Normal file
10
testBytecode/Lam1.java
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import java.util.function.Function;
|
||||||
|
public class Lam1{
|
||||||
|
public Lam1() {
|
||||||
|
Function<String,String> fun = (x) -> x+"1";
|
||||||
|
fun.apply("2");
|
||||||
|
|
||||||
|
Runnable lam = () -> System.out.println("lambda");
|
||||||
|
lam.run();
|
||||||
|
}
|
||||||
|
}
|
8
testBytecode/LamRun.java
Normal file
8
testBytecode/LamRun.java
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
public class LamRun{
|
||||||
|
|
||||||
|
public LamRun(){
|
||||||
|
|
||||||
|
Runnable lam = () -> System.out.println("lambda");
|
||||||
|
lam.run();
|
||||||
|
}
|
||||||
|
}
|
38
testBytecode/MethFieldVar.java
Normal file
38
testBytecode/MethFieldVar.java
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
public class MethFieldVar{
|
||||||
|
String s1;// = "";
|
||||||
|
String s2;
|
||||||
|
|
||||||
|
/* public void meth(Integer i, String j, Boolean b){
|
||||||
|
//String local = "a";
|
||||||
|
//int localL = local.length();
|
||||||
|
//int l = s.length();
|
||||||
|
String s = null;
|
||||||
|
//s = "";
|
||||||
|
//return s.length();//l+localL;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
public void mm(){
|
||||||
|
// return "mm";
|
||||||
|
}
|
||||||
|
public void m2(){
|
||||||
|
System.out.println("");
|
||||||
|
// Math.abs(1);
|
||||||
|
// String lV = "local";
|
||||||
|
// s1 = "1";
|
||||||
|
// s1.concat("2");
|
||||||
|
s2 = s1;
|
||||||
|
|
||||||
|
mm();
|
||||||
|
|
||||||
|
Clazz i = new Clazz();
|
||||||
|
|
||||||
|
Runnable lam = ()->{
|
||||||
|
String test = "";
|
||||||
|
String b = "b";
|
||||||
|
test = b;
|
||||||
|
System.out.println(test);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Clazz{}
|
6
testBytecode/Subclass.java
Normal file
6
testBytecode/Subclass.java
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
public class Subclass extends Superclass {
|
||||||
|
|
||||||
|
public void printMethod() {
|
||||||
|
super.printMethod();
|
||||||
|
}
|
||||||
|
}
|
14
testBytecode/SuperTest.java
Normal file
14
testBytecode/SuperTest.java
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
public class Superclass {
|
||||||
|
|
||||||
|
public void printMethod() {
|
||||||
|
System.out.println("Printed in Superclass.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Subclass extends Superclass {
|
||||||
|
|
||||||
|
public void printMethod() {
|
||||||
|
super.printMethod();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
6
testBytecode/Superclass.java
Normal file
6
testBytecode/Superclass.java
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
public class Superclass {
|
||||||
|
|
||||||
|
public void printMethod() {
|
||||||
|
System.out.println("Printed in Superclass.");
|
||||||
|
}
|
||||||
|
}
|
10
testBytecode/TestMyTest.java
Normal file
10
testBytecode/TestMyTest.java
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
class TestMyTest{
|
||||||
|
public static void main(String[] a){
|
||||||
|
//test1
|
||||||
|
//new TestClass();
|
||||||
|
//test if statement
|
||||||
|
//new TestIf(new Boolean(true));
|
||||||
|
// test lambda
|
||||||
|
new TestClass();
|
||||||
|
}
|
||||||
|
}
|
1
testBytecode/public
Normal file
1
testBytecode/public
Normal file
@ -0,0 +1 @@
|
|||||||
|
|
5
testBytecode/testF.java
Normal file
5
testBytecode/testF.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
public class testTets(){
|
||||||
|
public static void main(String[] args){
|
||||||
|
new tetsF();
|
||||||
|
}
|
||||||
|
}
|
5
testBytecode/testTets.java
Normal file
5
testBytecode/testTets.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
public class testTets{
|
||||||
|
public static void main(String[] args){
|
||||||
|
new TetsF();
|
||||||
|
}
|
||||||
|
}
|
5
testBytecode/testTetsF.java
Normal file
5
testBytecode/testTetsF.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
public class testTets(){
|
||||||
|
public static void main(String[] args){
|
||||||
|
new tetsF();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user