diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java index 86c0fc4f..7dd2403e 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java @@ -7,6 +7,10 @@ import java.lang.ClassNotFoundException; import de.dhbwstuttgart.exceptions.TypeinferenceException; import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.antlr.Java17Parser; +import de.dhbwstuttgart.parser.antlr.Java17ParserBaseVisitor; +import de.dhbwstuttgart.parser.antlr.Java17Parser.ClassorinterfacedeclContext; +import de.dhbwstuttgart.parser.antlr.Java17Parser.NoclassorinterfaceContext; +import de.dhbwstuttgart.parser.antlr.Java17Parser.SrcfileContext; import de.dhbwstuttgart.parser.scope.GatherNames; import de.dhbwstuttgart.parser.scope.GenericsRegistry; import de.dhbwstuttgart.parser.scope.JavaClassName; @@ -27,66 +31,91 @@ import java.util.stream.Collectors; //import jdk.internal.dynalink.support.TypeConverterFactory; import org.antlr.v4.runtime.CommonToken; import org.antlr.v4.runtime.Token; +import org.antlr.v4.runtime.tree.ParseTree; import org.antlr.v4.runtime.tree.TerminalNode; -public class SyntaxTreeGenerator{ +public class SyntaxTreeGenerator extends Java17ParserBaseVisitor { private JavaClassRegistry reg; private final GenericsRegistry globalGenerics; private String pkgName = ""; - Set imports = new HashSet(); - private Map fields = new HashMap<>(); //PL 2018-11-01 fields eingefuegt, damit die fields immer die gleiche TPH bekommen + Set imports = new HashSet<>(); + private Map fields = new HashMap<>(); // PL 2018-11-01 fields eingefuegt, + // damit die fields immer die gleiche + // TPH bekommen - List fieldInitializations = new ArrayList<>(); //PL 2019-10-23: Muss für jede Klasse neu initilisiert werden + List fieldInitializations = new ArrayList<>(); // PL 2019-10-23: Muss für jede Klasse neu initilisiert + // werden - public SyntaxTreeGenerator(JavaClassRegistry reg, GenericsRegistry globalGenerics){ - //Die Generics müssen während des Bauens des AST erstellt werden, + public SyntaxTreeGenerator(JavaClassRegistry reg, GenericsRegistry globalGenerics) { + // Die Generics müssen während des Bauens des AST erstellt werden, // da diese mit der Methode oder Klasse, in welcher sie deklariert werden - // verknüpft sein müssen. Dennoch werden die Namen aller Generics in einer globalen Datenbank benötigt. + // verknüpft sein müssen. Dennoch werden die Namen aller Generics in einer + // globalen Datenbank benötigt. this.globalGenerics = globalGenerics; this.reg = reg; } - - public JavaClassRegistry getReg(){ + public JavaClassRegistry getReg() { return this.reg; } - // Converts type name to String. - public String convertTypeName(Java17Parser.TypeNameContext ctx){ - String ret; - if(ctx.packageOrTypeName() == null){ - ret = ctx.Identifier().toString(); - } - else{ - ret = convertPackageOrTypeName(ctx.packageOrTypeName()) + "." + ctx.Identifier().toString(); - } - return ret; - } - // Converts PackageOrTypeName to String. - public String convertPackageOrTypeName(Java17Parser.PackageOrTypeNameContext ctx){ - String ret; - if(ctx.packageOrTypeName() == null){ - ret = ctx.Identifier().toString(); - } - else{ - ret = convertPackageOrTypeName(ctx.packageOrTypeName()) + "." + ctx.Identifier().toString(); + /* + * Package und Type Namen sind qualified Names und werden durch nachfolgende + * Methode behandelt. Diese Methode sollte damit überflüssig sein. + * + * public String convertPackageOrTypeName(Java17Parser.PackageOrTypeNameContext + * ctx) { + * String ret; + * if (ctx.packageOrTypeName() == null) { + * ret = ctx.Identifier().toString(); + * } else { + * ret = convertPackageOrTypeName(ctx.packageOrTypeName()) + "." + + * ctx.Identifier().toString(); + * } + * return ret; + * } + */ + + public String convertQualifiedName(Java17Parser.QualifiedNameContext ctx) { + String ret = ""; + for (Java17Parser.IdentifierContext ident : ctx.identifier()) { + ret += ident.getText(); + if (ctx.identifier().iterator().hasNext()) { + ret += '.'; + } } return ret; } - - public SourceFile convert(Java17Parser.CompilationUnitContext ctx, PackageCrawler packageCrawler, ClassLoader classLoader) throws ClassNotFoundException{ - if(ctx.packageDeclaration()!=null)this.pkgName = convert(ctx.packageDeclaration()); + + public SourceFile convert(Java17Parser.SourceFileContext ctx, PackageCrawler packageCrawler, ClassLoader classLoader) + throws ClassNotFoundException { + SrcfileContext srcfile = new SrcfileContext(ctx); + if (srcfile.packageDeclaration() != null) + this.pkgName = convert(srcfile.packageDeclaration()); List classes = new ArrayList<>(); Map imports = GatherNames.getImports(ctx, packageCrawler, classLoader); this.imports = imports.keySet().stream().map(name -> reg.getName(name)).collect(Collectors.toSet()); - for(Java17Parser.TypeDeclarationContext typeDecl : ctx.typeDeclaration()){ - ClassOrInterface newClass; - if(typeDecl.classDeclaration() != null){ - newClass = convertClass(typeDecl.classDeclaration()); + for (Java17Parser.ClassOrInterfaceContext member : srcfile.classOrInterface()) { + ClassorinterfacedeclContext clsoif; + if (member instanceof NoclassorinterfaceContext) { + continue; + } else { + clsoif = new ClassorinterfacedeclContext(member); } - else{ - newClass = convertInterface(typeDecl.interfaceDeclaration()); + ClassOrInterface newClass; + int modifiers = 0; + if (!clsoif.classOrInterfaceModifier().isEmpty()) { + for (Java17Parser.ClassOrInterfaceModifierContext mod : clsoif.classOrInterfaceModifier()) { + int newModifier = convert(mod); + 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()); + } else { + newClass = convertInterface(clsoif.interfaceDeclaration()); } classes.add(newClass); } @@ -94,175 +123,172 @@ public class SyntaxTreeGenerator{ } private String convert(Java17Parser.PackageDeclarationContext packageDeclarationContext) { - String ret = ""; - for(TerminalNode identifier : packageDeclarationContext.Identifier()){ - ret += identifier.getText()+"."; - } - ret = ret.substring(0, ret.length()-1); - return ret; + return convertQualifiedName(packageDeclarationContext.qualifiedName()); } - public Method convert(Java17Parser.MethodDeclarationContext methodDeclarationContext, JavaClassName parentClass, RefType superClass, GenericsRegistry generics) { + public Method convert(Java17Parser.MethodDeclarationContext methodDeclarationContext, JavaClassName parentClass, + RefType superClass, GenericsRegistry generics) { Java17Parser.MethodHeaderContext header = methodDeclarationContext.methodHeader(); int modifiers = SyntaxTreeGenerator.convert(methodDeclarationContext.methodModifier()); GenericsRegistry localGenerics = createGenerics(methodDeclarationContext.methodHeader().typeParameters(), - parentClass, header.methodDeclarator().Identifier().getText(), reg, generics); + parentClass, header.methodDeclarator().Identifier().getText(), reg, generics); localGenerics.putAll(generics); - return convert(modifiers, header, methodDeclarationContext.methodBody(),parentClass, superClass, localGenerics); + return convert(modifiers, header, methodDeclarationContext.methodBody(), parentClass, superClass, localGenerics); } - public Method convert(Java17Parser.InterfaceMethodDeclarationContext ctx, JavaClassName parentClass, RefType superClass, GenericsRegistry generics) { + public Method convert(Java17Parser.InterfaceMethodDeclarationContext ctx, JavaClassName parentClass, + RefType superClass, GenericsRegistry generics) { Java17Parser.MethodHeaderContext header = ctx.methodHeader(); int modifiers = SyntaxTreeGenerator.convertInterfaceModifier(ctx.interfaceMethodModifier()); - GenericsRegistry localGenerics = createGenerics(header.typeParameters(), parentClass, header.methodDeclarator().Identifier().getText(), reg, generics); + GenericsRegistry localGenerics = createGenerics(header.typeParameters(), parentClass, + header.methodDeclarator().Identifier().getText(), reg, generics); localGenerics.putAll(generics); - return convert(modifiers, header, ctx.methodBody(),parentClass, superClass, localGenerics); + return convert(modifiers, header, ctx.methodBody(), parentClass, superClass, localGenerics); } private Method convert(int modifiers, Java17Parser.MethodHeaderContext header, Java17Parser.MethodBodyContext body, - JavaClassName parentClass, RefType superClass, GenericsRegistry localGenerics) { + JavaClassName parentClass, RefType superClass, GenericsRegistry localGenerics) { StatementGenerator stmtGen = new StatementGenerator(reg, localGenerics, fields, new HashMap<>()); String name = header.methodDeclarator().Identifier().getText(); GenericDeclarationList gtvDeclarations = new GenericDeclarationList(new ArrayList<>(), header.getStart()); - if(header.typeParameters() != null){ + if (header.typeParameters() != null) { gtvDeclarations = TypeGenerator.convert(header.typeParameters(), parentClass, name, reg, localGenerics); - }else{ + } else { gtvDeclarations = new GenericDeclarationList(new ArrayList<>(), header.getStart()); } RefTypeOrTPHOrWildcardOrGeneric retType; - if(header.result() != null){ - if(header.result().unannType() != null){ + if (header.result() != null) { + if (header.result().unannType() != null) { retType = TypeGenerator.convert(header.result().unannType(), reg, localGenerics); - } - else retType = new de.dhbwstuttgart.syntaxtree.type.Void(header.result().getStart()); - }else{ + } else + retType = new de.dhbwstuttgart.syntaxtree.type.Void(header.result().getStart()); + } else { retType = TypePlaceholder.fresh(header.getStart()); } ParameterList parameterList = stmtGen.convert(header.methodDeclarator().formalParameterList()); Block block = null; - if(body.block() == null){ - if(! Modifier.isAbstract(modifiers)){ - //TODO: Error! Abstrakte Methode ohne abstrakt Keyword + if (body.block() == null) { + if (!Modifier.isAbstract(modifiers)) { + // TODO: Error! Abstrakte Methode ohne abstrakt Keyword } - }else{ - block = stmtGen.convert(body.block(),true); + } else { + block = stmtGen.convert(body.block(), true); } - if(parentClass.equals(new JavaClassName(name))){ - return new Constructor(modifiers, name, retType, parameterList, block, gtvDeclarations, header.getStart() /*, fieldInitializations geloescht PL 2018-11-24 */); - }else{ - return new Method(modifiers, name, retType, parameterList,block, gtvDeclarations, header.getStart()); + if (parentClass.equals(new JavaClassName(name))) { + return new Constructor(modifiers, name, retType, parameterList, block, gtvDeclarations, header.getStart() /* + * , + * fieldInitializations + * geloescht + * PL + * 2018- + * 11-24 + */); + } else { + return new Method(modifiers, name, retType, parameterList, block, gtvDeclarations, header.getStart()); } } - private ClassOrInterface convertClass(Java17Parser.ClassDeclarationContext ctx) { - ClassOrInterface newClass; - fieldInitializations = new ArrayList<>(); //PL 2019-10-22: muss für jede Klasse neu initilisiert werden - if(ctx.normalClassDeclaration() != null){ - newClass = convertNormal(ctx.normalClassDeclaration()); + private ClassOrInterface convertClass(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()); } - else{ - newClass = convertEnum(ctx.enumDeclaration()); - } - return newClass; - } - - private ClassOrInterface convertNormal(Java17Parser.NormalClassDeclarationContext ctx) { - int modifiers = 0; - if(ctx.classModifier() != null){ - for(Java17Parser.ClassModifierContext mod : ctx.classModifier()){ - int newModifier = convert(mod); - modifiers += newModifier; - } - } - 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.typeParameters(), name, "", reg, new GenericsRegistry(globalGenerics)); + GenericsRegistry generics = createGenerics(ctx.genericDeclarationList(), name, "", reg, + new GenericsRegistry(globalGenerics)); Token offset = ctx.getStart(); GenericDeclarationList genericClassParameters; - if(ctx.typeParameters() == null){ - genericClassParameters = createEmptyGenericDeclarationList(ctx.Identifier()); - }else{ - genericClassParameters = TypeGenerator.convert(ctx.typeParameters(), name, "",reg, generics); + if (ctx.genericDeclarationList() == null) { + genericClassParameters = createEmptyGenericDeclarationList(ctx.identifier().getStop()); + } else { + genericClassParameters = TypeGenerator.convert(ctx.genericDeclarationList(), name, "", reg, generics); } - RefType superClass ; - if(ctx.superclass() != null){ - superClass = convert(ctx.superclass()); - }else{ + RefType superClass; + if (ctx.EXTENDS() != null) { + superClass = convert(ctx.typeType()); + } else { superClass = new RefType(ASTFactory.createObjectClass().getClassName(), ctx.getStart()); } List fielddecl = convertFields(ctx.classBody(), generics); - //fieldInitializations = generateFieldInitializations(ctx.classBody(), generics); + // fieldInitializations = generateFieldInitializations(ctx.classBody(), + // generics); List methodsAndConstructors = convertMethods(ctx.classBody(), name, superClass, generics); List methods = new ArrayList<>(); List konstruktoren = new ArrayList<>(); - //int noOfMethods = methods.size(); - for(Method m : methodsAndConstructors){ - if(m instanceof Constructor){ + // int noOfMethods = methods.size(); + for (Method m : methodsAndConstructors) { + if (m instanceof Constructor) { konstruktoren.add((Constructor) m); - } - else { - methods.add(m); + } else { + methods.add(m); } } - if(konstruktoren.size()<1){//Standardkonstruktor anfügen: + if (konstruktoren.size() < 1) {// Standardkonstruktor anfügen: konstruktoren.add( - generateStandardConstructor( - ctx.Identifier().getText(), name, superClass, - genericClassParameters, offset) - ); + generateStandardConstructor( + ctx.Identifier().getText(), name, superClass, + genericClassParameters, offset)); } Boolean isInterface = false; List 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); + + 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 List generateFieldInitializations(Java17Parser.ClassBodyContext classBodyContext, GenericsRegistry generics) { - List ret = new ArrayList<>(); - for(Java17Parser.ClassBodyDeclarationContext classMember : classBodyContext.classBodyDeclaration()){ - if(classMember.classMemberDeclaration() != null){ - Java17Parser.ClassMemberDeclarationContext classMemberDeclarationContext = classMember.classMemberDeclaration(); - if(classMemberDeclarationContext.fieldDeclaration() != null - && classMemberDeclarationContext.fieldDeclaration().variableDeclaratorList() != null){ - for(Java17Parser.VariableDeclaratorContext ctx : classMemberDeclarationContext.fieldDeclaration().variableDeclaratorList().variableDeclarator()) { - String fieldName = ctx.variableDeclaratorId().Identifier().getText(); - if(ctx.variableDeclaratorId().dims() != null)throw new NotImplementedException(); - Token offset = ctx.getStart(); - RefTypeOrTPHOrWildcardOrGeneric fieldType; - for(Field f : fields) - AssignToField leftSide = new AssignToField(new FieldVar(new This(offset), fieldName, )); - ret.addAll(); - } - }else if(classMemberDeclarationContext.methodDeclaration()!= null){ - //Do nothing! - } - } - } - return ret; - } -*/ + * private List + * generateFieldInitializations(Java17Parser.ClassBodyContext classBodyContext, + * GenericsRegistry generics) { + * List ret = new ArrayList<>(); + * for(Java17Parser.ClassBodyDeclarationContext classMember : + * classBodyContext.classBodyDeclaration()){ + * if(classMember.classMemberDeclaration() != null){ + * Java17Parser.ClassMemberDeclarationContext classMemberDeclarationContext = + * classMember.classMemberDeclaration(); + * if(classMemberDeclarationContext.fieldDeclaration() != null + * && classMemberDeclarationContext.fieldDeclaration().variableDeclaratorList() + * != null){ + * for(Java17Parser.VariableDeclaratorContext ctx : + * classMemberDeclarationContext.fieldDeclaration().variableDeclaratorList(). + * variableDeclarator()) { + * String fieldName = ctx.variableDeclaratorId().Identifier().getText(); + * if(ctx.variableDeclaratorId().dims() != null)throw new + * NotImplementedException(); + * Token offset = ctx.getStart(); + * RefTypeOrTPHOrWildcardOrGeneric fieldType; + * for(Field f : fields) + * AssignToField leftSide = new AssignToField(new FieldVar(new This(offset), + * fieldName, )); + * ret.addAll(); + * } + * }else if(classMemberDeclarationContext.methodDeclaration()!= null){ + * //Do nothing! + * } + * } + * } + * return ret; + * } + */ private List convert(Java17Parser.SuperinterfacesContext ctx, GenericsRegistry generics) { - if(ctx == null)return new ArrayList<>(); + if (ctx == null) + return new ArrayList<>(); return convert(ctx.interfaceTypeList(), generics); } private List convert(Java17Parser.InterfaceTypeListContext ctx, GenericsRegistry generics) { List ret = new ArrayList<>(); - for(Java17Parser.InterfaceTypeContext interfaceType : ctx.interfaceType()){ + for (Java17Parser.InterfaceTypeContext interfaceType : ctx.interfaceType()) { ret.add((RefType) TypeGenerator.convert(interfaceType.classType(), reg, generics)); } return ret; @@ -271,44 +297,60 @@ public class SyntaxTreeGenerator{ /** * http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.8.9 */ - private Constructor generateStandardConstructor(String className, JavaClassName parentClass, RefType superClass, GenericDeclarationList classGenerics, Token offset){ + private Constructor generateStandardConstructor(String className, JavaClassName parentClass, RefType superClass, + GenericDeclarationList classGenerics, Token offset) { RefType classType = ClassOrInterface.generateTypeOfClass(reg.getName(className), classGenerics, offset); ParameterList params = new ParameterList(new ArrayList<>(), offset); Block block = new Block(new ArrayList<>(), offset); - return new Constructor(Modifier.PUBLIC, className, classType, params, block, classGenerics, offset /*, fieldInitializations geloescht PL 2018-11-24 */); + return new Constructor(Modifier.PUBLIC, className, classType, params, block, classGenerics, offset /* + * , + * fieldInitializations + * geloescht PL + * 2018-11-24 + */); + } + + /* + * fieldInitializations werden in einem Psedokonstruktor in der abstrakten + * Syntax gespeichert + */ + private Constructor generatePseudoConstructor(String className, JavaClassName parentClass, RefType superClass, + GenericDeclarationList classGenerics, Token offset) { + RefType classType = ClassOrInterface.generateTypeOfClass(reg.getName(className), classGenerics, offset); + ParameterList params = new ParameterList(new ArrayList<>(), offset); + Block block = new Block(new ArrayList<>(fieldInitializations), offset); + return new Constructor(Modifier.PUBLIC, className, classType, params, block, classGenerics, offset /* + * , + * fieldInitializations + * geloescht PL + * 2018-11-24 + */); } - - /* fieldInitializations werden in einem Psedokonstruktor in der abstrakten Syntax gespeichert */ - private Constructor generatePseudoConstructor(String className, JavaClassName parentClass, RefType superClass, GenericDeclarationList classGenerics, Token offset){ - RefType classType = ClassOrInterface.generateTypeOfClass(reg.getName(className), classGenerics, offset); - ParameterList params = new ParameterList(new ArrayList<>(), offset); - Block block = new Block(new ArrayList<>(fieldInitializations), offset); - return new Constructor(Modifier.PUBLIC, className, classType, params, block, classGenerics, offset /*, fieldInitializations geloescht PL 2018-11-24 */); - } private RefType convert(Java17Parser.SuperclassContext superclass) { - if(superclass.classType().classOrInterfaceType() != null){ + if (superclass.classType().classOrInterfaceType() != null) { throw new NotImplementedException(); - }else{ - RefTypeOrTPHOrWildcardOrGeneric ret = TypeGenerator.convertTypeName(superclass.classType().Identifier().getText(), superclass.classType().typeArguments(), - superclass.getStart(), reg, globalGenerics); - if(ret instanceof RefType){ + } else { + RefTypeOrTPHOrWildcardOrGeneric ret = TypeGenerator.convertTypeName(superclass.classType().Identifier().getText(), + superclass.classType().typeArguments(), + superclass.getStart(), reg, globalGenerics); + if (ret instanceof RefType) { return (RefType) ret; - }else{ + } else { throw new TypeinferenceException(superclass.getText() + " ist kein gültiger Supertyp", superclass.getStart()); } } } private List convertMethods(Java17Parser.ClassBodyContext classBodyContext, - JavaClassName parentClass, RefType superClass, GenericsRegistry generics) { + JavaClassName parentClass, RefType superClass, GenericsRegistry generics) { List ret = new ArrayList<>(); - for(Java17Parser.ClassBodyDeclarationContext classMember : classBodyContext.classBodyDeclaration()){ - if(classMember.classMemberDeclaration() != null){ + for (Java17Parser.ClassBodyDeclarationContext classMember : classBodyContext.classBodyDeclaration()) { + if (classMember.classMemberDeclaration() != null) { Java17Parser.ClassMemberDeclarationContext classMemberDeclarationContext = classMember.classMemberDeclaration(); - if(classMemberDeclarationContext.fieldDeclaration() != null){ - //Do nothing! - }else if(classMemberDeclarationContext.methodDeclaration()!= null){ + if (classMemberDeclarationContext.fieldDeclaration() != null) { + // Do nothing! + } else if (classMemberDeclarationContext.methodDeclaration() != null) { ret.add(this.convert(classMemberDeclarationContext.methodDeclaration(), parentClass, superClass, generics)); } @@ -319,13 +361,13 @@ public class SyntaxTreeGenerator{ private List convertFields(Java17Parser.ClassBodyContext classBodyContext, GenericsRegistry generics) { List ret = new ArrayList<>(); - for(Java17Parser.ClassBodyDeclarationContext classMember : classBodyContext.classBodyDeclaration()){ - if(classMember.classMemberDeclaration() != null){ + for (Java17Parser.ClassBodyDeclarationContext classMember : classBodyContext.classBodyDeclaration()) { + if (classMember.classMemberDeclaration() != null) { Java17Parser.ClassMemberDeclarationContext classMemberDeclarationContext = classMember.classMemberDeclaration(); - if(classMemberDeclarationContext.fieldDeclaration() != null){ + if (classMemberDeclarationContext.fieldDeclaration() != null) { ret.addAll(convert(classMember.classMemberDeclaration().fieldDeclaration(), generics)); - }else if(classMemberDeclarationContext.methodDeclaration()!= null){ - //Do nothing! + } else if (classMemberDeclarationContext.methodDeclaration() != null) { + // Do nothing! } } } @@ -334,40 +376,49 @@ public class SyntaxTreeGenerator{ public static int convert(List methodModifierContexts) { int ret = 0; - for(Java17Parser.MethodModifierContext mod : methodModifierContexts){ - if(mod.annotation() == null)convertModifier(mod.getText()); + for (Java17Parser.MethodModifierContext mod : methodModifierContexts) { + if (mod.annotation() == null) + convertModifier(mod.getText()); } return ret; } public static int convertInterfaceModifier(List methodModifierContexts) { int ret = 0; - for(Java17Parser.InterfaceMethodModifierContext mod : methodModifierContexts){ - if(mod.annotation() == null)convertModifier(mod.getText()); + for (Java17Parser.InterfaceMethodModifierContext mod : methodModifierContexts) { + if (mod.annotation() == null) + convertModifier(mod.getText()); } return ret; } - private List convert(Java17Parser.FieldDeclarationContext fieldDeclarationContext, GenericsRegistry generics) { + private List convert(Java17Parser.FieldDeclarationContext fieldDeclarationContext, + GenericsRegistry generics) { List ret = new ArrayList<>(); int modifiers = 0; - for(Java17Parser.FieldModifierContext fieldModifierContext : fieldDeclarationContext.fieldModifier()){ - modifiers+=(convert(fieldModifierContext)); + for (Java17Parser.FieldModifierContext fieldModifierContext : fieldDeclarationContext.fieldModifier()) { + modifiers += (convert(fieldModifierContext)); } RefTypeOrTPHOrWildcardOrGeneric fieldType; - if(fieldDeclarationContext.unannTypeOrAuto() != null - && fieldDeclarationContext.unannTypeOrAuto().unannType() != null){ + if (fieldDeclarationContext.unannTypeOrAuto() != null + && fieldDeclarationContext.unannTypeOrAuto().unannType() != null) { fieldType = TypeGenerator.convert(fieldDeclarationContext.unannTypeOrAuto().unannType(), reg, generics); - }else{ - fieldType = TypePlaceholder.fresh(fieldDeclarationContext.variableDeclaratorList().getStart()); //PL 2019-12-06: variableDeclaratorList() eingefuegt, um als Token nicht die Modifier zu bekommen + } else { + fieldType = TypePlaceholder.fresh(fieldDeclarationContext.variableDeclaratorList().getStart()); // PL 2019-12-06: + // variableDeclaratorList() + // eingefuegt, um + // als Token nicht + // die Modifier zu + // bekommen } - for(Java17Parser.VariableDeclaratorContext varCtx : fieldDeclarationContext.variableDeclaratorList().variableDeclarator()){ + for (Java17Parser.VariableDeclaratorContext varCtx : fieldDeclarationContext.variableDeclaratorList() + .variableDeclarator()) { String fieldName = convert(varCtx.variableDeclaratorId()); fields.put(fieldName, fieldType); - if(varCtx.variableInitializer() != null){ + if (varCtx.variableInitializer() != null) { initializeField(varCtx, fieldType, generics); } - ret.add(new Field(fieldName,fieldType,modifiers,varCtx.getStart())); + ret.add(new Field(fieldName, fieldType, modifiers, varCtx.getStart())); } return ret; } @@ -377,12 +428,13 @@ public class SyntaxTreeGenerator{ } // Initialize a field by creating implicit constructor. - private void initializeField(Java17Parser.VariableDeclaratorContext ctx, RefTypeOrTPHOrWildcardOrGeneric typeOfField, GenericsRegistry generics){ + private void initializeField(Java17Parser.VariableDeclaratorContext ctx, RefTypeOrTPHOrWildcardOrGeneric typeOfField, + GenericsRegistry generics) { StatementGenerator statementGenerator = new StatementGenerator(reg, generics, fields, new HashMap<>()); fieldInitializations.add(statementGenerator.generateFieldAssignment(ctx, typeOfField)); } - public static int convertModifier(String modifier){ + public static int convertModifier(String modifier) { HashMap modifiers = new HashMap<>(); modifiers.put(Modifier.toString(Modifier.PUBLIC), Modifier.PUBLIC); modifiers.put(Modifier.toString(Modifier.PRIVATE), Modifier.PRIVATE); @@ -397,57 +449,57 @@ public class SyntaxTreeGenerator{ modifiers.put(Modifier.toString(Modifier.NATIVE), Modifier.NATIVE); modifiers.put(Modifier.toString(Modifier.INTERFACE), Modifier.INTERFACE); int ret = 0; - for(String m : modifiers.keySet()){ - if(modifier.contains(m))ret+=modifiers.get(m); + for (String m : modifiers.keySet()) { + if (modifier.contains(m)) + ret += modifiers.get(m); } return ret; } - private int convert(Java17Parser.ClassModifierContext ctx){ - if(ctx.annotation() != null)return 0; - return convertModifier(ctx.getText()); - } - - private int convert(Java17Parser.FieldModifierContext ctx){ - if(ctx.annotation() != null)return 0; + private int convert(Java17Parser.ClassOrInterfaceModifierContext ctx) { + if (ctx.annotation() != null) + return 0; return convertModifier(ctx.getText()); } - private int convert(Java17Parser.InterfaceModifierContext ctx) { - if(ctx.annotation() != null)return 0; + private int convert(Java17Parser.FieldModifierContext ctx) { + if (ctx.annotation() != null) + return 0; return convertModifier(ctx.getText()); } - - private ClassOrInterface convertEnum(Java17Parser.EnumDeclarationContext ctx){ + + private ClassOrInterface convertEnum(Java17Parser.EnumDeclarationContext ctx) { return null; } - - private ClassOrInterface convertInterface(Java17Parser.InterfaceDeclarationContext ctx){ - if(ctx.normalInterfaceDeclaration() != null){ + + private ClassOrInterface convertInterface(Java17Parser.InterfaceDeclarationContext ctx) { + if (ctx.normalInterfaceDeclaration() != null) { return convertNormal(ctx.normalInterfaceDeclaration()); - }else{ + } else { throw new NotImplementedException(); } } private ClassOrInterface convertNormal(Java17Parser.NormalInterfaceDeclarationContext ctx) { int modifiers = 0; - if(ctx.interfaceModifier() != null){ - for( Java17Parser.InterfaceModifierContext mod : ctx.interfaceModifier()){ + if (ctx.interfaceModifier() != null) { + for (Java17Parser.InterfaceModifierContext mod : ctx.interfaceModifier()) { int newModifier = convert(mod); modifiers += newModifier; } } - if(!Modifier.isInterface(modifiers))modifiers += Modifier.INTERFACE; - + if (!Modifier.isInterface(modifiers)) + modifiers += Modifier.INTERFACE; + JavaClassName name = reg.getName(ctx.Identifier().getText()); - GenericsRegistry generics = createGenerics(ctx.typeParameters(), name, "", reg, new GenericsRegistry(globalGenerics)); + GenericsRegistry generics = createGenerics(ctx.typeParameters(), name, "", reg, + new GenericsRegistry(globalGenerics)); GenericDeclarationList genericParams; - if(ctx.typeParameters() != null){ - genericParams = TypeGenerator.convert(ctx.typeParameters(), name, "",reg, generics); - }else{ + if (ctx.typeParameters() != null) { + genericParams = TypeGenerator.convert(ctx.typeParameters(), name, "", reg, generics); + } else { genericParams = createEmptyGenericDeclarationList(ctx.Identifier()); } RefType superClass = ASTFactory.createObjectType(); @@ -458,42 +510,45 @@ public class SyntaxTreeGenerator{ List extendedInterfaces = convert(ctx.extendsInterfaces(), generics); return new ClassOrInterface(modifiers, name, fields, Optional.empty(), methods, new ArrayList<>(), - genericParams, superClass, true, extendedInterfaces, ctx.getStart()); + genericParams, superClass, true, extendedInterfaces, ctx.getStart()); } - private GenericDeclarationList createEmptyGenericDeclarationList(TerminalNode classNameIdentifier) { - CommonToken gtvOffset = new CommonToken(classNameIdentifier.getSymbol()); - gtvOffset.setCharPositionInLine(gtvOffset.getCharPositionInLine()+classNameIdentifier.getText().length()); - gtvOffset.setStartIndex(gtvOffset.getStopIndex()+1); + private GenericDeclarationList createEmptyGenericDeclarationList(Token classNameIdentifier) { + CommonToken gtvOffset = new CommonToken(classNameIdentifier); + gtvOffset.setCharPositionInLine(gtvOffset.getCharPositionInLine() + classNameIdentifier.getText().length()); + gtvOffset.setStartIndex(gtvOffset.getStopIndex() + 1); return new GenericDeclarationList(new ArrayList<>(), gtvOffset); } - private GenericsRegistry createGenerics(Java17Parser.TypeParametersContext ctx, JavaClassName parentClass, String parentMethod, JavaClassRegistry reg, GenericsRegistry generics) { + 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 || ctx.typeParameterList() == null)return ret; - for(Java17Parser.TypeParameterContext tp : ctx.typeParameterList().typeParameter()){ - ret.put(tp.Identifier().getText(), new GenericContext(parentClass, parentMethod)); - } - for(Java17Parser.TypeParameterContext tp : ctx.typeParameterList().typeParameter()){ + if (ctx == null) + return ret; + for (Java17Parser.GenericTypeVarContext tp : ctx.genericTypeVar()) { + ret.put(tp.identifier().getText(), new GenericContext(parentClass, parentMethod)); TypeGenerator.convert(tp, parentClass, parentMethod, reg, ret); } return ret; } - private List convert(Java17Parser.ExtendsInterfacesContext extendsInterfacesContext, GenericsRegistry generics) { - if(extendsInterfacesContext == null)return new ArrayList<>(); + private List convert(Java17Parser.ExtendsInterfacesContext extendsInterfacesContext, + GenericsRegistry generics) { + if (extendsInterfacesContext == null) + return new ArrayList<>(); return convert(extendsInterfacesContext.interfaceTypeList(), generics); } private List convertMethods(Java17Parser.InterfaceBodyContext interfaceBodyContext, - JavaClassName parentClass, RefType superClass, GenericsRegistry generics) { + JavaClassName parentClass, RefType superClass, GenericsRegistry generics) { List ret = new ArrayList<>(); - for(Java17Parser.InterfaceMemberDeclarationContext member : interfaceBodyContext.interfaceMemberDeclaration()){ - if(member.interfaceMethodDeclaration() != null){ + for (Java17Parser.InterfaceMemberDeclarationContext member : interfaceBodyContext.interfaceMemberDeclaration()) { + if (member.interfaceMethodDeclaration() != null) { ret.add(this.convert(member.interfaceMethodDeclaration(), parentClass, superClass, generics)); - //new Method(name, type, modifier, params, null, genericDecls, member.interfaceMethodDeclaration().getStart()); - }else{ + // new Method(name, type, modifier, params, null, genericDecls, + // member.interfaceMethodDeclaration().getStart()); + } else { throw new NotImplementedException(); } } @@ -502,14 +557,13 @@ public class SyntaxTreeGenerator{ private List convertFields(Java17Parser.InterfaceBodyContext interfaceBodyContext) { List ret = new ArrayList<>(); - for(Java17Parser.InterfaceMemberDeclarationContext member : interfaceBodyContext.interfaceMemberDeclaration()){ - if(member.constantDeclaration() != null){ - //TODO: Erstelle hier ein Feld! + for (Java17Parser.InterfaceMemberDeclarationContext member : interfaceBodyContext.interfaceMemberDeclaration()) { + if (member.constantDeclaration() != null) { + // TODO: Erstelle hier ein Feld! throw new NotImplementedException(); } } return ret; } - } diff --git a/src/main/java/de/dhbwstuttgart/parser/scope/GatherNames.java b/src/main/java/de/dhbwstuttgart/parser/scope/GatherNames.java index 9cfdc9c3..38fc3543 100644 --- a/src/main/java/de/dhbwstuttgart/parser/scope/GatherNames.java +++ b/src/main/java/de/dhbwstuttgart/parser/scope/GatherNames.java @@ -4,7 +4,7 @@ import java.net.URL; import java.net.URLClassLoader; import java.util.*; -import de.dhbwstuttgart.parser.antlr.Java17BaseListener; +import de.dhbwstuttgart.parser.antlr.Java17ParserBaseVisitor; import de.dhbwstuttgart.syntaxtree.AbstractASTWalker; import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor; import org.antlr.v4.runtime.tree.ParseTreeWalker; @@ -15,85 +15,99 @@ import de.dhbwstuttgart.parser.antlr.Java17Parser; public class GatherNames { - public static Map getNames(Java17Parser.CompilationUnitContext ctx, PackageCrawler packages, ClassLoader classLoader) throws ClassNotFoundException{ - Map ret = new HashMap<>(); - String pkgName = getPackageName(ctx); - String nameString = ""; - for (Java17Parser.TypeDeclarationContext typeDecl : ctx.typeDeclaration()){ - if(typeDecl.interfaceDeclaration() != null){ - if(typeDecl.interfaceDeclaration().normalInterfaceDeclaration() != null){ - if(pkgName != ""){ - nameString = pkgName + "." + typeDecl.interfaceDeclaration().normalInterfaceDeclaration().Identifier().toString(); - } - else{ - nameString = typeDecl.interfaceDeclaration().normalInterfaceDeclaration().Identifier().toString(); - } - int numGenerics = typeDecl.interfaceDeclaration().normalInterfaceDeclaration().typeParameters()!=null? - typeDecl.interfaceDeclaration().normalInterfaceDeclaration().typeParameters().typeParameterList().typeParameter().size():0; - //Die Generic TypeParameter Definitionen Nicht! an die JavaClassName-Registry anfügen: - /* //Diese gelängen dadurch in den globalen Scope, was sie schließlich nicht sind - if(typeDecl.classDeclaration().normalClassDeclaration().typeParameters() != null){ - for(Java17Parser.TypeParameterContext tp : typeDecl.classDeclaration().normalClassDeclaration().typeParameters().typeParameterList().typeParameter()){ - //this.reg.add(tp.Identifier().toString()); - } - } - */ - ret.put(nameString, numGenerics); - } - } - else{ - if(typeDecl.classDeclaration().normalClassDeclaration() != null){ - if(!pkgName.isEmpty()){ - nameString = pkgName + "." + typeDecl.classDeclaration().normalClassDeclaration().Identifier().toString(); - } - else{ - nameString = typeDecl.classDeclaration().normalClassDeclaration().Identifier().toString(); - } - //Die Generic TypeParameter Definitionen Nicht! an die JavaClassName-Registry anfügen: - /* //Diese gelängen dadurch in den globalen Scope, was sie schließlich nicht sind - if(typeDecl.classDeclaration().normalClassDeclaration().typeParameters() != null){ - for(Java17Parser.TypeParameterContext tp : typeDecl.classDeclaration().normalClassDeclaration().typeParameters().typeParameterList().typeParameter()){ - this.reg.add(tp.Identifier().toString()); - } - } - */ - int numGenerics = typeDecl.classDeclaration().normalClassDeclaration().typeParameters()!=null? - typeDecl.classDeclaration().normalClassDeclaration().typeParameters().typeParameterList().typeParameter().size():0; + public static Map getNames(Java17Parser.SourceFileContext ctx, PackageCrawler packages, + ClassLoader classLoader) throws ClassNotFoundException { + Map ret = new HashMap<>(); + String pkgName = getPackageName(ctx); + String nameString = ""; + for (Java17Parser.TypeDeclarationContext typeDecl : ctx.typeDeclaration()) { + if (typeDecl.interfaceDeclaration() != null) { + if (typeDecl.interfaceDeclaration().normalInterfaceDeclaration() != null) { + if (pkgName != "") { + nameString = pkgName + "." + + typeDecl.interfaceDeclaration().normalInterfaceDeclaration().Identifier().toString(); + } else { + nameString = typeDecl.interfaceDeclaration().normalInterfaceDeclaration().Identifier().toString(); + } + int numGenerics = typeDecl.interfaceDeclaration().normalInterfaceDeclaration().typeParameters() != null + ? typeDecl.interfaceDeclaration().normalInterfaceDeclaration().typeParameters().typeParameterList() + .typeParameter().size() + : 0; + // Die Generic TypeParameter Definitionen Nicht! an die JavaClassName-Registry + // anfügen: + /* + * //Diese gelängen dadurch in den globalen Scope, was sie schließlich nicht + * sind + * if(typeDecl.classDeclaration().normalClassDeclaration().typeParameters() != + * null){ + * for(Java17Parser.TypeParameterContext tp : + * typeDecl.classDeclaration().normalClassDeclaration().typeParameters(). + * typeParameterList().typeParameter()){ + * //this.reg.add(tp.Identifier().toString()); + * } + * } + */ + ret.put(nameString, numGenerics); + } + } else { + if (typeDecl.classDeclaration().normalClassDeclaration() != null) { + if (!pkgName.isEmpty()) { + nameString = pkgName + "." + typeDecl.classDeclaration().normalClassDeclaration().Identifier().toString(); + } else { + nameString = typeDecl.classDeclaration().normalClassDeclaration().Identifier().toString(); + } + // Die Generic TypeParameter Definitionen Nicht! an die JavaClassName-Registry + // anfügen: + /* + * //Diese gelängen dadurch in den globalen Scope, was sie schließlich nicht + * sind + * if(typeDecl.classDeclaration().normalClassDeclaration().typeParameters() != + * null){ + * for(Java17Parser.TypeParameterContext tp : + * typeDecl.classDeclaration().normalClassDeclaration().typeParameters(). + * typeParameterList().typeParameter()){ + * this.reg.add(tp.Identifier().toString()); + * } + * } + */ + int numGenerics = typeDecl.classDeclaration().normalClassDeclaration().typeParameters() != null + ? typeDecl.classDeclaration().normalClassDeclaration().typeParameters().typeParameterList() + .typeParameter().size() + : 0; - ret.put(nameString, numGenerics); - } - } - } - ret.putAll(getImports(ctx, packages, classLoader)); - return ret; - } + ret.put(nameString, numGenerics); + } + } + } + ret.putAll(getImports(ctx, packages, classLoader)); + return ret; + } - public static Map getImports(Java17Parser.CompilationUnitContext ctx, PackageCrawler packages, ClassLoader classLoader) throws ClassNotFoundException { - Map ret = new HashMap<>(); - //ret.putAll(packages.getClassNames("java.lang")); - for(Java17Parser.ImportDeclarationContext importDeclCtx : ctx.importDeclaration()){ - if(importDeclCtx.singleTypeImportDeclaration() != null){ - Class cl = classLoader.loadClass(importDeclCtx.singleTypeImportDeclaration().typeName().getText()); - ret.put(cl.getName(), cl.getTypeParameters().length); - } - else if(importDeclCtx.typeImportOnDemandDeclaration() != null){ - ret.putAll(packages.getClassNames(importDeclCtx.typeImportOnDemandDeclaration().packageOrTypeName().getText())); - } - else if(importDeclCtx.singleStaticImportDeclaration() != null){ - Class cl = classLoader.loadClass(importDeclCtx.singleStaticImportDeclaration().typeName().getText()+"."+importDeclCtx.singleStaticImportDeclaration().Identifier().getText()); - ret.put(cl.getName(), cl.getTypeParameters().length); - } - else{ - ret.putAll(packages.getClassNames(importDeclCtx.staticImportOnDemandDeclaration().typeName().getText())); - } - } - return ret; - } + public static Map getImports(Java17Parser.SourceFileContext ctx, PackageCrawler packages, + ClassLoader classLoader) throws ClassNotFoundException { + Map ret = new HashMap<>(); + // ret.putAll(packages.getClassNames("java.lang")); + for (Java17Parser.ImportDeclarationContext importDeclCtx : ctx.importDeclaration()) { + if (importDeclCtx.singleTypeImportDeclaration() != null) { + Class cl = classLoader.loadClass(importDeclCtx.singleTypeImportDeclaration().typeName().getText()); + ret.put(cl.getName(), cl.getTypeParameters().length); + } else if (importDeclCtx.typeImportOnDemandDeclaration() != null) { + ret.putAll(packages.getClassNames(importDeclCtx.typeImportOnDemandDeclaration().packageOrTypeName().getText())); + } else if (importDeclCtx.singleStaticImportDeclaration() != null) { + Class cl = classLoader.loadClass(importDeclCtx.singleStaticImportDeclaration().typeName().getText() + "." + + importDeclCtx.singleStaticImportDeclaration().Identifier().getText()); + ret.put(cl.getName(), cl.getTypeParameters().length); + } else { + ret.putAll(packages.getClassNames(importDeclCtx.staticImportOnDemandDeclaration().typeName().getText())); + } + } + return ret; + } - private static String getPackageName(Java17Parser.CompilationUnitContext ctx){ + private static String getPackageName(Java17Parser.CompilationUnitContext ctx) { String pkgName = ""; - if(ctx.packageDeclaration() != null){ - for(TerminalNode t : ctx.packageDeclaration().Identifier()){ + if (ctx.packageDeclaration() != null) { + for (TerminalNode t : ctx.packageDeclaration().Identifier()) { pkgName = pkgName + "." + t.toString(); } pkgName = pkgName.substring(1);