forked from JavaTX/JavaCompilerCore
Trying to add Names that are not imported to availableClasses (unfinished)
This commit is contained in:
parent
19989bfe11
commit
4b4087ae24
@ -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;
|
||||||
@ -63,12 +64,56 @@ public class GatherNames {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ret.putAll(getImports(ctx, packages, classLoader));
|
ret.putAll(getImports(ctx, packages, classLoader));
|
||||||
return ret;
|
ret.putAll(getUsedTypes(ctx, classLoader, ret.keySet()));
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static Map<String, Integer> getImports(Java8Parser.CompilationUnitContext ctx, PackageCrawler packages, ClassLoader classLoader) throws ClassNotFoundException {
|
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 {
|
||||||
Map<String, Integer> ret = new HashMap<>();
|
Map<String, Integer> ret = new HashMap<>();
|
||||||
//ret.putAll(packages.getClassNames("java.lang"));
|
//ret.putAll(packages.getClassNames("java.lang"));
|
||||||
for(Java8Parser.ImportDeclarationContext importDeclCtx : ctx.importDeclaration()){
|
for(Java8Parser.ImportDeclarationContext importDeclCtx : ctx.importDeclaration()){
|
||||||
|
Loading…
Reference in New Issue
Block a user