diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java index 7cb16ff7..a18202b7 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java @@ -3,6 +3,7 @@ package de.dhbwstuttgart.parser.SyntaxTreeGenerator; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.exceptions.TypeinferenceException; import de.dhbwstuttgart.parser.antlr.Java17Parser; +import de.dhbwstuttgart.parser.antlr.Java17Parser.WildcardTypeContext; import de.dhbwstuttgart.parser.scope.GenericsRegistry; import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.parser.scope.JavaClassRegistry; @@ -19,6 +20,7 @@ import org.antlr.v4.runtime.Token; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -90,6 +92,49 @@ public class TypeGenerator { } return convertTypeName(name, arguments, unannClassOrInterfaceTypeContext.getStart(), reg, generics); } + // TODO: Merge with method above + /* + * ClassType und UnannClassTypes waren in alter Grammatik getrennt, jetzt + * zusammen unter ClassOrInterfaceType + * + * 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()) { + * nextCtx = forEachCtx; + * typeName += "." + forEachCtx.Identifier().toString(); + * } + * Java17Parser.TypeArgumentsContext arguments = nextCtx != null ? + * nextCtx.typeArguments() : ctx.typeArguments(); + * return convertTypeName(typeName, arguments, + * classOrInterfaceTypeContext.getStart(), reg, generics); + * } + * + * private static RefTypeOrTPHOrWildcardOrGeneric + * convert(Java17Parser.InterfaceTypeContext interfaceTypeContext) { + * 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); + * } + */ public static RefTypeOrTPHOrWildcardOrGeneric convert(Java17Parser.TypeTypeContext typeContext, JavaClassRegistry reg, GenericsRegistry genericsRegistry) { @@ -115,6 +160,26 @@ public class TypeGenerator { return TypeGenerator.convert(typeContext.classOrInterfaceType(), reg, genericsRegistry); } + // TODO: Merge with method above + /* + * 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 { + * throw new NotImplementedException(); + * } + * } + */ public static GenericDeclarationList convert(Java17Parser.GenericDeclarationListContext typeParametersContext, JavaClassName parentClass, String parentMethod, JavaClassRegistry reg, GenericsRegistry generics) { @@ -164,53 +229,14 @@ public class TypeGenerator { } } - 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()) { - nextCtx = forEachCtx; - typeName += "." + forEachCtx.Identifier().toString(); - } - Java17Parser.TypeArgumentsContext arguments = nextCtx != null ? nextCtx.typeArguments() : ctx.typeArguments(); - return convertTypeName(typeName, arguments, classOrInterfaceTypeContext.getStart(), reg, generics); - } - - private static RefTypeOrTPHOrWildcardOrGeneric convert(Java17Parser.InterfaceTypeContext interfaceTypeContext) { - throw new NotImplementedException(); - } - - public static RefTypeOrTPHOrWildcardOrGeneric convert(Java17Parser.ReferenceTypeContext referenceTypeContext, + public static RefTypeOrTPHOrWildcardOrGeneric convert(Java17Parser.WildcardTypeContext wildcardContext, 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 { - 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), + if (wildcardContext.getChildCount() < 3) { + if (!Objects.isNull(wildcardContext.extendsWildcardType())) { + return new ExtendsWildcardType(convert(wildcardContext.extendsWildcardType().typeType(), reg, generics), wildcardContext.getStart()); } else { - return new SuperWildcardType(convert(wildcardContext.wildcardBounds().referenceType(), reg, generics), + return new SuperWildcardType(convert(wildcardContext.superWildcardType().typeType(), reg, generics), wildcardContext.getStart()); } } else { @@ -253,20 +279,14 @@ public class TypeGenerator { public static List convert(Java17Parser.TypeArgumentsContext typeArguments, JavaClassRegistry reg, GenericsRegistry generics) { List ret = new ArrayList<>(); - for (Java17Parser.TypeArgumentContext arg : typeArguments.typeArgumentList().typeArgument()) { - if (arg.wildcard() != null) { - ret.add(convert(arg.wildcard(), reg, generics)); + for (Java17Parser.TypeArgumentContext arg : typeArguments.typeArgument()) { + WildcardTypeContext wc = arg.wildcardType(); + if (!Objects.isNull(wc)) { + ret.add(convert(wc, reg, generics)); } else { - ret.add(convert(arg.referenceType(), reg, generics)); + ret.add(convert(arg.typeType(), reg, generics)); } } return ret; } - - 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); - } }