From 8d4213511d545b7f1afd5f4dc02f82b4fc395e1e Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Thu, 3 Apr 2014 10:35:25 +0200 Subject: [PATCH] Backup-commit --- src/mycompiler/SourceFile.java | 31 ++++++++---- src/mycompiler/myclass/FieldDeclaration.java | 11 +++++ src/mycompiler/myclass/Method.java | 4 +- src/mycompiler/mystatement/LocalVarDecl.java | 8 +++- src/mycompiler/mytype/GenericTypeVar.java | 11 +++++ .../assumptions/ClassAssumption.java | 22 +++++++++ .../assumptions/TypeAssumptions.java | 47 ++++++++++++++++++- .../TypeInsertTests/ImportTest.jav | 10 ++++ .../TypeInsertTests/ImportTest.java | 18 +++++++ 9 files changed, 149 insertions(+), 13 deletions(-) create mode 100644 src/typinferenz/assumptions/ClassAssumption.java create mode 100644 test/plugindevelopment/TypeInsertTests/ImportTest.jav create mode 100644 test/plugindevelopment/TypeInsertTests/ImportTest.java diff --git a/src/mycompiler/SourceFile.java b/src/mycompiler/SourceFile.java index 32ffc47a..357f7039 100755 --- a/src/mycompiler/SourceFile.java +++ b/src/mycompiler/SourceFile.java @@ -45,7 +45,6 @@ import mycompiler.mytypereconstruction.unify.Unify; import org.apache.log4j.Logger; import mycompiler.myclass.*; - import mycompiler.*; import sun.reflect.generics.reflectiveObjects.NotImplementedException; import sun.reflect.generics.reflectiveObjects.TypeVariableImpl; @@ -55,7 +54,9 @@ import typinferenz.FunNInterface; import typinferenz.FunNMethod; import typinferenz.ResultSet; import typinferenz.UndConstraint; +import typinferenz.assumptions.ClassAssumption; import typinferenz.assumptions.MethodAssumption; +import typinferenz.assumptions.ParameterAssumption; import typinferenz.assumptions.TypeAssumptions; import typinferenz.exceptions.TypinferenzException; @@ -660,6 +661,11 @@ public class SourceFile globalAssumptions.add(klasse.getPublicFieldAssumptions()); } + //Assumptions der importierten Klassen sammeln: + TypeAssumptions importAssumptions = this.makeBasicAssumptionsFromJRE(imports); + globalAssumptions.add(importAssumptions); + typinferenzLog.debug("Von JRE erstellte Assumptions: "+importAssumptions); + ConstraintsSet oderConstraints = new ConstraintsSet(); //Alle Constraints der in dieser SourceFile enthaltenen Klassen sammeln: for(Class klasse : KlassenVektor){ @@ -904,6 +910,7 @@ public class SourceFile * Erstellt die Basic Assumptions (siehe MakeBasicAssumptions) als AssumptionSet * @return */ + @Deprecated //angefügt von Andreas Stadelmeier. Grund: Die Funktion wurde neu als makeBasicAssumptionsFromJRE angelegt private TypeAssumptions getBasicAssumptions() { TypeAssumptions ret = new TypeAssumptions(null); @@ -1048,7 +1055,6 @@ public class SourceFile } // ino.method.makeBasicAssumptionsFromJRE.21409.definition - @Deprecated //angefügt von Andreas Stadelmeier. Grund: Die Funktion wurde neu als getBasicAssumptions angelegt private TypeAssumptions makeBasicAssumptionsFromJRE(Vector imports) // ino.end // ino.method.makeBasicAssumptionsFromJRE.21409.body @@ -1147,7 +1153,7 @@ public class SourceFile //CInstVarTypeAssumption instVar = new CInstVarTypeAssumption(className, fields[j].getName(), new RefType(fields[j].getType().getSimpleName()), MyCompiler.NO_LINENUMBER,MyCompiler.NO_LINENUMBER,new Vector()); CInstVarTypeAssumption instVar = new CInstVarTypeAssumption(className, fields[j].getName(), new RefType(fields[j].getType().getName(),-1), MyCompiler.NO_LINENUMBER,MyCompiler.NO_LINENUMBER,new Vector()); //basicAssumptions.addFieldOrLocalVarAssumption(instVar); - parentClass.addField(new FieldDeclaration(0)); + parentClass.addField(new FieldDeclaration(fields[j].getName(),new RefType(fields[j].getType().getName(),-1))); } } for(int j=0;j(),null); - + //CMethodTypeAssumption method = new CMethodTypeAssumption(new RefType(className, 0), methodName, returnType, pt.length,MyCompiler.NO_LINENUMBER,MyCompiler.NO_LINENUMBER,new Vector(),null); + Method method = mycompiler.myclass.Method.createEmptyMethod(methodName, parentClass); + method.setType(returnType); + ParameterList parameterList = new ParameterList(); for(int k=0;k())); + //method.addParaAssumption(new CParaTypeAssumption(className, methodName, pt.length,0,type.getName(), type, MyCompiler.NO_LINENUMBER,MyCompiler.NO_LINENUMBER,new Vector())); + FormalParameter parameter = new FormalParameter(new DeclId(type.get_Name())); + parameter.setType(type); + parameterList.formalparameter.add(parameter); } + method.setParameterList(parameterList); //basicAssumptions.addMethodIntersectionType(new CIntersectionType(method)); - parentClass.addField(mycompiler.myclass.Method.createEmptyMethod(methodName, parentClass)); + parentClass.addField(method); } } @@ -1187,12 +1199,13 @@ public class SourceFile parentClass.addField(new Constructor(constructorMethod)); } } - + + basicAssumptions.add(parentClass.getPublicFieldAssumptions()); + basicAssumptions.addClassAssumption(new ClassAssumption(parentClass)); imports.removeElement(importDecl); doneImports.addElement(importDecl); } - imports.addAll(doneImports); return basicAssumptions; //*/ diff --git a/src/mycompiler/myclass/FieldDeclaration.java b/src/mycompiler/myclass/FieldDeclaration.java index b4f147ed..89962e38 100644 --- a/src/mycompiler/myclass/FieldDeclaration.java +++ b/src/mycompiler/myclass/FieldDeclaration.java @@ -30,6 +30,16 @@ public class FieldDeclaration extends Field{ private Expr wert; //private Type type; + /** + * Dieser Konstruktor der FieldDeclaration erstellt den Syntaxknoten vollständig. + * Kein nachträgliches hinzfügen von Informationen oder aufrufen von parserPostProcessing ist notwendig. + */ + public FieldDeclaration(String name, Type typ){ + super(0);//Dieser Deklarator wird nicht vom Parser aufgerufen. Dadurch gibt es auch keinen Offset + this.setType(typ); + this.set_DeclId(new DeclId(name)); + } + public FieldDeclaration(int offset){ super(offset); } @@ -117,6 +127,7 @@ public class FieldDeclaration extends Field{ @Override public ConstraintsSet TYPE(TypeAssumptions publicAssumptions) { ConstraintsSet ret = new ConstraintsSet(); + this.setType(publicAssumptions.getTypeFor(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/Method.java b/src/mycompiler/myclass/Method.java index 3acb403a..f03a2fa5 100755 --- a/src/mycompiler/myclass/Method.java +++ b/src/mycompiler/myclass/Method.java @@ -465,7 +465,7 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable // ino.end // ino.method.toString.23605.body { - return this.getType() + " " +( (block!=null)?block.toString():""); + return this.getType() + " "+ this.get_Name() +( (block!=null)?block.toString():""); } // ino.end @@ -537,11 +537,13 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable public ConstraintsSet TYPE(TypeAssumptions ass) { + this.returntype = ass.getTypeFor(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)); } ret.add(this.block.TYPEStmt(localAss)); diff --git a/src/mycompiler/mystatement/LocalVarDecl.java b/src/mycompiler/mystatement/LocalVarDecl.java index 87cbb1d4..448a60d2 100755 --- a/src/mycompiler/mystatement/LocalVarDecl.java +++ b/src/mycompiler/mystatement/LocalVarDecl.java @@ -460,13 +460,19 @@ public class LocalVarDecl extends Statement implements TypeInsertable @Override public ConstraintsSet TYPEStmt(TypeAssumptions assumptions) { ConstraintsSet ret = new ConstraintsSet(); - if(this.getType()==null || this.getType() instanceof TypePlaceholder)this.setType(TypePlaceholder.fresh(this)); + this.setType(assumptions.getTypeFor(this.getType())); assumptions.addLocalVarAssumption(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; } + @Override + public void parserPostProcessing(SyntaxTreeNode parent) { + super.parserPostProcessing(parent); + if(this.getType()==null || this.getType() instanceof TypePlaceholder)this.setType(TypePlaceholder.fresh(this)); + } + @Override public String getTypeInformation(){ String ret = "VarDeclaration "; diff --git a/src/mycompiler/mytype/GenericTypeVar.java b/src/mycompiler/mytype/GenericTypeVar.java index 16f447a8..3f43ea4b 100755 --- a/src/mycompiler/mytype/GenericTypeVar.java +++ b/src/mycompiler/mytype/GenericTypeVar.java @@ -7,6 +7,7 @@ import java.util.Vector; // ino.end + import mycompiler.mytypereconstruction.replacementlistener.ITypeReplacementListener; import typinferenz.JavaCodeResult; import typinferenz.ResultSet; @@ -140,6 +141,16 @@ public class GenericTypeVar extends Type return this.tph; } + @Override + public String get_Name() { + return this.getName(); + } + + @Override + public String getName() { + return this.name; + } + } // ino.end diff --git a/src/typinferenz/assumptions/ClassAssumption.java b/src/typinferenz/assumptions/ClassAssumption.java new file mode 100644 index 00000000..d9bfd0a4 --- /dev/null +++ b/src/typinferenz/assumptions/ClassAssumption.java @@ -0,0 +1,22 @@ +package typinferenz.assumptions; + +import mycompiler.myclass.*; +import mycompiler.myclass.Class; +import mycompiler.mytype.*; + +public class ClassAssumption { + + private Class classType; + + public ClassAssumption(Class cls){ + this.classType = cls; + } + + public Class getAssumedClass(){ + return classType; + } + + public String toString(){ + return this.classType.getName(); + } +} diff --git a/src/typinferenz/assumptions/TypeAssumptions.java b/src/typinferenz/assumptions/TypeAssumptions.java index dfe67604..c49d065b 100755 --- a/src/typinferenz/assumptions/TypeAssumptions.java +++ b/src/typinferenz/assumptions/TypeAssumptions.java @@ -52,6 +52,7 @@ public class TypeAssumptions { private Vector fieldAssumptions = new Vector(); private Vector localVarAssumptions = new Vector(); private Vector parameterAssumptions = new Vector(); + private Vector classAssumptions = new Vector(); /** @@ -229,6 +230,7 @@ public class TypeAssumptions { for(Assumption f : this.methodAssumptions)ret.add(f); for(Assumption f : this.fieldAssumptions)ret.add(f); for(Assumption f : this.parameterAssumptions)ret.add(f); + for(Assumption f : this.constructorAssumptions)ret.add(f); return ret; } @@ -237,7 +239,8 @@ public class TypeAssumptions { this.fieldAssumptions.addAll(assumptions.fieldAssumptions); this.localVarAssumptions.addAll(assumptions.localVarAssumptions); this.parameterAssumptions.addAll(assumptions.parameterAssumptions); - + this.constructorAssumptions.addAll(assumptions.constructorAssumptions); + this.classAssumptions.addAll(assumptions.classAssumptions); return this; } @@ -254,11 +257,13 @@ public class TypeAssumptions { @Override public String toString(){ - String ret = "this: "+this.thisClassName; + String ret = "this: "+this.thisClassName+"\n"; ret += "Method Assumptions:\n" + this.methodAssumptions.toString() + "\n"; ret += "FieldVar Assumptions:\n" + this.fieldAssumptions.toString() + "\n"; ret += "LocalVar Assumptions:\n" + this.localVarAssumptions.toString() + "\n"; ret += "Parameter Assumptions:\n" + this.parameterAssumptions.toString() + "\n"; + ret += "Konstruktor Assumptions:\n" + this.constructorAssumptions.toString() + "\n"; + ret += "Class Assumptions:\n" + this.classAssumptions.toString() + "\n"; //return assumptions.toString(); return ret; } @@ -269,6 +274,44 @@ public class TypeAssumptions { return new RefType(thisClassName, 0); } + /** + * Kontrolliert den vom Parser gesetzten Typ. + * Erweitert dessen Bezeichnung, wenn nötig. + * @param t + * @return + * @throws TypinferenzException + */ + public Type getTypeFor(Type t) throws TypinferenzException{ + String typName = t.getName(); + String[] names = typName.split("[.]"); + for(ClassAssumption ass : this.classAssumptions){ + String name = ass.getAssumedClass().getName(); //Das kann auch java.util.Vector sein + String[] assNames = name.split("[.]"); + boolean match = true; + if(names.length == 1){ + match = names[0].equals(assNames[assNames.length-1]); + }else if(names.length == 0 || names.length != assNames.length){ + match = false; + }else for(int i = names.length-1; i>-1;i--){ + if(!names.equals(assNames))match = false; + } + if(match){ + return ass.getAssumedClass().getType(); + } + } + throw new TypinferenzException("Der Typ "+t.getName()+" ist nicht korrekt"); + } + + /** + * Fügt eine TypAssumption an. + * Dadurch wird ein Pool von Typen aufgebaut, welche überhaupt erlaubt sind. + * Wird genutzt um vom Parser eingelesene Typen auf ihre Korrektheit zu kontrollieren. + * @param classAssumption + */ + public void addClassAssumption(ClassAssumption classAssumption) { + this.classAssumptions.add(classAssumption); + } + /** * Prüft einen Typ auf das vorhandensein in den BasicAssumptions. * Dabei werden alle Konstruktoren nach diesem Typ durchsucht. Denn jede Klasse hat einen Konstruktor und der muss in den TypeAssumptions vorhanden sein. diff --git a/test/plugindevelopment/TypeInsertTests/ImportTest.jav b/test/plugindevelopment/TypeInsertTests/ImportTest.jav new file mode 100644 index 00000000..cf9cf8ba --- /dev/null +++ b/test/plugindevelopment/TypeInsertTests/ImportTest.jav @@ -0,0 +1,10 @@ +import java.util.Vector; + +class ImportTest{ +Vector var; +var2; +methode(){ + var.add(var2); +} + +} \ No newline at end of file diff --git a/test/plugindevelopment/TypeInsertTests/ImportTest.java b/test/plugindevelopment/TypeInsertTests/ImportTest.java new file mode 100644 index 00000000..54fd6612 --- /dev/null +++ b/test/plugindevelopment/TypeInsertTests/ImportTest.java @@ -0,0 +1,18 @@ +package plugindevelopment.TypeInsertTests; + +import java.util.Vector; + +import org.junit.Test; + +public class ImportTest { + + private static final String TEST_FILE = "ImportTest.jav"; + + @Test + public void run(){ + Vector mustContain = new Vector(); + mustContain.add("String var2"); + MultipleTypesInsertTester.test(this.TEST_FILE, mustContain); + } + +}