From c80dc162d93ab81e11198223d1d210a259f17efb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrico=20Schr=C3=B6dter?= Date: Sun, 20 Mar 2016 18:55:36 +0100 Subject: [PATCH 1/5] - Verhindern von der Generierung von doppelte Methoden --- src/de/dhbwstuttgart/syntaxtree/Class.java | 13 +- .../dhbwstuttgart/syntaxtree/Constructor.java | 2 +- src/de/dhbwstuttgart/syntaxtree/Method.java | 201 ++---------------- .../types/AutoOverloadingMultiResults.jav | 8 + .../AutoOverloadingMultiResultsTest.java | 36 ++++ .../types/AutoOverloadingVectorTest.java | 8 +- test/bytecode/types/ExtendsType.jav | 2 +- test/bytecode/types/ExtendsTypeTest.java | 2 +- .../types/ExtendsVectorStringTest.java | 11 +- 9 files changed, 85 insertions(+), 198 deletions(-) create mode 100644 test/bytecode/types/AutoOverloadingMultiResults.jav create mode 100644 test/bytecode/types/AutoOverloadingMultiResultsTest.java diff --git a/src/de/dhbwstuttgart/syntaxtree/Class.java b/src/de/dhbwstuttgart/syntaxtree/Class.java index 006e3537..a746b8c9 100755 --- a/src/de/dhbwstuttgart/syntaxtree/Class.java +++ b/src/de/dhbwstuttgart/syntaxtree/Class.java @@ -7,6 +7,8 @@ import java.util.Collection; import java.util.Enumeration; import java.util.Hashtable; import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; import javax.lang.model.element.Modifier; @@ -72,6 +74,7 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I protected UsedId pkgName; protected Modifiers modifiers; protected String name; + private List methodSignaturesAndNames = new LinkedList<>(); /** * @@ -106,7 +109,7 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I } //Zuerst die Methoden und Felder abarbeiten: for(Method m : methods){ - m.genByteCode(_cg); + m.genByteCode(_cg, this); } InstructionList fieldInitializations = new InstructionList(); for(FieldDeclaration f : fieldDeclarations){ @@ -1039,6 +1042,14 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I public boolean isInterface(){ return false; } + + protected boolean methodExists(String nameAndSignature) { + return methodSignaturesAndNames.contains(nameAndSignature); + } + + protected void addMethod(String nameAndSignature) { + methodSignaturesAndNames.add(nameAndSignature); + } /* private Collection getGenericClasses() { diff --git a/src/de/dhbwstuttgart/syntaxtree/Constructor.java b/src/de/dhbwstuttgart/syntaxtree/Constructor.java index 65a85522..9859a734 100644 --- a/src/de/dhbwstuttgart/syntaxtree/Constructor.java +++ b/src/de/dhbwstuttgart/syntaxtree/Constructor.java @@ -98,7 +98,7 @@ public class Constructor extends Method { } @Override - public void genByteCode(ClassGenerator cg) { + public void genByteCode(ClassGenerator cg, Class classObj) { this.genByteCode(cg, new InstructionList()); } // super statement muss drin sein diff --git a/src/de/dhbwstuttgart/syntaxtree/Method.java b/src/de/dhbwstuttgart/syntaxtree/Method.java index 8977fdd8..8a3370fb 100755 --- a/src/de/dhbwstuttgart/syntaxtree/Method.java +++ b/src/de/dhbwstuttgart/syntaxtree/Method.java @@ -1,6 +1,7 @@ // ino.module.Method.8564.package package de.dhbwstuttgart.syntaxtree; +import java.util.Arrays; // ino.end // ino.module.Method.8564.import import java.util.Enumeration; @@ -465,39 +466,7 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable } @Override - public void wandleRefTypeAttributes2GenericAttributes( - Menge classParalist) { - /* - * Menge paralist = new Menge();//Mit den Generischen Typen - * der Methode paralist.addAll(classParalist); - * paralist.addAll(this.genericMethodParameters); - * - * // Zuerst Returntype untersuchen Type returnType=getType(); Type - * pendantReturnType = null; if(returnType instanceof - * RefType)pendantReturnType = - * ((RefType)returnType).findGenericType(paralist, new - * Menge()); //GenericTypeVar - * pendantReturnType=ClassHelper.findGenericType(returnType, - * paralist,genericMethodParameters); if(pendantReturnType!=null){ - * //Wenn generisch, dann modifizieren setReturnType(pendantReturnType); - * } - * - * // Dann parameterlist untersuchen for(int - * par=0;par()); //GenericTypeVar - * pendantPara=ClassHelper.findGenericType - * (fpType,paralist,genericMethodParameters); if(pendantPara!=null){ - * //Wenn generisch, dann modifizieren fp.setType(pendantPara); } } - * - * // Zuletzt alle Lokalen Variablendeklarationen durchgehen - * if(block!=null){ - * block.wandleRefTypeAttributes2GenericAttributes(paralist - * ,genericMethodParameters); } - */ + public void wandleRefTypeAttributes2GenericAttributes(Menge classParalist) { } public void set_Method_Name(String string) { @@ -520,30 +489,11 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable // TypeCheck, falls es sich um einen RefType handelt: this.returntype = this.returntype.checkTYPE(localAss, this); - /* - * if(this.returntype!=null && (this.returntype instanceof RefType)&& - * !(this.returntype instanceof mycompiler.mytype.Void)){//Sonderfall - * der Methode: Ihr Typ darf Void definiert werden. Type replaceType = - * null; replaceType = ass.getTypeFor((RefType)this.returntype); - * if(replaceType == null)throw new - * TypeinferenceException("Der Typ "+this - * .getType().getName()+" ist nicht korrekt",this); this.returntype = - * replaceType; } - */ // Die Parameter zu den Assumptions hinzufügen: if (this.parameterlist != null) for (FormalParameter param : this.parameterlist) { param.setType(param.getType().checkTYPE(localAss, this)); - /* - * if(param.getType() instanceof RefType) { Type replaceType = - * null; replaceType = ass.getTypeFor((RefType)param.getType()); - * if(replaceType == null) throw new - * TypeinferenceException("Der Typ " - * +param.getType().getName()+" ist nicht korrekt",param); - * param.setType(replaceType); } - */ - localAss.addAssumption(new ParameterAssumption(param)); } ret.add(this.block.TYPEStmt(localAss)); @@ -587,107 +537,15 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable TypeAssumptions ret = new TypeAssumptions(); ret.addAssumption(new MethodAssumption(this, parentClass)); return ret; - /* - * TypeAssumptions assumptions = new TypeAssumptions(); this.assumedType - * = null; //if((this.get_Method_Name().equals(classmember.getName()) || - * this.get_Method_Name().equals("")) && - * ((this.getType().equals(new mycompiler.mytype.Void(0))) || - * this.getType() instanceof TypePlaceholder)){ - * if((this.get_Method_Name().equals(classmember.getName()) || - * this.get_Method_Name().equals(""))) { - * this.set_Method_Name(""); this.assumedType = new - * RefType(classmember.getName(),0); - * this.setReturnType(this.assumedType); this.assumedType = new - * RefType("void",0); //Return constructorReturnStatement = new - * Return(0,0); //constructorReturnStatement.retexpr = - * //this.block.statements.add(constructorReturnStatement); } //hoth: - * 06.04.2006 //durchlaufe Block und suche nach Objektvariablen fuer - * Offset-Markierung Iterator fieldVarIterator = - * assumptions.iterator(); while (fieldVarIterator.hasNext()) { //Wenn - * ObjektVariable CTypeAssumption dieAssum = fieldVarIterator.next(); - * if(dieAssum instanceof CInstVarTypeAssumption) { - * Class.isFirstLocalVarDecl=false; if(this.get_Block() != null) - * this.get_Block - * ().addOffsetsToAssumption(dieAssum,dieAssum.getIdentifier(),true); } - * } - * - * //methodList.addElement(method); - * - * //F�r V_fields_methods: CMethodTypeAssumption methodAssum - * = new CMethodTypeAssumption(classmember.getType(), - * this.get_Method_Name(), this.getType(), - * this.getParameterCount(),this.getLineNumber(),this.getOffset(),new - * Menge(),this.getGenericMethodParameters()); // Typannahme - * bauen... - * - * - * //Methode in V_Fields_methods ablegen //Dabei wird die - * OverloadedMethodID ermittelt !! //=> Method setzenuct - * - * - * assumptions.add(methodAssum); - * this.setOverloadedID(methodAssum.getHashSetKey - * ().getOverloadedMethodID()); - * - * - * //F�r die V_i: CTypeAssumptionSet localAssum = new - * CTypeAssumptionSet(); - * - * //Bauen... ParameterList parameterList = this.getParameterList(); - * if(parameterList!=null){ for(int i=0; - * i()); //fuege Offsets fuer Parameter hinzu, hoth: - * 06.04.2006 Class.isFirstLocalVarDecl=false; - * - * if(this.get_Block() != null) - * this.get_Block().addOffsetsToAssumption(paraAssum - * ,paraAssum.getIdentifier(),true); - * - * methodAssum.addParaAssumption(paraAssum); - * - * // F�r die V_i: CLocalVarTypeAssumption varAssum = new - * CLocalVarTypeAssumption(classmember.getName(), - * this.get_Method_Name(), this.getParameterCount(), - * this.getOverloadedID(),"1", para.get_Name(),para.getType(), - * para.getLineNumber(),para.getOffset(),new Menge()); - * localAssum.addElement(varAssum); - * //rememberLocals.addElement(varAssum); } } //...und - * hinzuf�gen: - * - * assumptions.add(localAssum);//Assumptions für lokale Variablen den - * Assumptions hinzufügen - * - * //Hier wird der Typ der als Assumption eingetragen wird in die - * Variable assumedType dieser Klasse geschrieben: if(this.assumedType - * == null) // Falls der Typ nicht schon gesetzt ist. Das ist der Fall, - * falls die Methode ein Konstruktor ist this.assumedType = - * methodAssum.getAssumedType(); - * - * return assumptions; - */ } @Override public void parserPostProcessing(SyntaxTreeNode parent) { if (this.getType() == null) this.setType(TypePlaceholder.fresh(this)); - // Bei dem Elterntyp der Methode darf es sich nur um eine Klasse - // handeln, daher Cast ohne Prüfung: - // Class parentClass = (Class)parent; if (this.returntype == null) this.returntype = TypePlaceholder.fresh(this); super.parserPostProcessing(parent); - /* - * this.returntype.parserPostProcessing(this); if(this.parameterlist != - * null){ for(FormalParameter fp : this.parameterlist){ - * fp.parserPostProcessing(this); } } for(GenericTypeVar gtv : - * this.getGenericParameter()){ gtv.parserPostProcessing(this); } - */ } @Override @@ -728,7 +586,6 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable DImethod.set_Name(withSignature); ret.set_DeclId(DImethod); Block tempBlock = new Block(); - // tempBlock.setType(new RefType(parent.getName(),0)); ret.set_Block(tempBlock); ret.parserPostProcessing(parent); return ret; @@ -751,7 +608,7 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable return super.equals(obj); } - public void genByteCode(ClassGenerator cg) { + public void genByteCode(ClassGenerator cg, Class classObj) { List typeInterferenceResults = cg.getTypeinferenceResults().getTypeReconstructions(this, cg); for(TypeinferenceResultSet t: typeInterferenceResults){ @@ -771,10 +628,15 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable argumentNames[i] = parameter.getIdentifier(); i++; } - - if(!createdMethods.contains(argumentTypes, new ArgumentTypeEquals())){ - createdMethods.add(argumentTypes); - } + } + + String nameAndSignature = get_Method_Name()+Arrays.toString(argumentTypes); + + Logger.getLogger("nameAndSignature").error(nameAndSignature, Section.CODEGEN); + + if(classObj.methodExists(nameAndSignature)){ + Logger.getLogger("methodExists").debug(this.toString(), Section.CODEGEN); + continue; } short constants = Constants.ACC_PUBLIC; //Per Definition ist jede Methode public @@ -787,45 +649,10 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable //Methode generieren und anfügen: cg.addMethod(method.createMethod(cg, getParameterList(), returnType, get_Block(), t)); + classObj.addMethod(nameAndSignature); - /* - short constants = Constants.ACC_PUBLIC; //Per Definition ist jede Methode public - if(this.modifiers != null && this.modifiers.includesModifier(new Static())) constants += Constants.ACC_STATIC; - - Type returnType = this.getType(); - - //Methode generieren: - MethodGenerator method = new MethodGenerator(constants, returnType.getBytecodeType(cg), argumentTypes , argumentNames, this.get_Method_Name(), parentClass.name, il, _cp); - - //Methode generieren und anfügen: - cg.addMethod(method.createMethod(cg, getParameterList(), returnType, get_Block())); - */ + Logger.getLogger("createMethod").debug(this.toString(), Section.CODEGEN); } } } - -class ArgumentTypeEquals implements Equal{ - - @Override - public boolean equal(org.apache.commons.bcel6.generic.Type[] a, org.apache.commons.bcel6.generic.Type[] b) { - if(a.length != b.length){ - Logger.getLogger("ArgumentTypeEquals").error("false(length)", Section.CODEGEN); - return false; - } - - for(int i = 0; i < a.length; i++){ - if(!a[i].equals(b[i])){ - String as = a[i].toString(); - String bs = b[i].toString(); - Logger.getLogger("ArgumentTypeEquals").error("false "+as+" != "+bs, Section.CODEGEN); - return false; - } - } - - Logger.getLogger("ArgumentTypeEquals").error("true", Section.CODEGEN); - - return true; - } - -} // ino.end diff --git a/test/bytecode/types/AutoOverloadingMultiResults.jav b/test/bytecode/types/AutoOverloadingMultiResults.jav new file mode 100644 index 00000000..d5c0f23f --- /dev/null +++ b/test/bytecode/types/AutoOverloadingMultiResults.jav @@ -0,0 +1,8 @@ +import java.util.Vector; + +class AutoOverloadingMultiResults{ + void method(Integer a) { + b; + b = 1; + } +} \ No newline at end of file diff --git a/test/bytecode/types/AutoOverloadingMultiResultsTest.java b/test/bytecode/types/AutoOverloadingMultiResultsTest.java new file mode 100644 index 00000000..37fa57a7 --- /dev/null +++ b/test/bytecode/types/AutoOverloadingMultiResultsTest.java @@ -0,0 +1,36 @@ +package bytecode.types; + +import static org.junit.Assert.*; + +import java.io.File; +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.Vector; + +import org.junit.Test; + +import org.junit.Ignore; + +import bytecode.SourceFileBytecodeTest; + + +public class AutoOverloadingMultiResultsTest extends SourceFileBytecodeTest{ + @Override + protected void init() { + testName = "AutoOverloadingMultiResults"; + rootDirectory = System.getProperty("user.dir")+"/test/bytecode/types/"; + } + + @Test + @Ignore + public void testConstruct() throws Exception{ + ClassLoader classLoader = getClassLoader(); + + Class cls = classLoader.loadClass(testName); + + Object obj = cls.newInstance(); + assertTrue(true); + } +} diff --git a/test/bytecode/types/AutoOverloadingVectorTest.java b/test/bytecode/types/AutoOverloadingVectorTest.java index aa4264b7..8dfe1d70 100644 --- a/test/bytecode/types/AutoOverloadingVectorTest.java +++ b/test/bytecode/types/AutoOverloadingVectorTest.java @@ -43,7 +43,7 @@ public class AutoOverloadingVectorTest extends SourceFileBytecodeTest{ URL url = file.toURL(); URL[] urls = new URL[]{url}; - Class string = classLoader.loadClass("java.lang.String"); + Class string = classLoader.loadClass("java%util%Vector%%java%lang%String%"); Class[] params = new Class[1]; params[0] = string; @@ -69,13 +69,13 @@ public class AutoOverloadingVectorTest extends SourceFileBytecodeTest{ URL url = file.toURL(); URL[] urls = new URL[]{url}; - Integer integer = new Integer(123); + Class integer = classLoader.loadClass("java%util%Vector%%java%lang%Integer%"); Class[] params = new Class[1]; - params[0] = integer.getClass(); + params[0] = integer; Method method = cls.getDeclaredMethod("method2", params); - method.invoke(obj, integer); + method.invoke(obj, integer.newInstance()); assertTrue(true); }catch(Exception e){ throw new RuntimeException(e); diff --git a/test/bytecode/types/ExtendsType.jav b/test/bytecode/types/ExtendsType.jav index 9b03238b..8bd50b6f 100644 --- a/test/bytecode/types/ExtendsType.jav +++ b/test/bytecode/types/ExtendsType.jav @@ -1,6 +1,6 @@ import java.util.Vector; -class SuperType{ +class ExtendsType{ Vector integerVector; void method() { diff --git a/test/bytecode/types/ExtendsTypeTest.java b/test/bytecode/types/ExtendsTypeTest.java index eb753389..a3560b9c 100644 --- a/test/bytecode/types/ExtendsTypeTest.java +++ b/test/bytecode/types/ExtendsTypeTest.java @@ -16,7 +16,7 @@ import bytecode.SourceFileBytecodeTest; public class ExtendsTypeTest extends SourceFileBytecodeTest{ @Override protected void init() { - testName = "SuperType"; + testName = "ExtendsType"; rootDirectory = System.getProperty("user.dir")+"/test/bytecode/types/"; } diff --git a/test/bytecode/types/ExtendsVectorStringTest.java b/test/bytecode/types/ExtendsVectorStringTest.java index d3ed9fb3..f92c61ad 100644 --- a/test/bytecode/types/ExtendsVectorStringTest.java +++ b/test/bytecode/types/ExtendsVectorStringTest.java @@ -4,6 +4,7 @@ import static org.junit.Assert.*; import java.lang.reflect.Constructor; import java.lang.reflect.Method; +import java.util.Vector; import org.junit.BeforeClass; import org.junit.Test; @@ -60,13 +61,13 @@ public class ExtendsVectorStringTest extends ASTBytecodeTest{ Object obj = cls.newInstance(); - Class stringClass = classLoader.loadClass("java.lang.Object"); + Class objectClass = classLoader.loadClass("java.lang.Object"); Class[] params = new Class[1]; - params[0] = stringClass; + params[0] = objectClass; Method method = cls.getDeclaredMethod("add", params); - method.invoke(obj, stringClass.newInstance()); + method.invoke(obj, objectClass.newInstance()); }catch(Exception e){ throw new RuntimeException(e); } @@ -83,5 +84,9 @@ public class ExtendsVectorStringTest extends ASTBytecodeTest{ public String getTestName() { return "ExtendsVectorString"; } + + class StringVector extends Vector{ + + } } From b4d7ab02eb4e42dec409c34e30af91ff10ecee0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrico=20Schr=C3=B6dter?= Date: Mon, 28 Mar 2016 10:33:04 +0200 Subject: [PATCH 2/5] =?UTF-8?q?-=20UnitTest=20f=C3=BCr=20MultiExtends=20Pr?= =?UTF-8?q?oblem=20-=20Verhindern=20von=20doppelten=20Methoden=20von=20Cla?= =?UTF-8?q?ss=20->=20ClassGen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bytecode/ClassGenerator.java | 20 +++++++++++ src/de/dhbwstuttgart/syntaxtree/Class.java | 9 ----- src/de/dhbwstuttgart/syntaxtree/Method.java | 5 --- .../AutoOverloadingMultiResultsTest.java | 1 - test/bytecode/types/MultiExtends.jav | 11 ++++++ test/bytecode/types/MultiExtendsTest.java | 35 +++++++++++++++++++ 6 files changed, 66 insertions(+), 15 deletions(-) create mode 100644 test/bytecode/types/MultiExtends.jav create mode 100644 test/bytecode/types/MultiExtendsTest.java diff --git a/src/de/dhbwstuttgart/bytecode/ClassGenerator.java b/src/de/dhbwstuttgart/bytecode/ClassGenerator.java index a13ed914..ef556c50 100644 --- a/src/de/dhbwstuttgart/bytecode/ClassGenerator.java +++ b/src/de/dhbwstuttgart/bytecode/ClassGenerator.java @@ -1,8 +1,12 @@ package de.dhbwstuttgart.bytecode; +import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; import java.util.Map; +import java.util.Vector; import org.apache.commons.bcel6.classfile.BootstrapMethod; import org.apache.commons.bcel6.classfile.BootstrapMethods; @@ -10,6 +14,7 @@ import org.apache.commons.bcel6.classfile.ConstantPool; import org.apache.commons.bcel6.classfile.InnerClass; import org.apache.commons.bcel6.classfile.InnerClasses; import org.apache.commons.bcel6.classfile.JavaClass; +import org.apache.commons.bcel6.classfile.Method; import org.apache.commons.bcel6.classfile.Signature; import org.apache.commons.bcel6.generic.ClassGen; import org.apache.commons.bcel6.generic.ConstantPoolGen; @@ -34,6 +39,7 @@ public class ClassGenerator extends ClassGen{ private Menge usedTPHs = new Menge<>(); private Map extraClasses = new HashMap<>(); + private List methodsNamesAndTypes = new LinkedList<>(); public ClassGenerator(String name, Type superClass, String string, short accessflags, String[] strings, TypeinferenceResults typeinferenceResults) { super(name,superClass.get_Name(),string,accessflags,strings, new DHBWConstantPoolGen()); @@ -162,6 +168,20 @@ public class ClassGenerator extends ClassGen{ public TypeinferenceResults getTypeinferenceResults() { return tiResult; } + + @Override + public void addMethod(Method m) { + String methodNameAndTypes = m.getName()+Arrays.toString(m.getArgumentTypes()); + + if(methodsNamesAndTypes.contains(methodNameAndTypes)){ + return; + } + + methodsNamesAndTypes.add(methodNameAndTypes); + super.addMethod(m); + } + + diff --git a/src/de/dhbwstuttgart/syntaxtree/Class.java b/src/de/dhbwstuttgart/syntaxtree/Class.java index a746b8c9..09c33f80 100755 --- a/src/de/dhbwstuttgart/syntaxtree/Class.java +++ b/src/de/dhbwstuttgart/syntaxtree/Class.java @@ -74,7 +74,6 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I protected UsedId pkgName; protected Modifiers modifiers; protected String name; - private List methodSignaturesAndNames = new LinkedList<>(); /** * @@ -1042,14 +1041,6 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I public boolean isInterface(){ return false; } - - protected boolean methodExists(String nameAndSignature) { - return methodSignaturesAndNames.contains(nameAndSignature); - } - - protected void addMethod(String nameAndSignature) { - methodSignaturesAndNames.add(nameAndSignature); - } /* private Collection getGenericClasses() { diff --git a/src/de/dhbwstuttgart/syntaxtree/Method.java b/src/de/dhbwstuttgart/syntaxtree/Method.java index 8a3370fb..ebed1381 100755 --- a/src/de/dhbwstuttgart/syntaxtree/Method.java +++ b/src/de/dhbwstuttgart/syntaxtree/Method.java @@ -634,10 +634,6 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable Logger.getLogger("nameAndSignature").error(nameAndSignature, Section.CODEGEN); - if(classObj.methodExists(nameAndSignature)){ - Logger.getLogger("methodExists").debug(this.toString(), Section.CODEGEN); - continue; - } short constants = Constants.ACC_PUBLIC; //Per Definition ist jede Methode public if(this.modifiers != null && this.modifiers.includesModifier(new Static())) constants += Constants.ACC_STATIC; @@ -649,7 +645,6 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable //Methode generieren und anfügen: cg.addMethod(method.createMethod(cg, getParameterList(), returnType, get_Block(), t)); - classObj.addMethod(nameAndSignature); Logger.getLogger("createMethod").debug(this.toString(), Section.CODEGEN); } diff --git a/test/bytecode/types/AutoOverloadingMultiResultsTest.java b/test/bytecode/types/AutoOverloadingMultiResultsTest.java index 37fa57a7..ce34f3f7 100644 --- a/test/bytecode/types/AutoOverloadingMultiResultsTest.java +++ b/test/bytecode/types/AutoOverloadingMultiResultsTest.java @@ -24,7 +24,6 @@ public class AutoOverloadingMultiResultsTest extends SourceFileBytecodeTest{ } @Test - @Ignore public void testConstruct() throws Exception{ ClassLoader classLoader = getClassLoader(); diff --git a/test/bytecode/types/MultiExtends.jav b/test/bytecode/types/MultiExtends.jav new file mode 100644 index 00000000..d4ada054 --- /dev/null +++ b/test/bytecode/types/MultiExtends.jav @@ -0,0 +1,11 @@ +import java.util.Vector; + +class MultiExtends{ + Integer method(Vector a){ + return method2(a); + } + + Integer method2(Vector b){ + return 1; + } +} \ No newline at end of file diff --git a/test/bytecode/types/MultiExtendsTest.java b/test/bytecode/types/MultiExtendsTest.java new file mode 100644 index 00000000..066ad1ff --- /dev/null +++ b/test/bytecode/types/MultiExtendsTest.java @@ -0,0 +1,35 @@ +package bytecode.types; + +import static org.junit.Assert.*; + +import java.io.File; +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.Vector; + +import org.junit.Test; + +import org.junit.Ignore; + +import bytecode.SourceFileBytecodeTest; + + +public class MultiExtendsTest extends SourceFileBytecodeTest{ + @Override + protected void init() { + testName = "MultiExtends"; + rootDirectory = System.getProperty("user.dir")+"/test/bytecode/types/"; + } + + @Test + public void testConstruct() throws Exception{ + ClassLoader classLoader = getClassLoader(); + + Class cls = classLoader.loadClass(testName); + + Object obj = cls.newInstance(); + assertTrue(true); + } +} From b51079280e503dfdb86bdd57aa42cf383840caba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrico=20Schr=C3=B6dter?= Date: Wed, 30 Mar 2016 10:09:17 +0200 Subject: [PATCH 3/5] - New Statement, Problem Syntax des Klassennamens --- src/de/dhbwstuttgart/syntaxtree/Class.java | 2 +- src/de/dhbwstuttgart/syntaxtree/Field.java | 3 +- .../syntaxtree/FieldDeclaration.java | 6 ++-- .../syntaxtree/FormalParameter.java | 5 +-- .../syntaxtree/SyntaxTreeNode.java | 6 ++-- .../syntaxtree/statement/Block.java | 2 +- .../syntaxtree/statement/LocalVarDecl.java | 2 +- .../syntaxtree/statement/NewClass.java | 4 +-- .../syntaxtree/statement/Statement.java | 2 +- .../syntaxtree/type/RefType.java | 7 ++++ test/bytecode/Parameter.jav | 3 ++ test/bytecode/ParameterTest.java | 34 +++++++++++++++++++ test/bytecode/types/AutoOverloadingVector.jav | 2 +- 13 files changed, 63 insertions(+), 15 deletions(-) create mode 100644 test/bytecode/Parameter.jav create mode 100644 test/bytecode/ParameterTest.java diff --git a/src/de/dhbwstuttgart/syntaxtree/Class.java b/src/de/dhbwstuttgart/syntaxtree/Class.java index 09c33f80..e2d97772 100755 --- a/src/de/dhbwstuttgart/syntaxtree/Class.java +++ b/src/de/dhbwstuttgart/syntaxtree/Class.java @@ -993,7 +993,7 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I } @Override - public String getDescription(){ + public String getDescription(ClassGenerator cg, TypeinferenceResultSet rs){ return "class "+this.getName(); } @Override diff --git a/src/de/dhbwstuttgart/syntaxtree/Field.java b/src/de/dhbwstuttgart/syntaxtree/Field.java index fabc1328..25e97f78 100644 --- a/src/de/dhbwstuttgart/syntaxtree/Field.java +++ b/src/de/dhbwstuttgart/syntaxtree/Field.java @@ -16,6 +16,7 @@ import de.dhbwstuttgart.typeinference.JavaCodeResult; import de.dhbwstuttgart.typeinference.ResultSet; import de.dhbwstuttgart.typeinference.TypeInsertable; import de.dhbwstuttgart.typeinference.Typeable; +import de.dhbwstuttgart.typeinference.TypeinferenceResultSet; import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions; import de.dhbwstuttgart.typeinference.typedeployment.TypeInsertPoint; @@ -107,7 +108,7 @@ public abstract class Field extends GTVDeclarationContext implements TypeInserta } @Override - public String getDescription(){ + public String getDescription(ClassGenerator cg, TypeinferenceResultSet rs){ return this.getIdentifier(); } diff --git a/src/de/dhbwstuttgart/syntaxtree/FieldDeclaration.java b/src/de/dhbwstuttgart/syntaxtree/FieldDeclaration.java index 7aba86b2..08f0df8d 100644 --- a/src/de/dhbwstuttgart/syntaxtree/FieldDeclaration.java +++ b/src/de/dhbwstuttgart/syntaxtree/FieldDeclaration.java @@ -173,7 +173,7 @@ public class FieldDeclaration extends Field{ */ public InstructionList genByteCode(ClassGenerator cg, TypeinferenceResultSet rs) { //Das Feld an die Klasse anfügen: - FieldGen field = new FieldGen(0, this.getType().getBytecodeType(cg, rs), this.getDescription(), cg.getConstantPool()); + FieldGen field = new FieldGen(0, this.getType().getBytecodeType(cg, rs), this.getDescription(cg, rs), cg.getConstantPool()); field.addAttribute(cg.getInstructionFactory().createSignatureAttribute(this.getType().getBytecodeSignature(cg, rs))); cg.addField(field.getField()); @@ -183,11 +183,11 @@ public class FieldDeclaration extends Field{ il.append(this.wert.genByteCode(cg, rs)); FieldInstruction putFieldInstruction = cg.getInstructionFactory().createFieldAccess(this.getParentClass().getName().toString(), - this.getDescription(), this.getType().getBytecodeType(cg, rs), Constants.PUTFIELD); + this.getDescription(cg, rs), this.getType().getBytecodeType(cg, rs), Constants.PUTFIELD); il.append(putFieldInstruction ); return il; } - + /*@Override public void genByteCode(ClassGen cg) { // TODO Auto-generated method stub diff --git a/src/de/dhbwstuttgart/syntaxtree/FormalParameter.java b/src/de/dhbwstuttgart/syntaxtree/FormalParameter.java index d3df4039..f897ac05 100755 --- a/src/de/dhbwstuttgart/syntaxtree/FormalParameter.java +++ b/src/de/dhbwstuttgart/syntaxtree/FormalParameter.java @@ -4,7 +4,7 @@ package de.dhbwstuttgart.syntaxtree; // ino.module.FormalParameter.8561.import import de.dhbwstuttgart.typeinference.Menge; - +import de.dhbwstuttgart.bytecode.ClassGenerator; import de.dhbwstuttgart.logger.Logger; import de.dhbwstuttgart.syntaxtree.misc.DeclId; @@ -14,6 +14,7 @@ import de.dhbwstuttgart.typeinference.JavaCodeResult; import de.dhbwstuttgart.typeinference.ResultSet; import de.dhbwstuttgart.typeinference.TypeInsertable; import de.dhbwstuttgart.typeinference.Typeable; +import de.dhbwstuttgart.typeinference.TypeinferenceResultSet; import de.dhbwstuttgart.typeinference.exceptions.NotImplementedException; import de.dhbwstuttgart.typeinference.exceptions.TypeinferenceException; import de.dhbwstuttgart.typeinference.typedeployment.TypeInsertPoint; @@ -233,7 +234,7 @@ public class FormalParameter extends SyntaxTreeNode implements Typeable, TypeIns } @Override - public String getDescription() { + public String getDescription(ClassGenerator cg, TypeinferenceResultSet rs){ String ret = ""; if(this.getType() != null && !(this.getType() instanceof TypePlaceholder)){ ret += this.getType().getBytecodeSignature(null, null); diff --git a/src/de/dhbwstuttgart/syntaxtree/SyntaxTreeNode.java b/src/de/dhbwstuttgart/syntaxtree/SyntaxTreeNode.java index f76249ba..b557ed74 100644 --- a/src/de/dhbwstuttgart/syntaxtree/SyntaxTreeNode.java +++ b/src/de/dhbwstuttgart/syntaxtree/SyntaxTreeNode.java @@ -3,6 +3,7 @@ package de.dhbwstuttgart.syntaxtree; import org.apache.commons.bcel6.generic.ClassGen; import de.dhbwstuttgart.typeinference.Menge; +import de.dhbwstuttgart.bytecode.ClassGenerator; import de.dhbwstuttgart.core.IItemWithOffset; import de.dhbwstuttgart.syntaxtree.type.GenericTypeVar; import de.dhbwstuttgart.syntaxtree.type.Type; @@ -10,6 +11,7 @@ import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.typeinference.Pair; import de.dhbwstuttgart.typeinference.ResultSet; import de.dhbwstuttgart.typeinference.TypeInsertable; +import de.dhbwstuttgart.typeinference.TypeinferenceResultSet; import de.dhbwstuttgart.typeinference.exceptions.DebugException; import de.dhbwstuttgart.typeinference.exceptions.TypeinferenceException; import de.dhbwstuttgart.typeinference.typedeployment.GenericTypeInsertPoint; @@ -55,7 +57,7 @@ public abstract class SyntaxTreeNode implements IItemWithOffset{ * Eine Beschreibung/Name des SyntaxTree-Nodes * @return */ - public String getDescription(){ + public String getDescription(ClassGenerator cg, TypeinferenceResultSet rs){ return this.toString(); } @@ -64,7 +66,7 @@ public abstract class SyntaxTreeNode implements IItemWithOffset{ public boolean equals(Object object){ if(!(object instanceof SyntaxTreeNode))return false; SyntaxTreeNode equal = (SyntaxTreeNode)object; - if(!equal.getDescription().equals(this.getDescription()))return false; + if(!equal.getDescription(null, null).equals(this.getDescription(null, null)))return false; if(this.getParent()!=null) if(!this.getParent().equals(equal.getParent()))return false; //auch das Elternelement überprüfen. return true; diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Block.java b/src/de/dhbwstuttgart/syntaxtree/statement/Block.java index a149a0cf..2625da53 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Block.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Block.java @@ -252,7 +252,7 @@ public class Block extends Statement } @Override - public String getDescription(){ + public String getDescription(ClassGenerator cg, TypeinferenceResultSet rs){ return "Block"; } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/LocalVarDecl.java b/src/de/dhbwstuttgart/syntaxtree/statement/LocalVarDecl.java index e03c1f81..423e143f 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/LocalVarDecl.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/LocalVarDecl.java @@ -379,7 +379,7 @@ public class LocalVarDecl extends Statement implements TypeInsertable } @Override - public String getDescription() { + public String getDescription(ClassGenerator cg, TypeinferenceResultSet rs){ if(this.getType() == null)return "no type " + declid.toString(); if(this.getType() instanceof TypePlaceholder)return declid.toString(); return this.getType().toString() + " " + declid.toString(); diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/NewClass.java b/src/de/dhbwstuttgart/syntaxtree/statement/NewClass.java index eefc048b..079f652c 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/NewClass.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/NewClass.java @@ -202,11 +202,11 @@ public class NewClass extends Expr if(arglist!=null){ il.append(arglist.generateBytecode(_cg, rs)); - il.append(_cg.getInstructionFactory().createInvoke(this.getType().getDescription(), "", + il.append(_cg.getInstructionFactory().createInvoke(this.getType().getDescription(_cg, rs), "", org.apache.commons.bcel6.generic.Type.VOID, this.arglist.getBytecodeTypeList(_cg, rs), Constants.INVOKESPECIAL)); }else{ - il.append(_cg.getInstructionFactory().createInvoke(this.getType().getDescription(), "", + il.append(_cg.getInstructionFactory().createInvoke(this.getType().getDescription(_cg, rs), "", org.apache.commons.bcel6.generic.Type.VOID, new org.apache.commons.bcel6.generic.Type[]{}, Constants.INVOKESPECIAL)); } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Statement.java b/src/de/dhbwstuttgart/syntaxtree/statement/Statement.java index d6e2e9f8..4e1ce702 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Statement.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Statement.java @@ -119,7 +119,7 @@ public abstract class Statement extends SyntaxTreeNode implements IItemWithOffse public abstract JavaCodeResult printJavaCode(ResultSet resultSet); @Override - public String getDescription(){ + public String getDescription(ClassGenerator cg, TypeinferenceResultSet rs){ return this.printJavaCode(new ResultSet(new Menge())).toString(); } diff --git a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java index ba03d583..0e8199e7 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java @@ -874,6 +874,13 @@ public class RefType extends ObjectType implements IMatchable public GenericClassType getGenericClassType(){ return new GenericClassType(getName().toString(), getParaList(), parent, getOffset()); } + + @Override + public String getDescription(ClassGenerator cg, TypeinferenceResultSet rs) { + return getBytecodeSignature(cg, rs); + } + + } // ino.end diff --git a/test/bytecode/Parameter.jav b/test/bytecode/Parameter.jav new file mode 100644 index 00000000..18517679 --- /dev/null +++ b/test/bytecode/Parameter.jav @@ -0,0 +1,3 @@ +class Parameter{ + public String param; +} \ No newline at end of file diff --git a/test/bytecode/ParameterTest.java b/test/bytecode/ParameterTest.java new file mode 100644 index 00000000..cd06eeef --- /dev/null +++ b/test/bytecode/ParameterTest.java @@ -0,0 +1,34 @@ +package bytecode; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.IOException; + +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 ParameterTest { + + public final static String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/"; + public final static String testFile = "Parameter.jav"; + public final static String outputFile = "Parameter.class"; + + @Test + public void test() { + SingleClassTester.compileToBytecode(rootDirectory+testFile, rootDirectory+outputFile); + } + +} diff --git a/test/bytecode/types/AutoOverloadingVector.jav b/test/bytecode/types/AutoOverloadingVector.jav index 4a3b1197..eb65ae4c 100644 --- a/test/bytecode/types/AutoOverloadingVector.jav +++ b/test/bytecode/types/AutoOverloadingVector.jav @@ -1,6 +1,6 @@ import java.util.Vector; -class AutoOverloadingVector{ +class AutoOverloadingVector{ method2(p){ method(p); } From 5b07d580934fa934db9590d888f12ea10de36b78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrico=20Schr=C3=B6dter?= Date: Wed, 30 Mar 2016 10:14:05 +0200 Subject: [PATCH 4/5] - Klassenvariable ohne Wert --- src/de/dhbwstuttgart/syntaxtree/FieldDeclaration.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/de/dhbwstuttgart/syntaxtree/FieldDeclaration.java b/src/de/dhbwstuttgart/syntaxtree/FieldDeclaration.java index 08f0df8d..7cc3c54b 100644 --- a/src/de/dhbwstuttgart/syntaxtree/FieldDeclaration.java +++ b/src/de/dhbwstuttgart/syntaxtree/FieldDeclaration.java @@ -180,7 +180,13 @@ public class FieldDeclaration extends Field{ //Die Felddekleration an den Konstruktor anhängen: InstructionList il = new InstructionList(); il.append(new This(this).genByteCode(cg, rs)); - il.append(this.wert.genByteCode(cg, rs)); + + if(wert != null){ + il.append(this.wert.genByteCode(cg, rs)); + }else{ + //todo: default wert? + } + FieldInstruction putFieldInstruction = cg.getInstructionFactory().createFieldAccess(this.getParentClass().getName().toString(), this.getDescription(cg, rs), this.getType().getBytecodeType(cg, rs), Constants.PUTFIELD); From 8d8e27c84cb5643264d294fcd53d441ecfaf3259 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrico=20Schr=C3=B6dter?= Date: Thu, 31 Mar 2016 08:52:23 +0200 Subject: [PATCH 5/5] - Bugfix new Statement, Deskription darf nicht mit Semikolon enden --- src/de/dhbwstuttgart/syntaxtree/type/RefType.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java index 0e8199e7..aded666a 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java @@ -877,11 +877,9 @@ public class RefType extends ObjectType implements IMatchable @Override public String getDescription(ClassGenerator cg, TypeinferenceResultSet rs) { - return getBytecodeSignature(cg, rs); + String signature = getBytecodeSignature(cg, rs); + return signature.substring(1, signature.length()-1); } - - - } // ino.end