Fertige class.java ByteCode Generierung

This commit is contained in:
Fikus, Evelyn (WWI2012D) 2015-05-22 13:50:43 +02:00
parent 2b62ac43e6
commit 57350e2095
4 changed files with 107 additions and 9 deletions

View File

@ -3,7 +3,7 @@
<classpathentry kind="src" path="src"/> <classpathentry kind="src" path="src"/>
<classpathentry excluding=".classpath|.cvsignore|.externalToolBuilders/|.project|.settings/|Papers/|bin/|doc/|examples/|lib/|notizen/|src/|test/|tools/" including="log4j.xml" kind="src" path=""/> <classpathentry excluding=".classpath|.cvsignore|.externalToolBuilders/|.project|.settings/|Papers/|bin/|doc/|examples/|lib/|notizen/|src/|test/|tools/" including="log4j.xml" kind="src" path=""/>
<classpathentry kind="src" path="test"/> <classpathentry kind="src" path="test"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="lib" path="lib/junit-4.0.jar" sourcepath="/home/janulrich/.m2/repository/junit/junit/4.0/junit-4.0-sources.jar"/> <classpathentry kind="lib" path="lib/junit-4.0.jar" sourcepath="/home/janulrich/.m2/repository/junit/junit/4.0/junit-4.0-sources.jar"/>
<classpathentry kind="lib" path="lib/cloning.jar"/> <classpathentry kind="lib" path="lib/cloning.jar"/>
<classpathentry kind="lib" path="C:/Users/fikus/Desktop/bcel-5.2/bcel-5.2.jar"/> <classpathentry kind="lib" path="C:/Users/fikus/Desktop/bcel-5.2/bcel-5.2.jar"/>

View File

@ -2,12 +2,12 @@ eclipse.preferences.version=1
instance/org.eclipse.core.net/org.eclipse.core.net.hasMigrated=true instance/org.eclipse.core.net/org.eclipse.core.net.hasMigrated=true
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.5 org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.5 org.eclipse.jdt.core.compiler.source=1.7

View File

@ -9,6 +9,11 @@ import java.util.Hashtable;
import java.util.Iterator; import java.util.Iterator;
import org.apache.bcel.generic.ClassGen; import org.apache.bcel.generic.ClassGen;
import org.apache.bcel.generic.ConstantPoolGen;
import org.apache.bcel.generic.InstructionFactory;
import org.apache.bcel.generic.InstructionHandle;
import org.apache.bcel.generic.InstructionList;
import org.apache.bcel.generic.MethodGen;
import de.dhbwstuttgart.typeinference.Menge; import de.dhbwstuttgart.typeinference.Menge;
@ -41,6 +46,8 @@ import org.apache.bcel.classfile.*;
import org.apache.bcel.*; import org.apache.bcel.*;
import java.io.*; import java.io.*;
// ino.class.Class.23010.declaration // ino.class.Class.23010.declaration
public class Class extends GTVDeclarationContext implements AClassOrInterface, IItemWithOffset, Generic, GenericTypeInsertable public class Class extends GTVDeclarationContext implements AClassOrInterface, IItemWithOffset, Generic, GenericTypeInsertable
// ino.end // ino.end
@ -55,15 +62,42 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I
protected UsedId pkgName; protected UsedId pkgName;
protected Modifiers modifiers; protected Modifiers modifiers;
protected String name; protected String name;
/* private ClassGen _cg; //For ByteCode Construction
private InstructionFactory _factory;
private ConstantPoolGen _cp;
private ClassGen _cg;
public ClassGen genByteCode() { //Method created with BCEL to generate ByteCode
ByteCodeResult bc = new ByteCodeResult(); public ByteCodeResult genByteCode() throws IOException {
ByteCodeResult code = new ByteCodeResult();
_cg = new ClassGen(pkgName.get_Name() + "/" + name, superClass.get_Name(), name + ".java", Constants.ACC_PUBLIC , new String[] { }); //new String necessary?
_cp = _cg.getConstantPool();
_factory = new InstructionFactory(_cg, _cp);
ByteArrayOutputStream out = new ByteArrayOutputStream();
InstructionList il = new InstructionList();
MethodGen method = new MethodGen(Constants.ACC_PUBLIC, org.apache.bcel.generic.Type.VOID, org.apache.bcel.generic.Type.NO_ARGS, new String[] { }, "<init>", "bceltesting.leer", il, _cp);
InstructionHandle ih_0 = il.append(_factory.createLoad(org.apache.bcel.generic.Type.OBJECT, 0)); //creates Constructor
il.append(_factory.createInvoke("java.lang.Object", "<init>", org.apache.bcel.generic.Type.VOID, org.apache.bcel.generic.Type.NO_ARGS, Constants.INVOKESPECIAL));
InstructionHandle ih_4 = il.append(_factory.createReturn(org.apache.bcel.generic.Type.VOID)); //creates Constructor
method.setMaxStack();
method.setMaxLocals();
_cg.addMethod(method.getMethod());
il.dispose();
_cg.getJavaClass().dump(out);
code.attach(out.toString());
return code;
/*ByteCodeResult bc = new ByteCodeResult();
_cg = new ClassGen(pkgName.get_Name() + "/" + name, superClass.get_Name(), name + ".java", Constants.ACC_PUBLIC , new String[] { }); _cg = new ClassGen(pkgName.get_Name() + "/" + name, superClass.get_Name(), name + ".java", Constants.ACC_PUBLIC , new String[] { });
//_cg zurückgeben //_cg zurückgeben
bc.append(BCELByteCodeOutput); bc.append(BCELByteCodeOutput);
return _cg; return _cg;*/
}*/ }
private Menge<Type> superif = new Menge<Type>(); private Menge<Type> superif = new Menge<Type>();

View File

@ -0,0 +1,64 @@
package de.dhbwstuttgart.typeinference;
import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
public class ByteCodeResult{
private String byteCode = "";
//TODO: unresolvedTPHs entfernen. BROKEN!
private Menge<TypePlaceholder> unresolvedTPHs = new Menge<TypePlaceholder>();
public ByteCodeResult(){
}
public ByteCodeResult(String byteCode){
this.byteCode += byteCode;
}
public String getByteCode(){
return byteCode;
}
public ByteCodeResult attach(ByteCodeResult byteCodeResult){
this.byteCode += byteCodeResult.getByteCode();
//Alle TPH anfügen:
for(TypePlaceholder tph : byteCodeResult.getUnresolvedTPH())this.addUnresolvedTPH(tph);
return this;
}
public ByteCodeResult attach(String javaCode){
this.byteCode += javaCode;
return this;
}
public void addUnresolvedTPH(TypePlaceholder typePlaceholder) {
unresolvedTPHs.add(typePlaceholder);
}
/**
* Liefert alle TPHs, welche in diesem ByteCodeResult nicht zu einem Typ aufgelÃst wurden.
* Diese TPHs stehen dadurch im ByteCode als Variablennamen ohne zugeordnetem Typ.
* @return
*/
public Menge<TypePlaceholder> getUnresolvedTPH(){
return unresolvedTPHs;
}
@Override
public String toString(){
return getByteCode();
}
@Override
public boolean equals(Object obj){
if(!(obj instanceof ByteCodeResult))return false;
ByteCodeResult equals = (ByteCodeResult)obj;
if(!equals.getByteCode().equals(this.getByteCode()))return false;
if(!equals.getUnresolvedTPH().equals(this.getUnresolvedTPH()))return false;
return true;
}
}