From e9607e37b101ac3d5d103cd8b0e942bf7e807453 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrico=20Schr=C3=B6dter?= Date: Fri, 30 Oct 2015 12:29:43 +0100 Subject: [PATCH] =?UTF-8?q?ASTFactory=20createClass=20setzt=20SuperType=20?= =?UTF-8?q?GenericClass=20erh=C3=A4lt=20richtigen=20SuperType=20Tests=20f?= =?UTF-8?q?=C3=BCr=20=3F=20extends=20und=20=3F=20super=20angelegt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/de/dhbwstuttgart/syntaxtree/Class.java | 4 +-- .../syntaxtree/factory/ASTFactory.java | 12 +++---- .../syntaxtree/type/GenericClassType.java | 23 +++++++++++++ .../syntaxtree/type/RefType.java | 25 ++++----------- test/bytecode/types/ExtendsType.jav | 13 ++++++++ test/bytecode/types/ExtendsType.java | 31 ++++++++++++++++++ test/bytecode/types/SuperType.jav | 13 ++++++++ test/bytecode/types/SuperType.java | 32 +++++++++++++++++++ 8 files changed, 126 insertions(+), 27 deletions(-) create mode 100644 src/de/dhbwstuttgart/syntaxtree/type/GenericClassType.java create mode 100644 test/bytecode/types/ExtendsType.jav create mode 100644 test/bytecode/types/ExtendsType.java create mode 100644 test/bytecode/types/SuperType.jav create mode 100644 test/bytecode/types/SuperType.java diff --git a/src/de/dhbwstuttgart/syntaxtree/Class.java b/src/de/dhbwstuttgart/syntaxtree/Class.java index 53adfd40..87abe745 100755 --- a/src/de/dhbwstuttgart/syntaxtree/Class.java +++ b/src/de/dhbwstuttgart/syntaxtree/Class.java @@ -240,7 +240,7 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I * @param modifiers * @param supertypeGenPara - Eine Liste von Namen, welche die Generischen Parameter der Klasse darstellen. */ - public Class(String name, RefType superClass, Modifiers modifiers, + public Class(String name, Type superClass, Modifiers modifiers, Menge supertypeGenPara) { this(name,superClass,modifiers,0); if(supertypeGenPara == null)return; @@ -252,7 +252,7 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I this.setGenericParameter(new GenericDeclarationList(gtvs,0)); } - public Class(String name, RefType superClass, Modifiers mod, int offset){ + public Class(String name, Type superClass, Modifiers mod, int offset){ this(name,mod,offset); if(superClass == null)this.superClass = new Class("java.lang.Object",-1).getType(); else this.superClass = superClass; diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java index f39e6968..b712296b 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java @@ -8,8 +8,11 @@ import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode; import de.dhbwstuttgart.syntaxtree.misc.DeclId; import de.dhbwstuttgart.syntaxtree.misc.UsedId; +import de.dhbwstuttgart.syntaxtree.modifier.Modifiers; import de.dhbwstuttgart.syntaxtree.statement.Block; import de.dhbwstuttgart.syntaxtree.statement.SuperCall; +import de.dhbwstuttgart.syntaxtree.type.RefType; +import de.dhbwstuttgart.syntaxtree.type.Type; import de.dhbwstuttgart.typeinference.Menge; import de.dhbwstuttgart.typeinference.ResultSet; import de.dhbwstuttgart.typeinference.TypeinferenceResultSet; @@ -48,15 +51,12 @@ public class ASTFactory { return new Constructor(method, superClass); } - public static Class createClass(String className, String superClass, SourceFile parent) { + public static Class createClass(String className, Type type, Modifiers modifiers, Menge supertypeGenPara, SourceFile parent) { // TODO bytecode createClass - Class generatedClass = new Class(className, 0); + //String name, RefType superClass, Modifiers modifiers, Menge supertypeGenPara + Class generatedClass = new Class(className, type, modifiers, supertypeGenPara); generatedClass.addField(ASTFactory.createEmptyConstructor(generatedClass)); - if(superClass != null){ - generatedClass.superclassid = UsedId.createFromQualifiedName(superClass, 0); - } - generatedClass.parserPostProcessing(parent); return generatedClass; diff --git a/src/de/dhbwstuttgart/syntaxtree/type/GenericClassType.java b/src/de/dhbwstuttgart/syntaxtree/type/GenericClassType.java new file mode 100644 index 00000000..2c83b0c9 --- /dev/null +++ b/src/de/dhbwstuttgart/syntaxtree/type/GenericClassType.java @@ -0,0 +1,23 @@ +package de.dhbwstuttgart.syntaxtree.type; + +import de.dhbwstuttgart.bytecode.ClassGenerator; +import de.dhbwstuttgart.parser.JavaClassName; +import de.dhbwstuttgart.syntaxtree.Class; +import de.dhbwstuttgart.syntaxtree.SourceFile; +import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode; +import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; +import de.dhbwstuttgart.typeinference.Menge; +import de.dhbwstuttgart.typeinference.ResultSet; +import de.dhbwstuttgart.typeinference.TypeinferenceResultSet; + +public class GenericClassType extends RefType{ + + public GenericClassType(String fullyQualifiedName, Menge parameter, SyntaxTreeNode parent, int offset) { + super(fullyQualifiedName, parameter, parent, offset); + } + + @Override + public String getBytecodeSignature(ClassGenerator cg) { + return "L"+getTypeName()+";"; + } +} diff --git a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java index 9f33148b..5b877197 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java @@ -833,27 +833,10 @@ public class RefType extends ObjectType implements IMatchable @Override public String getBytecodeSignature(ClassGenerator cg) { - /* - String paramString = ""; - if(this.parameter != null && this.parameter.size()>0){ - paramString+="<"; - Iterator it = this.parameter.iterator(); - while(it.hasNext()){ - Type param = it.next(); - paramString+=param.getBytecodeSignature(cg); - } - paramString+=">"; - } - String typeSignature = this.getBytecodeType(cg).getSignature(); - typeSignature = typeSignature.substring(0, typeSignature.length()-1); - return typeSignature+paramString+";"; - */ - //TODO: bytecode woher bekommt ich die parent klasse String combinedType = getCombinedType(cg); if(!combinedType.equals(getName().toString())){ - // TODO bytecode statt getParentClass die eigene Class?!?! - Class generatedClass = ASTFactory.createClass(getCombinedType(cg), getName().toString(), new SourceFile()); + Class generatedClass = ASTFactory.createClass(getCombinedType(cg), getGenericClassType(), null, null, new SourceFile()); cg.addExtraClass(generatedClass.genByteCode(new TypeinferenceResultSet(generatedClass, new Menge<>(), new ResultSet())).getByteCode()); } @@ -874,7 +857,7 @@ public class RefType extends ObjectType implements IMatchable }else if(type instanceof TypePlaceholder){ sb.append(((TypePlaceholder) type).getBytecodeType(cg).toString().replace(".", "%")); }else{ - sb.append(((TypePlaceholder) type).getBytecodeType(cg).toString().replace(".", "%")); + sb.append(type.getBytecodeType(cg).toString().replace(".", "%")); } sb.append("%"); @@ -886,6 +869,10 @@ public class RefType extends ObjectType implements IMatchable return sb.toString(); } + public GenericClassType getGenericClassType(){ + return new GenericClassType(getName().toString(), getParaList(), parent, getOffset()); + } + } // ino.end diff --git a/test/bytecode/types/ExtendsType.jav b/test/bytecode/types/ExtendsType.jav new file mode 100644 index 00000000..9b03238b --- /dev/null +++ b/test/bytecode/types/ExtendsType.jav @@ -0,0 +1,13 @@ +import java.util.Vector; + +class SuperType{ + Vector integerVector; + + void method() { + method(integerVector); + } + + void method(Vector v) { + + } +} \ No newline at end of file diff --git a/test/bytecode/types/ExtendsType.java b/test/bytecode/types/ExtendsType.java new file mode 100644 index 00000000..8821bdf1 --- /dev/null +++ b/test/bytecode/types/ExtendsType.java @@ -0,0 +1,31 @@ +package bytecode.types; + +import static org.junit.Assert.*; + +import java.io.File; +import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.Vector; + +import org.junit.Test; + +import bytecode.BytecodeTest; + +public class ExtendsType extends BytecodeTest{ + @Override + protected void init() { + testName = "SuperType"; + 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); + } +} diff --git a/test/bytecode/types/SuperType.jav b/test/bytecode/types/SuperType.jav new file mode 100644 index 00000000..f029c2d0 --- /dev/null +++ b/test/bytecode/types/SuperType.jav @@ -0,0 +1,13 @@ +import java.util.Vector; + +class SuperType{ + Vector numberVector; + + void method() { + method(numberVector); + } + + void method(Vector v) { + + } +} \ No newline at end of file diff --git a/test/bytecode/types/SuperType.java b/test/bytecode/types/SuperType.java new file mode 100644 index 00000000..425ba200 --- /dev/null +++ b/test/bytecode/types/SuperType.java @@ -0,0 +1,32 @@ +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 bytecode.BytecodeTest; + +public class SuperType extends BytecodeTest{ + @Override + protected void init() { + testName = "ExtendsType"; + 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); + } +}