From 694899e73ffc9463087b69bd7684d15fbe31bc64 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 16 Jul 2014 10:33:34 +0200 Subject: [PATCH] Einsetzen von generischen Variablen --- src/mycompiler/myclass/Class.java | 15 +++- src/mycompiler/myclass/Constructor.java | 3 +- src/mycompiler/myclass/Field.java | 11 ++- src/mycompiler/myclass/Generic.java | 8 +- src/mycompiler/myclass/Method.java | 10 ++- .../mytype/BoundedGenericTypeVar.java | 2 +- src/mycompiler/mytype/GenericTypeVar.java | 8 +- src/typinferenz/GenericTypeInsertable.java | 14 ++++ .../assumptions/GenericVarAssumption.java | 4 + .../GenericTypeInsertPoint.java | 84 ++++++++++++++++--- .../typedeployment/GenericVarPatch.java | 27 ------ .../typedeployment/SourcePatchPoint.java | 9 +- .../typedeployment/TypeInsertPoint.java | 10 ++- 13 files changed, 140 insertions(+), 65 deletions(-) create mode 100644 src/typinferenz/GenericTypeInsertable.java delete mode 100644 src/typinferenz/typedeployment/GenericVarPatch.java diff --git a/src/mycompiler/myclass/Class.java b/src/mycompiler/myclass/Class.java index 202ef45fc..0677fb79c 100755 --- a/src/mycompiler/myclass/Class.java +++ b/src/mycompiler/myclass/Class.java @@ -65,6 +65,9 @@ import org.apache.log4j.Logger; + + + import sun.reflect.generics.reflectiveObjects.NotImplementedException; import typinferenz.ConstraintsSet; import typinferenz.JavaCodeResult; @@ -75,13 +78,14 @@ import typinferenz.UndConstraint; import typinferenz.FunN; import typinferenz.assumptions.ClassAssumption; import typinferenz.assumptions.TypeAssumptions; +import typinferenz.exceptions.DebugException; import typinferenz.exceptions.TypeinferenceException; import typinferenz.typedeployment.TypeInsertPoint; import typinferenz.*; // ino.class.Class.23010.declaration -public class Class extends SyntaxTreeNode implements AClassOrInterface, IItemWithOffset, Generic +public class Class extends SyntaxTreeNode implements AClassOrInterface, IItemWithOffset, Generic, GenericTypeInsertable // ino.end // ino.class.Class.23010.body { @@ -1355,6 +1359,12 @@ public class Class extends SyntaxTreeNode implements AClassOrInterface, IItemWit return true; } + @Override + public Iterable getGenericParameter() { + if(this.genericClassParameters == null)return new Vector(); + return this.genericClassParameters; + } + @Override public String getDescription(){ return "class "+this.getName(); @@ -1373,8 +1383,9 @@ public class Class extends SyntaxTreeNode implements AClassOrInterface, IItemWit @Override public int getGenericDeclOffset() { //TODO: Falls Generische Parameterliste vorhanden, hier Wert der Liste zurückgegebn - return -1; + return this.getOffset(); } + } // ino.end diff --git a/src/mycompiler/myclass/Constructor.java b/src/mycompiler/myclass/Constructor.java index 0eb22ef34..be8572180 100644 --- a/src/mycompiler/myclass/Constructor.java +++ b/src/mycompiler/myclass/Constructor.java @@ -35,6 +35,7 @@ public class Constructor extends Method { } + /* @Override public void setGenericMethodParameters( Vector genericMethodParameters) { @@ -47,7 +48,7 @@ public class Constructor extends Method { return this.methode.getGenericMethodParameters(); } - + */ @Override public void sc_init_parameterlist(boolean ext) { diff --git a/src/mycompiler/myclass/Field.java b/src/mycompiler/myclass/Field.java index 86b6c1ee8..1d8398b43 100644 --- a/src/mycompiler/myclass/Field.java +++ b/src/mycompiler/myclass/Field.java @@ -11,6 +11,7 @@ import mycompiler.mytype.Type; import mycompiler.mytype.TypePlaceholder; import mycompiler.mytypereconstruction.replacementlistener.CReplaceTypeEvent; import typinferenz.ConstraintsSet; +import typinferenz.GenericTypeInsertable; import typinferenz.JavaCodeResult; import typinferenz.ResultSet; import typinferenz.Typeable; @@ -18,7 +19,7 @@ import typinferenz.TypeInsertable; import typinferenz.assumptions.TypeAssumptions; import typinferenz.typedeployment.TypeInsertPoint; -public abstract class Field extends SyntaxTreeNode implements TypeInsertable, Typeable, Generic{ +public abstract class Field extends SyntaxTreeNode implements TypeInsertable, Typeable, Generic, GenericTypeInsertable{ protected Vector declid = new Vector(); // Vector, da 'int a, b, c, ...' auch eingeparst werden muss @@ -53,7 +54,11 @@ public abstract class Field extends SyntaxTreeNode implements TypeInsertable, Ty public abstract void codegen(ClassFile classfile, Vector paralist) throws JVMCodeException; - + @Override + public Iterable getGenericParameter() { + if(this.genericParameters == null)return new Vector(); + return this.genericParameters; + } public void set_DeclId(DeclId did) { @@ -133,7 +138,7 @@ public abstract class Field extends SyntaxTreeNode implements TypeInsertable, Ty @Override public int getGenericDeclOffset() { //TODO: Falls Generische Parameterliste vorhanden, hier Wert der Liste zurückgegebn - return -1; + return this.offset; } @Override diff --git a/src/mycompiler/myclass/Generic.java b/src/mycompiler/myclass/Generic.java index ccc4bd188..0d2a37d1c 100644 --- a/src/mycompiler/myclass/Generic.java +++ b/src/mycompiler/myclass/Generic.java @@ -1,5 +1,6 @@ package mycompiler.myclass; +import java.util.Iterator; import java.util.Vector; import mycompiler.mytype.GenericTypeVar; @@ -11,9 +12,6 @@ import mycompiler.mytype.GenericTypeVar; */ public interface Generic { public void setGenericParameter(GenericDeclarationList params); - /** - * Gibt den Offset im Quellcode an, an dem neue Deklarationen von generischen Variablen eingesetzt werden können. - * @return - */ - public int getGenericDeclOffset(); + public Iterable getGenericParameter(); + } \ No newline at end of file diff --git a/src/mycompiler/myclass/Method.java b/src/mycompiler/myclass/Method.java index 69b9b4d13..ffddc8adf 100755 --- a/src/mycompiler/myclass/Method.java +++ b/src/mycompiler/myclass/Method.java @@ -79,7 +79,7 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable */ // ino.end // ino.attribute.genericMethodParameters.23500.declaration - private Vector genericMethodParameters=new Vector(); + //private Vector genericMethodParameters=new Vector(); // ino.end // ino.attribute.overloadedID.23503.declaration @@ -106,6 +106,7 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable super(offset); } + /* // ino.method.setGenericMethodParameters.23521.definition public void setGenericMethodParameters(Vector genericMethodParameters) // ino.end @@ -122,7 +123,7 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable return(genericMethodParameters); } // ino.end - + */ // ino.method.sc_init_parameterlist.23530.definition public void sc_init_parameterlist(boolean ext) // ino.end @@ -502,6 +503,7 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable @Override public void wandleRefTypeAttributes2GenericAttributes(Vector classParalist) { + /* Vector paralist = new Vector();//Mit den Generischen Typen der Methode paralist.addAll(classParalist); paralist.addAll(this.genericMethodParameters); @@ -532,6 +534,7 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable if(block!=null){ block.wandleRefTypeAttributes2GenericAttributes(paralist,genericMethodParameters); } + */ } // ino.end // ino.method.set_Method_Name.23617.definition @@ -550,7 +553,7 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable TypeAssumptions localAss = new TypeAssumptions(); localAss.add(ass); //Die globalen Assumptions anhängen //Generische Parameterdeklarationen den Assumptions anfügen: - for(GenericTypeVar gtv : this.genericMethodParameters){ + for(GenericTypeVar gtv : this.getGenericParameter()){ ret.add(gtv.TYPE(localAss)); } @@ -752,7 +755,6 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable if(!this.parameterlist.equals(equals.parameterlist))return false; return super.equals(obj); } - } diff --git a/src/mycompiler/mytype/BoundedGenericTypeVar.java b/src/mycompiler/mytype/BoundedGenericTypeVar.java index dca83d4dc..fa8661904 100755 --- a/src/mycompiler/mytype/BoundedGenericTypeVar.java +++ b/src/mycompiler/mytype/BoundedGenericTypeVar.java @@ -57,7 +57,7 @@ public class BoundedGenericTypeVar extends GenericTypeVar if(bounds != null)for(Type t : bounds){ //if(t!=null)this.extendVars.add(t); } - this.genericTypeVar = new RefType(s,offset); + //this.genericTypeVar = new RefType(s,offset); this.bounds = bounds; } // ino.end diff --git a/src/mycompiler/mytype/GenericTypeVar.java b/src/mycompiler/mytype/GenericTypeVar.java index f6260c8dc..6d2d9df78 100755 --- a/src/mycompiler/mytype/GenericTypeVar.java +++ b/src/mycompiler/mytype/GenericTypeVar.java @@ -41,7 +41,7 @@ public class GenericTypeVar extends Type // ino.end // ino.class.GenericTypeVar.26505.body { - Type genericTypeVar; + //Type genericTypeVar; //Vector extendVars = new Vector(); protected Pair genericConstraint; /** @@ -159,10 +159,11 @@ public class GenericTypeVar extends Type @Override public JavaCodeResult printJavaCode(ResultSet resultSet) { + /* if(this.genericTypeVar!=null){ JavaCodeResult ret = new JavaCodeResult(); ret.attach(this.genericTypeVar.printJavaCode(resultSet)); - /* + if(this.extendVars.size()>0){ ret.attach(" extends "); Iterator it = this.extendVars.iterator(); @@ -172,9 +173,10 @@ public class GenericTypeVar extends Type if(it.hasNext())ret.attach(", "); } } - */ + return ret; } + */ return new JavaCodeResult(this.name); } diff --git a/src/typinferenz/GenericTypeInsertable.java b/src/typinferenz/GenericTypeInsertable.java new file mode 100644 index 000000000..791fcbf7f --- /dev/null +++ b/src/typinferenz/GenericTypeInsertable.java @@ -0,0 +1,14 @@ +package typinferenz; + +/** + * Wird von Knoten im Syntaxbaum implementiert, welche ein Einsetzen von generischen Variablendeklarationen erlauben. + */ +public interface GenericTypeInsertable { + + /** + * Gibt den Offset im Quellcode an, an dem neue Deklarationen von generischen Variablen eingesetzt werden können. + * @return + */ + public int getGenericDeclOffset(); + +} diff --git a/src/typinferenz/assumptions/GenericVarAssumption.java b/src/typinferenz/assumptions/GenericVarAssumption.java index 2cc2a4655..f0cda2cf4 100644 --- a/src/typinferenz/assumptions/GenericVarAssumption.java +++ b/src/typinferenz/assumptions/GenericVarAssumption.java @@ -21,4 +21,8 @@ public class GenericVarAssumption extends Assumption{ public String getIdentifier(){ return genericVar.getName(); } + + public String toString(){ + return this.getIdentifier() +"::"+this.getAssumedType(); + } } diff --git a/src/typinferenz/typedeployment/GenericTypeInsertPoint.java b/src/typinferenz/typedeployment/GenericTypeInsertPoint.java index 0f7eb100d..174a9540d 100644 --- a/src/typinferenz/typedeployment/GenericTypeInsertPoint.java +++ b/src/typinferenz/typedeployment/GenericTypeInsertPoint.java @@ -3,6 +3,7 @@ package typinferenz.typedeployment; import java.util.Iterator; import java.util.Vector; +import typinferenz.GenericTypeInsertable; import typinferenz.JavaCodeResult; import typinferenz.ResultSet; import typinferenz.TypeInsertable; @@ -24,16 +25,19 @@ public class GenericTypeInsertPoint extends SourcePatchPoint { private GenericVarPatch patch; + private GenericTypeInsertable genericInsertPoint; + /* * TODO: Der Generic Type Insert Point sollte mit Generics konstruiert werden. * Dabei hält jeder InsertPoint nur ein Generic. Die <>-Klammern fügt das insertSet in den Quellcode ein. * Die Reihenfolge der GenericTIPs ist beim Einsetzen dann egal. */ - public GenericTypeInsertPoint(TypeInsertable syntaxTreeNode, + public GenericTypeInsertPoint(GenericTypeInsertable syntaxTreeNode, GenericVarPatch gtv ,ResultSet resultSet) { + this.resultSet = resultSet; this.patch = gtv; - this.point = syntaxTreeNode; + this.genericInsertPoint = syntaxTreeNode; } /* @@ -47,7 +51,7 @@ public class GenericTypeInsertPoint extends SourcePatchPoint { public String getTypeInsertString() { - return this.patch.getInsertString(); + return this.patch.getInsertString(this.resultSet); } /* @@ -96,10 +100,7 @@ public class GenericTypeInsertPoint extends SourcePatchPoint { return ret; } - - public TypeInsertable getInsertNode(){ - return this.point; - } + /** * Versucht den GenericTypeInsertPoint mit dem bergebenenen p2 zusam @@ -122,8 +123,11 @@ public class GenericTypeInsertPoint extends SourcePatchPoint { @Override public JavaCodeResult patch(String fileContent, int additionalOffset) { //TODO: Es kann sein, dass eine Methode bereits Generische Variablen hat, diese müssen dann an diese Liste angefügt werden. - - return super.patch(fileContent, additionalOffset); + JavaCodeResult ret = new JavaCodeResult(fileContent.substring(0, this.getOffset()+additionalOffset)); + JavaCodeResult mitte = new JavaCodeResult(this.getTypeInsertString()).attach(fileContent.substring(this.getOffset()+additionalOffset)); + ret.attach(mitte); + //this. + return ret; } /* @@ -143,6 +147,66 @@ public class GenericTypeInsertPoint extends SourcePatchPoint { */ @Override public int getOffset() { - return point.getOffset(); + return genericInsertPoint.getGenericDeclOffset(); } } + +/** + * Eine Hilfsklasse zur Einsetzung einer Generischen Variable. + * @author janulrich + * + */ +class GenericVarPatch { + + private Vector genericVarDeclarations = new Vector(); + + /** + * Fügt eine generische Variable in Form eines Pairs an. + * @param p + */ + public void add(Pair p){ + GenericVarDeclarationPatch toAdd = new GenericVarDeclarationPatch(p); + if(!this.genericVarDeclarations.contains(toAdd))this.genericVarDeclarations.add(toAdd); + } + + public String getInsertString(ResultSet rs){ + String ret = ""; + for(GenericVarDeclarationPatch p : this.genericVarDeclarations){ + ret += p.getInsertString(rs) + ", "; + } + return ret; + } +} + + +/** + * Eine Hilfsklasse zur Einsetzung einer Generischen Variable. + * @author janulrich + * + */ +class GenericVarDeclarationPatch { + + private Pair genericPair; + + public GenericVarDeclarationPatch(Pair p) { + this.genericPair = p; + } + + public String getInsertString(ResultSet resultSet){ + String ret = ""; + if(this.genericPair != null){ + ret += this.genericPair.TA1.printJavaCode(resultSet); + ret += " extends "; + ret += this.genericPair.TA2.printJavaCode(resultSet); + } + return ret; + } + + @Override + public boolean equals(Object o){ + if(!(o instanceof GenericVarDeclarationPatch))return false; + if(!(this.genericPair.equals(((GenericVarDeclarationPatch)o).genericPair)))return false; + return true; + } + +} diff --git a/src/typinferenz/typedeployment/GenericVarPatch.java b/src/typinferenz/typedeployment/GenericVarPatch.java deleted file mode 100644 index 25983aeb9..000000000 --- a/src/typinferenz/typedeployment/GenericVarPatch.java +++ /dev/null @@ -1,27 +0,0 @@ -package typinferenz.typedeployment; - -import java.util.Vector; - -import mycompiler.mytype.Pair; - -/** - * Eine Hilfsklasse zur Einsetzung einer Generischen Variable. - * @author janulrich - * - */ -public class GenericVarPatch { - - private Vector genericPairs = new Vector(); - - /** - * Fügt eine generische Variable in Form eines Pairs an. - * @param p - */ - public void add(Pair p){ - if(!this.genericPairs.contains(p))this.genericPairs.add(p); - } - - public String getInsertString(){ - return null; //TODO: - } -} diff --git a/src/typinferenz/typedeployment/SourcePatchPoint.java b/src/typinferenz/typedeployment/SourcePatchPoint.java index c4d4875a9..813e987df 100644 --- a/src/typinferenz/typedeployment/SourcePatchPoint.java +++ b/src/typinferenz/typedeployment/SourcePatchPoint.java @@ -5,23 +5,18 @@ import typinferenz.ResultSet; import typinferenz.TypeInsertable; public abstract class SourcePatchPoint implements Comparable{ - protected TypeInsertable point; protected ResultSet resultSet; public abstract int getOffset(); public ResultSet getResultSet() { return resultSet; } - public TypeInsertable getTIP() { - return point; - } @Override public int compareTo(SourcePatchPoint arg0) { return new Integer(this.getOffset()).compareTo(new Integer(arg0.getOffset())); } - public JavaCodeResult patch(String fileContent, int additionalOffset) { - return new JavaCodeResult(fileContent); - } + public abstract JavaCodeResult patch(String fileContent, int additionalOffset); + public int getInsertLength() { return 0; } diff --git a/src/typinferenz/typedeployment/TypeInsertPoint.java b/src/typinferenz/typedeployment/TypeInsertPoint.java index fc00d9c73..1dca872a3 100644 --- a/src/typinferenz/typedeployment/TypeInsertPoint.java +++ b/src/typinferenz/typedeployment/TypeInsertPoint.java @@ -2,6 +2,7 @@ package typinferenz.typedeployment; import java.util.Vector; +import typinferenz.GenericTypeInsertable; import typinferenz.JavaCodeResult; import typinferenz.ResultSet; import typinferenz.TypeInsertable; @@ -23,6 +24,7 @@ public class TypeInsertPoint extends SourcePatchPoint { public Type type; + private TypeInsertable point; /** * Dieser Konstruktor erstellt einen TypInsertPoint @@ -101,7 +103,7 @@ public class TypeInsertPoint extends SourcePatchPoint { - public TypeInsertable getGenericTypeVarInsertNode() { + public GenericTypeInsertable getGenericTypeVarInsertNode() { SyntaxTreeNode ret = (SyntaxTreeNode)this.getTIP(); while(!(ret instanceof Field || ret instanceof Class)){ ret = ret.getParent(); @@ -109,7 +111,11 @@ public class TypeInsertPoint extends SourcePatchPoint { throw new DebugException(this.getInsertNode().toString()+" hat kein Feld oder Klasse als Elternelement"); } } - return (TypeInsertable) ret; + return (GenericTypeInsertable) ret; + } + + private TypeInsertable getTIP() { + return this.point; } }