Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I change the method name and keep the original method body #1612

Open
gzvincen opened this issue Apr 3, 2024 · 1 comment
Open

How do I change the method name and keep the original method body #1612

gzvincen opened this issue Apr 3, 2024 · 1 comment
Assignees
Labels
Milestone

Comments

@gzvincen
Copy link

gzvincen commented Apr 3, 2024

There are the following classes and code

public class Foo {
        public String bar(String arg) {
            return "bar: " + arg;
        }
    }

public static void main(String[] args) throws Exception {
        new ByteBuddy().rebase(Foo.class)
            .name(Foo.class.getPackageName() + ".Bar")
            .method(ElementMatchers.named("bar"))
            .intercept(SuperMethodCall.INSTANCE)
            .make()
            .saveIn(new File("Bar.class"));
    }

The generated class file is as follows:

public class Bar {
    public Bar() {
    }

    public String bar(String arg) {
        return this.bar$original$T45krq9n(arg);
    }
}

But I expect the generated class to look like this, where the bar method name is changed to barTest,Just by changing the method name, the body of the method remains the same. I have tried the method transform, there is no suitable method. Is there a proper way for me to just change the name of the method in the class?

public class Bar {
    public Bar() {
    }

    public String barTest(String arg) {
        return this.bar$original$T45krq9n(arg);
    }
}
@raphw
Copy link
Owner

raphw commented Apr 3, 2024

Do you really need to change the method? I would change the modifier to private if so using ModifierTransformation and add another method with your desired name that invokes the now private method using MethodCall.

@raphw raphw self-assigned this Apr 3, 2024
@raphw raphw added the question label Apr 3, 2024
@raphw raphw added this to the 1.14.13 milestone Apr 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants