ASTFactory erweitert

This commit is contained in:
JanUlrich 2017-03-08 17:51:59 +01:00
parent 9fae52e1e6
commit 2cdca93077
6 changed files with 95 additions and 27 deletions

View File

@ -22,10 +22,10 @@ public class ClassOrInterface extends GTVDeclarationContext implements IItemWith
private Token offset;
private RefTypeOrTPH superClass;
protected boolean isInterface;
private List<RefTypeOrTPH> implementedInterfaces;
private List<? extends RefTypeOrTPH> implementedInterfaces;
public ClassOrInterface(int modifiers, JavaClassName name, List<Field> fielddecl, List<Method> methods, GenericDeclarationList genericClassParameters,
RefTypeOrTPH superClass, Boolean isInterface, List<RefTypeOrTPH> implementedInterfaces, Token offset){
RefTypeOrTPH superClass, Boolean isInterface, List<? extends RefTypeOrTPH> implementedInterfaces, Token offset){
super(offset);
this.modifiers = modifiers;
if(name != null){

View File

@ -1,20 +1,25 @@
package de.dhbwstuttgart.syntaxtree.factory;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.*;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.syntaxtree.Field;
import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPH;
import de.dhbwstuttgart.typecheck.GenericTypeName;
import de.dhbwstuttgart.typecheck.JavaClassName;
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.statement.SuperCall;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import org.antlr.v4.runtime.Token;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import sun.reflect.generics.reflectiveObjects.TypeVariableImpl;
/**
* Anmerkung:
@ -22,7 +27,6 @@ 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;
@ -30,11 +34,11 @@ public class ASTFactory {
names = scope;
}
public Class createClass(java.lang.Class jreClass){
public ClassOrInterface createClass(java.lang.Class jreClass){
JavaClassName name = names.getName(jreClass.getName());
List<Method> methoden = new ArrayList<>();
for(java.lang.reflect.Constructor constructor : jreClass.getConstructors()){
methoden.add(createConstructor(constructor, jreClass));
}
for(java.lang.reflect.Method method : jreClass.getMethods()){
methoden.add(createMethod(method, jreClass));
@ -43,7 +47,7 @@ public class ASTFactory {
int modifier = jreClass.getModifiers();
boolean isInterface = jreClass.isInterface();
java.lang.Class superjreClass = jreClass.getSuperclass();
RefType superClass = null;
RefType superClass;
if(superjreClass != null){
superClass = createType(superjreClass);
}else{//Jede Klasse und jedes Interface erbt von Object: (auch Object selbst!)
@ -53,11 +57,17 @@ public class ASTFactory {
for(java.lang.Class jreInterface : jreClass.getInterfaces()){
implementedInterfaces.add(createType(jreInterface));
}
int offset = 0; //Braucht keinen Offset, da diese Klasse nicht aus einem Quellcode geparst wurde
return new ClassOrInterface(name, methoden, felder, modifier, isInterface, superClass, implementedInterfaces, offset);
GenericDeclarationList genericDeclarationList = createGenerics(jreClass.getTypeParameters(), jreClass, null);
Token offset = new NullToken(); //Braucht keinen Offset, da diese Klasse nicht aus einem Quellcode geparst wurde
return new ClassOrInterface(modifier, name, felder, methoden, genericDeclarationList, superClass,isInterface, implementedInterfaces, offset);
}
public Method createMethod(java.lang.reflect.Method jreMethod, java.lang.Class inClass){
private Method createConstructor(Constructor constructor, Class jreClass) {
throw new NotImplementedException();
}
public Method createMethod(java.lang.reflect.Method jreMethod, java.lang.Class inClass){
String name = jreMethod.getName();
RefType returnType = createType(jreMethod.getReturnType());
Parameter[] jreParams = jreMethod.getParameters();
@ -68,31 +78,46 @@ public class ASTFactory {
}
ParameterList parameterList = new ParameterList(params, new NullToken());
Block block = new Block(new ArrayList<Statement>(), new NullToken());
List<GenericTypeVar> gtvs = new ArrayList<>();
for(TypeVariable jreTV : jreMethod.getTypeParameters()){
GenericTypeVar gtv = createGeneric(jreTV, jreTV.getName());
gtvs.add(gtv);
}
GenericDeclarationList gtvDeclarations = new GenericDeclarationList(gtvs,new NullToken());
GenericDeclarationList gtvDeclarations = createGenerics(jreMethod.getTypeParameters(), inClass, jreMethod);
Token offset = new NullToken();
int modifier = jreMethod.getModifiers();
return new Method(name,returnType, modifier, parameterList, block, gtvDeclarations, offset);
}
public GenericDeclarationList createGenerics(TypeVariable[] typeParameters, Class context, java.lang.reflect.Method contextM){
List<GenericTypeVar> gtvs = new ArrayList<>();
for(TypeVariable jreTV : typeParameters){
GenericTypeVar gtv = createGeneric(jreTV, jreTV.getName());
gtvs.add(gtv);
}
return new GenericDeclarationList(gtvs,new NullToken());
}
/*
public RefType createType(java.lang.Class jreClass){
List<RefType> params = new ArrayList<>();
List<RefTypeOrTPH> params = new ArrayList<>();
for(TypeVariable jreTV : jreClass.getTypeParameters()){
RefType gtv = createType(jreTV);
params.add(gtv);
}
jreClass
return new RefType(jreClass.getName(), params, -1);
return new RefType(names.getName(jreClass.getName()), params, new NullToken());
}
*/
public RefType createType(java.lang.reflect.Type type){
RefType ret = new RefType(type.getTypeName(), -1);
//TODO hier die Generischen Variablen extrahieren
if(type instanceof TypeVariable){
//GTVDeclarationContext via "(TypeVariable) type).getGenericDeclaration()"
return new GenericRefType(new GenericTypeName(type.getTypeName(),
new GTVDeclarationContext(new NullToken()) {}), new NullToken());
}
List<RefTypeOrTPH> params = new ArrayList<>();
if(type instanceof ParameterizedType){
for(Type t : ((ParameterizedType)type).getActualTypeArguments()){
params.add(createType(t));
}
}
RefType ret = new RefType(this.names.getName(type.getTypeName()), params, new NullToken());
return ret;
}
@ -107,11 +132,10 @@ public class ASTFactory {
return new GenericTypeVar(name, genericBounds, new NullToken(), new NullToken());
}
public Class createObjectClass() {
public ClassOrInterface createObjectClass() {
return this.createClass(Object.class);
}
*/
/*
public Constructor createEmptyConstructor(Class parent){
Block block = new Block();

View File

@ -20,6 +20,6 @@ public class Return extends Statement
@Override
public ConstraintSet getConstraints(TypeInferenceInformation info) {
throw new NotImplementedException();
return retexpr.getConstraints(info);
}
}

View File

@ -0,0 +1,13 @@
package de.dhbwstuttgart.syntaxtree.type;
import de.dhbwstuttgart.typecheck.JavaClassName;
import org.antlr.v4.runtime.Token;
public class GenericRefType extends RefType
{
public GenericRefType(JavaClassName fullyQualifiedName, Token offset)
{
super(fullyQualifiedName, offset);
}
}

View File

@ -0,0 +1,9 @@
package de.dhbwstuttgart.typecheck;
import de.dhbwstuttgart.syntaxtree.GTVDeclarationContext;
public class GenericTypeName extends JavaClassName {
public GenericTypeName(String name, GTVDeclarationContext declarationContext) {
super(name);
}
}

View File

@ -0,0 +1,22 @@
package astfactory;
import javafx.collections.ObservableList;
import org.junit.Test;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.Arrays;
import java.util.HashMap;
import static org.junit.Assert.*;
public class ASTFactoryTest<A> extends HashMap<String, A>{
@Test
public void test(){
System.out.println((this.getClass().getGenericSuperclass()));
Type[] arguments = ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments();
System.out.println(Arrays.toString(arguments));
System.out.println(((TypeVariable)arguments[1]).getGenericDeclaration());
}
}