Comments (4)
When JP assigns the parent node to the new node, it adds it to the list of children of the parent node, regardless of the order.
The children of a node are not necessarily of the same type, as is the case when you consult the list of statements in a block. For example, when you consult the list of children of a class declaration, there may be a name and one or more method declarations. It wouldn't make sense to insert a new method before or after the class name. You can consider that the list of children is not ordered.
from javaparser.
Thanks for clarifying this, but the problem with considering the list of children as unordered is that it is used for tree traversal. So the actual order of the tree doesn't match the (expected) order in the code.
Consider the following snippet is appended from the previous one.
System.out.println("----------------------------------------------------");
System.out.println(blockStmt.findAll(VariableDeclarator.class));
System.out.println("----------------------------------------------------");
blockStmt.walk(TreeTraversal.PREORDER, n -> System.out.println(n));
System.out.println("----------------------------------------------------");
This will output:
----------------------------------------------------
[b = 2, c = 3, a = 1]
----------------------------------------------------
{
int a = 1;
int b = 2;
int c = 3;
}
int b = 2;
int b = 2
b = 2
int
b
2
int c = 3;
int c = 3
c = 3
int
c
3
int a = 1;
int a = 1
a = 1
int
a
1
As we can see, the order from both walk
and findAll
differ from the one we can expect from looking at the printed code for the tree. Now, let's reparse the tree and check the traversal order.
var newTree = StaticJavaParser.parse(cu.toString());
var newBlockStmt = newTree.findFirst(BlockStmt.class).get();
System.out.println(newBlockStmt.findAll(VariableDeclarator.class));
System.out.println("----------------------------------------------------");
newBlockStmt.walk(TreeTraversal.PREORDER, n -> System.out.println(n));
System.out.println("----------------------------------------------------");
----------------------------------------------------
[a = 1, b = 2, c = 3]
----------------------------------------------------
{
int a = 1;
int b = 2;
int c = 3;
}
int a = 1;
int a = 1
a = 1
int
a
1
int b = 2;
int b = 2
b = 2
int
b
2
int c = 3;
int c = 3
c = 3
int
c
3
----------------------------------------------------
Ideally, this operation is idempotent, meaning, the new tree is the same as the old one to some degree (i.e. some things like node ranges will eventually differ), but the order differs despite both trees generating the same code.
For my use case, having a coherent order is important for calculating variable scopes and tracking assignments. Of course, there are workarounds (reparsing the tree will solve this, as shown above), but this still feels like more like a bug other than a design choice.
from javaparser.
In the second case, the result is what you would expect, because the parsing is performed on the printable version of the modified unit compilation, and the printable version makes an internal call to the PrettyPrinterVisitor class, which browses the statements of the block (versus the list of child nodes).
Given that the children of a node are not necessarily of the same type and that nodes do not necessarily have a range (for example, nodes that have just been added), you should not use the findAll method to obtain an ordered list of children.
from javaparser.
I'm closing this exit because we've answered your question. But I can reopen it if you have any further questions.
from javaparser.
Related Issues (20)
- Strange error when trying to find erasure of generic type where one of two type parameters is an array HOT 2
- Unable to check equality of `ResolvedReferenceType` with a `LazyType`
- Regression from #602 HOT 6
- StackOverflowError with import static in circular dependencies HOT 1
- Question about switch statements and pattern representation HOT 4
- Jar file location for JarTypeSolver HOT 5
- Disappearing comments with LexicalPreservingPrinter HOT 1
- 【Question】Can a API be provided with a very concise judgment of whether the current variable is a local variable or a ginseng or field? HOT 2
- 【Question】Should the results of `Expression#RESOLVE` cache? HOT 1
- Record Patterns Implementation Proposal HOT 12
- Different nodes have same hashcode HOT 3
- Meaning of LanguageLevel HOT 3
- Fail to use symbol resolver HOT 8
- Arbitrary new line when refactoring HOT 4
- MethodCallExpr inside lambda in assignment expression cannot be resolved HOT 4
- JavaParser finds "." in place of first element after a yeild statement. HOT 12
- Missing tokens in parent nodes HOT 4
- Spotless formatting implementation proposal HOT 3
- Tests systematically failing on Windows HOT 4
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.