Import Test funktioniert jetzt

This commit is contained in:
JanUlrich 2014-04-09 14:12:55 +02:00
parent 8d4213511d
commit 5316b6ca51
20 changed files with 160 additions and 57 deletions

View File

@ -703,6 +703,7 @@ public class SourceFile
}
}
/*
//Alle Generischen Typvariablen in TPH umwandeln:
HashMap<GenericTypeVar,TypePlaceholder> gtv2tph = new HashMap<GenericTypeVar,TypePlaceholder>();
for(Pair pair : constraints){
@ -723,7 +724,7 @@ public class SourceFile
pair.TA2 = tph;
}
}
*/
//Erst die Unifizierung erstellen:
Vector<Pair> constraintsClone = (Vector<Pair>)constraints.clone();
Vector<Vector<Pair>> unifyResult = Unify.unify(constraintsClone, finiteClosure);
@ -1159,6 +1160,8 @@ public class SourceFile
for(int j=0;j<methods.length;j++){
if(java.lang.reflect.Modifier.isPublic(methods[j].getModifiers())){
String methodName=methods[j].getName();
if(methodName.equals("add")){
java.lang.reflect.Type genericReturnType=methods[j].getGenericReturnType();
Type returnType=createTypeFromJavaGenericType(genericReturnType,methods[j].getReturnType(),jreSpiderRegistry);
@ -1181,7 +1184,9 @@ public class SourceFile
method.setParameterList(parameterList);
//basicAssumptions.addMethodIntersectionType(new CIntersectionType(method));
parentClass.addField(method);
}
}
}
}
for(int j=0;j<constructors.length;j++){

View File

@ -4,6 +4,7 @@ import java.util.Vector;
import typinferenz.exceptions.TypinferenzException;
import mycompiler.myclass.Class;
import mycompiler.mytype.GenericTypeVar;
public abstract class SyntaxTreeNode {
@ -14,6 +15,8 @@ public abstract class SyntaxTreeNode {
* Erfüllt folgenden Aufgaben:
* 1. Füllt fehlende Typangaben mit TPHs auf.
* 2. Verknüpft die Knoten des Syntaxbaums. (setzt Parent)
* 3. Wechselt RefTypes gegebenenfalls mit GenericTypeVars aus.
* 4. Führt einen Teil des Syntaxckecks durch.
*
*/
public void parserPostProcessing(SyntaxTreeNode parent) {
@ -42,6 +45,7 @@ public abstract class SyntaxTreeNode {
return this.toString();
}
@Override
public boolean equals(Object object){
if(!(object instanceof SyntaxTreeNode))return false;

View File

@ -59,6 +59,7 @@ import org.apache.log4j.Logger;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import typinferenz.ConstraintsSet;
import typinferenz.JavaCodeResult;
@ -67,6 +68,7 @@ import typinferenz.ResultSet;
import typinferenz.Typeable;
import typinferenz.UndConstraint;
import typinferenz.FunN;
import typinferenz.assumptions.ClassAssumption;
import typinferenz.assumptions.TypeAssumptions;
import typinferenz.exceptions.TypinferenzException;
import typinferenz.*;
@ -1247,6 +1249,7 @@ public class Class extends SyntaxTreeNode implements AClassOrInterface
/**
* Erstellt einen RefType, welcher auf diese Klasse verweist
* Ersetzt alle Generischen Variablen in der Parameterliste mit TPH
* @return
*/
public RefType getType() {
@ -1265,6 +1268,7 @@ public class Class extends SyntaxTreeNode implements AClassOrInterface
*/
public TypeAssumptions getPublicFieldAssumptions() {
TypeAssumptions ret = this.getPrivateFieldAssumptions();
ret.addClassAssumption(new ClassAssumption(this));
for(Field f : this.getFields()){
ret.add(f.createTypeAssumptions(this));
}

View File

@ -242,7 +242,7 @@ public class Constructor extends Method {
public TypeAssumptions createTypeAssumptions(Class classmember) {
Class parentClass = this.getParentClass();
TypeAssumptions ret = new TypeAssumptions();
ret.addConstructorAssumption(new ConstructorAssumption(this, parentClass));
ret.addAssumption(new ConstructorAssumption(this, parentClass));
return ret;
}

View File

@ -105,6 +105,12 @@ public abstract class Field extends SyntaxTreeNode implements TypeInsertable, Ty
return new TypeInsertPoint(tph, this, resultSet.getTypeEqualTo(tph), resultSet);
}
/**
* Wird im Zuge des ParserPostProcessing aufgerufen.
* Der Parser kann den Unterschied zwischen einem RefType und einer GTV nicht erkennen.
* Diese Methode ersetzt die RefTypes gegebenenfalls durch eine GTV.
* @param paralist
*/
public void wandleRefTypeAttributes2GenericAttributes(Vector<Type> paralist){
// Zuerst Returntype untersuchen
Type type=getType();

View File

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

View File

@ -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
/**
* <br/>Author: J<EFBFBD>rg B<EFBFBD>uerle
* @param t

View File

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

View File

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

View File

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

View File

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

View File

@ -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<EFBFBD>rg B<EFBFBD>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<String, Vector<GenericTypeVar>> m_TypeErasureList;
// ino.end
private TypePlaceholder tph;
private static HashMap<GenericTypeVar,TypePlaceholder> tph = new HashMap<GenericTypeVar,TypePlaceholder>();
/**
*
@ -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

View File

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

View File

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

View File

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

View File

@ -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<FormalParameter> it = this.method.parameterlist.formalparameter.iterator();
while(it.hasNext()){
FormalParameter fp = it.next();
ret+=fp.toString();
if(it.hasNext())ret += ",";
}
ret+=")";
return ret;
}
@Override

View File

@ -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,18 +213,35 @@ 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<Assumption> 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
public TypeAssumptions clone(){
TypeAssumptions ret = new TypeAssumptions();
@ -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");

View File

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

View File

@ -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<String> mustContain = new Vector<String>();
mustContain.add("String methode");
MultipleTypesInsertTester.test(this.TEST_FILE, mustContain);
}
*/
@Test
public void run2(){
Vector<String> mustContain = new Vector<String>();
mustContain.add("String var2");
MultipleTypesInsertTester.test(TEST_FILE2, mustContain);
}
}

View File

@ -0,0 +1,14 @@
class GTVTest<GTV1>{
GTVTest2<String> var;
var2;
methode(){
return var.test(var2);
}
}
class GTVTest2<GTV2>{
test(GTV2 param){
return param;
}
}