Comments (9)
Your code is invalid, still it treats the public void run()
method as a member of outer class.
from javaparser.
Is checking
if (node.getParentNode().isPresent() && node.getParentNode().get() == nodeClass)
in MethodVisitor a good way to do it?
from javaparser.
This is a design problem arising from the fact that you have nested 2 visitors and your code snippet has compilation errors.
The visitor you are using browses all the members of the class declaration or the included class is one of the members of the class. The solution would be to modify the visitor's behaviour so that it is only interested in method declarations or create your own visitor.
May be something like that
List<MethodDeclaration> methodDeclarationList = new ArrayList<>();
cu.accept(new VoidVisitorAdapter<List<MethodDeclaration>>()
{
// iterate class by class
@Override
public void visit(ClassOrInterfaceDeclaration nodeClass, List<MethodDeclaration> param)
{
System.out.println("CLASS = " + nodeClass.getNameAsString());
List<MethodDeclaration> methodDeclarations = nodeClass.findAll(MethodDeclaration.class);
methodDeclarations.forEach(md -> visit(md.asMethodDeclaration(), param));
// random order
Collections.shuffle(methodDeclarationList);
// reset index
Count index = new Count();
// insert shuffled methods one by one
methodDeclarations.forEach(md -> {
md.replace(param.get(index.compteur));
index.incr();
});
param.clear();
nodeClass.getMembers().stream()
.filter(member -> member.isClassOrInterfaceDeclaration())
.forEach(member -> visit(member.asClassOrInterfaceDeclaration(), param));
}
@Override
public void visit(MethodDeclaration node, List<MethodDeclaration> param)
{
System.out.println(node.getNameAsString());
param.add(node.clone());
}
}, methodDeclarationList);
from javaparser.
I cannot nest 2 visitors? Is that a problem too?
from javaparser.
The visitor you are using browses all the members of the class declaration...
You should look at the source of the VoidVisitorAdapter class and you'll immediately understand why.
from javaparser.
Is there a way from the visitor perspective to make it visit only the direct children, not the nested items?
from javaparser.
You probably need to develop your own visitor. In this case, the class included is a member of the "mother" class. You can try what you suggested, i.e. excluding the method if the class doesn't match.
from javaparser.
Your code is invalid, still it treats the
public void run()
method as a member of outer class.
It was an example of what you can do.
from javaparser.
Jup, checking for direct ownership
if (node.getParentNode().isPresent() && node.getParentNode().get() == nodeClass)
does the job. It would be cool if this sort of vistor be available by default.
Thank you for your help :)
from javaparser.
Related Issues (20)
- Symbol resolution issue with MethodDeclaration in JavaParser HOT 13
- How to access annotation from MethodDeclaration
- How to resolve ImportDeclaration HOT 5
- Unable to obtain the descriptor of ClassOrInterfaceType within the record. HOT 1
- Setting the position of nodes during pretty printing HOT 1
- record in an interface throw IllegalArgumentException HOT 7
- LexicalPreservingPrinter doesn't indent text blocks as one would expect. HOT 4
- IndexOutOfBoundsException when parsing Mybatis-Plus HOT 4
- IllegalStateException when removing all comments with LexicalPreservingPrinter HOT 2
- Add easy method to get the parsed information from java.lang.reflection HOT 2
- FieldAccessExpr does not print list of fields accesses of class HOT 4
- Access to the javaparser.org domain
- Getting error while calling calculateResolvedType() on scope in Java Parser HOT 9
- Method 'forEach' cannot be resolved in certain context HOT 5
- Regression in 3.25.9: `Cannot be 'abstract' and also 'private'.` for a `private` method in an interface HOT 2
- Find the first ancestor of a type? HOT 2
- Arrays as Variables HOT 1
- UnsolvedSymbolException ... cannot be resolved in context ... MethodCallExprContext HOT 2
- somtimes AssignExpr.target is wrong 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.