forked from JavaTX/JavaCompilerCore
ASTGen auf basis von antlr visitor
This commit is contained in:
parent
c78a148766
commit
a85b60b95f
@ -10,12 +10,11 @@ import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import de.dhbwstuttgart.environment.PackageCrawler;
|
||||
import de.dhbwstuttgart.exceptions.TypeinferenceException;
|
||||
import de.dhbwstuttgart.parser.antlr.Java17Parser;
|
||||
import de.dhbwstuttgart.parser.antlr.Java17ParserBaseVisitor;
|
||||
import de.dhbwstuttgart.parser.antlr.Java17Parser.AnnotationclassmodContext;
|
||||
import de.dhbwstuttgart.parser.antlr.Java17Parser.ClassOrInterfaceModifierContext;
|
||||
import de.dhbwstuttgart.parser.antlr.Java17Parser.ClassorinterfacedeclContext;
|
||||
import de.dhbwstuttgart.parser.antlr.Java17Parser.ClassorinterfacemodContext;
|
||||
import de.dhbwstuttgart.parser.antlr.Java17Parser.ModifierContext;
|
||||
import de.dhbwstuttgart.parser.antlr.Java17Parser.NoclassorinterfaceContext;
|
||||
import de.dhbwstuttgart.parser.scope.GatherNames;
|
||||
@ -24,6 +23,7 @@ import de.dhbwstuttgart.parser.scope.JavaClassName;
|
||||
import de.dhbwstuttgart.parser.scope.JavaClassRegistry;
|
||||
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
|
||||
import de.dhbwstuttgart.syntaxtree.SourceFile;
|
||||
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
|
||||
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
|
||||
import de.dhbwstuttgart.syntaxtree.statement.Statement;
|
||||
|
||||
@ -64,7 +64,6 @@ public class ASTGen extends Java17ParserBaseVisitor<Object> {
|
||||
|
||||
public SourceFile generate(Java17Parser.SourceFileContext ctx, PackageCrawler packageCrawler, ClassLoader classLoader)
|
||||
throws ClassNotFoundException {
|
||||
List<ClassOrInterface> classes = new ArrayList<>();
|
||||
Map<String, Integer> imports = GatherNames.getImports(ctx, packageCrawler, classLoader);
|
||||
this.imports = imports.keySet().stream().map(name -> reg.getName(name)).collect(Collectors.toSet());
|
||||
|
||||
@ -73,6 +72,7 @@ public class ASTGen extends Java17ParserBaseVisitor<Object> {
|
||||
|
||||
@Override
|
||||
public SourceFile visitSrcfile(Java17Parser.SrcfileContext ctx) {
|
||||
List<ClassOrInterface> classes = new ArrayList<>();
|
||||
for (Java17Parser.ClassOrInterfaceContext member : ctx.classOrInterface()) {
|
||||
ClassorinterfacedeclContext clsoif;
|
||||
if (member instanceof NoclassorinterfaceContext) {
|
||||
@ -84,13 +84,13 @@ public class ASTGen extends Java17ParserBaseVisitor<Object> {
|
||||
int modifiers = 0;
|
||||
if (!clsoif.classOrInterfaceModifier().isEmpty()) {
|
||||
for (Java17Parser.ClassOrInterfaceModifierContext mod : clsoif.classOrInterfaceModifier()) {
|
||||
int newModifier = (Integer) visit(mod);
|
||||
int newModifier = convertModifier(mod.getText());
|
||||
modifiers += newModifier;
|
||||
}
|
||||
}
|
||||
fieldInitializations = new ArrayList<>(); // PL 2019-10-22: muss für jede Klasse neu initilisiert werden
|
||||
if (clsoif.classDeclaration() != null) {
|
||||
newClass = convertClass(clsoif.classDeclaration());
|
||||
newClass = visitClassDeclaration(clsoif.classDeclaration());
|
||||
} else {
|
||||
newClass = convertInterface(clsoif.interfaceDeclaration());
|
||||
}
|
||||
@ -100,19 +100,71 @@ public class ASTGen extends Java17ParserBaseVisitor<Object> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visitModifier(ModifierContext ctx) {
|
||||
if (ctx.classOrInterfaceModifier() != null) {
|
||||
return visit(ctx.classOrInterfaceModifier());
|
||||
} else {
|
||||
return convertModifier(ctx.getText());
|
||||
public ClassOrInterface visitClassDeclaration(Java17Parser.ClassDeclarationContext ctx) {
|
||||
String className = this.pkgName + (this.pkgName.length() > 0 ? "." : "") + ctx.identifier().getText();
|
||||
JavaClassName name = reg.getName(className); // Holt den Package Namen mit dazu
|
||||
if (!name.toString().equals(className)) { // Kommt die Klasse schon in einem anderen Package vor?
|
||||
throw new TypeinferenceException(
|
||||
"Name " + className + " bereits vorhanden in " + reg.getName(className).toString(), ctx.getStart());
|
||||
}
|
||||
GenericsRegistry generics = createGenerics(ctx.genericDeclarationList(), name, "", reg,
|
||||
new GenericsRegistry(globalGenerics));
|
||||
Token offset = ctx.getStart();
|
||||
GenericDeclarationList genericClassParameters;
|
||||
if (ctx.genericDeclarationList() == null) {
|
||||
genericClassParameters = createEmptyGenericDeclarationList(ctx.identifier().getStop());
|
||||
} else {
|
||||
genericClassParameters = TypeGenerator.convert(ctx.genericDeclarationList(), name, "", reg, generics);
|
||||
}
|
||||
RefType superClass;
|
||||
if (ctx.EXTENDS() != null) {
|
||||
superClass = convert(ctx.typeType());
|
||||
} else {
|
||||
superClass = new RefType(ASTFactory.createObjectClass().getClassName(), ctx.getStart());
|
||||
}
|
||||
List<Field> fielddecl = convertFields(ctx.classBody(), generics);
|
||||
// fieldInitializations = generateFieldInitializations(ctx.classBody(),
|
||||
// generics);
|
||||
List<Method> methodsAndConstructors = convertMethods(ctx.classBody(), name, superClass, generics);
|
||||
List<Method> methods = new ArrayList<>();
|
||||
List<Constructor> konstruktoren = new ArrayList<>();
|
||||
// int noOfMethods = methods.size();
|
||||
for (Method m : methodsAndConstructors) {
|
||||
if (m instanceof Constructor) {
|
||||
konstruktoren.add((Constructor) m);
|
||||
} else {
|
||||
methods.add(m);
|
||||
}
|
||||
}
|
||||
if (konstruktoren.size() < 1) {// Standardkonstruktor anfügen:
|
||||
konstruktoren.add(
|
||||
generateStandardConstructor(
|
||||
ctx.Identifier().getText(), name, superClass,
|
||||
genericClassParameters, offset));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visitClassOrInterfaceModifier(ClassOrInterfaceModifierContext ctx) {
|
||||
if (ctx.annotation() != null)
|
||||
return 0;
|
||||
return convertModifier(ctx.getText());
|
||||
Boolean isInterface = false;
|
||||
List<RefType> implementedInterfaces = convert(ctx.superinterfaces(), generics);
|
||||
|
||||
return new ClassOrInterface(modifiers, name, fielddecl,
|
||||
Optional.of(this.generatePseudoConstructor(ctx.Identifier().getText(), name, superClass, genericClassParameters,
|
||||
offset)),
|
||||
methods, konstruktoren, genericClassParameters, superClass,
|
||||
isInterface, implementedInterfaces, offset);
|
||||
}
|
||||
|
||||
private GenericsRegistry createGenerics(Java17Parser.GenericDeclarationListContext ctx, JavaClassName parentClass,
|
||||
String parentMethod, JavaClassRegistry reg, GenericsRegistry generics) {
|
||||
GenericsRegistry ret = new GenericsRegistry(this.globalGenerics);
|
||||
ret.putAll(generics);
|
||||
if (ctx == null)
|
||||
return ret;
|
||||
ret.putAll(visitGenericDeclarationList(ctx));
|
||||
for (Java17Parser.GenericTypeVarContext tp : ctx.genericTypeVar()) {
|
||||
ret.put(tp.identifier().getText(), new GenericContext(parentClass, parentMethod));
|
||||
TypeGenerator.convert(tp, parentClass, parentMethod, reg, ret);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static int convertModifier(String modifier) {
|
||||
|
@ -25,123 +25,159 @@ import java.util.regex.Pattern;
|
||||
|
||||
public class TypeGenerator {
|
||||
|
||||
public static RefTypeOrTPHOrWildcardOrGeneric convert(Java17Parser.UnannClassOrInterfaceTypeContext unannClassOrInterfaceTypeContext, JavaClassRegistry reg, GenericsRegistry generics) {
|
||||
Java17Parser.TypeArgumentsContext arguments=null;
|
||||
/* PL 2019-03-19 auskommentiert ANFANG
|
||||
if(unannClassOrInterfaceTypeContext.unannClassType_lfno_unannClassOrInterfaceType() != null){
|
||||
arguments = unannClassOrInterfaceTypeContext.unannClassType_lfno_unannClassOrInterfaceType().typeArguments();
|
||||
}else{// if(unannClassOrInterfaceTypeContext.unannInterfaceType_lfno_unannClassOrInterfaceType() != null){
|
||||
arguments = unannClassOrInterfaceTypeContext.unannInterfaceType_lfno_unannClassOrInterfaceType().unannClassType_lfno_unannClassOrInterfaceType().typeArguments();
|
||||
}
|
||||
PL 2019-03-19 auskommentiert ENDE */
|
||||
public static RefTypeOrTPHOrWildcardOrGeneric convert(
|
||||
Java17Parser.UnannClassOrInterfaceTypeContext unannClassOrInterfaceTypeContext, JavaClassRegistry reg,
|
||||
GenericsRegistry generics) {
|
||||
Java17Parser.TypeArgumentsContext arguments = null;
|
||||
/*
|
||||
* PL 2019-03-19 auskommentiert ANFANG
|
||||
* if(unannClassOrInterfaceTypeContext.
|
||||
* unannClassType_lfno_unannClassOrInterfaceType() != null){
|
||||
* arguments = unannClassOrInterfaceTypeContext.
|
||||
* unannClassType_lfno_unannClassOrInterfaceType().typeArguments();
|
||||
* }else{// if(unannClassOrInterfaceTypeContext.
|
||||
* unannInterfaceType_lfno_unannClassOrInterfaceType() != null){
|
||||
* arguments = unannClassOrInterfaceTypeContext.
|
||||
* unannInterfaceType_lfno_unannClassOrInterfaceType().
|
||||
* unannClassType_lfno_unannClassOrInterfaceType().typeArguments();
|
||||
* }
|
||||
* PL 2019-03-19 auskommentiert ENDE
|
||||
*/
|
||||
/**
|
||||
* Problem sind hier die verschachtelten Typen mit verschachtelten Typargumenten
|
||||
* Beispiel: Typ<String>.InnererTyp<Integer>
|
||||
*/
|
||||
String name = unannClassOrInterfaceTypeContext.getText();
|
||||
if(name.contains("<")){
|
||||
name = name.split("<")[0]; //Der Typ ist alles vor den ersten Argumenten
|
||||
/* Fuer Debug-Zwecke
|
||||
unannClassOrInterfaceTypeContext.unannInterfaceType_lfno_unannClassOrInterfaceType();
|
||||
unannClassOrInterfaceTypeContext.unannClassType_lfno_unannClassOrInterfaceType().getText();
|
||||
//unannClassOrInterfaceTypeContext.unannInterfaceType_lfno_unannClassOrInterfaceType().unannClassType_lfno_unannClassOrInterfaceType().typeArguments();
|
||||
//UnannClassType_lfno_unannClassOrInterfaceTypeContext
|
||||
unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType(0);
|
||||
unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType(0).getText();
|
||||
unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType(1);
|
||||
unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType(1).getText();
|
||||
unannClassOrInterfaceTypeContext.unannClassType_lfno_unannClassOrInterfaceType().getText();
|
||||
//unannClassOrInterfaceTypeContext.unannInterfaceType_lf_unannClassOrInterfaceType();
|
||||
//unannClassOrInterfaceTypeContext.unannInterfaceType_lf_unannClassOrInterfaceType(0).getText();
|
||||
//unannClassOrInterfaceTypeContext.unannInterfaceType_lf_unannClassOrInterfaceType(1).getText();
|
||||
//unannClassOrInterfaceTypeContext.unannInterfaceType_lfno_unannClassOrInterfaceType().getText();
|
||||
if (name.contains("<")) {
|
||||
name = name.split("<")[0]; // Der Typ ist alles vor den ersten Argumenten
|
||||
/*
|
||||
* Fuer Debug-Zwecke
|
||||
* unannClassOrInterfaceTypeContext.
|
||||
* unannInterfaceType_lfno_unannClassOrInterfaceType();
|
||||
* unannClassOrInterfaceTypeContext.
|
||||
* unannClassType_lfno_unannClassOrInterfaceType().getText();
|
||||
* //unannClassOrInterfaceTypeContext.
|
||||
* unannInterfaceType_lfno_unannClassOrInterfaceType().
|
||||
* unannClassType_lfno_unannClassOrInterfaceType().typeArguments();
|
||||
* //UnannClassType_lfno_unannClassOrInterfaceTypeContext
|
||||
* unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType(
|
||||
* 0);
|
||||
* unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType(
|
||||
* 0).getText();
|
||||
* unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType(
|
||||
* 1);
|
||||
* unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType(
|
||||
* 1).getText();
|
||||
* unannClassOrInterfaceTypeContext.
|
||||
* unannClassType_lfno_unannClassOrInterfaceType().getText();
|
||||
* //unannClassOrInterfaceTypeContext.
|
||||
* unannInterfaceType_lf_unannClassOrInterfaceType();
|
||||
* //unannClassOrInterfaceTypeContext.
|
||||
* unannInterfaceType_lf_unannClassOrInterfaceType(0).getText();
|
||||
* //unannClassOrInterfaceTypeContext.
|
||||
* unannInterfaceType_lf_unannClassOrInterfaceType(1).getText();
|
||||
* //unannClassOrInterfaceTypeContext.
|
||||
* unannInterfaceType_lfno_unannClassOrInterfaceType().getText();
|
||||
*/
|
||||
int lastElement = new ArrayList<>(unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType()).size()-1;
|
||||
if (lastElement >=0) {//qualifizierter Name z.B.: java.util.Vector
|
||||
arguments = unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType(lastElement).typeArguments();
|
||||
}
|
||||
else { //unqualifizierter Name z.B.: Vector
|
||||
arguments = unannClassOrInterfaceTypeContext.unannClassType_lfno_unannClassOrInterfaceType().typeArguments();
|
||||
int lastElement = new ArrayList<>(
|
||||
unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType()).size() - 1;
|
||||
if (lastElement >= 0) {// qualifizierter Name z.B.: java.util.Vector
|
||||
arguments = unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType(lastElement)
|
||||
.typeArguments();
|
||||
} else { // unqualifizierter Name z.B.: Vector
|
||||
arguments = unannClassOrInterfaceTypeContext.unannClassType_lfno_unannClassOrInterfaceType()
|
||||
.typeArguments();
|
||||
}
|
||||
}
|
||||
return convertTypeName(name, arguments, unannClassOrInterfaceTypeContext.getStart(), reg, generics);
|
||||
}
|
||||
|
||||
public static RefTypeOrTPHOrWildcardOrGeneric convert(Java17Parser.UnannTypeContext unannTypeContext, JavaClassRegistry reg, GenericsRegistry genericsRegistry) {
|
||||
if(unannTypeContext.unannPrimitiveType()!=null){
|
||||
if(unannTypeContext.unannPrimitiveType().getText().equals("boolean")){
|
||||
public static RefTypeOrTPHOrWildcardOrGeneric convert(Java17Parser.UnannTypeContext unannTypeContext,
|
||||
JavaClassRegistry reg, GenericsRegistry genericsRegistry) {
|
||||
if (unannTypeContext.unannPrimitiveType() != null) {
|
||||
if (unannTypeContext.unannPrimitiveType().getText().equals("boolean")) {
|
||||
return new RefType(ASTFactory.createClass(Boolean.class).getClassName(), unannTypeContext.getStart());
|
||||
}else{
|
||||
} else {
|
||||
Java17Parser.NumericTypeContext numericType = unannTypeContext.unannPrimitiveType().numericType();
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}else
|
||||
if(unannTypeContext.unannReferenceType().unannArrayType()!=null){
|
||||
//System.out.println(unannTypeContext.getText());
|
||||
} else if (unannTypeContext.unannReferenceType().unannArrayType() != null) {
|
||||
// System.out.println(unannTypeContext.getText());
|
||||
throw new NotImplementedException();
|
||||
}else
|
||||
if(unannTypeContext.unannReferenceType().unannTypeVariable()!=null){
|
||||
JavaClassName name = reg.getName(unannTypeContext.unannReferenceType().unannTypeVariable().Identifier().toString());
|
||||
} else if (unannTypeContext.unannReferenceType().unannTypeVariable() != null) {
|
||||
JavaClassName name = reg
|
||||
.getName(unannTypeContext.unannReferenceType().unannTypeVariable().Identifier().toString());
|
||||
return new RefType(name, unannTypeContext.getStart());
|
||||
}
|
||||
return TypeGenerator.convert(unannTypeContext.unannReferenceType().unannClassOrInterfaceType(), reg, genericsRegistry);
|
||||
return TypeGenerator.convert(unannTypeContext.unannReferenceType().unannClassOrInterfaceType(), reg,
|
||||
genericsRegistry);
|
||||
}
|
||||
|
||||
public static GenericDeclarationList convert(Java17Parser.TypeParametersContext typeParametersContext,
|
||||
JavaClassName parentClass, String parentMethod, JavaClassRegistry reg, GenericsRegistry generics) {
|
||||
Token endOffset = typeParametersContext.getStop();
|
||||
List<GenericTypeVar> typeVars = new ArrayList<>();
|
||||
for(Java17Parser.TypeParameterContext typeParameter : typeParametersContext.typeParameterList().typeParameter()){
|
||||
for (Java17Parser.TypeParameterContext typeParameter : typeParametersContext.typeParameterList()
|
||||
.typeParameter()) {
|
||||
typeVars.add(convert(typeParameter, parentClass, parentMethod, reg, generics));
|
||||
endOffset = typeParameter.getStop();
|
||||
}
|
||||
return new GenericDeclarationList(typeVars, endOffset);
|
||||
}
|
||||
|
||||
public static GenericTypeVar convert(Java17Parser.TypeParameterContext typeParameter, JavaClassName parentClass, String parentMethod, JavaClassRegistry reg, GenericsRegistry generics) {
|
||||
public static GenericTypeVar convert(Java17Parser.TypeParameterContext typeParameter, JavaClassName parentClass,
|
||||
String parentMethod, JavaClassRegistry reg, GenericsRegistry generics) {
|
||||
String name = typeParameter.Identifier().getText();
|
||||
//TODO: Es müssen erst alle GenericTypeVars generiert werden, dann können die bounds dieser Generics ermittelt werden
|
||||
//Problem <A extends B, B> ist erlaubt, würde aber bei den Bounds von A den Generic B nicht als solchen erkennen
|
||||
List<RefTypeOrTPHOrWildcardOrGeneric> bounds = TypeGenerator.convert(typeParameter.typeBound(),reg, generics);
|
||||
// TODO: Es müssen erst alle GenericTypeVars generiert werden, dann können die
|
||||
// bounds dieser Generics ermittelt werden
|
||||
// Problem <A extends B, B> ist erlaubt, würde aber bei den Bounds von A den
|
||||
// Generic B nicht als solchen erkennen
|
||||
List<RefTypeOrTPHOrWildcardOrGeneric> bounds = TypeGenerator.convert(typeParameter.typeBound(), reg, generics);
|
||||
|
||||
GenericTypeVar ret = new GenericTypeVar(name, bounds, typeParameter.getStart(), typeParameter.getStop());
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static List<RefTypeOrTPHOrWildcardOrGeneric> convert(Java17Parser.TypeBoundContext typeBoundContext, JavaClassRegistry reg, GenericsRegistry generics) {
|
||||
public static List<RefTypeOrTPHOrWildcardOrGeneric> convert(Java17Parser.TypeBoundContext typeBoundContext,
|
||||
JavaClassRegistry reg, GenericsRegistry generics) {
|
||||
List<RefTypeOrTPHOrWildcardOrGeneric> ret = new ArrayList<>();
|
||||
if(typeBoundContext == null){
|
||||
if (typeBoundContext == null) {
|
||||
ret.add(ASTFactory.createObjectType());
|
||||
return ret;
|
||||
}
|
||||
if(typeBoundContext.typeVariable() != null){
|
||||
ret.add(convertTypeName(typeBoundContext.typeVariable().Identifier().getText(), null, typeBoundContext.typeVariable().getStart(), reg, generics));
|
||||
if (typeBoundContext.typeVariable() != null) {
|
||||
ret.add(convertTypeName(typeBoundContext.typeVariable().Identifier().getText(), null,
|
||||
typeBoundContext.typeVariable().getStart(), reg, generics));
|
||||
return ret;
|
||||
}
|
||||
if(typeBoundContext.classOrInterfaceType() != null){
|
||||
if (typeBoundContext.classOrInterfaceType() != null) {
|
||||
ret.add(convert(typeBoundContext.classOrInterfaceType(), reg, generics));
|
||||
if(typeBoundContext.additionalBound() != null)
|
||||
for(Java17Parser.AdditionalBoundContext addCtx : typeBoundContext.additionalBound()){
|
||||
if (typeBoundContext.additionalBound() != null)
|
||||
for (Java17Parser.AdditionalBoundContext addCtx : typeBoundContext.additionalBound()) {
|
||||
ret.add(convert(addCtx.interfaceType()));
|
||||
}
|
||||
return ret;
|
||||
}else{
|
||||
} else {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
private static RefTypeOrTPHOrWildcardOrGeneric convert(Java17Parser.ClassOrInterfaceTypeContext classOrInterfaceTypeContext, JavaClassRegistry reg, GenericsRegistry generics) {
|
||||
Java17Parser.ClassType_lfno_classOrInterfaceTypeContext ctx = classOrInterfaceTypeContext.classType_lfno_classOrInterfaceType();
|
||||
if(ctx.typeArguments() != null &&
|
||||
private static RefTypeOrTPHOrWildcardOrGeneric convert(
|
||||
Java17Parser.ClassOrInterfaceTypeContext classOrInterfaceTypeContext, JavaClassRegistry reg,
|
||||
GenericsRegistry generics) {
|
||||
Java17Parser.ClassType_lfno_classOrInterfaceTypeContext ctx = classOrInterfaceTypeContext
|
||||
.classType_lfno_classOrInterfaceType();
|
||||
if (ctx.typeArguments() != null &&
|
||||
classOrInterfaceTypeContext.classType_lf_classOrInterfaceType().size() > 0)
|
||||
throw new NotImplementedException();
|
||||
String typeName = ctx.Identifier().toString();
|
||||
Java17Parser.ClassType_lf_classOrInterfaceTypeContext nextCtx = null;
|
||||
for(Java17Parser.ClassType_lf_classOrInterfaceTypeContext forEachCtx : classOrInterfaceTypeContext.classType_lf_classOrInterfaceType()){
|
||||
for (Java17Parser.ClassType_lf_classOrInterfaceTypeContext forEachCtx : classOrInterfaceTypeContext
|
||||
.classType_lf_classOrInterfaceType()) {
|
||||
nextCtx = forEachCtx;
|
||||
typeName += "." + forEachCtx.Identifier().toString();
|
||||
}
|
||||
Java17Parser.TypeArgumentsContext arguments = nextCtx!=null?nextCtx.typeArguments():ctx.typeArguments();
|
||||
Java17Parser.TypeArgumentsContext arguments = nextCtx != null ? nextCtx.typeArguments() : ctx.typeArguments();
|
||||
return convertTypeName(typeName, arguments, classOrInterfaceTypeContext.getStart(), reg, generics);
|
||||
}
|
||||
|
||||
@ -149,74 +185,86 @@ public class TypeGenerator {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public static RefTypeOrTPHOrWildcardOrGeneric convert(Java17Parser.ReferenceTypeContext referenceTypeContext, JavaClassRegistry reg, GenericsRegistry generics) {
|
||||
if(referenceTypeContext.classOrInterfaceType() != null){
|
||||
if(referenceTypeContext.classOrInterfaceType().classType_lfno_classOrInterfaceType()!= null){
|
||||
return convert(referenceTypeContext.classOrInterfaceType(), reg, generics);//return convertTypeName(referenceTypeContext.getText(), ctx.typeArguments(),referenceTypeContext.getStart(), reg, generics);
|
||||
}else{
|
||||
public static RefTypeOrTPHOrWildcardOrGeneric convert(Java17Parser.ReferenceTypeContext referenceTypeContext,
|
||||
JavaClassRegistry reg, GenericsRegistry generics) {
|
||||
if (referenceTypeContext.classOrInterfaceType() != null) {
|
||||
if (referenceTypeContext.classOrInterfaceType().classType_lfno_classOrInterfaceType() != null) {
|
||||
return convert(referenceTypeContext.classOrInterfaceType(), reg, generics);// return
|
||||
// convertTypeName(referenceTypeContext.getText(),
|
||||
// ctx.typeArguments(),referenceTypeContext.getStart(),
|
||||
// reg, generics);
|
||||
} else {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}else{
|
||||
} else {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public static RefTypeOrTPHOrWildcardOrGeneric convert(Java17Parser.WildcardContext wildcardContext, JavaClassRegistry reg, GenericsRegistry generics) {
|
||||
if(wildcardContext.wildcardBounds() != null){
|
||||
if(wildcardContext.wildcardBounds().getText().substring(0, 7).equals("extends")){
|
||||
return new ExtendsWildcardType(convert(wildcardContext.wildcardBounds().referenceType(), reg, generics), wildcardContext.getStart());
|
||||
}else{
|
||||
return new SuperWildcardType(convert(wildcardContext.wildcardBounds().referenceType(), reg, generics), wildcardContext.getStart());
|
||||
public static RefTypeOrTPHOrWildcardOrGeneric convert(Java17Parser.WildcardContext wildcardContext,
|
||||
JavaClassRegistry reg, GenericsRegistry generics) {
|
||||
if (wildcardContext.wildcardBounds() != null) {
|
||||
if (wildcardContext.wildcardBounds().getText().substring(0, 7).equals("extends")) {
|
||||
return new ExtendsWildcardType(convert(wildcardContext.wildcardBounds().referenceType(), reg, generics),
|
||||
wildcardContext.getStart());
|
||||
} else {
|
||||
return new SuperWildcardType(convert(wildcardContext.wildcardBounds().referenceType(), reg, generics),
|
||||
wildcardContext.getStart());
|
||||
}
|
||||
}else{
|
||||
throw new NotImplementedException(); //Wildcard ohne Bound
|
||||
} else {
|
||||
throw new NotImplementedException(); // Wildcard ohne Bound
|
||||
}
|
||||
}
|
||||
|
||||
public static RefTypeOrTPHOrWildcardOrGeneric convertTypeName(String name, Token offset, JavaClassRegistry reg, GenericsRegistry generics){
|
||||
public static RefTypeOrTPHOrWildcardOrGeneric convertTypeName(String name, Token offset, JavaClassRegistry reg,
|
||||
GenericsRegistry generics) {
|
||||
return convertTypeName(name, null, offset, reg, generics);
|
||||
}
|
||||
|
||||
public static RefTypeOrTPHOrWildcardOrGeneric convertTypeName(
|
||||
String name, Java17Parser.TypeArgumentsContext typeArguments, Token offset, JavaClassRegistry reg, GenericsRegistry generics){
|
||||
if(!reg.contains(name)){ //Dann könnte es ein generischer Type oder ein FunN$$-Type sein
|
||||
if(generics.contains(name)){
|
||||
String name, Java17Parser.TypeArgumentsContext typeArguments, Token offset, JavaClassRegistry reg,
|
||||
GenericsRegistry generics) {
|
||||
if (!reg.contains(name)) { // Dann könnte es ein generischer Type oder ein FunN$$-Type sein
|
||||
if (generics.contains(name)) {
|
||||
return new GenericRefType(name, offset);
|
||||
}else{
|
||||
} else {
|
||||
Pattern p = Pattern.compile("Fun(\\d+)[$][$]");
|
||||
Matcher m = p.matcher(name);
|
||||
if (m.matches()) {//es ist FunN$$-Type
|
||||
if (m.matches()) {// es ist FunN$$-Type
|
||||
return new RefType(new JavaClassName(name), convert(typeArguments, reg, generics), offset);
|
||||
} else {
|
||||
throw new TypeinferenceException("Der Typ "+ name + " ist nicht vorhanden",offset);
|
||||
}}
|
||||
throw new TypeinferenceException("Der Typ " + name + " ist nicht vorhanden", offset);
|
||||
}
|
||||
if(typeArguments == null){
|
||||
}
|
||||
}
|
||||
if (typeArguments == null) {
|
||||
List<RefTypeOrTPHOrWildcardOrGeneric> params = new ArrayList<>();
|
||||
for(int i = 0; i<reg.getNumberOfGenerics(name);i++){
|
||||
for (int i = 0; i < reg.getNumberOfGenerics(name); i++) {
|
||||
params.add(TypePlaceholder.fresh(offset));
|
||||
}
|
||||
return new RefType(reg.getName(name), params, offset);
|
||||
}else{
|
||||
} else {
|
||||
return new RefType(reg.getName(name), convert(typeArguments, reg, generics), offset);
|
||||
}
|
||||
}
|
||||
|
||||
public static List<RefTypeOrTPHOrWildcardOrGeneric> convert(Java17Parser.TypeArgumentsContext typeArguments,
|
||||
JavaClassRegistry reg, GenericsRegistry generics){
|
||||
JavaClassRegistry reg, GenericsRegistry generics) {
|
||||
List<RefTypeOrTPHOrWildcardOrGeneric> ret = new ArrayList<>();
|
||||
for(Java17Parser.TypeArgumentContext arg : typeArguments.typeArgumentList().typeArgument()){
|
||||
if(arg.wildcard() != null){
|
||||
for (Java17Parser.TypeArgumentContext arg : typeArguments.typeArgumentList().typeArgument()) {
|
||||
if (arg.wildcard() != null) {
|
||||
ret.add(convert(arg.wildcard(), reg, generics));
|
||||
}else{
|
||||
} else {
|
||||
ret.add(convert(arg.referenceType(), reg, generics));
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static RefTypeOrTPHOrWildcardOrGeneric convert(Java17Parser.ClassTypeContext ctx, JavaClassRegistry reg, GenericsRegistry generics) {
|
||||
if(ctx.classOrInterfaceType() != null)throw new NotImplementedException();
|
||||
public static RefTypeOrTPHOrWildcardOrGeneric convert(Java17Parser.ClassTypeContext ctx, JavaClassRegistry reg,
|
||||
GenericsRegistry generics) {
|
||||
if (ctx.classOrInterfaceType() != null)
|
||||
throw new NotImplementedException();
|
||||
return convertTypeName(ctx.Identifier().getText(), ctx.typeArguments(), ctx.getStart(), reg, generics);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user