question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ChangeMethodName works only with static methods

See original GitHub issue

In this test project https://github.com/Guseyn/rewrite-java-definitions, I’ve created following class with regular and static methods:

package org.openrewrite.classes;

public class ClassWithMethods {
    public void foo() {}
    public void bar() {}

    public static void fooStatic() {}
    public static void barStatic() {}
}

I decided to include package names since I think it can be important.

Then I’ve created following test class E that contains the class above:

package org.openrewrite.before;

import org.openrewrite.classes.ClassWithMethods;

public class E  {
    void foo() {
        new ClassWithMethods().foo();
        ClassWithMethods.fooStatic();
    }
}

My Java Definition for E class:

        ChangeMethodName cmn = new ChangeMethodName();
        cmn.setMethod("org.openrewrite.classes.ClassWithMethods foo()");
        cmn.setName("bar");
        ChangeMethodName cmnStatic = new ChangeMethodName();
        cmnStatic.setMethod("org.openrewrite.classes.ClassWithMethods fooStatic()");
        cmnStatic.setName("barStatic");
        ChangePackageName cpn = new ChangePackageName();
        cpn.setNewPackageName("org.openrewrite.after");
        List<RefactorVisitor<?>> visitors = new ArrayList<>(){{
            add(cmn);
            add(cmnStatic);
            add(cpn);
        }};
        RefactorProcessor.run(visitors, "E.java");

I explain how RefactorProcessor in readme of the project: https://github.com/Guseyn/rewrite-java-definitions

When I run the this Java Definition, I get error:

Exception in thread "main" java.lang.NoSuchMethodError: org.openrewrite.java.tree.J$NewClass.<init>(Ljava/util/UUID;Lorg/openrewrite/java/tree/J$Ident;Lorg/openrewrite/java/tree/J$NewClass$New;Lorg/openrewrite/java/tree/TypeTree;Lorg/openrewrite/java/tree/J$NewClass$Arguments;Lorg/openrewrite/java/tree/J$Block;Lorg/openrewrite/java/tree/JavaType;Lorg/openrewrite/Formatting;)V
	at org.openrewrite.java.Java11ParserVisitor.visitNewClass(Java11ParserVisitor.java:945)
	at org.openrewrite.java.Java11ParserVisitor.visitNewClass(Java11ParserVisitor.java:56)
	at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCNewClass.accept(JCTree.java:1711)
	at jdk.compiler/com.sun.source.util.TreePathScanner.scan(TreePathScanner.java:82)
	at org.openrewrite.java.Java11ParserVisitor.convert(Java11ParserVisitor.java:1278)
	at org.openrewrite.java.Java11ParserVisitor.visitMethodInvocation(Java11ParserVisitor.java:734)

When I comment regular method:

package org.openrewrite.before;

import org.openrewrite.classes.ClassWithMethods;

public class E  {
    void foo() {
        // new ClassWithMethods().foo();
        ClassWithMethods.fooStatic();
    }
}

and run the definition again, I get expected result:

package org.openrewrite.after;

import org.openrewrite.classes.ClassWithMethods;

public class E  {
    void foo() {
        // new ClassWithMethods().foo();
        ClassWithMethods.barStatic();
    }
}

It seems to me that this visitor works only with static method, otherwise I get weird parsing error.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
sambsnydcommented, Oct 13, 2020

When I run this example I don’t get any error and it works as expected. I suspect that what made the difference is that I added a dependency on rewrite-java-11, also present in my pull request to your repository. I’m not sure exactly how your examples were working for you without a dependency on either rewrite-java-11 or rewrite-java-8, none of them would run for me before I added that dependency.

Try things out with that PR and let me know if this is still an issue.

0reactions
Guseyncommented, Oct 14, 2020

@sambsnyd It worked, thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Java best practice: Class with only static methods
I thought I'd go with only static methods so that I don't have to care about passing an instance to the GUI classes...
Read more >
Static methods not final - OpenRewrite
org.openrewrite.java.cleanup.StaticMethodNotFinal Static methods do not need to be declared final because they cannot be overridden.
Read more >
When to Make a Method Static in Java? Example - Javarevisited
3) A method is a good candidate for being static, if it only works on arguments provided to it e.g. public int factorial(int...
Read more >
kotlin abstract static method
A static method can access static data member and can change the value of it. As discussed above, Any static member can be...
Read more >
Refactoring (Spoon Core 10.3.0-SNAPSHOT API) - Inria
Contains all methods to refactor code elements in the AST. Method Summary. All Methods Static Methods ... public static void changeMethodName​(CtMethod<?> ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found