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] =?UTF-8?q?-=20UnitTest=20f=C3=BCr=20MultiExtends=20Proble?= =?UTF-8?q?m=20-=20Verhindern=20von=20doppelten=20Methoden=20von=20Class?= =?UTF-8?q?=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); + } +}