Closed maijun-sec closed 2 months ago
I'am using the following source code to do some test.
The maven configuration:
<dependency> <groupId>org.eclipse.jdt</groupId> <artifactId>org.eclipse.jdt.core</artifactId> <version>3.38.0</version> </dependency>
The source code:
package org.example; import org.eclipse.jdt.core.dom.*; public class App { public static void main( String[] args ) { ASTParser parser = ASTParser.newParser(AST.getJLSLatest()); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setResolveBindings(true); String sourceCode = """ class A { void method1(){ int a = 3; int b = 4; int c = a + b; System.out.println(c); } } """; parser.setSource(sourceCode.toCharArray()); ASTNode cu = parser.createAST(null); System.out.println(cu); ASTVisitor visitor = new ASTVisitor() { @Override public boolean visit(SimpleName node) { System.out.println(node.getIdentifier()); IBinding binding = node.resolveBinding(); if (binding != null) { System.out.println(binding); } return super.visit(node); } }; cu.accept(visitor); } }
However, I can't get the binding at line 30~33. Is there something wrong with my configuration? Thank you very much.
I don't know why, but I fixed the problem by adding the following two lines of code (both lines are required):
parser.setEnvironment(null, null, null, true); parser.setUnitName("A.java");
I'am using the following source code to do some test.
The maven configuration:
The source code:
However, I can't get the binding at line 30~33. Is there something wrong with my configuration? Thank you very much.