Search in sources :

Example 1 with RequireMethodsAdapter

use of com.newrelic.agent.instrumentation.RequireMethodsAdapter in project newrelic-java-agent by newrelic.

the class ApiImplementationUpdateTest method testMatcherMissingMethods.

@Test
public void testMatcherMissingMethods() throws Exception {
    ArgumentCaptor<ClassMatchVisitorFactory> arg = ArgumentCaptor.forClass(ClassMatchVisitorFactory.class);
    InstrumentationContext iInstrumentationContext = Mockito.mock(InstrumentationContext.class);
    // remove required methods from Request implementation class
    Class<?> clazz = RequestImpl.class;
    Set<Method> methodsToRemove = new HashSet<>();
    methodsToRemove.add(new Method("getHeaderType", "()Lcom/newrelic/api/agent/HeaderType;"));
    byte[] classBytes = removeMethods(clazz, methodsToRemove);
    // verify missing methods
    RequireMethodsAdapter adapter = getRequireMethodsAdapter(clazz, Request.class, REQUEST_METHODS);
    expectMissingMethods(adapter, classBytes);
    ApiImplementationUpdate transformer = new ApiImplementationUpdate();
    ClassMatchVisitorFactory matcher = transformer.getMatcher();
    ClassReader reader = new ClassReader(new ByteArrayInputStream(classBytes));
    ClassVisitor visitor = matcher.newClassMatchVisitor(clazz.getClassLoader(), null, reader, null, iInstrumentationContext);
    reader.accept(visitor, ClassReader.SKIP_CODE);
    Mockito.verify(iInstrumentationContext, Mockito.only()).putMatch(arg.capture(), Mockito.<Match>anyObject());
    Assert.assertSame(matcher, arg.getValue());
}
Also used : InstrumentationContext(com.newrelic.agent.instrumentation.context.InstrumentationContext) ClassMatchVisitorFactory(com.newrelic.agent.instrumentation.context.ClassMatchVisitorFactory) Method(org.objectweb.asm.commons.Method) ClassVisitor(org.objectweb.asm.ClassVisitor) ByteArrayInputStream(java.io.ByteArrayInputStream) RequireMethodsAdapter(com.newrelic.agent.instrumentation.RequireMethodsAdapter) ClassReader(org.objectweb.asm.ClassReader) HashSet(java.util.HashSet) Test(org.junit.Test) ClassMatcherTest(com.newrelic.agent.instrumentation.classmatchers.ClassMatcherTest)

Example 2 with RequireMethodsAdapter

use of com.newrelic.agent.instrumentation.RequireMethodsAdapter in project newrelic-java-agent by newrelic.

the class ApiImplementationUpdateTest method testTransform.

@Test
public void testTransform() throws Exception {
    // remove required methods from Request implementation class
    Class<?> clazz = RequestImpl.class;
    Set<Method> methodsToRemove = new HashSet<>();
    methodsToRemove.add(new Method("getHeaderType", "()Lcom/newrelic/api/agent/HeaderType;"));
    byte[] classBytes = removeMethods(clazz, methodsToRemove);
    // verify missing methods
    RequireMethodsAdapter adapter = getRequireMethodsAdapter(clazz, Request.class, REQUEST_METHODS);
    expectMissingMethods(adapter, classBytes);
    // should add default implementations for missing methods
    ApiImplementationUpdate transformer = new ApiImplementationUpdate();
    classBytes = transformer.transform(clazz.getClassLoader(), null, null, null, classBytes, null, null);
    // verify no missing methods
    adapter = getRequireMethodsAdapter(clazz, Request.class, REQUEST_METHODS);
    expectNoMissingMethods(adapter, classBytes);
}
Also used : RequireMethodsAdapter(com.newrelic.agent.instrumentation.RequireMethodsAdapter) Request(com.newrelic.api.agent.Request) Method(org.objectweb.asm.commons.Method) HashSet(java.util.HashSet) Test(org.junit.Test) ClassMatcherTest(com.newrelic.agent.instrumentation.classmatchers.ClassMatcherTest)

Example 3 with RequireMethodsAdapter

use of com.newrelic.agent.instrumentation.RequireMethodsAdapter in project newrelic-java-agent by newrelic.

the class ApiImplementationUpdateTest method testMatcher.

@Test
public void testMatcher() throws Exception {
    InstrumentationContext iInstrumentationContext = Mockito.mock(InstrumentationContext.class);
    Class<?> clazz = RequestImpl.class;
    Set<Method> methodsToRemove = new HashSet<>();
    byte[] classBytes = removeMethods(clazz, methodsToRemove);
    // verify no missing methods
    RequireMethodsAdapter adapter = getRequireMethodsAdapter(clazz, Request.class, REQUEST_METHODS);
    expectNoMissingMethods(adapter, classBytes);
    ApiImplementationUpdate transformer = new ApiImplementationUpdate();
    ClassMatchVisitorFactory matcher = transformer.getMatcher();
    ClassReader reader = new ClassReader(new ByteArrayInputStream(classBytes));
    ClassVisitor visitor = matcher.newClassMatchVisitor(clazz.getClassLoader(), null, reader, null, iInstrumentationContext);
    reader.accept(visitor, ClassReader.SKIP_CODE);
    Mockito.verify(iInstrumentationContext, Mockito.never()).putMatch(Mockito.<ClassMatchVisitorFactory>anyObject(), Mockito.<Match>anyObject());
}
Also used : InstrumentationContext(com.newrelic.agent.instrumentation.context.InstrumentationContext) ClassMatchVisitorFactory(com.newrelic.agent.instrumentation.context.ClassMatchVisitorFactory) Method(org.objectweb.asm.commons.Method) ClassVisitor(org.objectweb.asm.ClassVisitor) ByteArrayInputStream(java.io.ByteArrayInputStream) RequireMethodsAdapter(com.newrelic.agent.instrumentation.RequireMethodsAdapter) ClassReader(org.objectweb.asm.ClassReader) HashSet(java.util.HashSet) Test(org.junit.Test) ClassMatcherTest(com.newrelic.agent.instrumentation.classmatchers.ClassMatcherTest)

Aggregations

RequireMethodsAdapter (com.newrelic.agent.instrumentation.RequireMethodsAdapter)3 ClassMatcherTest (com.newrelic.agent.instrumentation.classmatchers.ClassMatcherTest)3 HashSet (java.util.HashSet)3 Test (org.junit.Test)3 Method (org.objectweb.asm.commons.Method)3 ClassMatchVisitorFactory (com.newrelic.agent.instrumentation.context.ClassMatchVisitorFactory)2 InstrumentationContext (com.newrelic.agent.instrumentation.context.InstrumentationContext)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ClassReader (org.objectweb.asm.ClassReader)2 ClassVisitor (org.objectweb.asm.ClassVisitor)2 Request (com.newrelic.api.agent.Request)1