New Instruction implementier, ArgumentList fehlt noch

This commit is contained in:
Enrico Schrödter 2015-10-31 18:48:46 +01:00
parent e9607e37b1
commit 947d145cf1
11 changed files with 154 additions and 77 deletions

View File

@ -23,7 +23,7 @@ public class JavaToBCEL {
public JavaToBCEL(){ public JavaToBCEL(){
try { try {
new BCELifier(new ClassParser(rootDirectory+"Lambda1.class").parse(), new FileOutputStream(new File(rootDirectory+"Lambda1Creator.java"))).start(); //new BCELifier(new ClassParser(rootDirectory+"Lambda1.class").parse(), new FileOutputStream(new File(rootDirectory+"Lambda1Creator.java"))).start();
new BCELifier(new ClassParser(rootDirectory+"This.class").parse(), new FileOutputStream(new File(rootDirectory+"ThisCreator.java"))).start(); new BCELifier(new ClassParser(rootDirectory+"This.class").parse(), new FileOutputStream(new File(rootDirectory+"ThisCreator.java"))).start();
new BCELifier(new ClassParser(rootDirectory+"IntLiteral.class").parse(), new FileOutputStream(new File(rootDirectory+"IntLiteralCreator.java"))).start(); new BCELifier(new ClassParser(rootDirectory+"IntLiteral.class").parse(), new FileOutputStream(new File(rootDirectory+"IntLiteralCreator.java"))).start();
new BCELifier(new ClassParser(rootDirectory+"MethodCall.class").parse(), new FileOutputStream(new File(rootDirectory+"MethodCallCreator.java"))).start(); new BCELifier(new ClassParser(rootDirectory+"MethodCall.class").parse(), new FileOutputStream(new File(rootDirectory+"MethodCallCreator.java"))).start();
@ -32,6 +32,7 @@ public class JavaToBCEL {
new BCELifier(new ClassParser(rootDirectory+"LocalVarAccess.class").parse(), new FileOutputStream(new File(rootDirectory+"LocalVarAccessCreator.java"))).start(); new BCELifier(new ClassParser(rootDirectory+"LocalVarAccess.class").parse(), new FileOutputStream(new File(rootDirectory+"LocalVarAccessCreator.java"))).start();
new BCELifier(new ClassParser(rootDirectory+"Wildcard.class").parse(), new FileOutputStream(new File(rootDirectory+"WildcardCreator.java"))).start(); new BCELifier(new ClassParser(rootDirectory+"Wildcard.class").parse(), new FileOutputStream(new File(rootDirectory+"WildcardCreator.java"))).start();
new BCELifier(new ClassParser(rootDirectory+"BooleanValue.class").parse(), new FileOutputStream(new File(rootDirectory+"BooleanValueCreator.java"))).start(); new BCELifier(new ClassParser(rootDirectory+"BooleanValue.class").parse(), new FileOutputStream(new File(rootDirectory+"BooleanValueCreator.java"))).start();
new BCELifier(new ClassParser(rootDirectory+"NewClass.class").parse(), new FileOutputStream(new File(rootDirectory+"NewClass.java"))).start();
} catch (ClassFormatException | IOException e) { } catch (ClassFormatException | IOException e) {
e.printStackTrace(); e.printStackTrace();

Binary file not shown.

View File

@ -1,70 +0,0 @@
package bcelifier;
import org.apache.commons.bcel6.generic.*;
import org.apache.commons.bcel6.classfile.*;
import org.apache.commons.bcel6.*;
import java.io.*;
public class Lambda1Creator implements Constants {
private InstructionFactory _factory;
private ConstantPoolGen _cp;
private ClassGen _cg;
public Lambda1Creator() {
_cg = new ClassGen("bcelifier.Lambda1", "java.lang.Object", "Lambda1.java", ACC_PUBLIC | ACC_SUPER, new String[] { });
_cp = _cg.getConstantPool();
_factory = new InstructionFactory(_cg, _cp);
}
public void create(OutputStream out) throws IOException {
createMethod_0();
createMethod_1();
createMethod_2();
_cg.getJavaClass().dump(out);
}
private void createMethod_0() {
InstructionList il = new InstructionList();
MethodGen method = new MethodGen(ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] { }, "<init>", "bcelifier.Lambda1", il, _cp);
InstructionHandle ih_0 = il.append(_factory.createLoad(Type.OBJECT, 0));
il.append(_factory.createInvoke("java.lang.Object", "<init>", Type.VOID, Type.NO_ARGS, Constants.INVOKESPECIAL));
InstructionHandle ih_4 = il.append(_factory.createReturn(Type.VOID));
method.setMaxStack();
method.setMaxLocals();
_cg.addMethod(method.getMethod());
il.dispose();
}
private void createMethod_1() {
InstructionList il = new InstructionList();
MethodGen method = new MethodGen(0, new ObjectType("java.lang.Runnable"), Type.NO_ARGS, new String[] { }, "methode", "bcelifier.Lambda1", il, _cp);
InstructionHandle ih_0 = il.append(_factory.createLoad(Type.OBJECT, 0));
InstructionHandle ih_1 = il.append(_factory.createReturn(Type.OBJECT));
method.setMaxStack();
method.setMaxLocals();
_cg.addMethod(method.getMethod());
il.dispose();
}
private void createMethod_2() {
InstructionList il = new InstructionList();
MethodGen method = new MethodGen(ACC_PRIVATE | ACC_SYNTHETIC, Type.VOID, Type.NO_ARGS, new String[] { }, "lambda$methode$0", "bcelifier.Lambda1", il, _cp);
InstructionHandle ih_0 = il.append(_factory.createFieldAccess("java.lang.System", "out", new ObjectType("java.io.PrintStream"), Constants.GETSTATIC));
il.append(_factory.createLoad(Type.OBJECT, 0));
il.append(_factory.createInvoke("java.io.PrintStream", "println", Type.VOID, new Type[] { Type.OBJECT }, Constants.INVOKEVIRTUAL));
InstructionHandle ih_7 = il.append(_factory.createReturn(Type.VOID));
method.setMaxStack();
method.setMaxLocals();
_cg.addMethod(method.getMethod());
il.dispose();
}
public static void main(String[] args) throws Exception {
bcelifier.Lambda1Creator creator = new bcelifier.Lambda1Creator();
creator.create(new FileOutputStream("bcelifier.Lambda1.class"));
}
}

View File

@ -0,0 +1,60 @@
package bcelifier;
import org.apache.commons.bcel6.generic.*;
import org.apache.commons.bcel6.classfile.*;
import org.apache.commons.bcel6.*;
import java.io.*;
public class NewClassCreator implements Constants {
private InstructionFactory _factory;
private ConstantPoolGen _cp;
private ClassGen _cg;
public NewClassCreator() {
_cg = new ClassGen("bcelifier.NewClass", "java.lang.Object", "NewClass.java", ACC_SUPER, new String[] { });
_cp = _cg.getConstantPool();
_factory = new InstructionFactory(_cg, _cp);
}
public void create(OutputStream out) throws IOException {
createMethod_0();
createMethod_1();
_cg.getJavaClass().dump(out);
}
private void createMethod_0() {
InstructionList il = new InstructionList();
MethodGen method = new MethodGen(0, Type.VOID, Type.NO_ARGS, new String[] { }, "<init>", "bcelifier.NewClass", il, _cp);
InstructionHandle ih_0 = il.append(_factory.createLoad(Type.OBJECT, 0));
il.append(_factory.createInvoke("java.lang.Object", "<init>", Type.VOID, Type.NO_ARGS, Constants.INVOKESPECIAL));
InstructionHandle ih_4 = il.append(_factory.createReturn(Type.VOID));
method.setMaxStack();
method.setMaxLocals();
_cg.addMethod(method.getMethod());
il.dispose();
}
private void createMethod_1() {
InstructionList il = new InstructionList();
MethodGen method = new MethodGen(0, Type.STRING, new Type[] { Type.STRING }, new String[] { "arg0" }, "method", "bcelifier.NewClass", il, _cp);
InstructionHandle ih_0 = il.append(_factory.createNew("java.lang.String"));
il.append(InstructionConstants.DUP);
il.append(_factory.createLoad(Type.OBJECT, 1));
il.append(_factory.createInvoke("java.lang.String", "<init>", Type.VOID, new Type[] { Type.STRING }, Constants.INVOKESPECIAL));
il.append(_factory.createStore(Type.OBJECT, 2));
InstructionHandle ih_9 = il.append(_factory.createLoad(Type.OBJECT, 2));
InstructionHandle ih_10 = il.append(_factory.createReturn(Type.OBJECT));
method.setMaxStack();
method.setMaxLocals();
_cg.addMethod(method.getMethod());
il.dispose();
}
public static void main(String[] args) throws Exception {
bcelifier.NewClassCreator creator = new bcelifier.NewClassCreator();
creator.create(new FileOutputStream("bcelifier.NewClass.class"));
}
}

Binary file not shown.

View File

@ -11,7 +11,7 @@ public class ThisCreator implements Constants {
private ClassGen _cg; private ClassGen _cg;
public ThisCreator() { public ThisCreator() {
_cg = new ClassGen("bcelifier.This", "java.lang.Object", "<Unknown>", ACC_PUBLIC | ACC_SUPER, new String[] { }); _cg = new ClassGen("bcelifier.This", "java.lang.Object", "This.java", ACC_PUBLIC | ACC_SUPER, new String[] { });
_cp = _cg.getConstantPool(); _cp = _cg.getConstantPool();
_factory = new InstructionFactory(_cg, _cp); _factory = new InstructionFactory(_cg, _cp);

View File

@ -184,8 +184,8 @@ public class Assign extends Expr
InstructionFactory _factory = new InstructionFactory(cg, cg.getConstantPool()); InstructionFactory _factory = new InstructionFactory(cg, cg.getConstantPool());
InstructionList il = expr2.genByteCode(cg);//expr2 rechte expr InstructionList il = expr2.genByteCode(cg);//expr2 rechte expr
//TODO: ^^
counterAssign++; //macht STORE f<EFBFBD>r meherere Variable nutzbar (nicht nur ISTORE_1, ISTORE_2, etc.)
/* /*
String expr2Type = expr2.getType().get_Name().toString(); String expr2Type = expr2.getType().get_Name().toString();
@ -214,6 +214,9 @@ public class Assign extends Expr
//Es wird momentan immer von RefType ausgegangen: //Es wird momentan immer von RefType ausgegangen:
il.append(new ASTORE(counterAssign)); il.append(new ASTORE(counterAssign));
//TODO: ^^
counterAssign++; //macht STORE f<EFBFBD>r meherere Variable nutzbar (nicht nur ISTORE_1, ISTORE_2, etc.)
return il; return il;
} }

View File

@ -6,8 +6,13 @@ import java.util.Enumeration;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.Iterator; import java.util.Iterator;
import org.apache.commons.bcel6.Constants;
import org.apache.commons.bcel6.generic.ClassGen; import org.apache.commons.bcel6.generic.ClassGen;
import org.apache.commons.bcel6.generic.InstructionConstants;
import org.apache.commons.bcel6.generic.InstructionFactory;
import org.apache.commons.bcel6.generic.InstructionHandle;
import org.apache.commons.bcel6.generic.InstructionList; import org.apache.commons.bcel6.generic.InstructionList;
import org.apache.commons.bcel6.generic.NEW;
import de.dhbwstuttgart.typeinference.Menge; import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.bytecode.ClassGenerator; import de.dhbwstuttgart.bytecode.ClassGenerator;
@ -208,8 +213,18 @@ public class NewClass extends Expr
@Override @Override
public InstructionList genByteCode(ClassGenerator _cg) { public InstructionList genByteCode(ClassGenerator _cg) {
// TODO Bytecode //TODO bytecode
throw new NotImplementedException(); InstructionFactory _factory = _cg.getInstructionFactory();
InstructionList il = new InstructionList();
String name = type.get_Name();
il.append(_factory.createNew(name));
il.append(InstructionConstants.DUP);
il.append(_factory.createInvoke(name, "<init>", org.apache.commons.bcel6.generic.Type.VOID, org.apache.commons.bcel6.generic.Type.NO_ARGS, Constants.INVOKESPECIAL));
//il.append(InstructionConstants.POP);
return il;
} }

View File

@ -52,7 +52,7 @@ public abstract class BytecodeTest extends TestCase{
} }
protected ClassLoader getClassLoader() throws Exception{ protected ClassLoader getClassLoader() throws Exception{
File file = new File(System.getProperty("user.dir")+"/test/bytecode/types/"); File file = new File(rootDirectory);
URL url = file.toURL(); URL url = file.toURL();
URL[] urls = new URL[]{url}; URL[] urls = new URL[]{url};

View File

@ -0,0 +1,7 @@
class NewClass{
String method() {
String a;
a = new String();
return a;
}
}

View File

@ -0,0 +1,61 @@
package bytecode;
import static org.junit.Assert.*;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import junit.framework.TestCase;
import org.junit.Test;
import plugindevelopment.TypeInsertTester;
import de.dhbwstuttgart.core.MyCompiler;
import de.dhbwstuttgart.core.MyCompilerAPI;
import de.dhbwstuttgart.logger.LoggerConfiguration;
import de.dhbwstuttgart.logger.Section;
import de.dhbwstuttgart.parser.JavaParser.yyException;
import de.dhbwstuttgart.typeinference.ByteCodeResult;
import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.typeinference.TypeinferenceResultSet;
import de.dhbwstuttgart.typeinference.typedeployment.TypeInsertSet;
public class NewClassTest extends BytecodeTest{
@Override
protected void init() {
testName = "NewClass";
}
@Test
public void testInit() throws Exception {
ClassLoader cl = getClassLoader();
cl.loadClass(testName).newInstance();
}
@Test
public void testMethod() throws Exception {
try{
ClassLoader classLoader = getClassLoader();
Class cls = classLoader.loadClass(testName);
Object obj = cls.newInstance();
File file = new File(rootDirectory);
URL url = file.toURL();
URL[] urls = new URL[]{url};
Class[] params = new Class[0];
Method method = cls.getDeclaredMethod("method", params);
Object returnValue = method.invoke(obj);
assertTrue(returnValue instanceof String);
}catch(Exception e){
throw new RuntimeException(e);
}
}
}