From 0048d5f305abe3a9b1bbbade2f03ff0d4a02f34b Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Fri, 6 Nov 2015 18:27:47 +0100 Subject: [PATCH] NewStatement bytecode --- BCEL/bcelifier/JavaToBCEL.java | 1 + .../syntaxtree/statement/ArgumentList.java | 45 ++++++++------- .../syntaxtree/statement/NewClass.java | 43 +++++++-------- .../syntaxtree/type/RefType.java | 4 +- test/bytecode/NewStatement.jav | 10 ++++ test/bytecode/NewStatementTest.java | 55 +++++++++++++++++++ test/bytecode/Test2.java | 6 ++ 7 files changed, 118 insertions(+), 46 deletions(-) create mode 100644 test/bytecode/NewStatement.jav create mode 100644 test/bytecode/NewStatementTest.java create mode 100644 test/bytecode/Test2.java diff --git a/BCEL/bcelifier/JavaToBCEL.java b/BCEL/bcelifier/JavaToBCEL.java index 10c74c32..c96990dd 100644 --- a/BCEL/bcelifier/JavaToBCEL.java +++ b/BCEL/bcelifier/JavaToBCEL.java @@ -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+"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+"NewClass.class").parse(), new FileOutputStream(new File(rootDirectory+"NewClassCreator.java"))).start(); } catch (ClassFormatException | IOException e) { e.printStackTrace(); diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/ArgumentList.java b/src/de/dhbwstuttgart/syntaxtree/statement/ArgumentList.java index 3644ec30..1b50ed01 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/ArgumentList.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/ArgumentList.java @@ -1,39 +1,26 @@ -// ino.module.ArgumentList.8621.package package de.dhbwstuttgart.syntaxtree.statement; -// ino.end -// ino.module.ArgumentList.8621.import -import java.util.Iterator; -import de.dhbwstuttgart.typeinference.Menge; +import java.util.Iterator; + +import org.apache.commons.bcel6.generic.InstructionList; + +import de.dhbwstuttgart.typeinference.Menge; +import de.dhbwstuttgart.bytecode.ClassGenerator; import de.dhbwstuttgart.myexception.JVMCodeException; import de.dhbwstuttgart.syntaxtree.FormalParameter; import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode; import de.dhbwstuttgart.typeinference.JavaCodeResult; import de.dhbwstuttgart.typeinference.ResultSet; - - - -// ino.class.ArgumentList.24911.declaration public class ArgumentList extends SyntaxTreeNode -// ino.end -// ino.class.ArgumentList.24911.body { - // ino.attribute.expr.24914.declaration public Menge expr = new Menge(); - // ino.end - // ino.method.get_firstElement.24923.definition public Object get_firstElement() - // ino.end - // ino.method.get_firstElement.24923.body { return expr.firstElement(); } - // ino.end - - /** * @author Andreas Stadelmeier, a10023 @@ -93,6 +80,22 @@ public class ArgumentList extends SyntaxTreeNode public Menge getChildren() { return expr; } - + + public InstructionList generateBytecode(ClassGenerator cg){ + InstructionList ret = new InstructionList(); + for(Expr e : expr){ + ret.append(e.genByteCode(cg)); + } + return ret; + } + + public org.apache.commons.bcel6.generic.Type[] getBytecodeTypeList(ClassGenerator cg){ + org.apache.commons.bcel6.generic.Type[] ret = new org.apache.commons.bcel6.generic.Type[expr.size()]; + int i = 0; + for(Expr e : expr){ + ret[i] = e.getType().getBytecodeType(cg); + i++; + } + return ret; + } } -// ino.end diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/NewClass.java b/src/de/dhbwstuttgart/syntaxtree/statement/NewClass.java index cc6d68b3..6d4d89d7 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/NewClass.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/NewClass.java @@ -6,8 +6,11 @@ import java.util.Enumeration; import java.util.Hashtable; import java.util.Iterator; +import org.apache.commons.bcel6.Constants; import org.apache.commons.bcel6.generic.ClassGen; +import org.apache.commons.bcel6.generic.InstructionConstants; import org.apache.commons.bcel6.generic.InstructionList; +import org.apache.commons.bcel6.generic.ObjectType; import de.dhbwstuttgart.typeinference.Menge; import de.dhbwstuttgart.bytecode.ClassGenerator; @@ -55,6 +58,7 @@ public class NewClass extends Expr // ino.attribute.arglist.25837.declaration private ArgumentList arglist; + private boolean isStatement = false; // ino.end // ino.attribute.parserlog.25840.declaration protected static Logger parserlog = Logger.getLogger("parser"); @@ -129,15 +133,7 @@ public class NewClass extends Expr //TODO: Das hier noch vervollständigen ConstraintsSet ret = new ConstraintsSet(); UndConstraint callConstraints = new UndConstraint(); - //Die Auskommentierten Zeilen gehören zu MethodRefNew - //Menge argumentTypeList = new Menge(); - //for(Expr expr : this.arglist.expr){ - // argumentTypeList.add(expr.getTypeVariable()); - //} - //FunN funN= new FunN(null,argumentTypeList); - //Constraint newClassTypeConstraint = new Constraint(null,null); - //ret.add(newClassTypeConstraint); int numArgs = 0; if(this.arglist != null)numArgs = this.arglist.size(); ConstructorAssumption cA = assumptions.getConstructorAssumption(this.get_Name(), numArgs); @@ -147,22 +143,9 @@ public class NewClass extends Expr ret.add(this.arglist.expr.elementAt(i).TYPEExpr(assumptions)); callConstraints.addConstraint( this.arglist.expr.elementAt(i).getType().TYPE(assumptions, this), cA.getParameterType(i).TYPE(assumptions, this)); } - //if(this.arglist != null && this.arglist.expr != null)for(Expr arg : this.arglist.expr){ - // ret.add(arg.TYPEExpr(assumptions)); - //} Type thisT = assumptions.checkType(new RefType(this.get_Name(),this,0), (SyntaxTreeNode)this); this.setType(thisT); - - /* - //Ein new-Aufruf ist nichts anderes als ein MethodCall der Methode - MethodCall newAufruf = new MethodCall(0,0); - this.setType(assumptions.getTypeFor(new RefType(this.get_Name(),0))); - newAufruf.type = this.getType(); - newAufruf.set_Name(""); - newAufruf.set_Receiver(null); - ret.add(new Overloading(assumptions, newAufruf, this.getType()).generateConsstraints()); - */ return ret; } @@ -176,6 +159,7 @@ public class NewClass extends Expr public ConstraintsSet TYPEStmt(TypeAssumptions assumptions){ ConstraintsSet ret = this.TYPEExpr(assumptions); //TypeExpr aufrufen this.setType(new Void(this,0)); //Typ des Statments auf Void setzen. + this.isStatement = true; return ret; } @@ -208,8 +192,21 @@ public class NewClass extends Expr @Override public InstructionList genByteCode(ClassGenerator _cg) { - // TODO Bytecode - throw new NotImplementedException(); + InstructionList il = new InstructionList(); + if(arglist!=null){ + il.append(arglist.generateBytecode(_cg)); + il.append(_cg.getInstructionFactory().createInvoke(this.get_Name(), "", + org.apache.commons.bcel6.generic.Type.VOID, + this.arglist.getBytecodeTypeList(_cg), Constants.INVOKESPECIAL)); + }else{ + il.append(_cg.getInstructionFactory().createInvoke(this.get_Name(), "", + org.apache.commons.bcel6.generic.Type.VOID, + new org.apache.commons.bcel6.generic.Type[]{}, Constants.INVOKESPECIAL)); + } + if(this.isStatement){ + il.append(InstructionConstants.POP); + } + return il; } diff --git a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java index 9f33148b..ef88050d 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java @@ -857,8 +857,8 @@ public class RefType extends ObjectType implements IMatchable cg.addExtraClass(generatedClass.genByteCode(new TypeinferenceResultSet(generatedClass, new Menge<>(), new ResultSet())).getByteCode()); } - - return "L"+combinedType+";"; + String ret = new org.apache.commons.bcel6.generic.ObjectType(combinedType).getSignature(); + return ret; } public String getCombinedType(ClassGenerator cg){ diff --git a/test/bytecode/NewStatement.jav b/test/bytecode/NewStatement.jav new file mode 100644 index 00000000..c654c116 --- /dev/null +++ b/test/bytecode/NewStatement.jav @@ -0,0 +1,10 @@ +class NewStatement{ +public NewStatement(Integer i){} +public NewStatement(String i){} +method() { + NewStatement n; + n = new NewStatement("hallo"); + n = new NewStatement(1); +} + +} \ No newline at end of file diff --git a/test/bytecode/NewStatementTest.java b/test/bytecode/NewStatementTest.java new file mode 100644 index 00000000..ff692332 --- /dev/null +++ b/test/bytecode/NewStatementTest.java @@ -0,0 +1,55 @@ +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 java.net.URLClassLoader; + +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 NewStatementTest { + + public final static String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/"; + public final static String testFile = "NewStatement.jav"; + public final static String outputFile = "NewStatement.class"; + + @Test + public void test() { + SingleClassTester.compileToBytecode(rootDirectory+testFile, rootDirectory); + } + @Test + public void testUntypedVectorDeclaredMethods() { + try{ + File file = new File(System.getProperty("user.dir")+"/test/bytecode/types/"); + URL url = file.toURL(); + URL[] urls = new URL[]{url}; + + ClassLoader classLoader = new URLClassLoader(urls); + + Class untypedVectorTest = classLoader.loadClass("UntypedVector"); + + for(Method method: untypedVectorTest.getDeclaredMethods()){ + System.out.println(method.toGenericString()); + } + }catch(Exception e){ + throw new RuntimeException(e); + } + } + +} diff --git a/test/bytecode/Test2.java b/test/bytecode/Test2.java new file mode 100644 index 00000000..767f6522 --- /dev/null +++ b/test/bytecode/Test2.java @@ -0,0 +1,6 @@ +class Test2{ + + public static void main(java.lang.String[] args){ + new NewStatement(1).method(); +} +}