You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to add this to the field reference that omits this in the method, but when there is a variable declaration with the same name in the method, the field reference before the variable declaration will be resolved as a variable instead of a field. Main function
public static void main(String[] args) throws IOException {
JavaParser javaParser = new JavaParser();
TypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver());
JavaSymbolSolver symbolSolver = new JavaSymbolSolver(typeSolver);
javaParser.getParserConfiguration().setSymbolResolver(symbolSolver);
javaParser.getParserConfiguration().setLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_17);
ParseResult<CompilationUnit> result = javaParser.parse(Paths.get("This.java"));
ModifierVisitor visit = new ModifierVisitor(){
public Visitable visit(MethodDeclaration n, Void arg) {
List<NameExpr> all = n.findAll(NameExpr.class);
all.stream().filter(nameExpr -> {
try {
if (nameExpr.resolve().isField())
return true;
} catch (UnsolvedSymbolException e){
return false;
}
return false;
}).forEach(nameExpr -> {
Node parent = nameExpr.getParentNode().get();
if(parent instanceof FieldAccessExpr && ((FieldAccessExpr) parent).getScope() instanceof ThisExpr) return;
FieldAccessExpr fieldAccessExpr = NodeFactory.getFieldAccessExpr(new ThisExpr(), nameExpr.getName());
nameExpr.replace(fieldAccessExpr);
});
return super.visit(n, arg);
}
};
visit.visit(result.getResult().get(), null);
System.out.println(result.getResult().get());
}
This.java
public class This {
int x = 5;
void test(){
System.out.println(x++); // x here is field
int x = 0;
System.out.println(x++); // x here is the variable
}
}
output:
public class This {
int x = 5;
void test() {
// x here is field
System.out.println(x++);
int x = 0;
// x here is the variable
System.out.println(x++);
}
}
The text was updated successfully, but these errors were encountered:
This is a bug that has already been identified but for which we do not yet have a solution.
It appears when JP tries to resolve a symbol in a block. JP tries to search for a variable declaration from the last statement in the block when it should be searching from the statement preceding the node being processed.
At this stage JP doesn't have this notion (current node) so it can't position itself correctly.
Fixing this problem would require an in-depth modification of the API. Uunfortunately, this is a design problem which will take time to fix.
I want to add this to the field reference that omits this in the method, but when there is a variable declaration with the same name in the method, the field reference before the variable declaration will be resolved as a variable instead of a field.
Main function
This.java
output:
The text was updated successfully, but these errors were encountered: