diff --git a/src/main/java/de/dhbwstuttgart/parser/scope/GatherNames.java b/src/main/java/de/dhbwstuttgart/parser/scope/GatherNames.java index 31c6d375..757c323b 100644 --- a/src/main/java/de/dhbwstuttgart/parser/scope/GatherNames.java +++ b/src/main/java/de/dhbwstuttgart/parser/scope/GatherNames.java @@ -7,6 +7,7 @@ import java.util.*; import de.dhbwstuttgart.parser.antlr.Java8BaseListener; import de.dhbwstuttgart.syntaxtree.AbstractASTWalker; import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor; +import org.antlr.v4.runtime.tree.ParseTreeWalker; import org.antlr.v4.runtime.tree.TerminalNode; import de.dhbwstuttgart.environment.PackageCrawler; @@ -63,12 +64,56 @@ public class GatherNames { } } } - ret.putAll(getImports(ctx, packages, classLoader)); - return ret; + ret.putAll(getImports(ctx, packages, classLoader)); + ret.putAll(getUsedTypes(ctx, classLoader, ret.keySet())); + return ret; } - - public static Map getImports(Java8Parser.CompilationUnitContext ctx, PackageCrawler packages, ClassLoader classLoader) throws ClassNotFoundException { + private static Map getUsedTypes(Java8Parser.CompilationUnitContext ctx, ClassLoader classLoader, Collection alreadyUsedNames) { + Map ret = new HashMap<>(); + new ParseTreeWalker().walk(new Java8BaseListener(){ + @Override + public void enterUnannClassType(Java8Parser.UnannClassTypeContext ctx) { + addClass(ctx.Identifier().getText()); + } + + @Override + public void enterUnannClassType_lf_unannClassOrInterfaceType(Java8Parser.UnannClassType_lf_unannClassOrInterfaceTypeContext ctx) { + addClass(ctx.Identifier().getText()); + } + + @Override + public void enterUnannClassType_lfno_unannClassOrInterfaceType(Java8Parser.UnannClassType_lfno_unannClassOrInterfaceTypeContext ctx) { + addClass(ctx.Identifier().getText()); + } + + @Override + public void enterClassInstanceCreationExpression(Java8Parser.ClassInstanceCreationExpressionContext ctx) { + addClass(ctx.Identifier(0).getText()); + } + + @Override + public void enterClassInstanceCreationExpression_lf_primary(Java8Parser.ClassInstanceCreationExpression_lf_primaryContext ctx) { + addClass(ctx.Identifier().getText()); + } + + @Override + public void enterClassInstanceCreationExpression_lfno_primary(Java8Parser.ClassInstanceCreationExpression_lfno_primaryContext ctx) { + addClass(ctx.Identifier(0).getText()); + } + + private void addClass(String name) { + try { + Class cl = classLoader.loadClass(name); + ret.put(cl.getName(), cl.getTypeParameters().length); + }catch (ClassNotFoundException e){ + } + } + }, ctx); + return ret; + } + + public static Map getImports(Java8Parser.CompilationUnitContext ctx, PackageCrawler packages, ClassLoader classLoader) throws ClassNotFoundException { Map ret = new HashMap<>(); //ret.putAll(packages.getClassNames("java.lang")); for(Java8Parser.ImportDeclarationContext importDeclCtx : ctx.importDeclaration()){