Comments (8)
Hi, Javaparser allows you to find the declaration corresponding to a variable, for example, but does not allow you to retrieve all the uses of this variable.
from javaparser.
Finding the declaration is fine to me, I can do the rest. Just need to know how to do it.
from javaparser.
Just to be precise, I am not asking about some general aspect. I already know that the parser provides, generally speaking, the relevant information to figure out whether a given name refers to a given declaration. You can pass through a Parameter in the AST, then find an AssignExp that uses the same name, so based on the context you can figure out it relates to that parameter (or not if another parameter/variable/field has been declared since then).
My question is really concrete: is there already something implemented that allows me to tell something like this AssignExp assigns something to a "myParam", which is resolved to this parameter/field/variable/etc. without having to browse the whole AST myself. I would expect something like that implemented in the symbol resolving part.
from javaparser.
The variable declaration is a node that can be resolved by the resolve() method, which returns an instance of ResolvedValueDeclaration, for which there are many implementations.
from javaparser.
Based on what you said and experimenting a lot, I was able to make a simple code work on my input:
class MyClass {
interface MyInt {
String myMethod(String myParam);
}
MyInt myMethod(MyInt myParam) {
myParam = new MyInt() {
@Override
public String myMethod(String myParam) {
return myParam;
}
};
return myParam;
}
}
In this simple code, I go straight to the point (with streams on the compilation unit) after parsing the input:
- I retrieve the
Parameter
at line 6 as a reference (what I expect at the end), - I retrieve the
SimpleName
at line 7 that states "myParam", - I take its parent
NameExpr
that Iresolve()
into aResolvedValueDeclaration
- I check
isParameter()
before to translate it withasParameter().toAst(Parameter.class)
- I get a
Parameter
that I compare to my reference, and it is the expected one
So it works, and it roughly reproduce what I do in my actual code.
Now in my actual code, which is more complex but where I parse the same input, I do virtually the same thing (at some point get a SimpleName
, then its parent NameExpr
and resolve) but it fails to resolve it:
UnsolvedSymbolException{context='null', name='We are unable to find the value declaration corresponding to myParam', cause='null'}
at [email protected]/com.github.javaparser.symbolsolver.JavaSymbolSolver.resolveDeclaration(JavaSymbolSolver.java:198)
at [email protected]/com.github.javaparser.ast.expr.NameExpr.resolve(NameExpr.java:166)
at [my own code where I call nameExpr.resolve()]
What could cause such an exception?
from javaparser.
I got it: I change the identifier of the declaration before to look for the SimpleName
related to it, and it seems it makes their resolve impossible. If I change first the SimpleName
occurrences then I can resolve the declaration, and then I rename the declaration at the end.
from javaparser.
What could cause such an exception?
I think it's the same bug as this one #3674.
If you share a code snippet we can confirm that it's the same bug.
from javaparser.
If you want to investigate you can clone my project and checkout this commit:
https://github.com/matthieu-vergne/STANOS/tree/121e3e54a45b3c1a0224572e6d56516df41b856c
Run the RefactorerTest unit tests:
https://github.com/matthieu-vergne/STANOS/blob/121e3e54a45b3c1a0224572e6d56516df41b856c/stanos-core/src/test/java/fr/vergne/stanos/core/refactorer/RefactorerTest.java
They should all be green.
Go to the Refactorer class at line 236:
https://github.com/matthieu-vergne/STANOS/blob/121e3e54a45b3c1a0224572e6d56516df41b856c/stanos-core/src/main/java/fr/vergne/stanos/core/refactorer/Refactorer.java#L236
Move this line after line 216 to rename the declaration before the other occurrences of the name.
Then you should have several tests failing with this exception:
UnsolvedSymbolException{context='null', name='We are unable to find the value declaration corresponding to myParam', cause='null'}
at [email protected]/com.github.javaparser.symbolsolver.JavaSymbolSolver.resolveDeclaration(JavaSymbolSolver.java:198)
at [email protected]/com.github.javaparser.ast.expr.NameExpr.resolve(NameExpr.java:166)
at [my own code where I call nameExpr.resolve()]
from javaparser.
Related Issues (20)
- Lexical parsing on a lambda with type fails to replace HOT 10
- 【bug】Error Parser : [ 'permits' cannot be used as an identifier as it is a keyword., 'permits' cannot be used as an identifier as it is a keyword ] HOT 9
- resolve LambdaExpr has NullPointException HOT 3
- There may be a 'JarType' matching error problem in multi-module projects HOT 3
- Record can't be resolved in lambda expressions HOT 4
- UnsolvedSymbolException{context='null', name='We are unable to find the method declaration corresponding to this::getAmount', cause='null'} HOT 2
- ParseProblemException when parsing record pattern with primitive types HOT 7
- `com.github.javaparser.ast.Node#setParentNode` maybe lost parentNode,There may be forced modifications to the original AST Node HOT 5
- FindAll does not retrieve all the MethodCallExpr within a Statement HOT 2
- javaparser-core.jar is not reproducible due to JavaParserBuild
- Out of Memory Error with `sourceZip` API while trying to parse java's Core Library by javaparser HOT 2
- JavaParser cannot resolve objectCreationExpression when type argument is a record type HOT 2
- Question: Getting the index of token in parsed text HOT 1
- ResolvedMethodDeclaration.toDescription fails in the presence of type variables HOT 1
- VariableDeclarator character range does not match range of children HOT 1
- How to find implementation by MethodCallExpr HOT 2
- Pattern variables not always introduced into the correct scope HOT 7
- Node.PostOrderIterator broken for root without children
- Parsing issue in switch with yield HOT 2
- parseVariableDeclarationExpr with `var` HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from javaparser.