GithubHelp home page GithubHelp logo

Comments (21)

raphw avatar raphw commented on July 19, 2024

Currently, you can use the MethodCall instrumentation which is composable. For field access, there is no short cut way to do explicit instrumentations yet. Note that you need to call the super constructor in any constructor. It seems like you use a MethodConstant where you probably want to invoke a constructor. This you can do via a MethodInvocation in combination with a TypeCreation.

from byte-buddy.

renjie-rm avatar renjie-rm commented on July 19, 2024

I would like to add the following code snippet to a class

public String traceId = ContextUtil.get(DidiContextEnum.TRACE_ID.name);
Can it be done?
@raphw

from byte-buddy.

raphw avatar raphw commented on July 19, 2024

You would need to define a field via defineField and then set the field via any constructor. You can for example use Advice to add this code to the end or beginning of each constructor.

from byte-buddy.

renjie-rm avatar renjie-rm commented on July 19, 2024

@raphw
First defineField,
then enhanced before and after the constructor is executed?

from byte-buddy.

renjie-rm avatar renjie-rm commented on July 19, 2024

I can't define fields and specify initialization code blocks? for example

new AgentBuilder.Default()
.type(named("com.Template"))
.transform((builder, typeDescription, classLoader, module) -> {
DynamicType.Builder.FieldDefinition.Optional.Valuable<?> name = builder.defineField("name", String.class, Modifier.PUBLIC);
// initialization code blocks. name = context.getname();
return name;
})
.installOn(inst);

from byte-buddy.

raphw avatar raphw commented on July 19, 2024

You can do both, but a non-static field must be initialized in a constructor.

For example, use Advice as follows:

static void exit(@FieldValue(value = "name", readOnly = false) String name) {
  name = ContextUtil.get(DidiContextEnum.TRACE_ID.name);
}

and then apply the visit method with Advice.to(MyAdvice.class).on(isConstructor()).

from byte-buddy.

renjie-rm avatar renjie-rm commented on July 19, 2024

ByteBuddyAgent.install();
new ByteBuddy()
.redefine(Source.class)
.defineField("name", String.class, Modifier.PUBLIC)
.make()
.load(Source.class.getClassLoader());

Exception in thread "main" java.lang.IllegalStateException: Class already loaded: class com.example.a.Source
at net.bytebuddy.dynamic.loading.ByteArrayClassLoader.load(ByteArrayClassLoader.java:307)
at net.bytebuddy.dynamic.loading.ClassLoadingStrategy$Default$WrappingDispatcher.load(ClassLoadingStrategy.java:357)
at net.bytebuddy.dynamic.loading.ClassLoadingStrategy$Default.load(ClassLoadingStrategy.java:143)
at net.bytebuddy.dynamic.TypeResolutionStrategy$Passive.initialize(TypeResolutionStrategy.java:100)
at net.bytebuddy.dynamic.DynamicType$Default$Unloaded.load(DynamicType.java:5686)
at net.bytebuddy.dynamic.DynamicType$Default$Unloaded.load(DynamicType.java:5675)
at com.example.a.Main.main(Main.java:19)

@raphw

from byte-buddy.

raphw avatar raphw commented on July 19, 2024

Do you want to add the field or create a new class? If you are doing the latter, you should rename the class. For the former, look into Java agents.

from byte-buddy.

renjie-rm avatar renjie-rm commented on July 19, 2024

I thought this would add fields to the original class

from byte-buddy.

renjie-rm avatar renjie-rm commented on July 19, 2024

Instrumentation install = ByteBuddyAgent.install();
new AgentBuilder.Default()
.type(ElementMatchers.is(Source.class))
.transform((builder, typeDescription, classLoader, javaModule) ->
builder.defineField("name", String.class, Modifier.PUBLIC))
.installOn(install);

Instrumentation install = ByteBuddyAgent.install();
new AgentBuilder.Default()
.type(ElementMatchers.named("com.example.a.Source"))
.transform((builder, typeDescription, classLoader, javaModule) ->
builder.defineField("name", String.class, Modifier.PUBLIC))
.installOn(install);

Don't these two codes do the same thing?@raphw

from byte-buddy.

raphw avatar raphw commented on July 19, 2024

Once you load a class via ElementMatchers.is(Source.class), its layout is set and the JVM does no longer allow adding fields to it.

You would need to add a matcher that does not load the class, for example via named("..."). Additionally, you'd need to run this code before you load the class in other code.

from byte-buddy.

renjie-rm avatar renjie-rm commented on July 19, 2024

You can do both, but a non-static field must be initialized in a constructor.

For example, use Advice as follows:

static void exit(@FieldValue(value = "name", readOnly = false) String name) {
  name = ContextUtil.get(DidiContextEnum.TRACE_ID.name);
}

and then apply the visit method with Advice.to(MyAdvice.class).on(isConstructor()).

       new AgentBuilder.Default()
           .type(ElementMatchers.named("com.example.a.Source"))
           .transform((builder, typeDescription, classLoader, javaModule) ->
               builder.defineField("name", String.class, Modifier.PUBLIC)
                   .visit(Advice.to(MyAdvice.class).on(MethodDescription::isConstructor)))
           .installOn(install);

@raphw There is something wrong with this code

from byte-buddy.

raphw avatar raphw commented on July 19, 2024

Have you imported the correct @FieldValue annotation? It needs to be the Advice related one. You can register an AgentBuilder.Listener.StreamWriting.toSystemOut() in the agent builder. Otherwise, you will not see any error messages.

from byte-buddy.

renjie-rm avatar renjie-rm commented on July 19, 2024

public class MyAdvice {

static void exit(@Advice.FieldValue(value = "name", readOnly = false) String name) {
    name = "andre";
}

}

from byte-buddy.

renjie-rm avatar renjie-rm commented on July 19, 2024

@GetMapping
public Source test() {
return new Source();
}

before I add this code. Visit (Advice to (MyAdvice. Class.) on (MethodDescription: : isConstructor))
curl localhost. return {"id":null,"name":null}

add this code Visit (Advice to (MyAdvice. Class.) on (MethodDescription: : isConstructor))
curl localhost. return {"id":null}
@raphw

from byte-buddy.

raphw avatar raphw commented on July 19, 2024

What does your error log say? You can add it as I suggested.

from byte-buddy.

renjie-rm avatar renjie-rm commented on July 19, 2024

I didn't add @advice. OnMethodExit to the exit method,
java.lang.IllegalArgumentException: No advice defined by class
I can only initialize the name variable after the constructor, right?
@raphw

from byte-buddy.

raphw avatar raphw commented on July 19, 2024

You can write to fields in an enter advice if that's what you mean.

from byte-buddy.

renjie-rm avatar renjie-rm commented on July 19, 2024
@Advice.OnMethodEnter
    static void exit( @Advice.FieldValue(value = "name", readOnly = false) String s) {
        s = "andre";
    }

java.lang.IllegalStateException: Cannot access non-static field before calling constructor: public com.example.a.Source()

from byte-buddy.

raphw avatar raphw commented on July 19, 2024

You are right. It's technically legal, but since reading is not allowed, it's fully forbidden. You would need to create your own offset mapping which does not have this restriction.

from byte-buddy.

renjie-rm avatar renjie-rm commented on July 19, 2024

thank you!@raphw

from byte-buddy.

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.