raphw / byte-buddy

Runtime code generation for the Java virtual machine.
https://bytebuddy.net
Apache License 2.0
6.29k stars 807 forks source link

Cannot define auxiliary field for frozen type: class org.apache.commons.httpclient.HttpMethodBase #1553

Closed edwardlzh closed 1 year ago

edwardlzh commented 1 year ago

i want to advice on org.apache.commons.httpclient.HttpMethodBase#setRequestHeader(java.lang.String, java.lang.String) and change its two imputParams.

my agent is like

public static void premain(String arguments, Instrumentation instrumentation) { new AgentBuilder.Default().disableClassFormatChanges() .type(is(HttpMethodBase.class).and(isAbstract())) .transform((builder, typeDescription, classLoader, module, protectionDomain) -> builder.method( named("setRequestHeader").and(takesArgument(0, String.class)).and(takesArgument(1, String.class))) .intercept(MethodDelegation.withDefaultConfiguration() .withBinders(Morph.Binder.install(MyCallable.class)) .to(new Test()))) .with(new Listener()) .with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION) .installOn(instrumentation); }

my interceptor is like

public class Test { @RuntimeType public Object intercept(@Morph MyCallable call, @AllArguments Object[] args) { /args[0] = "Host"; args[1] = "localhost:8080";/ return call.call(args); } }

about the MyCallable

public interface MyCallable { Object call(Object[] args); }

and There's a mistake be like

java.lang.IllegalStateException: Cannot define auxiliary field for frozen type: class org.apache.commons.httpclient.HttpMethodBase

can you tell me how to fix it?

raphw commented 1 year ago

You disable class file changes. This is however limiting you in shat you can do. Normally, you'd need to use Advice in such a case.

edwardlzh commented 1 year ago

glad to solve this problem,and also i want to ask ,when should i use Advice or Interceptor? what differences between them?