You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I think I am missing something very obvious but I couldn't figure it out myself.
The thing I want to do is the following:
public class Parent {
// method I want to override, but I can't, so I have to redefine the whole class because of it
final String importantMethod(String s) {
return s + "foo";
}
public void complicatedFunction() {
// pretty complicated method that I don't want to copy-paste with 100 lines of code or something, and then
var importantResult = importantMethod(someString);
// another 100 lines of code or something
}
}
public class Child extends Parent {
// pretty complicated subclass that I don't want to copy-paste
}
I don't have an option to use Java agent so I can't replace classes, however I would like to copy them with Parent.importantMethod to be changed. Here's how I can do what I need with parent:
var parentClass =
new ByteBuddy()
.redefine(Parent.class)
.suffix("_hacked")
.method(
ElementMatchers.named("importantMethod")
.and(ElementMatchers.isFinal())
.and(ElementMatchers.isDeclaredBy(Parent.class))
.and(ElementMatchers.returns(String.class)))
.intercept(FixedValue.value("The value I really want to use!"))
.make()
.load(someClassLoader)
.getLoaded();
but I have no clue how can I do something like this:
var childClass =
new ByteBuddy()
.subclass(parentClass)
.copyImplementationFrom(Child.class)
...
I know I can call defineMethod, defineField, etc, but I don't want to do it manually for every single method in Child because those are really complicated.
Thanks!
The text was updated successfully, but these errors were encountered:
However, it still cannot avoid the potential errors mentioned in the link above, so you must be clear about what you are doing and have the ability to handle those issues on your own.
Hi, I think I am missing something very obvious but I couldn't figure it out myself.
The thing I want to do is the following:
I don't have an option to use Java agent so I can't replace classes, however I would like to copy them with
Parent.importantMethod
to be changed. Here's how I can do what I need with parent:but I have no clue how can I do something like this:
I know I can call
defineMethod
,defineField
, etc, but I don't want to do it manually for every single method inChild
because those are really complicated.Thanks!
The text was updated successfully, but these errors were encountered: