diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index 5310d13b..6f371a1d 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -745,6 +745,7 @@ public class StatementGenerator { if(e.classInstanceCreationExpression_lf_primary() != null){ throw new NotImplementedException(); }else if(e.fieldAccess_lf_primary() != null){ + throw new NotImplementedException(); }else if(e.arrayAccess_lf_primary() != null){ throw new NotImplementedException(); @@ -799,8 +800,6 @@ public class StatementGenerator { if(newExpression.typeArgumentsOrDiamond()!= null){ if(newExpression.typeArgumentsOrDiamond().typeArguments()!=null){ genericArgs = newExpression.typeArgumentsOrDiamond().typeArguments(); - }else { - throw new NotImplementedException(); } } if(newExpression.typeArguments()!= null)throw new NotImplementedException(); diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java index efd20831..d92d0b9a 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java @@ -17,6 +17,7 @@ import de.dhbwstuttgart.syntaxtree.type.Void; import de.dhbwstuttgart.syntaxtree.*; import de.dhbwstuttgart.syntaxtree.statement.Block; import de.dhbwstuttgart.syntaxtree.statement.Statement; +import de.dhbwstuttgart.syntaxtree.type.WildcardType; import org.antlr.v4.runtime.Token; /** @@ -50,15 +51,15 @@ public class ASTFactory { java.lang.Class superjreClass = jreClass.getSuperclass(); RefType superClass; if(parameterSuperClass != null){ - superClass = (RefType) convertType(parameterSuperClass); + superClass = (RefType) createType(parameterSuperClass); }else if(superjreClass != null){ - superClass = (RefType) convertType(superjreClass); + superClass = (RefType) createType(superjreClass); }else{//Jede Klasse und jedes Interface erbt von Object: (auch Object selbst!) - superClass = (RefType) createType(java.lang.Object.class, name, ""); + superClass = (RefType) createType(java.lang.Object.class); } List implementedInterfaces = new ArrayList<>(); for(Type jreInterface : jreClass.getGenericInterfaces()){ - implementedInterfaces.add((RefType) createType(jreInterface, name, "")); + implementedInterfaces.add((RefType) createType(jreInterface)); } GenericDeclarationList genericDeclarationList = createGenerics(jreClass.getTypeParameters(), jreClass, null); @@ -67,29 +68,8 @@ public class ASTFactory { return new ClassOrInterface(modifier, name, felder, methoden, konstruktoren, genericDeclarationList, superClass,isInterface, implementedInterfaces, offset); } - private static RefTypeOrTPHOrWildcardOrGeneric convertType(Type type){ - if(type instanceof ParameterizedType){ - List params = new ArrayList<>(); - for(Type paramType : ((ParameterizedType)type).getActualTypeArguments()){ - params.add(convertType(paramType)); - } - JavaClassName name = new JavaClassName(((ParameterizedType) type).getRawType().getTypeName()); - return new RefType(name, params, new NullToken()); - }else if(type instanceof TypeVariable){ - return new GenericRefType(((TypeVariable) type).getName(), new NullToken()); - }else if(type instanceof Class){ - List params = new ArrayList<>(); - Class paramClass = (Class) type; - for(TypeVariable tv : paramClass.getTypeParameters()){ - params.add(new GenericRefType(tv.getName(), new NullToken())); - } - JavaClassName name = new JavaClassName(paramClass.getName()); - return new RefType(name, params, new NullToken()); - }else throw new NotImplementedException(); - } - private static Field createField(java.lang.reflect.Field field, JavaClassName jreClass) { - return new Field(field.getName(), createType(field.getGenericType(), jreClass, null), field.getModifiers(), new NullToken()); + return new Field(field.getName(), createType(field.getType()), field.getModifiers(), new NullToken()); } //private static RefType createType(Class classType) { @@ -98,13 +78,13 @@ public class ASTFactory { private static de.dhbwstuttgart.syntaxtree.Constructor createConstructor(Constructor constructor, Class inClass) { String name = constructor.getName(); - RefTypeOrTPHOrWildcardOrGeneric returnType = createType(inClass, new JavaClassName(inClass.getName()), name); + RefTypeOrTPHOrWildcardOrGeneric returnType = createType(inClass); Parameter[] jreParams = constructor.getParameters(); Type[] jreGenericParams = constructor.getGenericParameterTypes(); List params = new ArrayList<>(); int i = 0; for(Type jreParam : jreGenericParams){ - RefTypeOrTPHOrWildcardOrGeneric paramType = createType(jreParam,new JavaClassName(inClass.getName()), name); + RefTypeOrTPHOrWildcardOrGeneric paramType = createType(jreParam); params.add(new FormalParameter(jreParams[i].getName(),paramType, new NullToken())); i++; } @@ -130,13 +110,13 @@ public class ASTFactory { }else{ jreRetType = jreMethod.getReturnType(); } - returnType = createType(jreRetType,new JavaClassName(inClass.getName()), name); + returnType = createType(jreRetType); Parameter[] jreParams = jreMethod.getParameters(); Type[] jreGenericParams = jreMethod.getGenericParameterTypes(); List params = new ArrayList<>(); int i = 0; for(Type jreParam : jreGenericParams){ - RefTypeOrTPHOrWildcardOrGeneric paramType = createType(jreParam,new JavaClassName(inClass.getName()), name); + RefTypeOrTPHOrWildcardOrGeneric paramType = createType(jreParam); params.add(new FormalParameter(jreParams[i].getName(),paramType, new NullToken())); i++; } @@ -157,18 +137,7 @@ public class ASTFactory { return new GenericDeclarationList(gtvs,new NullToken()); } - /* - public RefType createType(java.lang.Class jreClass){ - List params = new ArrayList<>(); - for(TypeVariable jreTV : jreClass.getTypeParameters()){ - RefType gtv = createType(jreTV); - params.add(gtv); - } - return new RefType(names.getName(jreClass.getName()), params, new NullToken()); - } - */ - - private static RefTypeOrTPHOrWildcardOrGeneric createType(java.lang.reflect.Type type, JavaClassName parentClass, String parentMethod){ + private static RefTypeOrTPHOrWildcardOrGeneric createType(java.lang.reflect.Type type){ if(type.getTypeName().equals("void")){ return new Void(new NullToken()); }else if(type.getTypeName().equals("int")){ @@ -193,7 +162,7 @@ public class ASTFactory { List params = new ArrayList<>(); if(type instanceof ParameterizedType){ for(Type t : ((ParameterizedType)type).getActualTypeArguments()){ - params.add(createType(t, parentClass, parentMethod)); + params.add(createType(t)); } } String name = type.getTypeName(); @@ -201,8 +170,19 @@ public class ASTFactory { //Diese entfernen: name = name.split("<")[0]; } - RefType ret = new RefType(new JavaClassName(name), params, new NullToken()); - return ret; + if(type instanceof java.lang.reflect.WildcardType){ + java.lang.reflect.WildcardType wildcardType = (java.lang.reflect.WildcardType) type; + if(wildcardType.getLowerBounds().length > 0){ + return new SuperWildcardType(createType(wildcardType.getLowerBounds()[0]), new NullToken()); + }else if(wildcardType.getUpperBounds().length > 0){ + return new ExtendsWildcardType(createType(wildcardType.getUpperBounds()[0]), new NullToken()); + }else{//Es handelt sich um den '?'-Typ: + return new ExtendsWildcardType(createObjectType(), new NullToken()); + } + }else{ + RefType ret = new RefType(new JavaClassName(name), params, new NullToken()); + return ret; + } } } @@ -212,7 +192,7 @@ public class ASTFactory { java.lang.reflect.Type[] bounds = jreTypeVar.getBounds(); if(bounds.length > 0){ for(java.lang.reflect.Type bound : bounds){ - genericBounds.add((RefType) createType(bound, parentClass, parentMethod)); + genericBounds.add((RefType) createType(bound)); } } return new de.dhbwstuttgart.syntaxtree.GenericTypeVar(jreTVName, genericBounds, new NullToken(), new NullToken()); diff --git a/src/de/dhbwstuttgart/syntaxtree/type/ExtendsWildcardType.java b/src/de/dhbwstuttgart/syntaxtree/type/ExtendsWildcardType.java index d5abd93b..5f282e46 100644 --- a/src/de/dhbwstuttgart/syntaxtree/type/ExtendsWildcardType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/ExtendsWildcardType.java @@ -34,6 +34,10 @@ public class ExtendsWildcardType extends WildcardType{ return false; } + @Override + public String toString() { + return "? extends "+innerType.toString(); + } @Override public void accept(ASTVisitor visitor) { diff --git a/src/de/dhbwstuttgart/syntaxtree/type/SuperWildcardType.java b/src/de/dhbwstuttgart/syntaxtree/type/SuperWildcardType.java index 740c80e5..625fb01b 100644 --- a/src/de/dhbwstuttgart/syntaxtree/type/SuperWildcardType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/SuperWildcardType.java @@ -35,6 +35,11 @@ public class SuperWildcardType extends WildcardType{ return this.innerType; } + @Override + public String toString() { + return "? super "+innerType.toString(); + } + @Override public boolean isExtends() { return false; diff --git a/src/de/dhbwstuttgart/typeinference/assumptions/MethodAssumption.java b/src/de/dhbwstuttgart/typeinference/assumptions/MethodAssumption.java index e17281e5..d4f7f119 100644 --- a/src/de/dhbwstuttgart/typeinference/assumptions/MethodAssumption.java +++ b/src/de/dhbwstuttgart/typeinference/assumptions/MethodAssumption.java @@ -38,16 +38,13 @@ public class MethodAssumption extends Assumption{ } public RefTypeOrTPHOrWildcardOrGeneric getReturnType(GenericsResolver resolver) { - if(retType instanceof GenericRefType)return resolver.resolve(retType); - return retType; + return resolver.resolve(retType); } public List getArgTypes(GenericsResolver resolver) { List ret = new ArrayList<>(); for(RefTypeOrTPHOrWildcardOrGeneric param : params){ - if(param instanceof GenericRefType){ //Generics in den Assumptions müssen umgewandelt werden. - param = resolver.resolve((GenericRefType) param); - } + param = resolver.resolve(param); ret.add(param); } return ret; diff --git a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java index 4a41176c..6a441050 100644 --- a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java +++ b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java @@ -60,8 +60,7 @@ public class TYPE { return new TypeInferenceInformation(classes); } - - */ + */ private ConstraintSet getConstraintsMethod(Method m, TypeInferenceInformation info, ClassOrInterface currentClass) { if(m.block == null)return new ConstraintSet(); //Abstrakte Methoden generieren keine Constraints TypeInferenceBlockInformation blockInfo = new TypeInferenceBlockInformation(info.getAvailableClasses(), currentClass, m);