question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Adding a declaresMethod matcher to a AgentBuilder#type matcher makes method matchers fail

See original GitHub issue

I’d like to use declaresMethod matchers before hasSuperType matchers in order to pre-filter classes that don’t declare the method I want to instrument before applying the hasSuperType matcher, which is more expensive.

Unfortunately, using the declaresMethod matcher seems to mess with the method matcher (net.bytebuddy.agent.builder.AgentBuilder.Transformer.ForAdvice#advice(net.bytebuddy.matcher.ElementMatcher<? super net.bytebuddy.description.method.MethodDescription>, java.lang.String))

Example: https://github.com/elastic/apm-agent-java/blob/6d4a28535d822f731a227ec026e597d57629e801/apm-agent-plugins/apm-servlet-plugin/src/main/java/co/elastic/apm/servlet/AsyncInstrumentation.java#L75-L77

When adding .and(declaresMethod(any())) after line 76, the test co.elastic.apm.servlet.AsyncServletTest#testAsync fails. The logs tell me that I still get a type match, but I don’t get a method match anymore.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (12 by maintainers)

github_iconTop GitHub Comments

2reactions
raphwcommented, Sep 7, 2018

I see that can be a problem, especially in small environments, but in the end its a trade off between speed and memory as in any application.

0reactions
yaohwucommented, Jul 24, 2020

I see, the problem is class loading circularities. If you load a class during a class file transformation, this class will not be transformed. As a matter of fact, Byte Buddy has a mechanism for that, called a CircularityLock to suppress any class loading that occurs during a transformation. If it did not do this, the JVM would yield a CircularityError if the currently transformed class was reached via the load chain.

To avoid this, you need to avoid all class loading, for example by using Byte Buddy’s pool-only descriptor strategy where the library parses byte code even if a loaded class is available.

Same problem happened to my project. My plan is to call target.getDeclaredTypes() and target.getDeclaredFields() to load all the classes used by the target class during the matcher executing. But it did not work. Is the matcher-excuting a part of the class file transformation? @raphw

So that I have to make another way to load the class before actual class-transformation. A empty class file transformer installed for loading class as the last solution. Here is the demo.

Cause I can not load class by the class name for some reasons, so it works perfect for me. Is there more effective and elegant way?

Read more comments on GitHub >

github_iconTop Results From Across the Web

NPE in Mockito argument matcher with doReturn causing all ...
While using argument matchers, Mockito states that to match a primitive type we ... If I run the whole class, then all methods...
Read more >
Mockito Argument Matchers - any(), eq() - DigitalOcean
If we want to use a specific value for an argument, then we can use eq() method. when(mockFoo.bool(eq("false"), anyInt(), any(Object.class))).
Read more >
`respond_to` matcher - RSpec Expectations - Relish
Use the respond_to matcher to specify details of an object's interface. ... If the number of arguments accepted by the method is important...
Read more >
Matchers - jMock
An expectation that uses parameter matchers must use the "with" method to wrap every parameter, whether a matcher function or a literal value....
Read more >
Index (JUnit API)
Static method in class org.hamcrest.core.AllOf: Creates a matcher that matches if the examined object matches ALL of the specified matchers.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found