Giter VIP home page Giter VIP logo

Comments (8)

jlerbsc avatar jlerbsc commented on September 23, 2024

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.

matthieu-vergne avatar matthieu-vergne commented on September 23, 2024

Finding the declaration is fine to me, I can do the rest. Just need to know how to do it.

from javaparser.

matthieu-vergne avatar matthieu-vergne commented on September 23, 2024

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.

jlerbsc avatar jlerbsc commented on September 23, 2024

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.

matthieu-vergne avatar matthieu-vergne commented on September 23, 2024

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 I resolve() into a ResolvedValueDeclaration
  • I check isParameter() before to translate it with asParameter().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.

matthieu-vergne avatar matthieu-vergne commented on September 23, 2024

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.

jlerbsc avatar jlerbsc commented on September 23, 2024

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.

matthieu-vergne avatar matthieu-vergne commented on September 23, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.