From c5f0e73e50e40f77a296d431d7d88796ebb37f4a Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 23 Sep 2015 15:30:35 +0200 Subject: [PATCH] Identity implementiert --- .../bytecode/DHBWInstructionFactory.java | 7 ++++++- .../bytecode/MethodGenerator.java | 6 +++--- .../dhbwstuttgart/syntaxtree/Constructor.java | 15 +++++---------- .../syntaxtree/FieldDeclaration.java | 7 ++++--- src/de/dhbwstuttgart/syntaxtree/Method.java | 2 +- .../statement/LambdaExpression.java | 19 +++---------------- .../syntaxtree/type/ExtendsWildcardType.java | 2 ++ .../dhbwstuttgart/syntaxtree/type/FunN.java | 4 ++-- .../syntaxtree/type/RefType.java | 11 +++-------- .../syntaxtree/type/SuperWildcardType.java | 13 ++++++------- .../dhbwstuttgart/syntaxtree/type/Type.java | 16 +++++++++++----- .../syntaxtree/type/TypePlaceholder.java | 19 ++++++++++--------- .../syntaxtree/type/WildcardType.java | 8 +------- test/bytecode/Test.java | 3 ++- 14 files changed, 59 insertions(+), 73 deletions(-) diff --git a/src/de/dhbwstuttgart/bytecode/DHBWInstructionFactory.java b/src/de/dhbwstuttgart/bytecode/DHBWInstructionFactory.java index 0ae6a428..2c69189d 100644 --- a/src/de/dhbwstuttgart/bytecode/DHBWInstructionFactory.java +++ b/src/de/dhbwstuttgart/bytecode/DHBWInstructionFactory.java @@ -4,6 +4,7 @@ import java.awt.List; import java.util.ArrayList; import org.apache.commons.bcel6.Constants; +import org.apache.commons.bcel6.classfile.Attribute; import org.apache.commons.bcel6.classfile.BootstrapMethod; import org.apache.commons.bcel6.classfile.ConstantInvokeDynamic; import org.apache.commons.bcel6.classfile.ConstantMethodHandle; @@ -139,7 +140,11 @@ public class DHBWInstructionFactory extends InstructionFactory{ return InstructionFactory.createLoad(bytecodeType, index); } - public Type createObjectType() { + public static Type createObjectType() { return new org.apache.commons.bcel6.generic.ObjectType("java.lang.Object"); } + + public Attribute createSignatureAttribute(String signature) { + return new Signature(cp.addUtf8("Signature"),2,cp.addUtf8(signature),cp.getConstantPool()); + } } diff --git a/src/de/dhbwstuttgart/bytecode/MethodGenerator.java b/src/de/dhbwstuttgart/bytecode/MethodGenerator.java index 2b592a4b..f783c0ef 100644 --- a/src/de/dhbwstuttgart/bytecode/MethodGenerator.java +++ b/src/de/dhbwstuttgart/bytecode/MethodGenerator.java @@ -39,11 +39,11 @@ public class MethodGenerator extends MethodGen{ //Die korrekte Signatur für die Methode anhängen. Hier sind dann auch die Parameter von RefTypes enthalten: String paramTypesSig = "("; for(FormalParameter p : parameter){ - paramTypesSig += p.getType().getBytecodeMethodSignature(cg); + paramTypesSig += p.getType().getBytecodeSignature(cg); } paramTypesSig += ")"; - String retTypeSig = retType.getBytecodeMethodSignature(cg); - method.addAttribute(new Signature(cp.addUtf8("Signature"),2,cp.addUtf8(paramTypesSig+retTypeSig),cp.getConstantPool())); + String retTypeSig = retType.getBytecodeSignature(cg); + method.addAttribute(factory.createSignatureAttribute(paramTypesSig+retTypeSig)); return method.getMethod(); } diff --git a/src/de/dhbwstuttgart/syntaxtree/Constructor.java b/src/de/dhbwstuttgart/syntaxtree/Constructor.java index 737feb95..251124cd 100644 --- a/src/de/dhbwstuttgart/syntaxtree/Constructor.java +++ b/src/de/dhbwstuttgart/syntaxtree/Constructor.java @@ -13,6 +13,7 @@ import de.dhbwstuttgart.typeinference.Menge; import de.dhbwstuttgart.bytecode.ClassGenerator; import de.dhbwstuttgart.bytecode.DHBWConstantPoolGen; import de.dhbwstuttgart.bytecode.DHBWInstructionFactory; +import de.dhbwstuttgart.bytecode.MethodGenerator; import de.dhbwstuttgart.myexception.JVMCodeException; import de.dhbwstuttgart.parser.JavaClassName; import de.dhbwstuttgart.syntaxtree.misc.DeclId; @@ -65,25 +66,19 @@ public class Constructor extends Method { InstructionList il = new InstructionList(); //sollte nicht new sein sondern aus Block kommen Class parentClass = this.getParentClass(); - //TODO: Alles dynamisch gestalten - //init darf nur drin stehen, wenn Konstruktor; - this.method = new MethodGen(Constants.ACC_PUBLIC, this.getType().getBytecodeType(cg), org.apache.commons.bcel6.generic.Type.NO_ARGS , new String[] { }, "", parentClass.name, il, _cp); + MethodGenerator method = new MethodGenerator(Constants.ACC_PUBLIC, this.getType().getBytecodeType(cg), org.apache.commons.bcel6.generic.Type.NO_ARGS , new String[] { }, "", parentClass.name, il, _cp); - //Iteration �ber Block starten + //FieldInitializations an Block anfügen Block block = this.get_Block(); if(block.statements.firstElement() instanceof SuperCall){ block.statements.insertElementAt(new BytecodeInstructionBlock(fieldInitializations), 1); }else{ throw new DebugException("Fehlender SuperCall im Konstruktor"); } - InstructionList blockInstructions = block.genByteCode(cg); - il.append(blockInstructions); - il.append(DHBWInstructionFactory.createReturn( org.apache.commons.bcel6.generic.Type.VOID)); //Konstruktor hat immer VOID Return type + //method.setMaxStack(); //Die Stack Größe automatisch berechnen lassen (erst nach dem alle Instructions angehängt wurden) - this.method.setMaxStack(); //Die Stack Größe automatisch berechnen lassen (erst nach dem alle Instructions angehängt wurden) - - cg.addMethod(this.method.getMethod()); + cg.addMethod(method.createMethod(cg, getParameterList(), this.getType(), get_Block())); } /** diff --git a/src/de/dhbwstuttgart/syntaxtree/FieldDeclaration.java b/src/de/dhbwstuttgart/syntaxtree/FieldDeclaration.java index 68914f9f..d989a691 100644 --- a/src/de/dhbwstuttgart/syntaxtree/FieldDeclaration.java +++ b/src/de/dhbwstuttgart/syntaxtree/FieldDeclaration.java @@ -173,16 +173,17 @@ public class FieldDeclaration extends Field{ public InstructionList genByteCode(ClassGenerator cg) { //Das Feld an die Klasse anfügen: FieldGen field = new FieldGen(0, this.getType().getBytecodeType(cg), this.getDescription(), cg.getConstantPool()); + field.addAttribute(cg.getInstructionFactory().createSignatureAttribute(this.getType().getBytecodeSignature(cg))); cg.addField(field.getField()); - //Die Felddekleration an den Konstruktor anhängen: + + //Die Felddekleration an den Konstruktor anhängen: InstructionList il = new InstructionList(); il.append(new This(this).genByteCode(cg)); il.append(this.wert.genByteCode(cg)); FieldInstruction putFieldInstruction = cg.getInstructionFactory().createFieldAccess(this.getParentClass().getName().toString(), this.getDescription(), this.getType().getBytecodeType(cg), Constants.PUTFIELD); - - il.append(putFieldInstruction ); + il.append(putFieldInstruction ); return il; } diff --git a/src/de/dhbwstuttgart/syntaxtree/Method.java b/src/de/dhbwstuttgart/syntaxtree/Method.java index 2a7c055b..ba8cba98 100755 --- a/src/de/dhbwstuttgart/syntaxtree/Method.java +++ b/src/de/dhbwstuttgart/syntaxtree/Method.java @@ -73,7 +73,7 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable // ino.attribute.returntype.23497.declaration private Type returntype; // ino.end - protected MethodGen method; + // ino.attribute.genericMethodParameters.23500.decldescription type=javadoc /** * HOTI 4.5.06 Dieser Vektor beinhaltet alle Generischen Typen und v.a. die diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/LambdaExpression.java b/src/de/dhbwstuttgart/syntaxtree/statement/LambdaExpression.java index e266f972..4eee4c29 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/LambdaExpression.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/LambdaExpression.java @@ -243,21 +243,8 @@ public class LambdaExpression extends Expr{ InstructionList il = new InstructionList(); org.apache.commons.bcel6.generic.Type[] paramTypes = params.getBytecodeTypeList(cg); - org.apache.commons.bcel6.generic.Type[] paramTypesTemp = new org.apache.commons.bcel6.generic.Type[paramTypes.length]; - for(int i = 0; i < paramTypes.length ; i++) { - if(paramTypes[i] instanceof TypePlaceholderType){ - //TODO: Das kann auch ein spezifischer Typ als Object sein - paramTypesTemp[i] = cg.getNearestNonTPHType(((TypePlaceholderType)paramTypes[i]).getTPH()); - }else{ - paramTypesTemp[i] = paramTypes[i]; - } - } - paramTypes = paramTypesTemp; org.apache.commons.bcel6.generic.Type retType = method_body.getType().getBytecodeType(cg); - if(retType instanceof TypePlaceholderType){ - //Die TypePlaceholderTypen aussortieren. Sie werden nur in der Signatur erwähnt. Kommen im Bytecode sonst eigentlich nicht vor - retType = cg.getNearestNonTPHType(((TypePlaceholderType) retType).getTPH()); - } + MethodGenerator lambdaMethod = new MethodGenerator(0, retType, paramTypes, params.getParameterNameArray(), cg.createLambdaMethodName(), this.getParentClass().getName().toString(), new InstructionList(), cg.getConstantPool()); @@ -277,9 +264,9 @@ public class LambdaExpression extends Expr{ private String getLambdaSignature(ClassGenerator cg){ String typeSignature = "("; - typeSignature+=this.getParentClass().getType().getBytecodeSignature(cg); + typeSignature+=this.getParentClass().getType().getBytecodeType(cg).getSignature(); typeSignature += ")"; - typeSignature += this.lambdaType.getBytecodeSignature(cg); + typeSignature += this.lambdaType.getBytecodeType(cg).getSignature(); return typeSignature; } } diff --git a/src/de/dhbwstuttgart/syntaxtree/type/ExtendsWildcardType.java b/src/de/dhbwstuttgart/syntaxtree/type/ExtendsWildcardType.java index 2590e8d6..22abeea0 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/ExtendsWildcardType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/ExtendsWildcardType.java @@ -157,8 +157,10 @@ public class ExtendsWildcardType extends WildcardType implements ITypeContainer, return super.TYPE(ass, parent); } + /* @Override public org.apache.commons.bcel6.generic.Type getBytecodeType(ClassGenerator cg) { return new de.dhbwstuttgart.bytecode.WildcardType(this.innerType.get_Name(), "+"); } + */ } diff --git a/src/de/dhbwstuttgart/syntaxtree/type/FunN.java b/src/de/dhbwstuttgart/syntaxtree/type/FunN.java index fe853b5e..e3eff8bd 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/FunN.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/FunN.java @@ -119,10 +119,10 @@ public class FunN extends RefType { public String getBytecodeInvokeDynamicSignature(ClassGenerator cg) { String ret = "("; for(Type t : T){ - ret+=t.getBytecodeMethodSignature(cg); + ret+=t.getBytecodeType(cg).getSignature(); } ret +=")"; - ret+= R.getBytecodeMethodSignature(cg); + ret+= R.getBytecodeType(cg).getSignature(); return ret; } diff --git a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java index 4c09c990..097ed4e4 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java @@ -818,29 +818,24 @@ public class RefType extends ObjectType implements IMatchable } @Override - public String getBytecodeMethodSignature(ClassGenerator cg) { + 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.getBytecodeMethodSignature(cg); + paramString+=param.getBytecodeSignature(cg); //if(it.hasNext()) //paramString+=";"; //kein Delimiter zwischen den Typen } paramString+=">"; } - String typeSignature = this.getBytecodeSignature(cg); + String typeSignature = this.getBytecodeType(cg).getSignature(); typeSignature = typeSignature.substring(0, typeSignature.length()-1); return typeSignature+paramString+";"; } - @Override - public String getBytecodeSignature(ClassGenerator cg) { - return this.getBytecodeType(cg).getSignature(); - } - } // ino.end diff --git a/src/de/dhbwstuttgart/syntaxtree/type/SuperWildcardType.java b/src/de/dhbwstuttgart/syntaxtree/type/SuperWildcardType.java index 34f3527f..a9de5914 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/SuperWildcardType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/SuperWildcardType.java @@ -154,19 +154,18 @@ public class SuperWildcardType extends WildcardType implements ITypeContainer, I return super.TYPE(ass, parent); } + /* @Override public org.apache.commons.bcel6.generic.Type getBytecodeType(ClassGenerator cg) { - return new de.dhbwstuttgart.bytecode.WildcardType(this.innerType.get_Name(), "-"); + //return new de.dhbwstuttgart.bytecode.WildcardType(this.innerType.get_Name(), "-"); + return this.innerType.getBytecodeType(cg); } - + */ + @Override public String getBytecodeSignature(ClassGenerator cg) { - return this.innerType.getBytecodeSignature(cg); + return "-" + this.innerType.getBytecodeSignature(cg); } - @Override - public String getBytecodeMethodSignature(ClassGenerator cg) { - return "-" + this.innerType.getBytecodeMethodSignature(cg); - } } diff --git a/src/de/dhbwstuttgart/syntaxtree/type/Type.java b/src/de/dhbwstuttgart/syntaxtree/type/Type.java index 8bec8732..2ee31e55 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/Type.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/Type.java @@ -307,16 +307,22 @@ public abstract class Type extends SyntaxTreeNode implements IItemWithOffset public abstract org.apache.commons.bcel6.generic.Type getBytecodeType(ClassGenerator cg); + /** + * Erzeugt einen String, welcher den Typ genauer angibt. + * Dieser kann dann in Methoden und Feldersignaturen verwendet werden + * @param cg + * @return + */ public String getBytecodeSignature(ClassGenerator cg) { return this.getBytecodeType(cg).getSignature(); } - public String getBytecodeMethodSignature(ClassGenerator cg) { + /** + * @param cg + * @return Die Signatur wie sie in Klassensignaturen verwendet wird + */ + public String getClassSignature(ClassGenerator cg){ return this.getBytecodeSignature(cg); } - - public String getClassSignature(ClassGenerator cg) { - return this.getBytecodeMethodSignature(cg); - } } // ino.end diff --git a/src/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java b/src/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java index 9f9de1e3..533daf5b 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java @@ -9,6 +9,7 @@ import java.util.Iterator; import de.dhbwstuttgart.typeinference.Menge; import de.dhbwstuttgart.logger.*; import de.dhbwstuttgart.bytecode.ClassGenerator; +import de.dhbwstuttgart.bytecode.DHBWInstructionFactory; import de.dhbwstuttgart.bytecode.TypePlaceholderType; import de.dhbwstuttgart.core.MyCompiler; import de.dhbwstuttgart.parser.JavaClassName; @@ -434,26 +435,26 @@ public class TypePlaceholder extends ObjectType public org.apache.commons.bcel6.generic.Type getBytecodeType(ClassGenerator cg) { Type resolvedType = cg.resolveTPH(this); if(resolvedType instanceof TypePlaceholder){ - cg.addUsedTPH((TypePlaceholder)resolvedType); - return new TypePlaceholderType((TypePlaceholder)resolvedType); + return DHBWInstructionFactory.createObjectType(); } return resolvedType.getBytecodeType(cg); } @Override public String getBytecodeSignature(ClassGenerator cg) { - return cg.resolveTPH(this).getBytecodeMethodSignature(cg); - } - - public String getBytecodeMethodSignature(ClassGenerator cg) { - return super.getBytecodeSignature(cg); + Type resolvedType = cg.resolveTPH(this); + if(resolvedType instanceof TypePlaceholder){ + cg.addUsedTPH((TypePlaceholder)resolvedType); + return new TypePlaceholderType((TypePlaceholder)resolvedType).getSignature(); + } + return resolvedType.getBytecodeSignature(cg); } @Override public String getClassSignature(ClassGenerator cg) { //Die Signaturen in der Klasse bauen sich für Generische Variabeln (also TPHs) folgendermaßen auf: "GVAR:SuperClass" - String ret = this.getBytecodeMethodSignature(cg); - ret = ret.substring(0, ret.length()-1) + ":"; //";" mit ":" ersetzen + String ret = this.getBytecodeSignature(cg); + ret = ret.substring(1, ret.length()-1) + ":"; //";" mit ":" ersetzen org.apache.commons.bcel6.generic.Type nearestType = cg.getNearestNonTPHType(this); if(nearestType instanceof TypePlaceholderType){ //Handelt es sich um einen weiteren TPH als nächsten Typ, so ist es ein allgemeiner Typ und wir nehmen Object als Superklasse ret += cg.getInstructionFactory().createObjectType().getSignature(); diff --git a/src/de/dhbwstuttgart/syntaxtree/type/WildcardType.java b/src/de/dhbwstuttgart/syntaxtree/type/WildcardType.java index a2d35abf..feff1e55 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/WildcardType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/WildcardType.java @@ -107,15 +107,9 @@ public class WildcardType extends Type{ @Override public String getBytecodeSignature(ClassGenerator cg) { - return this.innerType.getBytecodeMethodSignature(cg); + return this.innerType.getBytecodeSignature(cg); } - @Override - public String getBytecodeMethodSignature(ClassGenerator cg) { - return this.innerType.getBytecodeMethodSignature(cg); - } - - } diff --git a/test/bytecode/Test.java b/test/bytecode/Test.java index cb8ff7b7..b11f155d 100644 --- a/test/bytecode/Test.java +++ b/test/bytecode/Test.java @@ -10,6 +10,7 @@ public static void main(String[] args){ System.out.println(new FieldDeclaration().field); System.out.println(new Runnable().method().apply()); Runnable r = new Runnable().method().apply(); - Test t = new Identity().op.apply(this); + Test t = new Identity().op.apply(new Test()); + System.out.println(t); } }