Einsetzen von generischen Variablen

This commit is contained in:
JanUlrich 2014-07-16 10:33:34 +02:00
parent 0327894686
commit 694899e73f
13 changed files with 140 additions and 65 deletions

View File

@ -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<GenericTypeVar> getGenericParameter() {
if(this.genericClassParameters == null)return new Vector<GenericTypeVar>();
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

View File

@ -35,6 +35,7 @@ public class Constructor extends Method {
}
/*
@Override
public void setGenericMethodParameters(
Vector<GenericTypeVar> genericMethodParameters) {
@ -47,7 +48,7 @@ public class Constructor extends Method {
return this.methode.getGenericMethodParameters();
}
*/
@Override
public void sc_init_parameterlist(boolean ext) {

View File

@ -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> declid = new Vector<DeclId>(); // 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<GenericTypeVar> getGenericParameter() {
if(this.genericParameters == null)return new Vector<GenericTypeVar>();
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

View File

@ -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<GenericTypeVar> getGenericParameter();
}

View File

@ -79,7 +79,7 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable
*/
// ino.end
// ino.attribute.genericMethodParameters.23500.declaration
private Vector<GenericTypeVar> genericMethodParameters=new Vector<GenericTypeVar>();
//private Vector<GenericTypeVar> genericMethodParameters=new Vector<GenericTypeVar>();
// 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<GenericTypeVar> 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<Type> classParalist)
{
/*
Vector<Type> paralist = new Vector<Type>();//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);
}
}

View File

@ -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

View File

@ -41,7 +41,7 @@ public class GenericTypeVar extends Type
// ino.end
// ino.class.GenericTypeVar.26505.body
{
Type genericTypeVar;
//Type genericTypeVar;
//Vector<Type> extendVars = new Vector<Type>();
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<Type> 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);
}

View File

@ -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();
}

View File

@ -21,4 +21,8 @@ public class GenericVarAssumption extends Assumption{
public String getIdentifier(){
return genericVar.getName();
}
public String toString(){
return this.getIdentifier() +"::"+this.getAssumedType();
}
}

View File

@ -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 <FC>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<GenericVarDeclarationPatch> genericVarDeclarations = new Vector<GenericVarDeclarationPatch>();
/**
* 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;
}
}

View File

@ -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<Pair> genericPairs = new Vector<Pair>();
/**
* 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:
}
}

View File

@ -5,23 +5,18 @@ import typinferenz.ResultSet;
import typinferenz.TypeInsertable;
public abstract class SourcePatchPoint implements Comparable<SourcePatchPoint>{
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;
}

View File

@ -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;
}
}