Giter Site home page Giter Site logo

Comments (9)

PELock avatar PELock commented on June 18, 2024 1

Your code is invalid, still it treats the public void run() method as a member of outer class.

from javaparser.

PELock avatar PELock commented on June 18, 2024

Is checking

if (node.getParentNode().isPresent() && node.getParentNode().get() == nodeClass)

in MethodVisitor a good way to do it?

from javaparser.

jlerbsc avatar jlerbsc commented on June 18, 2024

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.

PELock avatar PELock commented on June 18, 2024

I cannot nest 2 visitors? Is that a problem too?

from javaparser.

jlerbsc avatar jlerbsc commented on June 18, 2024

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.

PELock avatar PELock commented on June 18, 2024

Is there a way from the visitor perspective to make it visit only the direct children, not the nested items?

from javaparser.

jlerbsc avatar jlerbsc commented on June 18, 2024

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.

jlerbsc avatar jlerbsc commented on June 18, 2024

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.

PELock avatar PELock commented on June 18, 2024

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)

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.