Merge branch 'bigRefactoring' into unify-test

Fehelr mit elementAt geloest
This commit is contained in:
Martin Plümicke 2018-03-13 10:41:57 +01:00
commit dea3da3b69
21 changed files with 179 additions and 115 deletions

View File

@ -42,6 +42,12 @@
<artifactId>asm-all</artifactId> <artifactId>asm-all</artifactId>
<version>[4.0.0,)</version> <version>[4.0.0,)</version>
</dependency> </dependency>
<!--
<dependency>
<groupId>org.bitbucket.mstrobel</groupId>
<artifactId>procyon-reflection</artifactId>
<version>[0.5.32,)</version>
</dependency> -->
</dependencies> </dependencies>
<build> <build>

View File

@ -10,12 +10,10 @@ import org.objectweb.asm.signature.SignatureWriter;
import de.dhbwstuttgart.bytecode.descriptor.TypeToDescriptor; import de.dhbwstuttgart.bytecode.descriptor.TypeToDescriptor;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.Constructor; import de.dhbwstuttgart.syntaxtree.Constructor;
import de.dhbwstuttgart.syntaxtree.FormalParameter;
import de.dhbwstuttgart.syntaxtree.GenericTypeVar; import de.dhbwstuttgart.syntaxtree.GenericTypeVar;
import de.dhbwstuttgart.syntaxtree.Method; import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression; import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression;
import de.dhbwstuttgart.syntaxtree.type.GenericRefType; import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.typeinference.result.ResultSet; import de.dhbwstuttgart.typeinference.result.ResultSet;
@ -165,7 +163,7 @@ public class Signature {
* @param genAndBounds * @param genAndBounds
*/ */
private void getBoundsOfTypeVar(GenericTypeVar g, HashMap<String, String> genAndBounds) { private void getBoundsOfTypeVar(GenericTypeVar g, HashMap<String, String> genAndBounds) {
sw.visitFormalTypeParameter(g.getParsedName()); sw.visitFormalTypeParameter(g.getName());
Iterator<? extends RefTypeOrTPHOrWildcardOrGeneric> bItr = g.getBounds().iterator(); Iterator<? extends RefTypeOrTPHOrWildcardOrGeneric> bItr = g.getBounds().iterator();
while(bItr.hasNext()) { while(bItr.hasNext()) {
@ -174,7 +172,7 @@ public class Signature {
// System.out.println("GetBounds: " + boundDesc); // System.out.println("GetBounds: " + boundDesc);
// Ensure that <...> extends java.lang.Object OR ... // Ensure that <...> extends java.lang.Object OR ...
sw.visitClassBound().visitClassType(boundDesc); sw.visitClassBound().visitClassType(boundDesc);
genAndBounds.put(g.getParsedName(), boundDesc); genAndBounds.put(g.getName(), boundDesc);
} }
sw.visitClassBound().visitEnd(); sw.visitClassBound().visitEnd();
} }

View File

@ -1,7 +1,6 @@
package de.dhbwstuttgart.parser.SyntaxTreeGenerator; package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
import de.dhbwstuttgart.exceptions.DebugException; import de.dhbwstuttgart.exceptions.DebugException;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.GenericTypeVar; import de.dhbwstuttgart.syntaxtree.GenericTypeVar;
@ -51,11 +50,11 @@ public class FCGenerator {
HashMap<String, RefTypeOrTPHOrWildcardOrGeneric> newGTVs = new HashMap<>(); HashMap<String, RefTypeOrTPHOrWildcardOrGeneric> newGTVs = new HashMap<>();
//Generics mit gleichem Namen müssen den selben TPH bekommen //Generics mit gleichem Namen müssen den selben TPH bekommen
for(GenericTypeVar gtv : forType.getGenerics()){ for(GenericTypeVar gtv : forType.getGenerics()){
if(!gtvs.containsKey(gtv.getParsedName())){ if(!gtvs.containsKey(gtv.getName())){
gtvs.put(gtv.getParsedName(), TypePlaceholder.fresh(new NullToken())); gtvs.put(gtv.getName(), TypePlaceholder.fresh(new NullToken()));
newGTVs.put(gtv.getParsedName(), TypePlaceholder.fresh(new NullToken())); newGTVs.put(gtv.getName(), TypePlaceholder.fresh(new NullToken()));
} }
params.add(gtvs.get(gtv.getParsedName())); params.add(gtvs.get(gtv.getName()));
} }
Optional<ClassOrInterface> hasSuperclass = availableClasses.stream().filter(cl -> forType.getSuperClass().getName().equals(cl.getClassName())).findAny(); Optional<ClassOrInterface> hasSuperclass = availableClasses.stream().filter(cl -> forType.getSuperClass().getName().equals(cl.getClassName())).findAny();
@ -79,7 +78,7 @@ public class FCGenerator {
RefTypeOrTPHOrWildcardOrGeneric setType = itSetParams.next(); RefTypeOrTPHOrWildcardOrGeneric setType = itSetParams.next();
//In diesem Typ die GTVs durch TPHs und Einsetzungen austauschen: //In diesem Typ die GTVs durch TPHs und Einsetzungen austauschen:
RefTypeOrTPHOrWildcardOrGeneric setSetType = setType.acceptTV(new TypeExchanger(gtvs)); RefTypeOrTPHOrWildcardOrGeneric setSetType = setType.acceptTV(new TypeExchanger(gtvs));
newGTVs.put(itGenParams.next().getParsedName(), setSetType); newGTVs.put(itGenParams.next().getName(), setSetType);
} }
RefTypeOrTPHOrWildcardOrGeneric superType = forType.getSuperClass().acceptTV(new TypeExchanger(newGTVs)); RefTypeOrTPHOrWildcardOrGeneric superType = forType.getSuperClass().acceptTV(new TypeExchanger(newGTVs));

View File

@ -3,8 +3,8 @@ package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.parser.scope.JavaClassName;
public class GenericContext { public class GenericContext {
public final String parentMethod; private final String parentMethod;
public final JavaClassName parentClass; private final JavaClassName parentClass;
public GenericContext(JavaClassName parentClass, String parentMethod) { public GenericContext(JavaClassName parentClass, String parentMethod) {
if(parentMethod == null)parentMethod = ""; if(parentMethod == null)parentMethod = "";

View File

@ -3,7 +3,6 @@ package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.exceptions.TypeinferenceException; import de.dhbwstuttgart.exceptions.TypeinferenceException;
import de.dhbwstuttgart.parser.antlr.Java8Parser; import de.dhbwstuttgart.parser.antlr.Java8Parser;
import de.dhbwstuttgart.parser.scope.GenericTypeName;
import de.dhbwstuttgart.parser.scope.GenericsRegistry; import de.dhbwstuttgart.parser.scope.GenericsRegistry;
import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.parser.scope.JavaClassRegistry; import de.dhbwstuttgart.parser.scope.JavaClassRegistry;
@ -75,7 +74,7 @@ public class TypeGenerator {
//Problem <A extends B, B> ist erlaubt, würde aber bei den Bounds von A den Generic B nicht als solchen erkennen //Problem <A extends B, B> ist erlaubt, würde aber bei den Bounds von A den Generic B nicht als solchen erkennen
List<RefTypeOrTPHOrWildcardOrGeneric> bounds = TypeGenerator.convert(typeParameter.typeBound(),reg, generics); List<RefTypeOrTPHOrWildcardOrGeneric> bounds = TypeGenerator.convert(typeParameter.typeBound(),reg, generics);
GenericTypeVar ret = new GenericTypeVar(new GenericTypeName(new GenericContext(parentClass, parentMethod), name), bounds, typeParameter.getStart(), typeParameter.getStop()); GenericTypeVar ret = new GenericTypeVar(name, bounds, typeParameter.getStart(), typeParameter.getStop());
generics.put(name, new GenericContext(parentClass, parentMethod)); generics.put(name, new GenericContext(parentClass, parentMethod));
return ret; return ret;
} }
@ -131,7 +130,7 @@ public class TypeGenerator {
String name, Java8Parser.TypeArgumentsContext typeArguments, Token offset, JavaClassRegistry reg, GenericsRegistry generics){ String name, Java8Parser.TypeArgumentsContext typeArguments, Token offset, JavaClassRegistry reg, GenericsRegistry generics){
if(!reg.contains(name)){ //Dann könnte es ein Generische Type sein if(!reg.contains(name)){ //Dann könnte es ein Generische Type sein
if(generics.contains(name)){ if(generics.contains(name)){
return new GenericRefType(new GenericTypeName(generics.get(name),name), offset); return new GenericRefType(name, offset);
}else{ }else{
throw new TypeinferenceException("Der Typ "+ name + " ist nicht vorhanden",offset); throw new TypeinferenceException("Der Typ "+ name + " ist nicht vorhanden",offset);
} }

View File

@ -1,26 +0,0 @@
package de.dhbwstuttgart.parser.scope;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.GenericContext;
public class GenericTypeName extends JavaClassName {
private final static String DELIMITER = "%";
private final JavaClassName parentClass;
private final String methodName;
public GenericTypeName(GenericContext genericContext, String name) {
super(name);
this.parentClass = genericContext.parentClass;
this.methodName = genericContext.parentMethod;
}
public String getUniqueIdentifier() {
return parentClass.toString()
+ DELIMITER + methodName
+ DELIMITER + super.toString();
}
public JavaClassName getParentClass() {
return parentClass;
}
}

View File

@ -2,12 +2,10 @@ package de.dhbwstuttgart.sat.asp.writer;
import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.FCGenerator; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.FCGenerator;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.sat.asp.ASPStringConverter; import de.dhbwstuttgart.sat.asp.ASPStringConverter;
import de.dhbwstuttgart.sat.asp.writer.model.*; import de.dhbwstuttgart.sat.asp.writer.model.*;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.GenericTypeVar; import de.dhbwstuttgart.syntaxtree.GenericTypeVar;
import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory;
import de.dhbwstuttgart.syntaxtree.type.*; import de.dhbwstuttgart.syntaxtree.type.*;
import de.dhbwstuttgart.typeinference.constraints.Constraint; import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
@ -100,7 +98,7 @@ public class ASPGenerator {
@Override @Override
public ASPType visit(GenericRefType genericRefType) { public ASPType visit(GenericRefType genericRefType) {
return new ASPRefType(ASPStringConverter.toConstant(genericRefType.getName()), return new ASPRefType(ASPStringConverter.toConstant(genericRefType.getParsedName()),
new ASPParameterList(new ArrayList<>(), writer)); new ASPParameterList(new ArrayList<>(), writer));
} }
} }

View File

@ -1,7 +1,5 @@
package de.dhbwstuttgart.syntaxtree; package de.dhbwstuttgart.syntaxtree;
import de.dhbwstuttgart.parser.scope.GenericTypeName;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.Token;
@ -26,9 +24,9 @@ public class GenericTypeVar extends SyntaxTreeNode
*/ */
List<? extends RefTypeOrTPHOrWildcardOrGeneric> bounds=new ArrayList<RefTypeOrTPHOrWildcardOrGeneric>(); List<? extends RefTypeOrTPHOrWildcardOrGeneric> bounds=new ArrayList<RefTypeOrTPHOrWildcardOrGeneric>();
private Token endOffset; private Token endOffset;
private GenericTypeName name; private String name;
public GenericTypeVar(GenericTypeName s, List<? extends RefTypeOrTPHOrWildcardOrGeneric> bounds, Token offset, Token endOffset) public GenericTypeVar(String s, List<? extends RefTypeOrTPHOrWildcardOrGeneric> bounds, Token offset, Token endOffset)
{ {
super(offset); super(offset);
name = s; name = s;
@ -50,18 +48,15 @@ public class GenericTypeVar extends SyntaxTreeNode
return "BoGTV " + this.name; return "BoGTV " + this.name;
} }
public GenericTypeName getName() { public String getName(){
return name;
}
public String getParsedName(){
return name.toString(); return name.toString();
} }
/*
public JavaClassName definingClass(){ public JavaClassName definingClass(){
return name.getParentClass(); return name.getParentClass();
} }
*/
@Override @Override
public void accept(ASTVisitor visitor) { public void accept(ASTVisitor visitor) {
visitor.visit(this); visitor.visit(this);

View File

@ -8,7 +8,6 @@ import java.util.List;
import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.GenericContext; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.GenericContext;
import de.dhbwstuttgart.parser.scope.GenericTypeName;
import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.parser.scope.JavaClassRegistry; import de.dhbwstuttgart.parser.scope.JavaClassRegistry;
import de.dhbwstuttgart.syntaxtree.Field; import de.dhbwstuttgart.syntaxtree.Field;
@ -43,12 +42,17 @@ public class ASTFactory {
} }
int modifier = jreClass.getModifiers(); int modifier = jreClass.getModifiers();
boolean isInterface = jreClass.isInterface(); boolean isInterface = jreClass.isInterface();
//see: https://stackoverflow.com/questions/9934774/getting-generic-parameter-from-supertype-class
ParameterizedType parameterSuperClass = null;
Type tempSuperClass = jreClass.getGenericSuperclass();
if(tempSuperClass != null && tempSuperClass instanceof ParameterizedType)
parameterSuperClass = (ParameterizedType) tempSuperClass;
java.lang.Class superjreClass = jreClass.getSuperclass(); java.lang.Class superjreClass = jreClass.getSuperclass();
RefType superClass; RefType superClass;
if(superjreClass != null){ if(superjreClass != null){
List<RefTypeOrTPHOrWildcardOrGeneric> params = new ArrayList<>(); List<RefTypeOrTPHOrWildcardOrGeneric> params = new ArrayList<>();
for(TypeVariable tv : superjreClass.getTypeParameters()){ for(TypeVariable tv : superjreClass.getTypeParameters()){
params.add(new GenericRefType(new GenericTypeName(new GenericContext( name, null),tv.getName()), new NullToken())); params.add(new GenericRefType(tv.getName(), new NullToken()));
} }
superClass = new RefType(new JavaClassName(superjreClass.getName()), params, new NullToken()); superClass = new RefType(new JavaClassName(superjreClass.getName()), params, new NullToken());
}else{//Jede Klasse und jedes Interface erbt von Object: (auch Object selbst!) }else{//Jede Klasse und jedes Interface erbt von Object: (auch Object selbst!)
@ -65,18 +69,41 @@ public class ASTFactory {
return new ClassOrInterface(modifier, name, felder, methoden, konstruktoren, genericDeclarationList, superClass,isInterface, implementedInterfaces, offset); return new ClassOrInterface(modifier, name, felder, methoden, konstruktoren, genericDeclarationList, superClass,isInterface, implementedInterfaces, offset);
} }
private static RefTypeOrTPHOrWildcardOrGeneric convertType(Type type){
JavaClassName name = null;
List<RefTypeOrTPHOrWildcardOrGeneric> params = new ArrayList<>();
if(type instanceof ParameterizedType){
for(Type paramType : ((ParameterizedType)type).getActualTypeArguments()){
params.add(convertType(paramType));
}
}else if(type instanceof TypeVariable){
}else if(type instanceof Class){
Class paramClass = (Class) type;
//params.add(new RefType(paramClass.getName())
}else throw new NotImplementedException();
return new RefType(name, params, new NullToken());
}
private static Field createField(java.lang.reflect.Field field, JavaClassName jreClass) { private static Field createField(java.lang.reflect.Field field, JavaClassName jreClass) {
return new Field(field.getName(), createType(field.getType(), jreClass, null), field.getModifiers(), new NullToken()); return new Field(field.getName(), createType(field.getType(), jreClass, null), field.getModifiers(), new NullToken());
} }
//private static RefType createType(Class classType) {
// return createClass(classType).getType();
//}
private static de.dhbwstuttgart.syntaxtree.Constructor createConstructor(Constructor constructor, Class inClass) { private static de.dhbwstuttgart.syntaxtree.Constructor createConstructor(Constructor constructor, Class inClass) {
String name = constructor.getName(); String name = constructor.getName();
RefTypeOrTPHOrWildcardOrGeneric returnType = createType(inClass, new JavaClassName(inClass.getName()), name); RefTypeOrTPHOrWildcardOrGeneric returnType = createType(inClass, new JavaClassName(inClass.getName()), name);
Parameter[] jreParams = constructor.getParameters(); Parameter[] jreParams = constructor.getParameters();
Type[] jreGenericParams = constructor.getGenericParameterTypes();
List<FormalParameter> params = new ArrayList<>(); List<FormalParameter> params = new ArrayList<>();
for(Parameter jreParam : jreParams){ int i = 0;
RefTypeOrTPHOrWildcardOrGeneric paramType = createType(jreParam.getType(),new JavaClassName(inClass.getName()), name); for(Type jreParam : jreGenericParams){
params.add(new FormalParameter(jreParam.getName(),paramType, new NullToken())); RefTypeOrTPHOrWildcardOrGeneric paramType = createType(jreParam,new JavaClassName(inClass.getName()), name);
params.add(new FormalParameter(jreParams[i].getName(),paramType, new NullToken()));
i++;
} }
ParameterList parameterList = new ParameterList(params, new NullToken()); ParameterList parameterList = new ParameterList(params, new NullToken());
Block block = new Block(new ArrayList<Statement>(), new NullToken()); Block block = new Block(new ArrayList<Statement>(), new NullToken());
@ -89,21 +116,26 @@ public class ASTFactory {
} }
return new de.dhbwstuttgart.syntaxtree.Constructor(modifier, name,returnType, parameterList, block, gtvDeclarations, offset, new ArrayList<>()); return new de.dhbwstuttgart.syntaxtree.Constructor(modifier, name,returnType, parameterList, block, gtvDeclarations, offset, new ArrayList<>());
} }
//private static RefType createType(Class classType) {
// return createClass(classType).getType();
//}
public static Method createMethod(java.lang.reflect.Method jreMethod, java.lang.Class inClass){ public static Method createMethod(java.lang.reflect.Method jreMethod, java.lang.Class inClass){
String name = jreMethod.getName(); String name = jreMethod.getName();
RefTypeOrTPHOrWildcardOrGeneric returnType; RefTypeOrTPHOrWildcardOrGeneric returnType;
returnType = createType(jreMethod.getReturnType(),new JavaClassName(inClass.getName()), name); Type jreRetType;
if(jreMethod.getGenericReturnType()!=null){
jreRetType = jreMethod.getGenericReturnType();
}else{
jreRetType = jreMethod.getReturnType();
}
returnType = createType(jreRetType,new JavaClassName(inClass.getName()), name);
Parameter[] jreParams = jreMethod.getParameters(); Parameter[] jreParams = jreMethod.getParameters();
Type[] jreGenericParams = jreMethod.getGenericParameterTypes();
List<FormalParameter> params = new ArrayList<>(); List<FormalParameter> params = new ArrayList<>();
for(Parameter jreParam : jreParams){ int i = 0;
RefTypeOrTPHOrWildcardOrGeneric paramType = createType(jreParam.getType(),new JavaClassName(inClass.getName()), name); for(Type jreParam : jreGenericParams){
params.add(new FormalParameter(jreParam.getName(),paramType, new NullToken())); RefTypeOrTPHOrWildcardOrGeneric paramType = createType(jreParam,new JavaClassName(inClass.getName()), name);
params.add(new FormalParameter(jreParams[i].getName(),paramType, new NullToken()));
i++;
} }
ParameterList parameterList = new ParameterList(params, new NullToken()); ParameterList parameterList = new ParameterList(params, new NullToken());
Block block = new Block(new ArrayList<Statement>(), new NullToken()); Block block = new Block(new ArrayList<Statement>(), new NullToken());
@ -153,9 +185,7 @@ public class ASTFactory {
}else{ }else{
if(type instanceof TypeVariable){ if(type instanceof TypeVariable){
//GTVDeclarationContext via "(TypeVariable) type).getGenericDeclaration()" //GTVDeclarationContext via "(TypeVariable) type).getGenericDeclaration()"
return new GenericRefType( return new GenericRefType(type.getTypeName(), new NullToken());
new GenericTypeName(new GenericContext(parentClass, parentMethod),type.getTypeName()),
new NullToken());
} }
List<RefTypeOrTPHOrWildcardOrGeneric> params = new ArrayList<>(); List<RefTypeOrTPHOrWildcardOrGeneric> params = new ArrayList<>();
if(type instanceof ParameterizedType){ if(type instanceof ParameterizedType){
@ -177,9 +207,7 @@ public class ASTFactory {
genericBounds.add((RefType) createType(bound, parentClass, parentMethod)); genericBounds.add((RefType) createType(bound, parentClass, parentMethod));
} }
} }
return new de.dhbwstuttgart.syntaxtree.GenericTypeVar( return new de.dhbwstuttgart.syntaxtree.GenericTypeVar(jreTVName, genericBounds, new NullToken(), new NullToken());
new GenericTypeName(new GenericContext(parentClass, parentMethod), jreTVName)
, genericBounds, new NullToken(), new NullToken());
} }
public static ClassOrInterface createObjectClass() { public static ClassOrInterface createObjectClass() {

View File

@ -1,25 +1,19 @@
package de.dhbwstuttgart.syntaxtree.type; package de.dhbwstuttgart.syntaxtree.type;
import de.dhbwstuttgart.parser.scope.GenericTypeName;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.syntaxtree.ASTVisitor; import de.dhbwstuttgart.syntaxtree.ASTVisitor;
import de.dhbwstuttgart.typeinference.result.ResultSetVisitor; import de.dhbwstuttgart.typeinference.result.ResultSetVisitor;
import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.Token;
public class GenericRefType extends RefTypeOrTPHOrWildcardOrGeneric public class GenericRefType extends RefTypeOrTPHOrWildcardOrGeneric
{ {
private GenericTypeName name; private String name;
public GenericRefType(GenericTypeName name, Token offset) public GenericRefType(String name, Token offset)
{ {
super(offset); super(offset);
this.name = name; this.name = name;
} }
public GenericTypeName getName(){
return name;
}
public String getParsedName(){ public String getParsedName(){
return name.toString(); return name.toString();
} }

View File

@ -30,7 +30,7 @@ public class SuperWildcardType extends WildcardType{
* Beispiel: ? super Integer. * Beispiel: ? super Integer.
* Integer wird zurückgegeben. * Integer wird zurückgegeben.
*/ */
public RefTypeOrTPHOrWildcardOrGeneric get_SuperType() public RefTypeOrTPHOrWildcardOrGeneric getInnerType()
{ {
return this.innerType; return this.innerType;
} }

View File

@ -1,6 +1,5 @@
package de.dhbwstuttgart.syntaxtree.visual; package de.dhbwstuttgart.syntaxtree.visual;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal;
import de.dhbwstuttgart.syntaxtree.*; import de.dhbwstuttgart.syntaxtree.*;
import de.dhbwstuttgart.syntaxtree.statement.*; import de.dhbwstuttgart.syntaxtree.statement.*;
@ -171,7 +170,7 @@ public class OutputGenerator implements ASTVisitor{
@Override @Override
public void visit(GenericRefType genericRefType) { public void visit(GenericRefType genericRefType) {
out.append(genericRefType.getName().toString()); out.append(genericRefType.getParsedName().toString());
} }
@Override @Override

View File

@ -126,7 +126,7 @@ class TypeToInsertString implements ResultSetVisitor{
@Override @Override
public void visit(GenericRefType genericRefType) { public void visit(GenericRefType genericRefType) {
insert += genericRefType.getName(); insert += genericRefType.getParsedName();
} }
@Override @Override

View File

@ -1,8 +1,6 @@
package de.dhbwstuttgart.typeinference.assumptions; package de.dhbwstuttgart.typeinference.assumptions;
import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.GenericContext;
import de.dhbwstuttgart.parser.scope.GenericTypeName;
import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.GenericDeclarationList; import de.dhbwstuttgart.syntaxtree.GenericDeclarationList;
@ -29,8 +27,7 @@ public class FunNClass extends ClassOrInterface {
private static GenericDeclarationList createGenerics(List<RefTypeOrTPHOrWildcardOrGeneric> funNParams) { private static GenericDeclarationList createGenerics(List<RefTypeOrTPHOrWildcardOrGeneric> funNParams) {
List<GenericTypeVar> generics = new ArrayList<>(); List<GenericTypeVar> generics = new ArrayList<>();
for(RefTypeOrTPHOrWildcardOrGeneric param : funNParams){ for(RefTypeOrTPHOrWildcardOrGeneric param : funNParams){
generics.add(new GenericTypeVar(new GenericTypeName(new GenericContext( generics.add(new GenericTypeVar(NameGenerator.makeNewName(),
new JavaClassName("Fun"+(funNParams.size()-1)), null), NameGenerator.makeNewName()),
new ArrayList<>(), new NullToken(), new NullToken())); new ArrayList<>(), new NullToken(), new NullToken()));
} }
return new GenericDeclarationList(generics, new NullToken()); return new GenericDeclarationList(generics, new NullToken());

View File

@ -3,9 +3,7 @@ package de.dhbwstuttgart.typeinference.assumptions;
import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.GenericTypeVar; import de.dhbwstuttgart.syntaxtree.GenericTypeVar;
import de.dhbwstuttgart.syntaxtree.ParameterList;
import de.dhbwstuttgart.syntaxtree.TypeScope; import de.dhbwstuttgart.syntaxtree.TypeScope;
import de.dhbwstuttgart.syntaxtree.statement.Assign;
import de.dhbwstuttgart.syntaxtree.type.FunN; import de.dhbwstuttgart.syntaxtree.type.FunN;
import de.dhbwstuttgart.syntaxtree.type.GenericRefType; import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefType;
@ -14,7 +12,6 @@ import de.dhbwstuttgart.typeinference.constraints.GenericsResolver;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
public class MethodAssumption extends Assumption{ public class MethodAssumption extends Assumption{
private ClassOrInterface receiver; private ClassOrInterface receiver;

View File

@ -4,7 +4,6 @@ import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.exceptions.TypeinferenceException; import de.dhbwstuttgart.exceptions.TypeinferenceException;
import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal;
import de.dhbwstuttgart.parser.antlr.Java8Parser;
import de.dhbwstuttgart.syntaxtree.*; import de.dhbwstuttgart.syntaxtree.*;
import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
import de.dhbwstuttgart.syntaxtree.statement.*; import de.dhbwstuttgart.syntaxtree.statement.*;
@ -206,6 +205,7 @@ public class TYPEStmt implements StatementVisitor{
@Override @Override
public void visit(BinaryExpr binary) { public void visit(BinaryExpr binary) {
if(binary.operation.equals(BinaryExpr.Operator.DIV) || if(binary.operation.equals(BinaryExpr.Operator.DIV) ||
binary.operation.equals(BinaryExpr.Operator.MUL)|| binary.operation.equals(BinaryExpr.Operator.MUL)||
binary.operation.equals(BinaryExpr.Operator.MOD)|| binary.operation.equals(BinaryExpr.Operator.MOD)||
@ -217,9 +217,12 @@ public class TYPEStmt implements StatementVisitor{
//Expression muss zu Numeric Convertierbar sein. also von Numeric erben //Expression muss zu Numeric Convertierbar sein. also von Numeric erben
numeric.add(new Pair(binary.lexpr.getType(), number, PairOperator.SMALLERDOT)); numeric.add(new Pair(binary.lexpr.getType(), number, PairOperator.SMALLERDOT));
numeric.add(new Pair(binary.rexpr.getType(), number, PairOperator.SMALLERDOT)); numeric.add(new Pair(binary.rexpr.getType(), number, PairOperator.SMALLERDOT));
//The type of a multiplicative expression is the promoted type of its operands. /*
numeric.add(new Pair(binary.rexpr.getType(), binary.getType(), PairOperator.SMALLERDOT)); In Java passiert bei den binären Operatoren eine sogenannte Type Promotion:
numeric.add(new Pair(binary.lexpr.getType(), binary.getType(), PairOperator.SMALLERDOT)); https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.6.2
Das bedeutet, dass Java die Typen je nach belieben castet, so lange sie nur von Number erben
*/
numeric.add(new Pair(binary.getType(), number, PairOperator.SMALLERDOT));
numericAdditionOrStringConcatenation.add(numeric); numericAdditionOrStringConcatenation.add(numeric);
if(binary.operation.equals(BinaryExpr.Operator.ADD)) { if(binary.operation.equals(BinaryExpr.Operator.ADD)) {
//Dann kann der Ausdruck auch das aneinanderfügen zweier Strings sein: ("a" + "b") oder (1 + 2) //Dann kann der Ausdruck auch das aneinanderfügen zweier Strings sein: ("a" + "b") oder (1 + 2)
@ -339,7 +342,7 @@ public class TYPEStmt implements StatementVisitor{
List<RefTypeOrTPHOrWildcardOrGeneric> params = new ArrayList<>(); List<RefTypeOrTPHOrWildcardOrGeneric> params = new ArrayList<>();
for(GenericTypeVar gtv : receiverCl.getGenerics()){ for(GenericTypeVar gtv : receiverCl.getGenerics()){
//Die Generics werden alle zu TPHs umgewandelt. //Die Generics werden alle zu TPHs umgewandelt.
params.add(resolver.resolve(gtv.getParsedName())); params.add(resolver.resolve(gtv.getName()));
} }
RefTypeOrTPHOrWildcardOrGeneric receiverType = new RefType(assumption.getReceiver().getClassName(), params, forMethod.getOffset()); RefTypeOrTPHOrWildcardOrGeneric receiverType = new RefType(assumption.getReceiver().getClassName(), params, forMethod.getOffset());

View File

@ -20,7 +20,6 @@ import java.io.*;
import java.util.*; import java.util.*;
public class ClingoTest { public class ClingoTest {
public static final String rootDirectory = "~/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards";
public static final String tempDirectory = "/tmp/"; public static final String tempDirectory = "/tmp/";
private final TypePlaceholder testType = TypePlaceholder.fresh(new NullToken()); private final TypePlaceholder testType = TypePlaceholder.fresh(new NullToken());
@Test @Test

View File

@ -1,29 +1,98 @@
package asp; package asp;
import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.sat.asp.Clingo;
import de.dhbwstuttgart.sat.asp.parser.ASPParser;
import de.dhbwstuttgart.sat.asp.writer.ASPGenerator;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.syntaxtree.type.*;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.constraints.Pair; import de.dhbwstuttgart.typeinference.constraints.Pair;
import de.dhbwstuttgart.typeinference.result.ResultSet;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator; import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
import org.junit.Test;
import java.util.Collection; import java.io.File;
import java.util.HashSet; import java.io.IOException;
import java.util.Set; import java.io.PrintWriter;
import java.sql.Ref;
import java.util.*;
public class UnifyWithoutWildcards { public class UnifyWithoutWildcards {
public Collection<ClassOrInterface> getFC() { public static final String tempDirectory = "/tmp/";
Set<ClassOrInterface> ret = new HashSet<>();
ret.add(ASTFactory.createObjectClass()); @Test
ret.add(ASTFactory.createClass(java.util.List.class)); public void adapt() throws InterruptedException, IOException, ClassNotFoundException {
ConstraintSet<Pair> testSet = new ConstraintSet<>();
List<RefTypeOrTPHOrWildcardOrGeneric> list = Arrays.asList(TypePlaceholder.fresh(new NullToken()));
RefType t1 = new RefType(new JavaClassName("Matrix"), new NullToken());
RefType t2 = new RefType(new JavaClassName("Vector"), list, new NullToken());
testSet.addUndConstraint(new Pair(t1, t2, PairOperator.SMALLERDOT));
run(testSet);
}
public ResultSet run(ConstraintSet<Pair> toTest) throws IOException, InterruptedException, ClassNotFoundException {
String content = "";
content = new ASPGenerator(toTest, this.getFC()).getASP();
PrintWriter writer = new PrintWriter(tempDirectory + "test.lp", "UTF-8");
writer.println(content);
writer.close();
Clingo clingo = new Clingo(Arrays.asList(new File(tempDirectory + "test.lp")));
String result = clingo.runClingo();
ResultSet resultSet = ASPParser.parse(result, getInvolvedTPHS(toTest));
return resultSet;
}
private static class TPHExtractor implements TypeVisitor<List<TypePlaceholder>>{
@Override
public List<TypePlaceholder> visit(RefType refType) {
ArrayList<TypePlaceholder> ret = new ArrayList<>();
for(RefTypeOrTPHOrWildcardOrGeneric param : refType.getParaList()){
ret.addAll(param.acceptTV(this));
}
return ret;
}
@Override
public List<TypePlaceholder> visit(SuperWildcardType superWildcardType) {
return superWildcardType.getInnerType().acceptTV(this);
}
@Override
public List<TypePlaceholder> visit(TypePlaceholder typePlaceholder) {
return Arrays.asList(typePlaceholder);
}
@Override
public List<TypePlaceholder> visit(ExtendsWildcardType extendsWildcardType) {
return extendsWildcardType.getInnerType().acceptTV(this);
}
@Override
public List<TypePlaceholder> visit(GenericRefType genericRefType) {
return new ArrayList<>();
}
}
private Collection<TypePlaceholder> getInvolvedTPHS(ConstraintSet<Pair> toTest) {
List<TypePlaceholder> ret = new ArrayList<>();
toTest.map((Pair p)-> {
ret.addAll(p.TA1.acceptTV(new TPHExtractor()));
ret.addAll(p.TA2.acceptTV(new TPHExtractor()));
return p;
});
return ret; return ret;
} }
public ConstraintSet<Pair> getPairs() { private Collection<ClassOrInterface> getFC() {
ConstraintSet<Pair> ret = new ConstraintSet<>(); Set<ClassOrInterface> ret = new HashSet<>();
ret.addUndConstraint(new Pair(TypePlaceholder.fresh(new NullToken()), ASTFactory.createObjectType(), PairOperator.SMALLERDOT)); ret.add(ASTFactory.createClass(Matrix.class));
//ret.add(ASTFactory.createObjectClass());
//ret.add(ASTFactory.createClass(java.util.List.class));
return ret; return ret;
} }
private class Matrix<A> extends HashMap<A,Map<Integer, A>>{}
} }

View File

@ -0,0 +1,9 @@
import java.lang.Integer;
import java.lang.Long;
public class AddLong{
add(Integer a, Long b) {
Long c = a+b;
return c;
}
}

View File

@ -3,10 +3,6 @@ import java.util.Vector;
class MyVector{ class MyVector{
id(x){ id(x){
return id2(x); return (x.elementAt(0));
}
id2(x){
return id(x);
} }
} }

View File

@ -72,6 +72,10 @@ public class JavaTXCompilerTest {
public void expressions() throws IOException, ClassNotFoundException { public void expressions() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"Expressions.jav")); execute(new File(rootDirectory+"Expressions.jav"));
} }
@Test
public void addLong() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"AddLong.jav"));
}
private static class TestResultSet{ private static class TestResultSet{