Generics anfügen

This commit is contained in:
JanUlrich 2017-03-29 17:28:29 +02:00
parent 56e4192df4
commit 4ecf526b14
26 changed files with 209 additions and 75 deletions

View File

@ -39,6 +39,8 @@ public class JavaTXCompiler {
for(Constraint<UnifyPair> constraint : xCons){
xConsSet.addAll(constraint);
}
System.out.println(xConsSet);
Set<Set<UnifyPair>> result = unify.unify(xConsSet, finiteClosure);
System.out.println(result);
}

View File

@ -0,0 +1,14 @@
package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
import de.dhbwstuttgart.typecheck.JavaClassName;
public class GenericContext {
public final String parentMethod;
public final JavaClassName parentClass;
public GenericContext(JavaClassName parentClass, String parentMethod) {
if(parentMethod == null)parentMethod = "";
this.parentClass = parentClass;
this.parentMethod = parentMethod;
}
}

View File

@ -1,7 +1,7 @@
package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
import java.util.HashSet;
import java.util.HashMap;
public class GenericsRegistry extends HashSet<String> {
public class GenericsRegistry extends HashMap<String, GenericContext> {
}

View File

@ -10,6 +10,7 @@ import de.dhbwstuttgart.syntaxtree.statement.literal.*;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typecheck.JavaClassName;
import de.dhbwstuttgart.typecheck.JavaClassRegistry;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.TerminalNode;
@ -33,12 +34,12 @@ public class StatementGenerator {
}
public Method convert(Java8Parser.MethodDeclarationContext methodDeclarationContext) {
public Method convert(Java8Parser.MethodDeclarationContext methodDeclarationContext, JavaClassName parentClass) {
Java8Parser.MethodHeaderContext header = methodDeclarationContext.methodHeader();
String name = header.methodDeclarator().Identifier().getText();
GenericDeclarationList gtvDeclarations = new GenericDeclarationList(new ArrayList<>(), methodDeclarationContext.methodHeader().getStart());
if(methodDeclarationContext.methodHeader().typeParameters() != null){
gtvDeclarations = TypeGenerator.convert(methodDeclarationContext.methodHeader().typeParameters(), reg, generics);
gtvDeclarations = TypeGenerator.convert(methodDeclarationContext.methodHeader().typeParameters(), parentClass, name,reg, generics);
}
RefTypeOrTPHOrWildcardOrGeneric retType;
if(header.result() != null){

View File

@ -0,0 +1,16 @@
package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
import de.dhbwstuttgart.syntaxtree.statement.Return;
import de.dhbwstuttgart.syntaxtree.statement.Statement;
import java.util.List;
public class SyntacticSugar {
public List<Statement> addTrailingReturn(List<Statement> statements){
Statement lastStmt = statements.get(statements.size()-1);
if(lastStmt instanceof Return)return statements;
return statements;
}
}

View File

@ -200,10 +200,9 @@ public class SyntaxTreeGenerator{
}
}
JavaClassName name = reg.getName(ctx.Identifier().getText());
GenericDeclarationList genericClassParameters = TypeGenerator.convert(ctx.typeParameters(), reg, generics);
Block class_block = null;
GenericDeclarationList genericClassParameters = TypeGenerator.convert(ctx.typeParameters(), name, "",reg, generics);
List<Field> fielddecl = convertFields(ctx.classBody());
List<Method> methods = convertMethods(ctx.classBody());
List<Method> methods = convertMethods(ctx.classBody(), name);
Token offset = ctx.getStart();
RefType superClass ;
@ -221,7 +220,7 @@ public class SyntaxTreeGenerator{
throw new NotImplementedException();
}
private List<Method> convertMethods(Java8Parser.ClassBodyContext classBodyContext) {
private List<Method> convertMethods(Java8Parser.ClassBodyContext classBodyContext, JavaClassName parentClass) {
List<Method> ret = new ArrayList<>();
for(Java8Parser.ClassBodyDeclarationContext classMember : classBodyContext.classBodyDeclaration()){
if(classMember.classMemberDeclaration() != null){
@ -230,7 +229,7 @@ public class SyntaxTreeGenerator{
//Do nothing!
}else if(classMemberDeclarationContext.methodDeclaration()!= null){
StatementGenerator stmtGen = new StatementGenerator(reg, generics);
ret.add(stmtGen.convert(classMemberDeclarationContext.methodDeclaration()));
ret.add(stmtGen.convert(classMemberDeclarationContext.methodDeclaration(), parentClass));
}
}
}

View File

@ -25,8 +25,8 @@ public class TypeGenerator {
public static RefTypeOrTPHOrWildcardOrGeneric convert(Java8Parser.UnannClassOrInterfaceTypeContext unannClassOrInterfaceTypeContext, JavaClassRegistry reg, GenericsRegistry generics) {
String name = unannClassOrInterfaceTypeContext.getText();
if(!reg.contains(name)){ //Dann könnte es ein Generische Type sein:
if(generics.contains(name)){
return new GenericRefType(new GenericTypeName(name), unannClassOrInterfaceTypeContext.getStart());
if(generics.keySet().contains(name)){
return new GenericRefType(new GenericTypeName(generics.get(name),name), unannClassOrInterfaceTypeContext.getStart());
}else{
throw new TypeinferenceException("Der Typ "+ name + " ist nicht vorhanden",unannClassOrInterfaceTypeContext.getStart());
}
@ -48,25 +48,25 @@ public class TypeGenerator {
return TypeGenerator.convert(unannTypeContext.unannReferenceType().unannClassOrInterfaceType(), reg, genericsRegistry);
}
public static GenericDeclarationList convert(Java8Parser.TypeParametersContext typeParametersContext, JavaClassRegistry reg, GenericsRegistry generics) {
public static GenericDeclarationList convert(Java8Parser.TypeParametersContext typeParametersContext, JavaClassName parentClass, String parentMethod, JavaClassRegistry reg, GenericsRegistry generics) {
if(typeParametersContext == null){
return new GenericDeclarationList(new ArrayList<>(), new NullToken());
}
Token endOffset = typeParametersContext.getStop();
List<GenericTypeVar> typeVars = new ArrayList<>();
for(Java8Parser.TypeParameterContext typeParameter : typeParametersContext.typeParameterList().typeParameter()){
typeVars.add(convert(typeParameter, reg, generics));
typeVars.add(convert(typeParameter, parentClass, parentMethod, reg, generics));
endOffset = typeParameter.getStop();
}
return new GenericDeclarationList(typeVars, endOffset);
}
public static GenericTypeVar convert(Java8Parser.TypeParameterContext typeParameter, JavaClassRegistry reg, GenericsRegistry generics) {
public static GenericTypeVar convert(Java8Parser.TypeParameterContext typeParameter, JavaClassName parentClass, String parentMethod, JavaClassRegistry reg, GenericsRegistry generics) {
String name = typeParameter.Identifier().getText();
List<RefTypeOrTPHOrWildcardOrGeneric> bounds = TypeGenerator.convert(typeParameter.typeBound(),reg, generics);
GenericTypeVar ret = new GenericTypeVar(name, bounds, typeParameter.getStart(), typeParameter.getStop());
generics.add(name);
GenericTypeVar ret = new GenericTypeVar(new GenericTypeName(new GenericContext(parentClass, parentMethod), name), bounds, typeParameter.getStart(), typeParameter.getStop());
generics.put(name, new GenericContext(parentClass, parentMethod));
return ret;
}

View File

@ -81,4 +81,8 @@ public class ClassOrInterface extends GTVDeclarationContext implements IItemWith
public RefTypeOrTPHOrWildcardOrGeneric getSuperClass() {
return superClass;
}
public Iterable<? extends GenericTypeVar> getGenerics() {
return this.genericClassParameters;
}
}

View File

@ -24,6 +24,6 @@ public class GenericDeclarationList extends SyntaxTreeNode implements Iterable<G
@Override
public Iterator<GenericTypeVar> iterator() {
return null;
return gtvs.iterator();
}
}

View File

@ -1,6 +1,7 @@
package de.dhbwstuttgart.syntaxtree;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.typecheck.GenericTypeName;
import org.antlr.v4.runtime.Token;
import java.util.ArrayList;
@ -23,9 +24,9 @@ public class GenericTypeVar extends SyntaxTreeNode
*/
List<? extends RefTypeOrTPHOrWildcardOrGeneric> bounds=new ArrayList<RefTypeOrTPHOrWildcardOrGeneric>();
private Token endOffset;
private String name;
private GenericTypeName name;
public GenericTypeVar(String s, List<? extends RefTypeOrTPHOrWildcardOrGeneric> bounds, Token offset, Token endOffset)
public GenericTypeVar(GenericTypeName s, List<? extends RefTypeOrTPHOrWildcardOrGeneric> bounds, Token offset, Token endOffset)
{
super(offset);
name = s;
@ -47,4 +48,7 @@ public class GenericTypeVar extends SyntaxTreeNode
return "BoGTV " + this.name;
}
public GenericTypeName getName() {
return name;
}
}

View File

@ -5,8 +5,11 @@ import java.util.ArrayList;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintsFactory;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import org.antlr.v4.runtime.Token;
import de.dhbwstuttgart.core.IItemWithOffset;
@ -25,21 +28,32 @@ public class Method extends Field implements IItemWithOffset
private Block block;
private ParameterList parameterlist = new ParameterList(new ArrayList<>(), new NullToken());
private ExceptionList exceptionlist;
private GenericDeclarationList generics;
public Method(String name, RefTypeOrTPHOrWildcardOrGeneric returnType, int modifiers, ParameterList parameterList, Block block,
GenericDeclarationList gtvDeclarations, Token offset) {
super(name, returnType, modifiers, offset);
this.parameterlist = parameterList;
this.block = block;
this.generics = gtvDeclarations;
}
public ConstraintSet getConstraints(TypeInferenceInformation info, ClassOrInterface currentClass) {
ConstraintSet ret = new ConstraintSet();
ret.addAll(block.getConstraints(new TypeInferenceBlockInformation(info.getAvailableClasses(), currentClass, this)));
TypeInferenceBlockInformation blockInfo = new TypeInferenceBlockInformation(info.getAvailableClasses(), currentClass, this);
ret.addAll(block.getConstraints(blockInfo));
/*
ret.addUndConstraint(
ConstraintsFactory.createPair(block.getType(), this.getType(), blockInfo));
*/
return ret;
}
public ParameterList getParameterList() {
return parameterlist;
}
public Iterable<? extends GenericTypeVar> getGenerics() {
return generics;
}
}

View File

@ -6,10 +6,10 @@ import java.util.ArrayList;
import java.util.List;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.GenericContext;
import de.dhbwstuttgart.syntaxtree.Field;
import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.*;
import de.dhbwstuttgart.syntaxtree.type.Void;
import de.dhbwstuttgart.typecheck.GenericTypeName;
import de.dhbwstuttgart.typecheck.JavaClassName;
@ -17,7 +17,6 @@ import de.dhbwstuttgart.typecheck.JavaClassRegistry;
import de.dhbwstuttgart.syntaxtree.*;
import de.dhbwstuttgart.syntaxtree.statement.Block;
import de.dhbwstuttgart.syntaxtree.statement.Statement;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import org.antlr.v4.runtime.Token;
/**
@ -48,13 +47,13 @@ public class ASTFactory {
java.lang.Class superjreClass = jreClass.getSuperclass();
RefType superClass;
if(superjreClass != null){
superClass = (RefType) createType(superjreClass);
superClass = (RefType) createType(superjreClass, name, "");
}else{//Jede Klasse und jedes Interface erbt von Object: (auch Object selbst!)
superClass = (RefType) createType(java.lang.Object.class);
superClass = (RefType) createType(java.lang.Object.class, name, "");
}
List<RefType> implementedInterfaces = new ArrayList<>();
for(java.lang.Class jreInterface : jreClass.getInterfaces()){
implementedInterfaces.add((RefType) createType(jreInterface));
implementedInterfaces.add((RefType) createType(jreInterface, name, ""));
}
GenericDeclarationList genericDeclarationList = createGenerics(jreClass.getTypeParameters(), jreClass, null);
@ -64,11 +63,11 @@ public class ASTFactory {
private de.dhbwstuttgart.syntaxtree.Constructor createConstructor(Constructor constructor, Class inClass) {
String name = constructor.getName();
RefTypeOrTPHOrWildcardOrGeneric returnType = createType(inClass);
RefTypeOrTPHOrWildcardOrGeneric returnType = createType(inClass, names.getName(inClass.getName()), name);
Parameter[] jreParams = constructor.getParameters();
List<FormalParameter> params = new ArrayList<>();
for(Parameter jreParam : jreParams){
RefTypeOrTPHOrWildcardOrGeneric paramType = createType(jreParam.getType());
RefTypeOrTPHOrWildcardOrGeneric paramType = createType(jreParam.getType(),names.getName(inClass.getName()), name);
params.add(new FormalParameter(jreParam.getName(),paramType, new NullToken()));
}
ParameterList parameterList = new ParameterList(params, new NullToken());
@ -83,11 +82,11 @@ public class ASTFactory {
public Method createMethod(java.lang.reflect.Method jreMethod, java.lang.Class inClass){
String name = jreMethod.getName();
RefTypeOrTPHOrWildcardOrGeneric returnType;
returnType = createType(jreMethod.getReturnType());
returnType = createType(jreMethod.getReturnType(),names.getName(inClass.getName()), name);
Parameter[] jreParams = jreMethod.getParameters();
List<FormalParameter> params = new ArrayList<>();
for(Parameter jreParam : jreParams){
RefTypeOrTPHOrWildcardOrGeneric paramType = createType(jreParam.getType());
RefTypeOrTPHOrWildcardOrGeneric paramType = createType(jreParam.getType(),names.getName(inClass.getName()), name);
params.add(new FormalParameter(jreParam.getName(),paramType, new NullToken()));
}
ParameterList parameterList = new ParameterList(params, new NullToken());
@ -102,7 +101,7 @@ public class ASTFactory {
public GenericDeclarationList createGenerics(TypeVariable[] typeParameters, Class context, String methodName){
List<de.dhbwstuttgart.syntaxtree.GenericTypeVar> gtvs = new ArrayList<>();
for(TypeVariable jreTV : typeParameters){
de.dhbwstuttgart.syntaxtree.GenericTypeVar gtv = createGeneric(jreTV, jreTV.getName());
de.dhbwstuttgart.syntaxtree.GenericTypeVar gtv = createGeneric(jreTV, jreTV.getName(), context, methodName);
gtvs.add(gtv);
}
return new GenericDeclarationList(gtvs,new NullToken());
@ -119,7 +118,7 @@ public class ASTFactory {
}
*/
public RefTypeOrTPHOrWildcardOrGeneric createType(java.lang.reflect.Type type){
public RefTypeOrTPHOrWildcardOrGeneric createType(java.lang.reflect.Type type, JavaClassName parentClass, String parentMethod){
if(type.getTypeName().equals("void")){
return new Void(new NullToken());
}else if(type.getTypeName().equals("int")){
@ -139,12 +138,14 @@ public class ASTFactory {
}else{
if(type instanceof TypeVariable){
//GTVDeclarationContext via "(TypeVariable) type).getGenericDeclaration()"
return new GenericRefType(new GenericTypeName(type.getTypeName()), new NullToken());
return new GenericRefType(
new GenericTypeName(new GenericContext(parentClass, parentMethod),type.getTypeName()),
new NullToken());
}
List<RefTypeOrTPHOrWildcardOrGeneric> params = new ArrayList<>();
if(type instanceof ParameterizedType){
for(Type t : ((ParameterizedType)type).getActualTypeArguments()){
params.add(createType(t));
params.add(createType(t, parentClass, parentMethod));
}
}
RefType ret = new RefType(this.names.getName(type.getTypeName()), params, new NullToken());
@ -152,15 +153,18 @@ public class ASTFactory {
}
}
public de.dhbwstuttgart.syntaxtree.GenericTypeVar createGeneric(TypeVariable jreTypeVar, String name){
public de.dhbwstuttgart.syntaxtree.GenericTypeVar createGeneric(TypeVariable jreTypeVar, String jreTVName, Class context, String parentMethod){
JavaClassName parentClass = names.getName(context.getName());
List<RefType> genericBounds = new ArrayList<>();
java.lang.reflect.Type[] bounds = jreTypeVar.getBounds();
if(bounds.length > 0){
for(java.lang.reflect.Type bound : bounds){
genericBounds.add((RefType) createType(bound));
genericBounds.add((RefType) createType(bound, parentClass, parentMethod));
}
}
return new de.dhbwstuttgart.syntaxtree.GenericTypeVar(name, genericBounds, new NullToken(), new NullToken());
return new de.dhbwstuttgart.syntaxtree.GenericTypeVar(
new GenericTypeName(new GenericContext(parentClass, parentMethod), jreTVName)
, genericBounds, new NullToken(), new NullToken());
}
public ClassOrInterface createObjectClass() {

View File

@ -3,8 +3,8 @@ package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintsFactory;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
import org.antlr.v4.runtime.Token;
@ -23,7 +23,8 @@ public class Assign extends Statement
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
ConstraintSet ret = lefSide.getConstraints(info);
ret.addAll(rightSide.getConstraints(info));
ret.addUndConstraint(new Pair(rightSide.getType(), lefSide.getType()));
ret.addUndConstraint(ConstraintsFactory.createPair(
rightSide.getType(), lefSide.getType(), PairOperator.SMALLERDOT, info));
return ret;
}
}

View File

@ -31,8 +31,6 @@ public class Block extends Statement
}
return ret;
}
}

View File

@ -6,9 +6,9 @@ import de.dhbwstuttgart.typeinference.assumptions.FieldAssumption;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import de.dhbwstuttgart.typeinference.constraints.ConstraintsFactory;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
import org.antlr.v4.runtime.Token;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import java.util.HashSet;
import java.util.Set;
@ -31,8 +31,10 @@ public class FieldVar extends Expression {
ret.addAll(receiver.getConstraints(info));
for(FieldAssumption fieldAssumption : info.getFields(fieldVarName)){
Constraint constraint = new Constraint();
constraint.add(new Pair(receiver.getType(),fieldAssumption.getReceiverType()));
constraint.add(new Pair(this.getType(),fieldAssumption.getType()));
constraint.add(ConstraintsFactory.createPair(
receiver.getType(),fieldAssumption.getReceiverType(), info));
constraint.add(ConstraintsFactory.createPair(
this.getType(),fieldAssumption.getType(), info));
oderConstraints.add(constraint);
}
if(oderConstraints.size() == 0)

View File

@ -4,8 +4,9 @@ import de.dhbwstuttgart.exceptions.TypeinferenceException;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import de.dhbwstuttgart.typeinference.constraints.ConstraintsFactory;
import de.dhbwstuttgart.typeinference.assumptions.MethodAssumption;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
import org.antlr.v4.runtime.Token;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
@ -38,11 +39,11 @@ public class MethodCall extends Statement
Set<Constraint> methodConstraints = new HashSet<>();
for(MethodAssumption m : info.getMethods(name, arglist)){
Constraint methodConstraint = new Constraint();
methodConstraint.add(new Pair(receiver.getType(), m.getReceiverType()));
methodConstraint.add(new Pair(m.getReturnType(), this.getType()));
methodConstraint.add(ConstraintsFactory.createPair(receiver.getType(), m.getReceiverType(), PairOperator.SMALLERDOT, info));
methodConstraint.add(ConstraintsFactory.createPair(m.getReturnType(), this.getType(), PairOperator.SMALLERDOT, info));
for(int i = 0;i<arglist.getArguments().size();i++){
methodConstraint.add(new Pair(m.getArgTypes().get(i),
arglist.getArguments().get(i).getType()));
methodConstraint.add(ConstraintsFactory.createPair(m.getArgTypes().get(i),
arglist.getArguments().get(i).getType(), PairOperator.SMALLERDOT, info));
}
methodConstraints.add(methodConstraint);
}

View File

@ -3,6 +3,7 @@ package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintsFactory;
import org.antlr.v4.runtime.Token;
@ -19,6 +20,9 @@ public class Return extends Statement
@Override
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
return retexpr.getConstraints(info);
ConstraintSet ret = retexpr.getConstraints(info);
ret.addUndConstraint(ConstraintsFactory.createPair(
this.getType(),info.getCurrentMethod().getType(), info));
return ret;
}
}

View File

@ -6,6 +6,7 @@ import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintsFactory;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
import org.antlr.v4.runtime.Token;
@ -24,7 +25,7 @@ public class This extends Expression
@Override
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
ConstraintSet ret = new ConstraintSet();
ret.addUndConstraint(new Pair( this.getType(), info.getCurrentClass().getType(), PairOperator.EQUALSDOT));
ret.addUndConstraint(ConstraintsFactory.createPair( this.getType(), info.getCurrentClass().getType(), PairOperator.EQUALSDOT, info));
return ret;
}
}

View File

@ -12,5 +12,10 @@ public class GenericRefType extends RefTypeOrTPHOrWildcardOrGeneric
super(offset);
this.name = name;
}
public GenericTypeName getName(){
return name;
}
}

View File

@ -32,6 +32,11 @@ public class RefType extends RefTypeOrTPHOrWildcardOrGeneric
this.name = (fullyQualifiedName);
}
@Override
public String toString(){
return this.name.toString();
}
@Override
public int hashCode() {
int hash = 0;

View File

@ -1,16 +1,10 @@
package de.dhbwstuttgart.syntaxtree.type;
import java.util.ArrayList;
import java.util.List;
import de.dhbwstuttgart.typecheck.JavaClassName;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import org.antlr.v4.runtime.Token;
public abstract class RefTypeOrTPHOrWildcardOrGeneric extends SyntaxTreeNode
{
public class RefTypeOrTPHOrWildcardOrGeneric extends SyntaxTreeNode{
public RefTypeOrTPHOrWildcardOrGeneric(Token offset) {
super(offset);
}
}

View File

@ -1,9 +1,22 @@
package de.dhbwstuttgart.typecheck;
import de.dhbwstuttgart.syntaxtree.GTVDeclarationContext;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.GenericContext;
public class GenericTypeName extends JavaClassName {
public GenericTypeName(String name) {
private final static String DELIMITER = "%";
private final JavaClassName parentClass;
private final String methodName;
public GenericTypeName(GenericContext parentClass, String name) {
super(name);
this.parentClass = parentClass.parentClass;
this.methodName = parentClass.parentMethod;
}
public String toString(){
return parentClass.toString()
+ DELIMITER + methodName
+ DELIMITER + super.toString();
}
}

View File

@ -1,5 +1,6 @@
package de.dhbwstuttgart.typeinference.assumptions;
import com.sun.istack.internal.NotNull;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.FormalParameter;
import de.dhbwstuttgart.syntaxtree.Method;
@ -10,7 +11,7 @@ public class TypeInferenceBlockInformation extends TypeInferenceInformation {
private Method methodContext;
private ClassOrInterface currentClass;
public TypeInferenceBlockInformation(Set<ClassOrInterface> availableClasses, ClassOrInterface currentClass, Method methodContext) {
public TypeInferenceBlockInformation(Set<ClassOrInterface> availableClasses, @NotNull ClassOrInterface currentClass, Method methodContext) {
super(availableClasses);
this.methodContext = methodContext;
this.currentClass = currentClass;
@ -19,4 +20,7 @@ public class TypeInferenceBlockInformation extends TypeInferenceInformation {
public ClassOrInterface getCurrentClass() {
return currentClass;
}
public Method getCurrentMethod() {
return methodContext;
}
}

View File

@ -0,0 +1,47 @@
package de.dhbwstuttgart.typeinference.constraints;
import de.dhbwstuttgart.syntaxtree.GenericTypeVar;
import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ConstraintsFactory {
public static Pair createPair(RefTypeOrTPHOrWildcardOrGeneric t1, RefTypeOrTPHOrWildcardOrGeneric t2, PairOperator equalsdot, TypeInferenceBlockInformation info){
//Check whether Generics are in the same class:
return new Pair(checkGeneric(t1, info), checkGeneric(t2, info));
}
public static Pair createPair(RefTypeOrTPHOrWildcardOrGeneric t1,
RefTypeOrTPHOrWildcardOrGeneric t2, TypeInferenceBlockInformation info){
return createPair(t1,t2,PairOperator.SMALLERDOT, info);
}
private static RefTypeOrTPHOrWildcardOrGeneric checkGeneric(RefTypeOrTPHOrWildcardOrGeneric type, TypeInferenceBlockInformation info){
if(type instanceof GenericRefType){
List<GenericTypeVar> genericsInContext = new ArrayList<>();
for(GenericTypeVar genericTypeVar : info.getCurrentClass().getGenerics()){
genericsInContext.add(genericTypeVar);
}
if(info.getCurrentMethod() != null)for(GenericTypeVar genericTypeVar : info.getCurrentMethod().getGenerics()){
genericsInContext.add(genericTypeVar);
}
for(GenericTypeVar genericTypeVar : genericsInContext){
if(genericTypeVar.getName().equals(((GenericRefType)type).getName())){
return new RefType(((GenericRefType)type).getName(),type.getOffset());
}
}
//Nicht in den Generics in diesem Kontext enthalten:
return TypePlaceholder.fresh(type.getOffset());
}else{
return type;
}
}
}

View File

@ -4,6 +4,7 @@ import java.io.Serializable;
import com.sun.istack.internal.NotNull;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
public class Pair implements Serializable
@ -13,7 +14,7 @@ public class Pair implements Serializable
private PairOperator eOperator = PairOperator.SMALLER;
public Pair(@NotNull RefTypeOrTPHOrWildcardOrGeneric TA1, @NotNull RefTypeOrTPHOrWildcardOrGeneric TA2 )
Pair(@NotNull RefTypeOrTPHOrWildcardOrGeneric TA1, @NotNull RefTypeOrTPHOrWildcardOrGeneric TA2 )
{
this.TA1 = TA1;
this.TA2 = TA2;
@ -22,7 +23,7 @@ public class Pair implements Serializable
eOperator = PairOperator.SMALLER;
}
public Pair(RefTypeOrTPHOrWildcardOrGeneric TA1, RefTypeOrTPHOrWildcardOrGeneric TA2, PairOperator eOp)
Pair(@NotNull RefTypeOrTPHOrWildcardOrGeneric TA1, @NotNull RefTypeOrTPHOrWildcardOrGeneric TA2, PairOperator eOp)
{
// Konstruktor
this(TA1,TA2);

View File

@ -16,7 +16,7 @@ public class JavaTXCompilerTest {
@Test
public void test() throws IOException, ClassNotFoundException {
JavaTXCompiler compiler = new JavaTXCompiler();
compiler.parse(new File(rootDirectory+"Methods.jav"));
//compiler.parse(new File(rootDirectory+"Methods.jav"));
compiler.parse(new File(rootDirectory+"Generics.jav"));
compiler.typeInference();
}