Bytecode TEst

This commit is contained in:
Fayez Abu Alia 2017-08-23 17:26:59 +02:00
parent 1e6f046eff
commit 43f017a224
14 changed files with 156 additions and 11 deletions

View File

@ -17,5 +17,6 @@
<classpathentry kind="lib" path="lib/reflections-0.9.10-sources.jar"/>
<classpathentry kind="lib" path="lib/reflections-0.9.10.jar" sourcepath="/reflections/src"/>
<classpathentry kind="lib" path="lib/guava-22.0.jar" sourcepath="lib/guava-22.0-sources.jar"/>
<classpathentry kind="lib" path="/Users/fayez/Downloads/asm-6.0_BETA.jar" sourcepath="/Users/fayez/Downloads/asm-6.0_BETA/src.zip"/>
<classpathentry kind="output" path="bin"/>
</classpath>

BIN
src/.DS_Store vendored Normal file

Binary file not shown.

BIN
src/de/.DS_Store vendored Normal file

Binary file not shown.

View File

@ -0,0 +1,129 @@
package de.dhbwstuttgart.bytecode;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.invoke.CallSite;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.net.URL;
import java.net.URLClassLoader;
import org.objectweb.asm.ClassReader;
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;
public class Test {
private static final String rootDirectory = System.getProperty("user.dir")+"/bin/de/dhbwstuttgart/bytecode/";
protected static ClassLoader getClassLoader() throws Exception{
File file = new File(rootDirectory);
URL url = file.toURI().toURL();
URL[] urls = new URL[]{url};
System.out.println(urls[0]);
return new URLClassLoader(urls);
}
public static void main(String[] args) {
// Test Lambda
ClassWriter cw =new ClassWriter(ClassWriter.COMPUTE_MAXS);
cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC+Opcodes.ACC_SUPER, "TestClass", null, "java/lang/Object", null);
// Create Constructor
MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "(Ljava/lang/Boolean;)V", null, null);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
// mv.visitMethodInsn(INVOKEDYNAMIC, "#0", "run", "()Ljava/lang/Runnable");
MethodType mt = MethodType.methodType(CallSite.class, MethodHandles.Lookup.class,
String.class, MethodType.class);
Handle bootstrap = new Handle(Opcodes.H_INVOKESTATIC, "java/lang/invoke/LambdaMetafactory",
"metafactory", mt.toMethodDescriptorString());
mv.visitInvokeDynamicInsn("run", "()Ljava/lang/Runnable;", bootstrap);
mv.visitVarInsn(Opcodes.ASTORE, 1);
mv.visitVarInsn(Opcodes.ALOAD, 1);
mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/lang/Runnable", "run", "()V");
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(1, 2);
mv.visitEnd();
cw.visitEnd();
byte[] b = cw.toByteArray();
// Test if statement
/* ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES|ClassWriter.COMPUTE_MAXS);
cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC+Opcodes.ACC_SUPER, "TestIf", null, "java/lang/Object", null);
MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "(Ljava/lang/Boolean;)V", null, null);
mv.visitCode();
// Label l0 = new Label();
// mv.visitLabel(l0);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
// Label l1 = new Label();
// mv.visitLabel(l1);
mv.visitVarInsn(Opcodes.ALOAD, 1);
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z");
Label label = new Label();
mv.visitJumpInsn(Opcodes.IFEQ, label);
mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
mv.visitLdcInsn("1");
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
Label endLabel = new Label();
mv.visitJumpInsn(Opcodes.GOTO, endLabel);
mv.visitLabel(label);
mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
mv.visitLdcInsn("0");
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
mv.visitLabel(endLabel);
mv.visitInsn(Opcodes.RETURN);
// Label l2 = new Label();
// mv.visitLabel(l2);
// mv.visitLocalVariable("this", "LTestIf;", null, l0, l2, 0);
// mv.visitLocalVariable("b", "Ljava/lang/Boolean;", null, l0, l2, 1);
mv.visitMaxs(2, 2);
mv.visitEnd();
cw.visitEnd();
byte[] b = cw.toByteArray();
*/
FileOutputStream output;
try {
output = new FileOutputStream(new File(System.getProperty("user.dir")+"/testBytecode/TestClass.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();
}
}
}

View File

@ -0,0 +1,8 @@
package de.dhbwstuttgart.bytecode;
public class TestClass {
public TestClass() {
Runnable lam = () -> System.out.println("lambda");
lam.run();
}
}

View File

@ -0,0 +1,11 @@
package de.dhbwstuttgart.bytecode;
public class TestIf {
public TestIf(Boolean b) {
if(b) {
System.out.println("1");
}else {
System.out.println("0");
}
}
}

View File

@ -1,8 +1,5 @@
package de.dhbwstuttgart.core;
import de.dhbwstuttgart.parser.ClassNotFoundException;
import java.io.File;
import java.io.IOException;
import java.util.*;

View File

@ -1,7 +1,6 @@
package de.dhbwstuttgart.core;
import de.dhbwstuttgart.exceptions.DebugException;
import de.dhbwstuttgart.parser.ClassNotFoundException;
import de.dhbwstuttgart.parser.JavaTXParser;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.SourceFile;

View File

@ -1,4 +0,0 @@
package de.dhbwstuttgart.parser;
public class ClassNotFoundException extends Exception{
}

BIN
test/.DS_Store vendored Normal file

Binary file not shown.

7
test/javFiles/test1.jav Normal file
View File

@ -0,0 +1,7 @@
class Faculty {
int a;
m (int x) {
return a+x;
}
}

View File

@ -1,6 +1,5 @@
package parser;
import de.dhbwstuttgart.parser.ClassNotFoundException;
import de.dhbwstuttgart.parser.JavaTXParser;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.Field;

View File

@ -1,6 +1,5 @@
package parser;
import de.dhbwstuttgart.parser.ClassNotFoundException;
import de.dhbwstuttgart.parser.JavaTXParser;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.Field;

View File

@ -2,7 +2,6 @@ package typeinference;
import de.dhbwstuttgart.core.JavaTXCompiler;
import de.dhbwstuttgart.exceptions.DebugException;
import de.dhbwstuttgart.parser.ClassNotFoundException;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;