From 932b277dcc856dac74c325f8ad6078428aa7ee7c Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Mon, 28 Aug 2017 17:36:51 +0200 Subject: [PATCH] Imports implementieren --- .../SyntaxTreeGenerator.java | 4 +- .../SyntaxTreeGenerator/TypeGenerator.java | 2 +- .../syntaxtree/factory/ASTFactory.java | 50 ++++++++----------- .../typeinference/typeAlgo/TYPE.java | 13 ++++- test/javFiles/Import.jav | 8 +++ 5 files changed, 45 insertions(+), 32 deletions(-) create mode 100644 test/javFiles/Import.jav diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java index 941e7032c..b6e85c9ac 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java @@ -290,7 +290,7 @@ public class SyntaxTreeGenerator{ if(ctx.superclass() != null){ superClass = convert(ctx.superclass()); }else{ - superClass = new ASTFactory(reg).createObjectClass().getType(); + superClass = ASTFactory.createObjectClass().getType(); } Boolean isInterface = false; List implementedInterfaces = convert(ctx.superinterfaces(), generics); @@ -474,7 +474,7 @@ public class SyntaxTreeGenerator{ }else{ genericParams = createEmptyGenericDeclarationList(ctx.Identifier()); } - RefType superClass = new ASTFactory(reg).createObjectClass().getType(); + RefType superClass = ASTFactory.createObjectClass().getType(); List extendedInterfaces = convert(ctx.extendsInterfaces(), generics); diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java index c1f608de5..b5f725325 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java @@ -76,7 +76,7 @@ public class TypeGenerator { public static List convert(Java8Parser.TypeBoundContext typeBoundContext, JavaClassRegistry reg, GenericsRegistry generics) { if(typeBoundContext == null){ List ret = new ArrayList<>(); - ret.add(new ASTFactory(reg).createObjectClass().getType()); + ret.add(ASTFactory.createObjectClass().getType()); return ret; } diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java index e46eec3aa..68a153428 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java @@ -25,15 +25,9 @@ import org.antlr.v4.runtime.Token; * dass alle Imports und Typnamen korrekt sind und müssen diese nicht überprüfen. */ public class ASTFactory { - private final JavaClassRegistry names; - - public ASTFactory(JavaClassRegistry scope){ - names = scope; - } - - public ClassOrInterface createClass(java.lang.Class jreClass){ - JavaClassName name = names.getName(jreClass.getName()); + public static ClassOrInterface createClass(java.lang.Class jreClass){ + JavaClassName name = new JavaClassName(jreClass.getName()); List methoden = new ArrayList<>(); List konstruktoren = new ArrayList<>(); for(java.lang.reflect.Constructor constructor : jreClass.getConstructors()){ @@ -63,13 +57,13 @@ public class ASTFactory { return new ClassOrInterface(modifier, name, felder, methoden, konstruktoren, genericDeclarationList, superClass,isInterface, implementedInterfaces, offset); } - private de.dhbwstuttgart.syntaxtree.Constructor createConstructor(Constructor constructor, Class inClass) { + private static de.dhbwstuttgart.syntaxtree.Constructor createConstructor(Constructor constructor, Class inClass) { String name = constructor.getName(); - RefTypeOrTPHOrWildcardOrGeneric returnType = createType(inClass, names.getName(inClass.getName()), name); + RefTypeOrTPHOrWildcardOrGeneric returnType = createType(inClass, new JavaClassName(inClass.getName()), name); Parameter[] jreParams = constructor.getParameters(); List params = new ArrayList<>(); for(Parameter jreParam : jreParams){ - RefTypeOrTPHOrWildcardOrGeneric paramType = createType(jreParam.getType(),names.getName(inClass.getName()), name); + RefTypeOrTPHOrWildcardOrGeneric paramType = createType(jreParam.getType(),new JavaClassName(inClass.getName()), name); params.add(new FormalParameter(jreParam.getName(),paramType, new NullToken())); } ParameterList parameterList = new ParameterList(params, new NullToken()); @@ -81,14 +75,14 @@ public class ASTFactory { return new de.dhbwstuttgart.syntaxtree.Constructor(name,returnType, modifier, parameterList, block, gtvDeclarations, offset, new ArrayList<>()); } - public 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(); RefTypeOrTPHOrWildcardOrGeneric returnType; - returnType = createType(jreMethod.getReturnType(),names.getName(inClass.getName()), name); + returnType = createType(jreMethod.getReturnType(),new JavaClassName(inClass.getName()), name); Parameter[] jreParams = jreMethod.getParameters(); List params = new ArrayList<>(); for(Parameter jreParam : jreParams){ - RefTypeOrTPHOrWildcardOrGeneric paramType = createType(jreParam.getType(),names.getName(inClass.getName()), name); + RefTypeOrTPHOrWildcardOrGeneric paramType = createType(jreParam.getType(),new JavaClassName(inClass.getName()), name); params.add(new FormalParameter(jreParam.getName(),paramType, new NullToken())); } ParameterList parameterList = new ParameterList(params, new NullToken()); @@ -100,7 +94,7 @@ public class ASTFactory { return new Method(name,returnType, modifier, parameterList, block, gtvDeclarations, offset); } - public GenericDeclarationList createGenerics(TypeVariable[] typeParameters, Class context, String methodName){ + public static GenericDeclarationList createGenerics(TypeVariable[] typeParameters, Class context, String methodName){ List gtvs = new ArrayList<>(); for(TypeVariable jreTV : typeParameters){ de.dhbwstuttgart.syntaxtree.GenericTypeVar gtv = createGeneric(jreTV, jreTV.getName(), context, methodName); @@ -120,23 +114,23 @@ public class ASTFactory { } */ - public RefTypeOrTPHOrWildcardOrGeneric createType(java.lang.reflect.Type type, JavaClassName parentClass, String parentMethod){ + public static 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")){ - return new RefType(this.names.getName("java.lang.Integer"), new ArrayList<>(), new NullToken()); + return new RefType(new JavaClassName("java.lang.Integer"), new ArrayList<>(), new NullToken()); }else if(type.getTypeName().equals("byte")){ - return new RefType(this.names.getName("java.lang.Byte"), new ArrayList<>(), new NullToken()); + return new RefType(new JavaClassName("java.lang.Byte"), new ArrayList<>(), new NullToken()); }else if(type.getTypeName().equals("boolean")){ - return new RefType(this.names.getName("java.lang.Boolean"), new ArrayList<>(), new NullToken()); + return new RefType(new JavaClassName("java.lang.Boolean"), new ArrayList<>(), new NullToken()); }else if(type.getTypeName().equals("char")){ - return new RefType(this.names.getName("java.lang.Char"), new ArrayList<>(), new NullToken()); + return new RefType(new JavaClassName("java.lang.Char"), new ArrayList<>(), new NullToken()); }else if(type.getTypeName().equals("short")){ - return new RefType(this.names.getName("java.lang.Short"), new ArrayList<>(), new NullToken()); + return new RefType(new JavaClassName("java.lang.Short"), new ArrayList<>(), new NullToken()); }else if(type.getTypeName().equals("double")){ - return new RefType(this.names.getName("java.lang.Double"), new ArrayList<>(), new NullToken()); + return new RefType(new JavaClassName("java.lang.Double"), new ArrayList<>(), new NullToken()); }else if(type.getTypeName().equals("long")){ - return new RefType(this.names.getName("java.lang.Long"), new ArrayList<>(), new NullToken()); + return new RefType(new JavaClassName("java.lang.Long"), new ArrayList<>(), new NullToken()); }else{ if(type instanceof TypeVariable){ //GTVDeclarationContext via "(TypeVariable) type).getGenericDeclaration()" @@ -150,13 +144,13 @@ public class ASTFactory { params.add(createType(t, parentClass, parentMethod)); } } - RefType ret = new RefType(this.names.getName(type.getTypeName()), params, new NullToken()); + RefType ret = new RefType(new JavaClassName(type.getTypeName()), params, new NullToken()); return ret; } } - public de.dhbwstuttgart.syntaxtree.GenericTypeVar createGeneric(TypeVariable jreTypeVar, String jreTVName, Class context, String parentMethod){ - JavaClassName parentClass = names.getName(context.getName()); + public static de.dhbwstuttgart.syntaxtree.GenericTypeVar createGeneric(TypeVariable jreTypeVar, String jreTVName, Class context, String parentMethod){ + JavaClassName parentClass = new JavaClassName(context.getName()); List genericBounds = new ArrayList<>(); java.lang.reflect.Type[] bounds = jreTypeVar.getBounds(); if(bounds.length > 0){ @@ -169,8 +163,8 @@ public class ASTFactory { , genericBounds, new NullToken(), new NullToken()); } - public ClassOrInterface createObjectClass() { - return this.createClass(Object.class); + public static ClassOrInterface createObjectClass() { + return createClass(Object.class); } /* diff --git a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java index 17da11169..2a9ef6d0f 100644 --- a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java +++ b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java @@ -1,6 +1,8 @@ package de.dhbwstuttgart.typeinference.typeAlgo; +import de.dhbwstuttgart.exceptions.DebugException; import de.dhbwstuttgart.syntaxtree.*; +import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; import de.dhbwstuttgart.syntaxtree.statement.Statement; import de.dhbwstuttgart.typecheck.JavaClassName; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; @@ -32,14 +34,23 @@ public class TYPE { for(Method m : cl.getMethods()){ ret.addAll(getConstraintsMethod(m,info, cl)); } + for(Constructor m : cl.getConstructors()){ + ret.addAll(getConstraintsConstructor(m,info, cl)); + } return ret; } - private TypeInferenceInformation getTypeInferenceInformation(){ + private TypeInferenceInformation getTypeInferenceInformation() { + ClassLoader classLoader = ClassLoader.getSystemClassLoader(); Set classes = new HashSet<>(); for(SourceFile sourceFile : sfs){ for(JavaClassName importName : sourceFile.imports){ System.out.println(importName); + try { + classes.add(ASTFactory.createClass(classLoader.loadClass(importName.toString()))); + } catch (ClassNotFoundException e) { + throw new DebugException("Klasse " + importName + " konnte nicht geladen werden"); + } } classes.addAll(sourceFile.KlassenVektor); } diff --git a/test/javFiles/Import.jav b/test/javFiles/Import.jav new file mode 100644 index 000000000..c48082baa --- /dev/null +++ b/test/javFiles/Import.jav @@ -0,0 +1,8 @@ +import java.util.Vector; + +class Import { + void methode(){ + Vector v; + v.add(v); + } +} \ No newline at end of file