Trying to add Names that are not imported to availableClasses (unfinished)

This commit is contained in:
JanUlrich 2019-12-27 18:51:04 +01:00
parent 19989bfe11
commit 4b4087ae24

View File

@ -7,6 +7,7 @@ import java.util.*;
import de.dhbwstuttgart.parser.antlr.Java8BaseListener; import de.dhbwstuttgart.parser.antlr.Java8BaseListener;
import de.dhbwstuttgart.syntaxtree.AbstractASTWalker; import de.dhbwstuttgart.syntaxtree.AbstractASTWalker;
import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor; import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import org.antlr.v4.runtime.tree.TerminalNode; import org.antlr.v4.runtime.tree.TerminalNode;
import de.dhbwstuttgart.environment.PackageCrawler; import de.dhbwstuttgart.environment.PackageCrawler;
@ -64,9 +65,53 @@ public class GatherNames {
} }
} }
ret.putAll(getImports(ctx, packages, classLoader)); ret.putAll(getImports(ctx, packages, classLoader));
ret.putAll(getUsedTypes(ctx, classLoader, ret.keySet()));
return ret; return ret;
} }
private static Map<String, Integer> getUsedTypes(Java8Parser.CompilationUnitContext ctx, ClassLoader classLoader, Collection<String> alreadyUsedNames) {
Map<String, Integer> 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<String, Integer> getImports(Java8Parser.CompilationUnitContext ctx, PackageCrawler packages, ClassLoader classLoader) throws ClassNotFoundException { public static Map<String, Integer> getImports(Java8Parser.CompilationUnitContext ctx, PackageCrawler packages, ClassLoader classLoader) throws ClassNotFoundException {
Map<String, Integer> ret = new HashMap<>(); Map<String, Integer> ret = new HashMap<>();