forked from JavaTX/JavaCompilerCore
Generics anfügen
This commit is contained in:
parent
56e4192df4
commit
4ecf526b14
@ -39,6 +39,8 @@ public class JavaTXCompiler {
|
|||||||
for(Constraint<UnifyPair> constraint : xCons){
|
for(Constraint<UnifyPair> constraint : xCons){
|
||||||
xConsSet.addAll(constraint);
|
xConsSet.addAll(constraint);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
System.out.println(xConsSet);
|
||||||
Set<Set<UnifyPair>> result = unify.unify(xConsSet, finiteClosure);
|
Set<Set<UnifyPair>> result = unify.unify(xConsSet, finiteClosure);
|
||||||
System.out.println(result);
|
System.out.println(result);
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -1,7 +1,7 @@
|
|||||||
package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
|
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> {
|
||||||
|
|
||||||
}
|
}
|
@ -10,6 +10,7 @@ import de.dhbwstuttgart.syntaxtree.statement.literal.*;
|
|||||||
import de.dhbwstuttgart.syntaxtree.type.RefType;
|
import de.dhbwstuttgart.syntaxtree.type.RefType;
|
||||||
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
|
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
|
||||||
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
|
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
|
||||||
|
import de.dhbwstuttgart.typecheck.JavaClassName;
|
||||||
import de.dhbwstuttgart.typecheck.JavaClassRegistry;
|
import de.dhbwstuttgart.typecheck.JavaClassRegistry;
|
||||||
import org.antlr.v4.runtime.Token;
|
import org.antlr.v4.runtime.Token;
|
||||||
import org.antlr.v4.runtime.tree.TerminalNode;
|
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();
|
Java8Parser.MethodHeaderContext header = methodDeclarationContext.methodHeader();
|
||||||
String name = header.methodDeclarator().Identifier().getText();
|
String name = header.methodDeclarator().Identifier().getText();
|
||||||
GenericDeclarationList gtvDeclarations = new GenericDeclarationList(new ArrayList<>(), methodDeclarationContext.methodHeader().getStart());
|
GenericDeclarationList gtvDeclarations = new GenericDeclarationList(new ArrayList<>(), methodDeclarationContext.methodHeader().getStart());
|
||||||
if(methodDeclarationContext.methodHeader().typeParameters() != null){
|
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;
|
RefTypeOrTPHOrWildcardOrGeneric retType;
|
||||||
if(header.result() != null){
|
if(header.result() != null){
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -200,10 +200,9 @@ public class SyntaxTreeGenerator{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
JavaClassName name = reg.getName(ctx.Identifier().getText());
|
JavaClassName name = reg.getName(ctx.Identifier().getText());
|
||||||
GenericDeclarationList genericClassParameters = TypeGenerator.convert(ctx.typeParameters(), reg, generics);
|
GenericDeclarationList genericClassParameters = TypeGenerator.convert(ctx.typeParameters(), name, "",reg, generics);
|
||||||
Block class_block = null;
|
|
||||||
List<Field> fielddecl = convertFields(ctx.classBody());
|
List<Field> fielddecl = convertFields(ctx.classBody());
|
||||||
List<Method> methods = convertMethods(ctx.classBody());
|
List<Method> methods = convertMethods(ctx.classBody(), name);
|
||||||
|
|
||||||
Token offset = ctx.getStart();
|
Token offset = ctx.getStart();
|
||||||
RefType superClass ;
|
RefType superClass ;
|
||||||
@ -221,7 +220,7 @@ public class SyntaxTreeGenerator{
|
|||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Method> convertMethods(Java8Parser.ClassBodyContext classBodyContext) {
|
private List<Method> convertMethods(Java8Parser.ClassBodyContext classBodyContext, JavaClassName parentClass) {
|
||||||
List<Method> ret = new ArrayList<>();
|
List<Method> ret = new ArrayList<>();
|
||||||
for(Java8Parser.ClassBodyDeclarationContext classMember : classBodyContext.classBodyDeclaration()){
|
for(Java8Parser.ClassBodyDeclarationContext classMember : classBodyContext.classBodyDeclaration()){
|
||||||
if(classMember.classMemberDeclaration() != null){
|
if(classMember.classMemberDeclaration() != null){
|
||||||
@ -230,7 +229,7 @@ public class SyntaxTreeGenerator{
|
|||||||
//Do nothing!
|
//Do nothing!
|
||||||
}else if(classMemberDeclarationContext.methodDeclaration()!= null){
|
}else if(classMemberDeclarationContext.methodDeclaration()!= null){
|
||||||
StatementGenerator stmtGen = new StatementGenerator(reg, generics);
|
StatementGenerator stmtGen = new StatementGenerator(reg, generics);
|
||||||
ret.add(stmtGen.convert(classMemberDeclarationContext.methodDeclaration()));
|
ret.add(stmtGen.convert(classMemberDeclarationContext.methodDeclaration(), parentClass));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,8 +25,8 @@ public class TypeGenerator {
|
|||||||
public static RefTypeOrTPHOrWildcardOrGeneric convert(Java8Parser.UnannClassOrInterfaceTypeContext unannClassOrInterfaceTypeContext, JavaClassRegistry reg, GenericsRegistry generics) {
|
public static RefTypeOrTPHOrWildcardOrGeneric convert(Java8Parser.UnannClassOrInterfaceTypeContext unannClassOrInterfaceTypeContext, JavaClassRegistry reg, GenericsRegistry generics) {
|
||||||
String name = unannClassOrInterfaceTypeContext.getText();
|
String name = unannClassOrInterfaceTypeContext.getText();
|
||||||
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.keySet().contains(name)){
|
||||||
return new GenericRefType(new GenericTypeName(name), unannClassOrInterfaceTypeContext.getStart());
|
return new GenericRefType(new GenericTypeName(generics.get(name),name), unannClassOrInterfaceTypeContext.getStart());
|
||||||
}else{
|
}else{
|
||||||
throw new TypeinferenceException("Der Typ "+ name + " ist nicht vorhanden",unannClassOrInterfaceTypeContext.getStart());
|
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);
|
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){
|
if(typeParametersContext == null){
|
||||||
return new GenericDeclarationList(new ArrayList<>(), new NullToken());
|
return new GenericDeclarationList(new ArrayList<>(), new NullToken());
|
||||||
}
|
}
|
||||||
Token endOffset = typeParametersContext.getStop();
|
Token endOffset = typeParametersContext.getStop();
|
||||||
List<GenericTypeVar> typeVars = new ArrayList<>();
|
List<GenericTypeVar> typeVars = new ArrayList<>();
|
||||||
for(Java8Parser.TypeParameterContext typeParameter : typeParametersContext.typeParameterList().typeParameter()){
|
for(Java8Parser.TypeParameterContext typeParameter : typeParametersContext.typeParameterList().typeParameter()){
|
||||||
typeVars.add(convert(typeParameter, reg, generics));
|
typeVars.add(convert(typeParameter, parentClass, parentMethod, reg, generics));
|
||||||
endOffset = typeParameter.getStop();
|
endOffset = typeParameter.getStop();
|
||||||
}
|
}
|
||||||
return new GenericDeclarationList(typeVars, endOffset);
|
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();
|
String name = typeParameter.Identifier().getText();
|
||||||
List<RefTypeOrTPHOrWildcardOrGeneric> bounds = TypeGenerator.convert(typeParameter.typeBound(),reg, generics);
|
List<RefTypeOrTPHOrWildcardOrGeneric> bounds = TypeGenerator.convert(typeParameter.typeBound(),reg, generics);
|
||||||
|
|
||||||
GenericTypeVar ret = new GenericTypeVar(name, bounds, typeParameter.getStart(), typeParameter.getStop());
|
GenericTypeVar ret = new GenericTypeVar(new GenericTypeName(new GenericContext(parentClass, parentMethod), name), bounds, typeParameter.getStart(), typeParameter.getStop());
|
||||||
generics.add(name);
|
generics.put(name, new GenericContext(parentClass, parentMethod));
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,4 +81,8 @@ public class ClassOrInterface extends GTVDeclarationContext implements IItemWith
|
|||||||
public RefTypeOrTPHOrWildcardOrGeneric getSuperClass() {
|
public RefTypeOrTPHOrWildcardOrGeneric getSuperClass() {
|
||||||
return superClass;
|
return superClass;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Iterable<? extends GenericTypeVar> getGenerics() {
|
||||||
|
return this.genericClassParameters;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,6 +24,6 @@ public class GenericDeclarationList extends SyntaxTreeNode implements Iterable<G
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Iterator<GenericTypeVar> iterator() {
|
public Iterator<GenericTypeVar> iterator() {
|
||||||
return null;
|
return gtvs.iterator();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package de.dhbwstuttgart.syntaxtree;
|
package de.dhbwstuttgart.syntaxtree;
|
||||||
|
|
||||||
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
|
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
|
||||||
|
import de.dhbwstuttgart.typecheck.GenericTypeName;
|
||||||
import org.antlr.v4.runtime.Token;
|
import org.antlr.v4.runtime.Token;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -23,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 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);
|
super(offset);
|
||||||
name = s;
|
name = s;
|
||||||
@ -47,4 +48,7 @@ public class GenericTypeVar extends SyntaxTreeNode
|
|||||||
return "BoGTV " + this.name;
|
return "BoGTV " + this.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public GenericTypeName getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,8 +5,11 @@ import java.util.ArrayList;
|
|||||||
import de.dhbwstuttgart.parser.NullToken;
|
import de.dhbwstuttgart.parser.NullToken;
|
||||||
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
|
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
|
||||||
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
|
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
|
||||||
|
import de.dhbwstuttgart.typeinference.constraints.Constraint;
|
||||||
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
|
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
|
||||||
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
|
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 org.antlr.v4.runtime.Token;
|
||||||
|
|
||||||
import de.dhbwstuttgart.core.IItemWithOffset;
|
import de.dhbwstuttgart.core.IItemWithOffset;
|
||||||
@ -25,21 +28,32 @@ public class Method extends Field implements IItemWithOffset
|
|||||||
private Block block;
|
private Block block;
|
||||||
private ParameterList parameterlist = new ParameterList(new ArrayList<>(), new NullToken());
|
private ParameterList parameterlist = new ParameterList(new ArrayList<>(), new NullToken());
|
||||||
private ExceptionList exceptionlist;
|
private ExceptionList exceptionlist;
|
||||||
|
private GenericDeclarationList generics;
|
||||||
|
|
||||||
public Method(String name, RefTypeOrTPHOrWildcardOrGeneric returnType, int modifiers, ParameterList parameterList, Block block,
|
public Method(String name, RefTypeOrTPHOrWildcardOrGeneric returnType, int modifiers, ParameterList parameterList, Block block,
|
||||||
GenericDeclarationList gtvDeclarations, Token offset) {
|
GenericDeclarationList gtvDeclarations, Token offset) {
|
||||||
super(name, returnType, modifiers, offset);
|
super(name, returnType, modifiers, offset);
|
||||||
this.parameterlist = parameterList;
|
this.parameterlist = parameterList;
|
||||||
this.block = block;
|
this.block = block;
|
||||||
|
this.generics = gtvDeclarations;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ConstraintSet getConstraints(TypeInferenceInformation info, ClassOrInterface currentClass) {
|
public ConstraintSet getConstraints(TypeInferenceInformation info, ClassOrInterface currentClass) {
|
||||||
ConstraintSet ret = new ConstraintSet();
|
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;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ParameterList getParameterList() {
|
public ParameterList getParameterList() {
|
||||||
return parameterlist;
|
return parameterlist;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Iterable<? extends GenericTypeVar> getGenerics() {
|
||||||
|
return generics;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,10 +6,10 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import de.dhbwstuttgart.parser.NullToken;
|
import de.dhbwstuttgart.parser.NullToken;
|
||||||
|
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.GenericContext;
|
||||||
import de.dhbwstuttgart.syntaxtree.Field;
|
import de.dhbwstuttgart.syntaxtree.Field;
|
||||||
import de.dhbwstuttgart.syntaxtree.Method;
|
import de.dhbwstuttgart.syntaxtree.Method;
|
||||||
import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
|
import de.dhbwstuttgart.syntaxtree.type.*;
|
||||||
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
|
|
||||||
import de.dhbwstuttgart.syntaxtree.type.Void;
|
import de.dhbwstuttgart.syntaxtree.type.Void;
|
||||||
import de.dhbwstuttgart.typecheck.GenericTypeName;
|
import de.dhbwstuttgart.typecheck.GenericTypeName;
|
||||||
import de.dhbwstuttgart.typecheck.JavaClassName;
|
import de.dhbwstuttgart.typecheck.JavaClassName;
|
||||||
@ -17,7 +17,6 @@ import de.dhbwstuttgart.typecheck.JavaClassRegistry;
|
|||||||
import de.dhbwstuttgart.syntaxtree.*;
|
import de.dhbwstuttgart.syntaxtree.*;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.Block;
|
import de.dhbwstuttgart.syntaxtree.statement.Block;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.Statement;
|
import de.dhbwstuttgart.syntaxtree.statement.Statement;
|
||||||
import de.dhbwstuttgart.syntaxtree.type.RefType;
|
|
||||||
import org.antlr.v4.runtime.Token;
|
import org.antlr.v4.runtime.Token;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -48,13 +47,13 @@ public class ASTFactory {
|
|||||||
java.lang.Class superjreClass = jreClass.getSuperclass();
|
java.lang.Class superjreClass = jreClass.getSuperclass();
|
||||||
RefType superClass;
|
RefType superClass;
|
||||||
if(superjreClass != null){
|
if(superjreClass != null){
|
||||||
superClass = (RefType) createType(superjreClass);
|
superClass = (RefType) createType(superjreClass, name, "");
|
||||||
}else{//Jede Klasse und jedes Interface erbt von Object: (auch Object selbst!)
|
}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<>();
|
List<RefType> implementedInterfaces = new ArrayList<>();
|
||||||
for(java.lang.Class jreInterface : jreClass.getInterfaces()){
|
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);
|
GenericDeclarationList genericDeclarationList = createGenerics(jreClass.getTypeParameters(), jreClass, null);
|
||||||
|
|
||||||
@ -64,11 +63,11 @@ public class ASTFactory {
|
|||||||
|
|
||||||
private de.dhbwstuttgart.syntaxtree.Constructor createConstructor(Constructor constructor, Class inClass) {
|
private de.dhbwstuttgart.syntaxtree.Constructor createConstructor(Constructor constructor, Class inClass) {
|
||||||
String name = constructor.getName();
|
String name = constructor.getName();
|
||||||
RefTypeOrTPHOrWildcardOrGeneric returnType = createType(inClass);
|
RefTypeOrTPHOrWildcardOrGeneric returnType = createType(inClass, names.getName(inClass.getName()), name);
|
||||||
Parameter[] jreParams = constructor.getParameters();
|
Parameter[] jreParams = constructor.getParameters();
|
||||||
List<FormalParameter> params = new ArrayList<>();
|
List<FormalParameter> params = new ArrayList<>();
|
||||||
for(Parameter jreParam : jreParams){
|
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()));
|
params.add(new FormalParameter(jreParam.getName(),paramType, new NullToken()));
|
||||||
}
|
}
|
||||||
ParameterList parameterList = new ParameterList(params, 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){
|
public 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());
|
returnType = createType(jreMethod.getReturnType(),names.getName(inClass.getName()), name);
|
||||||
Parameter[] jreParams = jreMethod.getParameters();
|
Parameter[] jreParams = jreMethod.getParameters();
|
||||||
List<FormalParameter> params = new ArrayList<>();
|
List<FormalParameter> params = new ArrayList<>();
|
||||||
for(Parameter jreParam : jreParams){
|
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()));
|
params.add(new FormalParameter(jreParam.getName(),paramType, new NullToken()));
|
||||||
}
|
}
|
||||||
ParameterList parameterList = new ParameterList(params, new NullToken());
|
ParameterList parameterList = new ParameterList(params, new NullToken());
|
||||||
@ -99,14 +98,14 @@ public class ASTFactory {
|
|||||||
return new Method(name,returnType, modifier, parameterList, block, gtvDeclarations, offset);
|
return new Method(name,returnType, modifier, parameterList, block, gtvDeclarations, offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
public GenericDeclarationList createGenerics(TypeVariable[] typeParameters, Class context, String methodName){
|
public GenericDeclarationList createGenerics(TypeVariable[] typeParameters, Class context, String methodName){
|
||||||
List<de.dhbwstuttgart.syntaxtree.GenericTypeVar> gtvs = new ArrayList<>();
|
List<de.dhbwstuttgart.syntaxtree.GenericTypeVar> gtvs = new ArrayList<>();
|
||||||
for(TypeVariable jreTV : typeParameters){
|
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);
|
gtvs.add(gtv);
|
||||||
}
|
}
|
||||||
return new GenericDeclarationList(gtvs,new NullToken());
|
return new GenericDeclarationList(gtvs,new NullToken());
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
public RefType createType(java.lang.Class jreClass){
|
public RefType createType(java.lang.Class jreClass){
|
||||||
@ -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")){
|
if(type.getTypeName().equals("void")){
|
||||||
return new Void(new NullToken());
|
return new Void(new NullToken());
|
||||||
}else if(type.getTypeName().equals("int")){
|
}else if(type.getTypeName().equals("int")){
|
||||||
@ -139,12 +138,14 @@ 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(new GenericTypeName(type.getTypeName()), new NullToken());
|
return new GenericRefType(
|
||||||
|
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){
|
||||||
for(Type t : ((ParameterizedType)type).getActualTypeArguments()){
|
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());
|
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<>();
|
List<RefType> genericBounds = new ArrayList<>();
|
||||||
java.lang.reflect.Type[] bounds = jreTypeVar.getBounds();
|
java.lang.reflect.Type[] bounds = jreTypeVar.getBounds();
|
||||||
if(bounds.length > 0){
|
if(bounds.length > 0){
|
||||||
for(java.lang.reflect.Type bound : bounds){
|
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() {
|
public ClassOrInterface createObjectClass() {
|
||||||
@ -212,4 +216,4 @@ public class ASTFactory {
|
|||||||
return createObjectClass().getType();
|
return createObjectClass().getType();
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
}
|
}
|
@ -3,8 +3,8 @@ package de.dhbwstuttgart.syntaxtree.statement;
|
|||||||
|
|
||||||
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
|
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
|
||||||
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
|
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
|
||||||
import de.dhbwstuttgart.typeinference.constraints.Pair;
|
import de.dhbwstuttgart.typeinference.constraints.ConstraintsFactory;
|
||||||
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
|
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
|
||||||
import org.antlr.v4.runtime.Token;
|
import org.antlr.v4.runtime.Token;
|
||||||
|
|
||||||
|
|
||||||
@ -23,7 +23,8 @@ public class Assign extends Statement
|
|||||||
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
|
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
|
||||||
ConstraintSet ret = lefSide.getConstraints(info);
|
ConstraintSet ret = lefSide.getConstraints(info);
|
||||||
ret.addAll(rightSide.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;
|
return ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,8 +31,6 @@ public class Block extends Statement
|
|||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -6,9 +6,9 @@ import de.dhbwstuttgart.typeinference.assumptions.FieldAssumption;
|
|||||||
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
|
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
|
||||||
import de.dhbwstuttgart.typeinference.constraints.Constraint;
|
import de.dhbwstuttgart.typeinference.constraints.Constraint;
|
||||||
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
|
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 org.antlr.v4.runtime.Token;
|
||||||
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
|
|
||||||
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
@ -31,8 +31,10 @@ public class FieldVar extends Expression {
|
|||||||
ret.addAll(receiver.getConstraints(info));
|
ret.addAll(receiver.getConstraints(info));
|
||||||
for(FieldAssumption fieldAssumption : info.getFields(fieldVarName)){
|
for(FieldAssumption fieldAssumption : info.getFields(fieldVarName)){
|
||||||
Constraint constraint = new Constraint();
|
Constraint constraint = new Constraint();
|
||||||
constraint.add(new Pair(receiver.getType(),fieldAssumption.getReceiverType()));
|
constraint.add(ConstraintsFactory.createPair(
|
||||||
constraint.add(new Pair(this.getType(),fieldAssumption.getType()));
|
receiver.getType(),fieldAssumption.getReceiverType(), info));
|
||||||
|
constraint.add(ConstraintsFactory.createPair(
|
||||||
|
this.getType(),fieldAssumption.getType(), info));
|
||||||
oderConstraints.add(constraint);
|
oderConstraints.add(constraint);
|
||||||
}
|
}
|
||||||
if(oderConstraints.size() == 0)
|
if(oderConstraints.size() == 0)
|
||||||
|
@ -4,8 +4,9 @@ import de.dhbwstuttgart.exceptions.TypeinferenceException;
|
|||||||
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
|
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
|
||||||
import de.dhbwstuttgart.typeinference.constraints.Constraint;
|
import de.dhbwstuttgart.typeinference.constraints.Constraint;
|
||||||
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
|
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.assumptions.MethodAssumption;
|
||||||
|
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
|
||||||
import org.antlr.v4.runtime.Token;
|
import org.antlr.v4.runtime.Token;
|
||||||
|
|
||||||
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
|
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
|
||||||
@ -38,11 +39,11 @@ public class MethodCall extends Statement
|
|||||||
Set<Constraint> methodConstraints = new HashSet<>();
|
Set<Constraint> methodConstraints = new HashSet<>();
|
||||||
for(MethodAssumption m : info.getMethods(name, arglist)){
|
for(MethodAssumption m : info.getMethods(name, arglist)){
|
||||||
Constraint methodConstraint = new Constraint();
|
Constraint methodConstraint = new Constraint();
|
||||||
methodConstraint.add(new Pair(receiver.getType(), m.getReceiverType()));
|
methodConstraint.add(ConstraintsFactory.createPair(receiver.getType(), m.getReceiverType(), PairOperator.SMALLERDOT, info));
|
||||||
methodConstraint.add(new Pair(m.getReturnType(), this.getType()));
|
methodConstraint.add(ConstraintsFactory.createPair(m.getReturnType(), this.getType(), PairOperator.SMALLERDOT, info));
|
||||||
for(int i = 0;i<arglist.getArguments().size();i++){
|
for(int i = 0;i<arglist.getArguments().size();i++){
|
||||||
methodConstraint.add(new Pair(m.getArgTypes().get(i),
|
methodConstraint.add(ConstraintsFactory.createPair(m.getArgTypes().get(i),
|
||||||
arglist.getArguments().get(i).getType()));
|
arglist.getArguments().get(i).getType(), PairOperator.SMALLERDOT, info));
|
||||||
}
|
}
|
||||||
methodConstraints.add(methodConstraint);
|
methodConstraints.add(methodConstraint);
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package de.dhbwstuttgart.syntaxtree.statement;
|
|||||||
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
|
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
|
||||||
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
|
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
|
||||||
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
|
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
|
||||||
|
import de.dhbwstuttgart.typeinference.constraints.ConstraintsFactory;
|
||||||
import org.antlr.v4.runtime.Token;
|
import org.antlr.v4.runtime.Token;
|
||||||
|
|
||||||
|
|
||||||
@ -19,6 +20,9 @@ public class Return extends Statement
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
|
|||||||
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
|
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
|
||||||
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
|
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
|
||||||
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
|
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
|
||||||
|
import de.dhbwstuttgart.typeinference.constraints.ConstraintsFactory;
|
||||||
import de.dhbwstuttgart.typeinference.constraints.Pair;
|
import de.dhbwstuttgart.typeinference.constraints.Pair;
|
||||||
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
|
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
|
||||||
import org.antlr.v4.runtime.Token;
|
import org.antlr.v4.runtime.Token;
|
||||||
@ -24,7 +25,7 @@ public class This extends Expression
|
|||||||
@Override
|
@Override
|
||||||
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
|
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
|
||||||
ConstraintSet ret = new ConstraintSet();
|
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;
|
return ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,5 +12,10 @@ public class GenericRefType extends RefTypeOrTPHOrWildcardOrGeneric
|
|||||||
super(offset);
|
super(offset);
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public GenericTypeName getName(){
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,6 +32,11 @@ public class RefType extends RefTypeOrTPHOrWildcardOrGeneric
|
|||||||
this.name = (fullyQualifiedName);
|
this.name = (fullyQualifiedName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString(){
|
||||||
|
return this.name.toString();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
int hash = 0;
|
int hash = 0;
|
||||||
|
8
src/de/dhbwstuttgart/syntaxtree/type/RefTypeOrTPHOrWildcardOrGeneric.java
Executable file → Normal file
8
src/de/dhbwstuttgart/syntaxtree/type/RefTypeOrTPHOrWildcardOrGeneric.java
Executable file → Normal file
@ -1,16 +1,10 @@
|
|||||||
package de.dhbwstuttgart.syntaxtree.type;
|
package de.dhbwstuttgart.syntaxtree.type;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import de.dhbwstuttgart.typecheck.JavaClassName;
|
|
||||||
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
|
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
|
||||||
import org.antlr.v4.runtime.Token;
|
import org.antlr.v4.runtime.Token;
|
||||||
|
|
||||||
|
public class RefTypeOrTPHOrWildcardOrGeneric extends SyntaxTreeNode{
|
||||||
public abstract class RefTypeOrTPHOrWildcardOrGeneric extends SyntaxTreeNode
|
|
||||||
{
|
|
||||||
public RefTypeOrTPHOrWildcardOrGeneric(Token offset) {
|
public RefTypeOrTPHOrWildcardOrGeneric(Token offset) {
|
||||||
super(offset);
|
super(offset);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,9 +1,22 @@
|
|||||||
package de.dhbwstuttgart.typecheck;
|
package de.dhbwstuttgart.typecheck;
|
||||||
|
|
||||||
import de.dhbwstuttgart.syntaxtree.GTVDeclarationContext;
|
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.GenericContext;
|
||||||
|
|
||||||
public class GenericTypeName extends JavaClassName {
|
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);
|
super(name);
|
||||||
|
this.parentClass = parentClass.parentClass;
|
||||||
|
this.methodName = parentClass.parentMethod;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString(){
|
||||||
|
return parentClass.toString()
|
||||||
|
+ DELIMITER + methodName
|
||||||
|
+ DELIMITER + super.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package de.dhbwstuttgart.typeinference.assumptions;
|
package de.dhbwstuttgart.typeinference.assumptions;
|
||||||
|
|
||||||
|
import com.sun.istack.internal.NotNull;
|
||||||
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
|
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
|
||||||
import de.dhbwstuttgart.syntaxtree.FormalParameter;
|
import de.dhbwstuttgart.syntaxtree.FormalParameter;
|
||||||
import de.dhbwstuttgart.syntaxtree.Method;
|
import de.dhbwstuttgart.syntaxtree.Method;
|
||||||
@ -10,7 +11,7 @@ public class TypeInferenceBlockInformation extends TypeInferenceInformation {
|
|||||||
private Method methodContext;
|
private Method methodContext;
|
||||||
private ClassOrInterface currentClass;
|
private ClassOrInterface currentClass;
|
||||||
|
|
||||||
public TypeInferenceBlockInformation(Set<ClassOrInterface> availableClasses, ClassOrInterface currentClass, Method methodContext) {
|
public TypeInferenceBlockInformation(Set<ClassOrInterface> availableClasses, @NotNull ClassOrInterface currentClass, Method methodContext) {
|
||||||
super(availableClasses);
|
super(availableClasses);
|
||||||
this.methodContext = methodContext;
|
this.methodContext = methodContext;
|
||||||
this.currentClass = currentClass;
|
this.currentClass = currentClass;
|
||||||
@ -19,4 +20,7 @@ public class TypeInferenceBlockInformation extends TypeInferenceInformation {
|
|||||||
public ClassOrInterface getCurrentClass() {
|
public ClassOrInterface getCurrentClass() {
|
||||||
return currentClass;
|
return currentClass;
|
||||||
}
|
}
|
||||||
|
public Method getCurrentMethod() {
|
||||||
|
return methodContext;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
5
src/de/dhbwstuttgart/typeinference/constraints/Pair.java
Executable file → Normal file
5
src/de/dhbwstuttgart/typeinference/constraints/Pair.java
Executable file → Normal file
@ -4,6 +4,7 @@ import java.io.Serializable;
|
|||||||
import com.sun.istack.internal.NotNull;
|
import com.sun.istack.internal.NotNull;
|
||||||
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
|
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
|
||||||
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
|
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
|
||||||
|
import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
|
||||||
|
|
||||||
|
|
||||||
public class Pair implements Serializable
|
public class Pair implements Serializable
|
||||||
@ -13,7 +14,7 @@ public class Pair implements Serializable
|
|||||||
|
|
||||||
private PairOperator eOperator = PairOperator.SMALLER;
|
private PairOperator eOperator = PairOperator.SMALLER;
|
||||||
|
|
||||||
public Pair(@NotNull RefTypeOrTPHOrWildcardOrGeneric TA1, @NotNull RefTypeOrTPHOrWildcardOrGeneric TA2 )
|
Pair(@NotNull RefTypeOrTPHOrWildcardOrGeneric TA1, @NotNull RefTypeOrTPHOrWildcardOrGeneric TA2 )
|
||||||
{
|
{
|
||||||
this.TA1 = TA1;
|
this.TA1 = TA1;
|
||||||
this.TA2 = TA2;
|
this.TA2 = TA2;
|
||||||
@ -22,7 +23,7 @@ public class Pair implements Serializable
|
|||||||
eOperator = PairOperator.SMALLER;
|
eOperator = PairOperator.SMALLER;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Pair(RefTypeOrTPHOrWildcardOrGeneric TA1, RefTypeOrTPHOrWildcardOrGeneric TA2, PairOperator eOp)
|
Pair(@NotNull RefTypeOrTPHOrWildcardOrGeneric TA1, @NotNull RefTypeOrTPHOrWildcardOrGeneric TA2, PairOperator eOp)
|
||||||
{
|
{
|
||||||
// Konstruktor
|
// Konstruktor
|
||||||
this(TA1,TA2);
|
this(TA1,TA2);
|
||||||
|
@ -16,7 +16,7 @@ public class JavaTXCompilerTest {
|
|||||||
@Test
|
@Test
|
||||||
public void test() throws IOException, ClassNotFoundException {
|
public void test() throws IOException, ClassNotFoundException {
|
||||||
JavaTXCompiler compiler = new JavaTXCompiler();
|
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.parse(new File(rootDirectory+"Generics.jav"));
|
||||||
compiler.typeInference();
|
compiler.typeInference();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user