Imports implementieren

This commit is contained in:
JanUlrich 2017-08-28 17:36:51 +02:00
parent 45c85b7686
commit 932b277dcc
5 changed files with 45 additions and 32 deletions

View File

@ -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<RefTypeOrTPHOrWildcardOrGeneric> 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<RefTypeOrTPHOrWildcardOrGeneric> extendedInterfaces = convert(ctx.extendsInterfaces(), generics);

View File

@ -76,7 +76,7 @@ public class TypeGenerator {
public static List<RefTypeOrTPHOrWildcardOrGeneric> convert(Java8Parser.TypeBoundContext typeBoundContext, JavaClassRegistry reg, GenericsRegistry generics) {
if(typeBoundContext == null){
List<RefTypeOrTPHOrWildcardOrGeneric> ret = new ArrayList<>();
ret.add(new ASTFactory(reg).createObjectClass().getType());
ret.add(ASTFactory.createObjectClass().getType());
return ret;
}

View File

@ -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<Method> methoden = new ArrayList<>();
List<de.dhbwstuttgart.syntaxtree.Constructor> 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<FormalParameter> 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<FormalParameter> 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<de.dhbwstuttgart.syntaxtree.GenericTypeVar> 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<RefType> 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);
}
/*

View File

@ -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<ClassOrInterface> 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);
}

8
test/javFiles/Import.jav Normal file
View File

@ -0,0 +1,8 @@
import java.util.Vector;
class Import {
void methode(){
Vector v;
v.add(v);
}
}