From 5316b6ca517a16cebb0dd83f6287814a8c531ae5 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 9 Apr 2014 14:12:55 +0200 Subject: [PATCH] Import Test funktioniert jetzt --- src/mycompiler/SourceFile.java | 9 ++- src/mycompiler/SyntaxTreeNode.java | 4 ++ src/mycompiler/myclass/Class.java | 4 ++ src/mycompiler/myclass/Constructor.java | 2 +- src/mycompiler/myclass/Field.java | 6 ++ src/mycompiler/myclass/FieldDeclaration.java | 11 +++- src/mycompiler/myclass/FormalParameter.java | 14 ++++- src/mycompiler/myclass/Method.java | 8 +-- src/mycompiler/myclass/ParameterList.java | 15 ++++- .../mystatement/LambdaExpression.java | 2 +- src/mycompiler/mystatement/LocalVarDecl.java | 4 +- src/mycompiler/mytype/GenericTypeVar.java | 23 ++++++-- src/typinferenz/FunNInterface.java | 2 +- src/typinferenz/SingleConstraint.java | 6 +- .../assumptions/ClassAssumption.java | 8 ++- .../assumptions/MethodAssumption.java | 15 ++++- .../assumptions/TypeAssumptions.java | 58 +++++++++---------- test/plugindevelopment/TypeInsertTester.java | 1 + .../TypeInsertTests/GenericTypeVarTest.java | 11 +++- .../TypeInsertTests/GenericTypeVarTest2.jav | 14 +++++ 20 files changed, 160 insertions(+), 57 deletions(-) create mode 100644 test/plugindevelopment/TypeInsertTests/GenericTypeVarTest2.jav diff --git a/src/mycompiler/SourceFile.java b/src/mycompiler/SourceFile.java index 357f70399..deae80ba9 100755 --- a/src/mycompiler/SourceFile.java +++ b/src/mycompiler/SourceFile.java @@ -703,6 +703,7 @@ public class SourceFile } } + /* //Alle Generischen Typvariablen in TPH umwandeln: HashMap gtv2tph = new HashMap(); for(Pair pair : constraints){ @@ -723,7 +724,7 @@ public class SourceFile pair.TA2 = tph; } } - + */ //Erst die Unifizierung erstellen: Vector constraintsClone = (Vector)constraints.clone(); Vector> unifyResult = Unify.unify(constraintsClone, finiteClosure); @@ -1159,6 +1160,8 @@ public class SourceFile for(int j=0;j paralist){ // Zuerst Returntype untersuchen Type type=getType(); diff --git a/src/mycompiler/myclass/FieldDeclaration.java b/src/mycompiler/myclass/FieldDeclaration.java index 89962e389..948035933 100644 --- a/src/mycompiler/myclass/FieldDeclaration.java +++ b/src/mycompiler/myclass/FieldDeclaration.java @@ -15,6 +15,7 @@ import mycompiler.mybytecode.ClassFile; import mycompiler.myexception.JVMCodeException; import mycompiler.mystatement.Expr; import mycompiler.mytype.GenericTypeVar; +import mycompiler.mytype.RefType; import mycompiler.mytype.Type; import mycompiler.mytype.TypePlaceholder; import mycompiler.mytypereconstruction.replacementlistener.CReplaceTypeEvent; @@ -91,7 +92,7 @@ public class FieldDeclaration extends Field{ */ if(this.getType() == null)throw new TypinferenzException("Der Typ eines Feldes darf nicht null sein"); //assumptions.add(TypeAssumptions.createFieldVarAssumption(classmember.getName(), this.getName(), this.getType())); - assumptions.addFieldAssumption(new FieldAssumption(this,classmember)); + assumptions.addAssumption(new FieldAssumption(this,classmember)); return assumptions; } @@ -127,7 +128,13 @@ public class FieldDeclaration extends Field{ @Override public ConstraintsSet TYPE(TypeAssumptions publicAssumptions) { ConstraintsSet ret = new ConstraintsSet(); - this.setType(publicAssumptions.getTypeFor(this.getType())); + /* + if(this.getType() instanceof GenericTypeVar){ + //Falls Typ ein GTV ist muss er syntaktisch kontrolliert werden... + GenericTypeVar gtv = (GenericTypeVar) this.getType(); + } + */ + if(this.getType()!=null && (this.getType() instanceof RefType))this.setType(publicAssumptions.getTypeFor((RefType)this.getType())); SingleConstraint c1 = new SingleConstraint(this.getType(), this.getType()); ret.add(c1); //Damit die TypVariable des Felds in den Constraints auftaucht diff --git a/src/mycompiler/myclass/FormalParameter.java b/src/mycompiler/myclass/FormalParameter.java index f8bd9bee9..3ac257cc8 100755 --- a/src/mycompiler/myclass/FormalParameter.java +++ b/src/mycompiler/myclass/FormalParameter.java @@ -25,6 +25,7 @@ import org.apache.log4j.Logger; + import sun.reflect.generics.reflectiveObjects.NotImplementedException; import typinferenz.JavaCodeResult; import typinferenz.ResultSet; @@ -53,7 +54,18 @@ public class FormalParameter extends SyntaxTreeNode implements ITypeReplacementL } - // ino.method.setType.23404.defdescription type=javadoc + @Override + public boolean equals(Object object) { + if(!super.equals(object))return false; + if(!(object instanceof FormalParameter))return false; + FormalParameter equals = (FormalParameter)object; + if((this.type==null)!=(equals.type == null))return false; + if(this.type != null)return this.type.equals(equals.type); + return true; + } + + + // ino.method.setType.23404.defdescription type=javadoc /** *
Author: J�rg B�uerle * @param t diff --git a/src/mycompiler/myclass/Method.java b/src/mycompiler/myclass/Method.java index f03a2fa5b..d982b4471 100755 --- a/src/mycompiler/myclass/Method.java +++ b/src/mycompiler/myclass/Method.java @@ -537,14 +537,14 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable public ConstraintsSet TYPE(TypeAssumptions ass) { - this.returntype = ass.getTypeFor(this.returntype); + if((this.returntype instanceof RefType))this.returntype = ass.getTypeFor((RefType)this.returntype); ConstraintsSet ret = new ConstraintsSet(); TypeAssumptions localAss = new TypeAssumptions(); localAss.add(ass); //Die globalen Assumptions anhängen //Die Parameter zu den Assumptions hinzufügen: if(this.parameterlist!=null)for(FormalParameter param : this.parameterlist){ - param.setType(ass.getTypeFor(param.getType())); - localAss.addParameterAssumption(new ParameterAssumption(param)); + if(param.getType() instanceof RefType)param.setType(ass.getTypeFor((RefType)param.getType())); + localAss.addAssumption(new ParameterAssumption(param)); } ret.add(this.block.TYPEStmt(localAss)); //eine Verknüpfung mit der Type Assumption aus dem Assumption Set und dem ermittelten Typ der Methode: @@ -574,7 +574,7 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable public TypeAssumptions createTypeAssumptions(Class classmember) { Class parentClass = this.getParentClass(); TypeAssumptions ret = new TypeAssumptions(); - ret.addMethodAssumption(new MethodAssumption(this, parentClass)); + ret.addAssumption(new MethodAssumption(this, parentClass)); return ret; /* TypeAssumptions assumptions = new TypeAssumptions(); diff --git a/src/mycompiler/myclass/ParameterList.java b/src/mycompiler/myclass/ParameterList.java index ba299a550..ee4e75bf9 100755 --- a/src/mycompiler/myclass/ParameterList.java +++ b/src/mycompiler/myclass/ParameterList.java @@ -3,11 +3,13 @@ package mycompiler.myclass; // ino.end // ino.module.ParameterList.8565.import import java.util.Vector; + import typinferenz.JavaCodeResult; import typinferenz.ResultSet; import mycompiler.mybytecode.ClassFile; import mycompiler.mybytecode.CodeAttribute; import mycompiler.mytype.*; + import java.util.Iterator; // ino.end @@ -37,7 +39,10 @@ public class ParameterList implements Iterable - // ino.method.get_codegen_ParameterList.23629.definition + + + + // ino.method.get_codegen_ParameterList.23629.definition public String get_codegen_ParameterList(Vector paralist) // ino.end // ino.method.get_codegen_ParameterList.23629.body @@ -153,5 +158,13 @@ public class ParameterList implements Iterable return this.formalparameter.iterator(); } + @Override + public boolean equals(Object obj) { + if(!(obj instanceof ParameterList))return false; + ParameterList equals = (ParameterList)obj; + if((this.formalparameter == null )!=(equals.formalparameter==null))return false; + if(this.formalparameter!=null)return this.formalparameter.equals(equals.formalparameter); + return true; + } } // ino.end diff --git a/src/mycompiler/mystatement/LambdaExpression.java b/src/mycompiler/mystatement/LambdaExpression.java index c0142aa87..9075b7c3b 100755 --- a/src/mycompiler/mystatement/LambdaExpression.java +++ b/src/mycompiler/mystatement/LambdaExpression.java @@ -152,7 +152,7 @@ public class LambdaExpression extends Expr{ if(param.getType()==null)param.setType(TypePlaceholder.fresh(this)); int offset = 0; //Jeder Parameter der LambdaExpression wird als CParaTypeAssumption der Assumption liste hinzugefügt: - ArgumentAssumptions.addParameterAssumption(new ParameterAssumption(param)); + ArgumentAssumptions.addAssumption(new ParameterAssumption(param)); paramTypes.add(param.getType()); } this.setType(TypePlaceholder.fresh(this)); diff --git a/src/mycompiler/mystatement/LocalVarDecl.java b/src/mycompiler/mystatement/LocalVarDecl.java index 448a60d25..3a57847c0 100755 --- a/src/mycompiler/mystatement/LocalVarDecl.java +++ b/src/mycompiler/mystatement/LocalVarDecl.java @@ -460,8 +460,8 @@ public class LocalVarDecl extends Statement implements TypeInsertable @Override public ConstraintsSet TYPEStmt(TypeAssumptions assumptions) { ConstraintsSet ret = new ConstraintsSet(); - this.setType(assumptions.getTypeFor(this.getType())); - assumptions.addLocalVarAssumption(new LocalVarAssumption(this)); + if((this.getType() instanceof RefType))this.setType(assumptions.getTypeFor((RefType)this.getType())); + assumptions.addAssumption(new LocalVarAssumption(this)); //assumptions.remove(null); // falls Variable mit diesem Namen bereits vorhanden. this.setType(new Void(0)); //Return typ einer Variablendeklaration ist Void return ret; diff --git a/src/mycompiler/mytype/GenericTypeVar.java b/src/mycompiler/mytype/GenericTypeVar.java index 3f43ea4ba..db7249833 100755 --- a/src/mycompiler/mytype/GenericTypeVar.java +++ b/src/mycompiler/mytype/GenericTypeVar.java @@ -3,11 +3,13 @@ package mycompiler.mytype; // ino.end // ino.module.GenericTypeVar.8671.import +import java.util.HashMap; import java.util.Vector; // ino.end + import mycompiler.mytypereconstruction.replacementlistener.ITypeReplacementListener; import typinferenz.JavaCodeResult; import typinferenz.ResultSet; @@ -15,7 +17,7 @@ import typinferenz.ResultSet; // ino.class.GenericTypeVar.26505.description type=javadoc /** - * + * TODO: Diese Klasse überarbeiten. Pair genericTypeVar ist nicht implementiert. * @author J�rg B�uerle * @version $Date: 2013/09/22 20:12:53 $ */ @@ -34,7 +36,7 @@ public class GenericTypeVar extends Type // ino.method.GenericTypeVar.26509.defdescription type=line // private Hashtable> m_TypeErasureList; // ino.end - private TypePlaceholder tph; + private static HashMap tph = new HashMap(); /** * @@ -91,6 +93,8 @@ public class GenericTypeVar extends Type } // ino.end + + // ino.method.toString.26518.definition public String toString() // ino.end @@ -100,7 +104,12 @@ public class GenericTypeVar extends Type } // ino.end - // ino.method.get_codegen_Type.26521.defdescription type=javadoc + @Override + public int hashCode() { + return this.name.hashCode(); + } + + // ino.method.get_codegen_Type.26521.defdescription type=javadoc /** * hoti 4.5.06 * Generische Typen werden im Bytecode @@ -137,8 +146,12 @@ public class GenericTypeVar extends Type } public TypePlaceholder getTypePlaceHolder() { - if(this.tph == null)this.tph = TypePlaceholder.fresh(); - return this.tph; + if(!this.tph.containsKey(this)){ + this.tph.put(this, TypePlaceholder.fresh()); + } + return this.tph.get(this); + //if(this.tph == null)this.tph = TypePlaceholder.fresh(); + //return this.tph; } @Override diff --git a/src/typinferenz/FunNInterface.java b/src/typinferenz/FunNInterface.java index aaf24c60f..83fb2c07d 100644 --- a/src/typinferenz/FunNInterface.java +++ b/src/typinferenz/FunNInterface.java @@ -39,7 +39,7 @@ public class FunNInterface extends Class{ public TypeAssumptions getPublicFieldAssumptions() { //return super.getPublicFieldAssumptions(); TypeAssumptions ret = new TypeAssumptions(); - ret.addMethodAssumption(new MethodAssumption(this.getApplyFunction(), this)); + ret.addAssumption(new MethodAssumption(this.getApplyFunction(), this)); return ret; } diff --git a/src/typinferenz/SingleConstraint.java b/src/typinferenz/SingleConstraint.java index 7546e0919..650b6da97 100755 --- a/src/typinferenz/SingleConstraint.java +++ b/src/typinferenz/SingleConstraint.java @@ -54,10 +54,8 @@ public class SingleConstraint extends UndConstraint{ if(p1 instanceof RefType)((RefType)p1).GTV2TPH(); if(p2 instanceof RefType)((RefType)p2).GTV2TPH(); - if((p1 instanceof GenericTypeVar)) - p1 = ((GenericTypeVar)p1).getTypePlaceHolder(); - if((p2 instanceof GenericTypeVar)) - p2 = ((GenericTypeVar)p2).getTypePlaceHolder(); + if((p1 instanceof GenericTypeVar))p1 = ((GenericTypeVar)p1).getTypePlaceHolder(); + if((p2 instanceof GenericTypeVar))p2 = ((GenericTypeVar)p2).getTypePlaceHolder(); // BaseTypes werden in RefTypes umgewandelt. Constraints dürfen nur RefTypes oder TypePlaceholder enthalten, da sonst der Unify-Algorithmus nicht funktioniert. if(!(p1 instanceof RefType) && !(p1 instanceof TypePlaceholder) && !(p1 instanceof GenericTypeVar))p1 = new RefType(p1); diff --git a/src/typinferenz/assumptions/ClassAssumption.java b/src/typinferenz/assumptions/ClassAssumption.java index d9bfd0a42..8c0da6c82 100644 --- a/src/typinferenz/assumptions/ClassAssumption.java +++ b/src/typinferenz/assumptions/ClassAssumption.java @@ -4,7 +4,13 @@ import mycompiler.myclass.*; import mycompiler.myclass.Class; import mycompiler.mytype.*; -public class ClassAssumption { +/** + * Nicht wirklich eine Assumption. + * Wird benutzt um Typen von Variablen zu verifizieren. + * @author janulrich + * + */ +public class ClassAssumption{ private Class classType; diff --git a/src/typinferenz/assumptions/MethodAssumption.java b/src/typinferenz/assumptions/MethodAssumption.java index 5d258564e..5981d0a1e 100644 --- a/src/typinferenz/assumptions/MethodAssumption.java +++ b/src/typinferenz/assumptions/MethodAssumption.java @@ -1,5 +1,8 @@ package typinferenz.assumptions; +import java.util.Iterator; + +import mycompiler.myclass.FormalParameter; import mycompiler.myclass.Method; import mycompiler.myclass.Class; import mycompiler.mytypereconstruction.typeassumption.CParaTypeAssumption; @@ -41,7 +44,17 @@ public class MethodAssumption extends FieldAssumption { public String toString(){ - return "MethodAssumption: "+this.method.toString(); + String ret = "MethodAssumption: "; + ret += this.method.getType().toString()+" "; + ret += this.method.get_Name().toString()+"("; + Iterator it = this.method.parameterlist.formalparameter.iterator(); + while(it.hasNext()){ + FormalParameter fp = it.next(); + ret+=fp.toString(); + if(it.hasNext())ret += ","; + } + ret+=")"; + return ret; } @Override diff --git a/src/typinferenz/assumptions/TypeAssumptions.java b/src/typinferenz/assumptions/TypeAssumptions.java index c49d065bd..3d0788062 100755 --- a/src/typinferenz/assumptions/TypeAssumptions.java +++ b/src/typinferenz/assumptions/TypeAssumptions.java @@ -68,27 +68,6 @@ public class TypeAssumptions { this.thisClassName = thisClassName; } - public void addMethodAssumption(MethodAssumption mAss){ - if(!this.methodAssumptions.contains(mAss))this.methodAssumptions.add(mAss); - } - - public void addConstructorAssumption( - ConstructorAssumption constructorAssumption) { - this.constructorAssumptions.add(constructorAssumption); - } - - public void addFieldAssumption(FieldAssumption ass){ - this.fieldAssumptions.add(ass); - } - - public void addLocalVarAssumption(LocalVarAssumption ass){ - this.localVarAssumptions.add(ass); - } - - public void addParameterAssumption(ParameterAssumption ass){ - this.parameterAssumptions.add(ass); - } - /** * Liefert alle bekannten öffentlichen Feldern mit dem Namen withName. Dabei werden alle bekannten Klassen durchsucht. * @param withName @@ -234,16 +213,33 @@ public class TypeAssumptions { return ret; } + public void addAssumption(Assumption ass){ + if(ass instanceof MethodAssumption)this.methodAssumptions.add((MethodAssumption)ass);//if(!this.methodAssumptions.contains(ass))this.methodAssumptions.add((MethodAssumption)ass); + if(ass instanceof FieldAssumption)this.fieldAssumptions.add((FieldAssumption)ass);//if(!this.fieldAssumptions.contains(ass))this.fieldAssumptions.add((FieldAssumption)ass); + if(ass instanceof LocalVarAssumption)this.localVarAssumptions.add((LocalVarAssumption)ass);//if(!this.localVarAssumptions.contains(ass))this.localVarAssumptions.add((LocalVarAssumption)ass); + if(ass instanceof ParameterAssumption)if(!this.parameterAssumptions.contains(ass))this.parameterAssumptions.add((ParameterAssumption)ass);//this.parameterAssumptions.add((ParameterAssumption)ass); + if(ass instanceof ConstructorAssumption)if(!this.constructorAssumptions.contains(ass))this.constructorAssumptions.add((ConstructorAssumption)ass);//this.constructorAssumptions.add((ConstructorAssumption)ass); + } + + private void addAllAssumptions(Vector assumptions){ + for(Assumption ass : assumptions){ + this.addAssumption(ass); + } + } + public TypeAssumptions add(TypeAssumptions assumptions){ - this.methodAssumptions.addAll(assumptions.methodAssumptions); - this.fieldAssumptions.addAll(assumptions.fieldAssumptions); - this.localVarAssumptions.addAll(assumptions.localVarAssumptions); - this.parameterAssumptions.addAll(assumptions.parameterAssumptions); - this.constructorAssumptions.addAll(assumptions.constructorAssumptions); + //for(MethodAssumption ass : assumptions.methodAssumptions){ + // if(!this.methodAssumptions.contains(ass))this.methodAssumptions.add(ass); + //} + //this.methodAssumptions.addAll(assumptions.methodAssumptions); + //this.fieldAssumptions.addAll(assumptions.fieldAssumptions); + //this.localVarAssumptions.addAll(assumptions.localVarAssumptions); + //this.parameterAssumptions.addAll(assumptions.parameterAssumptions); + //this.constructorAssumptions.addAll(assumptions.constructorAssumptions); + this.addAllAssumptions(assumptions.getAllAssumptions()); this.classAssumptions.addAll(assumptions.classAssumptions); return this; } - @Override @@ -281,7 +277,9 @@ public class TypeAssumptions { * @return * @throws TypinferenzException */ - public Type getTypeFor(Type t) throws TypinferenzException{ + public Type getTypeFor(RefType t) throws TypinferenzException{ + //TODO: Die Parameterliste noch kontrollieren: (hier könnte es Constraints geben: "? extends String") + //Alle bekannten Klassen nach diesem Typ durchsuchen: String typName = t.getName(); String[] names = typName.split("[.]"); for(ClassAssumption ass : this.classAssumptions){ @@ -296,7 +294,9 @@ public class TypeAssumptions { if(!names.equals(assNames))match = false; } if(match){ - return ass.getAssumedClass().getType(); + RefType ret = ass.getAssumedClass().getType(); //Dadurch erhält der RefType den vollen Namen (bsp. java.lang.Integer) + ret.set_ParaList(t.get_ParaList()); + return ret; } } throw new TypinferenzException("Der Typ "+t.getName()+" ist nicht korrekt"); diff --git a/test/plugindevelopment/TypeInsertTester.java b/test/plugindevelopment/TypeInsertTester.java index 7cb863b93..cb275bd55 100644 --- a/test/plugindevelopment/TypeInsertTester.java +++ b/test/plugindevelopment/TypeInsertTester.java @@ -68,6 +68,7 @@ public class TypeInsertTester{ } //Source: https://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file + //PS: benötigt Java 7 public static String getFileContent(String path)throws IOException { byte[] encoded = Files.readAllBytes(Paths.get(path)); diff --git a/test/plugindevelopment/TypeInsertTests/GenericTypeVarTest.java b/test/plugindevelopment/TypeInsertTests/GenericTypeVarTest.java index a42cf7add..8166cf86f 100644 --- a/test/plugindevelopment/TypeInsertTests/GenericTypeVarTest.java +++ b/test/plugindevelopment/TypeInsertTests/GenericTypeVarTest.java @@ -7,12 +7,19 @@ import org.junit.Test; public class GenericTypeVarTest { private static final String TEST_FILE = "GenericTypeVarTest.jav"; - + private static final String TEST_FILE2 = "GenericTypeVarTest2.jav"; + /* @Test public void run(){ Vector mustContain = new Vector(); mustContain.add("String methode"); MultipleTypesInsertTester.test(this.TEST_FILE, mustContain); } - + */ + @Test + public void run2(){ + Vector mustContain = new Vector(); + mustContain.add("String var2"); + MultipleTypesInsertTester.test(TEST_FILE2, mustContain); + } } diff --git a/test/plugindevelopment/TypeInsertTests/GenericTypeVarTest2.jav b/test/plugindevelopment/TypeInsertTests/GenericTypeVarTest2.jav new file mode 100644 index 000000000..30a9b171a --- /dev/null +++ b/test/plugindevelopment/TypeInsertTests/GenericTypeVarTest2.jav @@ -0,0 +1,14 @@ +class GTVTest{ +GTVTest2 var; +var2; + +methode(){ + return var.test(var2); +} +} + +class GTVTest2{ + test(GTV2 param){ + return param; + } +} \ No newline at end of file