RDV
RDV

Reputation: 333

ByteBuddy ElementMatchers for regular expressions regex

Do you know how to use bytebuddy Advice only for elements with name matching certain regular expression? For instance, if I need MyAdvice only applies for methods whose name match the expression *.business* I would use ElementMatchers.nameMatches like this:

final String regexp = ".*\\.business.*";
new AgentBuilder.Default()
     .disableClassFormatChanges()
     .with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION)
     .type(ElementMatchers.nameMatches(regexp))
     .transform((builder, type, classLoader, module) -> 
          builder.visit(Advice.to(MyAdvice.class).on(ElementMatchers.isMethod()))
     ).installOn(instrumentation);

I already tried this and it didn't work. I don't know is this regex applies to the element full name or just for the element simple name.

Thanks guys !

Upvotes: 0

Views: 306

Answers (1)

Rafael Winterhalter
Rafael Winterhalter

Reputation: 43997

Simply use an ElementMatcher for it, they can be chained:

ElementMatchers.isMethod().and(ElementMatchers.nameMatches(...))

You can also implement your own matcher if that's simpler.

Upvotes: 1

Related Questions