在JavaParser中查找Super关键字的类名

huangapple go评论77阅读模式
英文:

Find Class name of Super Keyword in JavaParser

问题

我正在基于JavaParser开发一个Java应用程序。我不知道如何获取方法体中使用的Super关键字所指的类名。例如,我需要知道下面代码中的Super关键字是指类A。

class A {
   public void bar(){...}
}

class B extends A {}

class C extends B {
   void foo(){
      super.bar() // 我想知道super关键字指的是类A。
    }
}

我检查了JavaParser提供的这些函数(1、2和3),但它们都不起作用,都返回null。

MethodCallExpr methodCallExpr = ...
Optional<Expression> scope = methodCallExpr.getScope();
SuperExpr superExp = scope.get().asSuperExpr();
1. superExp.findAll(ClassOrInterfaceDeclaration.class);2. superExp.getTypeName();
3. superExp.getClassExpr();  //我不知道为什么这个方法也返回null
英文:

I am developing a Java application based on JavaParser. I do not know how to get class name of Super keyword used in a method body. As an example, I need to know Super keyword in the below code is referred to class A.

class A { 
   public void bar(){...}
}

class B extends A {}

class C extends B {
   void foo(){
      super.bar() // I want to find that the super keyword is referred to Class A.
    }
}

I checked these functions (1, 2, and 3) provided by JavaParser, but none of them worked, all return null.

MethodCallExpr methodCallExpr = ...
Optional&lt;Expression&gt; scope = methodCallExpr.getScope();
SuperExpr superExp = scope.get().asSuperExpr();
1. superExp.findAll(ClassOrInterfaceDeclaration.class);   and 
2. superExp.getTypeName();
3. superExp.getClassExpr();  //I do not know why this method also returns null

答案1

得分: 3

我找到了正确的方法。

ResolvedType resolvedType = superExp.calculateResolvedType();

如果您还将JavaSymbolSolver添加到解析器配置中,此方法将正常工作。JavaSymbolSolver对于解析引用并查找节点之间关系是必要的。

TypeSolver reflectionTypeSolver = new ReflectionTypeSolver();
TypeSolver javaParserTypeSolver = new JavaParserTypeSolver(projectSourceDir);
CombinedTypeSolver combinedSolver = new CombinedTypeSolver();
combinedSolver.add(reflectionTypeSolver);
combinedSolver.add(javaParserTypeSolver);
		
ParserConfiguration parserConfiguration = new ParserConfiguration()
								         .setSymbolResolver(new JavaSymbolSolver(combinedSolver));
		
SourceRoot sourceRoot = new SourceRoot(projectSourceDir.toPath());
sourceRoot.setParserConfiguration(parserConfiguration);
英文:

I find the right method.

ResolvedType resolvedType = superExp.calculateResolvedType();

This method works correctly if you also add the JavaSymbolSolver to the parser configuration. JavaSymbolSolver is necessary to resolve references and find relations between nodes.

TypeSolver reflectionTypeSolver = new ReflectionTypeSolver();
TypeSolver javaParserTypeSolver = new JavaParserTypeSolver(projectSourceDir);
CombinedTypeSolver combinedSolver = new CombinedTypeSolver();
combinedSolver.add(reflectionTypeSolver);
combinedSolver.add(javaParserTypeSolver);
		
ParserConfiguration parserConfiguration = new ParserConfiguration()
								         .setSymbolResolver(new JavaSymbolSolver(combinedSolver));
		
SourceRoot sourceRoot = new SourceRoot(projectSourceDir.toPath());
sourceRoot.setParserConfiguration(parserConfiguration);

huangapple
  • 本文由 发表于 2020年9月2日 14:46:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63700103.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定