ASTGen auf basis von antlr visitor

This commit is contained in:
luca9913 2023-01-20 02:54:59 +01:00
parent c78a148766
commit a85b60b95f
2 changed files with 212 additions and 112 deletions

View File

@ -10,12 +10,11 @@ import java.lang.reflect.Modifier;
import java.util.ArrayList; import java.util.ArrayList;
import de.dhbwstuttgart.environment.PackageCrawler; import de.dhbwstuttgart.environment.PackageCrawler;
import de.dhbwstuttgart.exceptions.TypeinferenceException;
import de.dhbwstuttgart.parser.antlr.Java17Parser; import de.dhbwstuttgart.parser.antlr.Java17Parser;
import de.dhbwstuttgart.parser.antlr.Java17ParserBaseVisitor; 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.ClassOrInterfaceModifierContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.ClassorinterfacedeclContext; 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.ModifierContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.NoclassorinterfaceContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.NoclassorinterfaceContext;
import de.dhbwstuttgart.parser.scope.GatherNames; 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.parser.scope.JavaClassRegistry;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.statement.Statement; 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) public SourceFile generate(Java17Parser.SourceFileContext ctx, PackageCrawler packageCrawler, ClassLoader classLoader)
throws ClassNotFoundException { throws ClassNotFoundException {
List<ClassOrInterface> classes = new ArrayList<>();
Map<String, Integer> imports = GatherNames.getImports(ctx, packageCrawler, classLoader); Map<String, Integer> imports = GatherNames.getImports(ctx, packageCrawler, classLoader);
this.imports = imports.keySet().stream().map(name -> reg.getName(name)).collect(Collectors.toSet()); this.imports = imports.keySet().stream().map(name -> reg.getName(name)).collect(Collectors.toSet());
@ -73,6 +72,7 @@ public class ASTGen extends Java17ParserBaseVisitor<Object> {
@Override @Override
public SourceFile visitSrcfile(Java17Parser.SrcfileContext ctx) { public SourceFile visitSrcfile(Java17Parser.SrcfileContext ctx) {
List<ClassOrInterface> classes = new ArrayList<>();
for (Java17Parser.ClassOrInterfaceContext member : ctx.classOrInterface()) { for (Java17Parser.ClassOrInterfaceContext member : ctx.classOrInterface()) {
ClassorinterfacedeclContext clsoif; ClassorinterfacedeclContext clsoif;
if (member instanceof NoclassorinterfaceContext) { if (member instanceof NoclassorinterfaceContext) {
@ -84,13 +84,13 @@ public class ASTGen extends Java17ParserBaseVisitor<Object> {
int modifiers = 0; int modifiers = 0;
if (!clsoif.classOrInterfaceModifier().isEmpty()) { if (!clsoif.classOrInterfaceModifier().isEmpty()) {
for (Java17Parser.ClassOrInterfaceModifierContext mod : clsoif.classOrInterfaceModifier()) { for (Java17Parser.ClassOrInterfaceModifierContext mod : clsoif.classOrInterfaceModifier()) {
int newModifier = (Integer) visit(mod); int newModifier = convertModifier(mod.getText());
modifiers += newModifier; modifiers += newModifier;
} }
} }
fieldInitializations = new ArrayList<>(); // PL 2019-10-22: muss für jede Klasse neu initilisiert werden fieldInitializations = new ArrayList<>(); // PL 2019-10-22: muss für jede Klasse neu initilisiert werden
if (clsoif.classDeclaration() != null) { if (clsoif.classDeclaration() != null) {
newClass = convertClass(clsoif.classDeclaration()); newClass = visitClassDeclaration(clsoif.classDeclaration());
} else { } else {
newClass = convertInterface(clsoif.interfaceDeclaration()); newClass = convertInterface(clsoif.interfaceDeclaration());
} }
@ -100,19 +100,71 @@ public class ASTGen extends Java17ParserBaseVisitor<Object> {
} }
@Override @Override
public Object visitModifier(ModifierContext ctx) { public ClassOrInterface visitClassDeclaration(Java17Parser.ClassDeclarationContext ctx) {
if (ctx.classOrInterfaceModifier() != null) { String className = this.pkgName + (this.pkgName.length() > 0 ? "." : "") + ctx.identifier().getText();
return visit(ctx.classOrInterfaceModifier()); JavaClassName name = reg.getName(className); // Holt den Package Namen mit dazu
} else { if (!name.toString().equals(className)) { // Kommt die Klasse schon in einem anderen Package vor?
return convertModifier(ctx.getText()); 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));
}
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);
} }
@Override private GenericsRegistry createGenerics(Java17Parser.GenericDeclarationListContext ctx, JavaClassName parentClass,
public Object visitClassOrInterfaceModifier(ClassOrInterfaceModifierContext ctx) { String parentMethod, JavaClassRegistry reg, GenericsRegistry generics) {
if (ctx.annotation() != null) GenericsRegistry ret = new GenericsRegistry(this.globalGenerics);
return 0; ret.putAll(generics);
return convertModifier(ctx.getText()); 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) { public static int convertModifier(String modifier) {

View File

@ -25,123 +25,159 @@ import java.util.regex.Pattern;
public class TypeGenerator { public class TypeGenerator {
public static RefTypeOrTPHOrWildcardOrGeneric convert(Java17Parser.UnannClassOrInterfaceTypeContext unannClassOrInterfaceTypeContext, JavaClassRegistry reg, GenericsRegistry generics) { public static RefTypeOrTPHOrWildcardOrGeneric convert(
Java17Parser.TypeArgumentsContext arguments=null; Java17Parser.UnannClassOrInterfaceTypeContext unannClassOrInterfaceTypeContext, JavaClassRegistry reg,
/* PL 2019-03-19 auskommentiert ANFANG GenericsRegistry generics) {
if(unannClassOrInterfaceTypeContext.unannClassType_lfno_unannClassOrInterfaceType() != null){ Java17Parser.TypeArgumentsContext arguments = null;
arguments = unannClassOrInterfaceTypeContext.unannClassType_lfno_unannClassOrInterfaceType().typeArguments(); /*
}else{// if(unannClassOrInterfaceTypeContext.unannInterfaceType_lfno_unannClassOrInterfaceType() != null){ * PL 2019-03-19 auskommentiert ANFANG
arguments = unannClassOrInterfaceTypeContext.unannInterfaceType_lfno_unannClassOrInterfaceType().unannClassType_lfno_unannClassOrInterfaceType().typeArguments(); * if(unannClassOrInterfaceTypeContext.
} * unannClassType_lfno_unannClassOrInterfaceType() != null){
PL 2019-03-19 auskommentiert ENDE */ * 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 * Problem sind hier die verschachtelten Typen mit verschachtelten Typargumenten
* Beispiel: Typ<String>.InnererTyp<Integer> * Beispiel: Typ<String>.InnererTyp<Integer>
*/ */
String name = unannClassOrInterfaceTypeContext.getText(); String name = unannClassOrInterfaceTypeContext.getText();
if(name.contains("<")){ if (name.contains("<")) {
name = name.split("<")[0]; //Der Typ ist alles vor den ersten Argumenten name = name.split("<")[0]; // Der Typ ist alles vor den ersten Argumenten
/* Fuer Debug-Zwecke /*
unannClassOrInterfaceTypeContext.unannInterfaceType_lfno_unannClassOrInterfaceType(); * Fuer Debug-Zwecke
unannClassOrInterfaceTypeContext.unannClassType_lfno_unannClassOrInterfaceType().getText(); * unannClassOrInterfaceTypeContext.
//unannClassOrInterfaceTypeContext.unannInterfaceType_lfno_unannClassOrInterfaceType().unannClassType_lfno_unannClassOrInterfaceType().typeArguments(); * unannInterfaceType_lfno_unannClassOrInterfaceType();
//UnannClassType_lfno_unannClassOrInterfaceTypeContext * unannClassOrInterfaceTypeContext.
unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType(0); * unannClassType_lfno_unannClassOrInterfaceType().getText();
unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType(0).getText(); * //unannClassOrInterfaceTypeContext.
unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType(1); * unannInterfaceType_lfno_unannClassOrInterfaceType().
unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType(1).getText(); * unannClassType_lfno_unannClassOrInterfaceType().typeArguments();
unannClassOrInterfaceTypeContext.unannClassType_lfno_unannClassOrInterfaceType().getText(); * //UnannClassType_lfno_unannClassOrInterfaceTypeContext
//unannClassOrInterfaceTypeContext.unannInterfaceType_lf_unannClassOrInterfaceType(); * unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType(
//unannClassOrInterfaceTypeContext.unannInterfaceType_lf_unannClassOrInterfaceType(0).getText(); * 0);
//unannClassOrInterfaceTypeContext.unannInterfaceType_lf_unannClassOrInterfaceType(1).getText(); * unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType(
//unannClassOrInterfaceTypeContext.unannInterfaceType_lfno_unannClassOrInterfaceType().getText(); * 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; int lastElement = new ArrayList<>(
if (lastElement >=0) {//qualifizierter Name z.B.: java.util.Vector unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType()).size() - 1;
arguments = unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType(lastElement).typeArguments(); if (lastElement >= 0) {// qualifizierter Name z.B.: java.util.Vector
} arguments = unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType(lastElement)
else { //unqualifizierter Name z.B.: Vector .typeArguments();
arguments = unannClassOrInterfaceTypeContext.unannClassType_lfno_unannClassOrInterfaceType().typeArguments(); } else { // unqualifizierter Name z.B.: Vector
arguments = unannClassOrInterfaceTypeContext.unannClassType_lfno_unannClassOrInterfaceType()
.typeArguments();
} }
} }
return convertTypeName(name, arguments, unannClassOrInterfaceTypeContext.getStart(), reg, generics); return convertTypeName(name, arguments, unannClassOrInterfaceTypeContext.getStart(), reg, generics);
} }
public static RefTypeOrTPHOrWildcardOrGeneric convert(Java17Parser.UnannTypeContext unannTypeContext, JavaClassRegistry reg, GenericsRegistry genericsRegistry) { public static RefTypeOrTPHOrWildcardOrGeneric convert(Java17Parser.UnannTypeContext unannTypeContext,
if(unannTypeContext.unannPrimitiveType()!=null){ JavaClassRegistry reg, GenericsRegistry genericsRegistry) {
if(unannTypeContext.unannPrimitiveType().getText().equals("boolean")){ if (unannTypeContext.unannPrimitiveType() != null) {
if (unannTypeContext.unannPrimitiveType().getText().equals("boolean")) {
return new RefType(ASTFactory.createClass(Boolean.class).getClassName(), unannTypeContext.getStart()); return new RefType(ASTFactory.createClass(Boolean.class).getClassName(), unannTypeContext.getStart());
}else{ } else {
Java17Parser.NumericTypeContext numericType = unannTypeContext.unannPrimitiveType().numericType(); Java17Parser.NumericTypeContext numericType = unannTypeContext.unannPrimitiveType().numericType();
throw new NotImplementedException(); throw new NotImplementedException();
} }
}else } else if (unannTypeContext.unannReferenceType().unannArrayType() != null) {
if(unannTypeContext.unannReferenceType().unannArrayType()!=null){ // System.out.println(unannTypeContext.getText());
//System.out.println(unannTypeContext.getText());
throw new NotImplementedException(); throw new NotImplementedException();
}else } else if (unannTypeContext.unannReferenceType().unannTypeVariable() != null) {
if(unannTypeContext.unannReferenceType().unannTypeVariable()!=null){ JavaClassName name = reg
JavaClassName name = reg.getName(unannTypeContext.unannReferenceType().unannTypeVariable().Identifier().toString()); .getName(unannTypeContext.unannReferenceType().unannTypeVariable().Identifier().toString());
return new RefType(name, unannTypeContext.getStart()); 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, public static GenericDeclarationList convert(Java17Parser.TypeParametersContext typeParametersContext,
JavaClassName parentClass, String parentMethod, JavaClassRegistry reg, GenericsRegistry generics) { JavaClassName parentClass, String parentMethod, JavaClassRegistry reg, GenericsRegistry generics) {
Token endOffset = typeParametersContext.getStop(); Token endOffset = typeParametersContext.getStop();
List<GenericTypeVar> typeVars = new ArrayList<>(); 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)); 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(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(); String name = typeParameter.Identifier().getText();
//TODO: Es müssen erst alle GenericTypeVars generiert werden, dann können die bounds dieser Generics ermittelt werden // TODO: Es müssen erst alle GenericTypeVars generiert werden, dann können die
//Problem <A extends B, B> ist erlaubt, würde aber bei den Bounds von A den Generic B nicht als solchen erkennen // bounds dieser Generics ermittelt werden
List<RefTypeOrTPHOrWildcardOrGeneric> bounds = TypeGenerator.convert(typeParameter.typeBound(),reg, generics); // 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()); GenericTypeVar ret = new GenericTypeVar(name, bounds, typeParameter.getStart(), typeParameter.getStop());
return ret; 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<>(); List<RefTypeOrTPHOrWildcardOrGeneric> ret = new ArrayList<>();
if(typeBoundContext == null){ if (typeBoundContext == null) {
ret.add(ASTFactory.createObjectType()); ret.add(ASTFactory.createObjectType());
return ret; return ret;
} }
if(typeBoundContext.typeVariable() != null){ if (typeBoundContext.typeVariable() != null) {
ret.add(convertTypeName(typeBoundContext.typeVariable().Identifier().getText(), null, typeBoundContext.typeVariable().getStart(), reg, generics)); ret.add(convertTypeName(typeBoundContext.typeVariable().Identifier().getText(), null,
typeBoundContext.typeVariable().getStart(), reg, generics));
return ret; return ret;
} }
if(typeBoundContext.classOrInterfaceType() != null){ if (typeBoundContext.classOrInterfaceType() != null) {
ret.add(convert(typeBoundContext.classOrInterfaceType(), reg, generics)); ret.add(convert(typeBoundContext.classOrInterfaceType(), reg, generics));
if(typeBoundContext.additionalBound() != null) if (typeBoundContext.additionalBound() != null)
for(Java17Parser.AdditionalBoundContext addCtx : typeBoundContext.additionalBound()){ for (Java17Parser.AdditionalBoundContext addCtx : typeBoundContext.additionalBound()) {
ret.add(convert(addCtx.interfaceType())); ret.add(convert(addCtx.interfaceType()));
} }
return ret; return ret;
}else{ } else {
throw new NotImplementedException(); throw new NotImplementedException();
} }
} }
private static RefTypeOrTPHOrWildcardOrGeneric convert(Java17Parser.ClassOrInterfaceTypeContext classOrInterfaceTypeContext, JavaClassRegistry reg, GenericsRegistry generics) { private static RefTypeOrTPHOrWildcardOrGeneric convert(
Java17Parser.ClassType_lfno_classOrInterfaceTypeContext ctx = classOrInterfaceTypeContext.classType_lfno_classOrInterfaceType(); Java17Parser.ClassOrInterfaceTypeContext classOrInterfaceTypeContext, JavaClassRegistry reg,
if(ctx.typeArguments() != null && GenericsRegistry generics) {
Java17Parser.ClassType_lfno_classOrInterfaceTypeContext ctx = classOrInterfaceTypeContext
.classType_lfno_classOrInterfaceType();
if (ctx.typeArguments() != null &&
classOrInterfaceTypeContext.classType_lf_classOrInterfaceType().size() > 0) classOrInterfaceTypeContext.classType_lf_classOrInterfaceType().size() > 0)
throw new NotImplementedException(); throw new NotImplementedException();
String typeName = ctx.Identifier().toString(); String typeName = ctx.Identifier().toString();
Java17Parser.ClassType_lf_classOrInterfaceTypeContext nextCtx = null; 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; nextCtx = forEachCtx;
typeName += "." + forEachCtx.Identifier().toString(); 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); return convertTypeName(typeName, arguments, classOrInterfaceTypeContext.getStart(), reg, generics);
} }
@ -149,74 +185,86 @@ public class TypeGenerator {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public static RefTypeOrTPHOrWildcardOrGeneric convert(Java17Parser.ReferenceTypeContext referenceTypeContext, JavaClassRegistry reg, GenericsRegistry generics) { public static RefTypeOrTPHOrWildcardOrGeneric convert(Java17Parser.ReferenceTypeContext referenceTypeContext,
if(referenceTypeContext.classOrInterfaceType() != null){ JavaClassRegistry reg, GenericsRegistry generics) {
if(referenceTypeContext.classOrInterfaceType().classType_lfno_classOrInterfaceType()!= null){ if (referenceTypeContext.classOrInterfaceType() != null) {
return convert(referenceTypeContext.classOrInterfaceType(), reg, generics);//return convertTypeName(referenceTypeContext.getText(), ctx.typeArguments(),referenceTypeContext.getStart(), reg, generics); if (referenceTypeContext.classOrInterfaceType().classType_lfno_classOrInterfaceType() != null) {
}else{ return convert(referenceTypeContext.classOrInterfaceType(), reg, generics);// return
// convertTypeName(referenceTypeContext.getText(),
// ctx.typeArguments(),referenceTypeContext.getStart(),
// reg, generics);
} else {
throw new NotImplementedException(); throw new NotImplementedException();
} }
}else{ } else {
throw new NotImplementedException(); throw new NotImplementedException();
} }
} }
public static RefTypeOrTPHOrWildcardOrGeneric convert(Java17Parser.WildcardContext wildcardContext, JavaClassRegistry reg, GenericsRegistry generics) { public static RefTypeOrTPHOrWildcardOrGeneric convert(Java17Parser.WildcardContext wildcardContext,
if(wildcardContext.wildcardBounds() != null){ JavaClassRegistry reg, GenericsRegistry generics) {
if(wildcardContext.wildcardBounds().getText().substring(0, 7).equals("extends")){ if (wildcardContext.wildcardBounds() != null) {
return new ExtendsWildcardType(convert(wildcardContext.wildcardBounds().referenceType(), reg, generics), wildcardContext.getStart()); if (wildcardContext.wildcardBounds().getText().substring(0, 7).equals("extends")) {
}else{ return new ExtendsWildcardType(convert(wildcardContext.wildcardBounds().referenceType(), reg, generics),
return new SuperWildcardType(convert(wildcardContext.wildcardBounds().referenceType(), reg, generics), wildcardContext.getStart()); wildcardContext.getStart());
} else {
return new SuperWildcardType(convert(wildcardContext.wildcardBounds().referenceType(), reg, generics),
wildcardContext.getStart());
} }
}else{ } else {
throw new NotImplementedException(); //Wildcard ohne Bound 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); return convertTypeName(name, null, offset, reg, generics);
} }
public static RefTypeOrTPHOrWildcardOrGeneric convertTypeName( public static RefTypeOrTPHOrWildcardOrGeneric convertTypeName(
String name, Java17Parser.TypeArgumentsContext typeArguments, Token offset, JavaClassRegistry reg, GenericsRegistry generics){ String name, Java17Parser.TypeArgumentsContext typeArguments, Token offset, JavaClassRegistry reg,
if(!reg.contains(name)){ //Dann könnte es ein generischer Type oder ein FunN$$-Type sein GenericsRegistry generics) {
if(generics.contains(name)){ 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); return new GenericRefType(name, offset);
}else{
Pattern p = Pattern.compile("Fun(\\d+)[$][$]");
Matcher m = p.matcher(name);
if (m.matches()) {//es ist FunN$$-Type
return new RefType(new JavaClassName(name), convert(typeArguments, reg, generics), offset);
} else { } else {
throw new TypeinferenceException("Der Typ "+ name + " ist nicht vorhanden",offset); Pattern p = Pattern.compile("Fun(\\d+)[$][$]");
}} Matcher m = p.matcher(name);
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);
}
}
} }
if(typeArguments == null){ if (typeArguments == null) {
List<RefTypeOrTPHOrWildcardOrGeneric> params = new ArrayList<>(); 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)); params.add(TypePlaceholder.fresh(offset));
} }
return new RefType(reg.getName(name), params, offset); return new RefType(reg.getName(name), params, offset);
}else{ } else {
return new RefType(reg.getName(name), convert(typeArguments, reg, generics), offset); return new RefType(reg.getName(name), convert(typeArguments, reg, generics), offset);
} }
} }
public static List<RefTypeOrTPHOrWildcardOrGeneric> convert(Java17Parser.TypeArgumentsContext typeArguments, public static List<RefTypeOrTPHOrWildcardOrGeneric> convert(Java17Parser.TypeArgumentsContext typeArguments,
JavaClassRegistry reg, GenericsRegistry generics){ JavaClassRegistry reg, GenericsRegistry generics) {
List<RefTypeOrTPHOrWildcardOrGeneric> ret = new ArrayList<>(); List<RefTypeOrTPHOrWildcardOrGeneric> ret = new ArrayList<>();
for(Java17Parser.TypeArgumentContext arg : typeArguments.typeArgumentList().typeArgument()){ for (Java17Parser.TypeArgumentContext arg : typeArguments.typeArgumentList().typeArgument()) {
if(arg.wildcard() != null){ if (arg.wildcard() != null) {
ret.add(convert(arg.wildcard(), reg, generics)); ret.add(convert(arg.wildcard(), reg, generics));
}else{ } else {
ret.add(convert(arg.referenceType(), reg, generics)); ret.add(convert(arg.referenceType(), reg, generics));
} }
} }
return ret; return ret;
} }
public static RefTypeOrTPHOrWildcardOrGeneric convert(Java17Parser.ClassTypeContext ctx, JavaClassRegistry reg, GenericsRegistry generics) { public static RefTypeOrTPHOrWildcardOrGeneric convert(Java17Parser.ClassTypeContext ctx, JavaClassRegistry reg,
if(ctx.classOrInterfaceType() != null)throw new NotImplementedException(); GenericsRegistry generics) {
if (ctx.classOrInterfaceType() != null)
throw new NotImplementedException();
return convertTypeName(ctx.Identifier().getText(), ctx.typeArguments(), ctx.getStart(), reg, generics); return convertTypeName(ctx.Identifier().getText(), ctx.typeArguments(), ctx.getStart(), reg, generics);
} }
} }