Search in sources :

Example 1 with ExactReturnTypeMethodMatcher

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

the class OptimizedClassMatcherTest method testReturnTypeMatch.

@Test
public void testReturnTypeMatch() throws IOException {
    ClassMatchVisitorFactory matcher = OptimizedClassMatcherBuilder.newBuilder().addClassMethodMatcher(new DefaultClassAndMethodMatcher(new AllClassesMatcher(), new ExactReturnTypeMethodMatcher(Type.getType(List.class)))).build();
    InstrumentationContext instrumentationContext = getInstrumentationContext(matcher, Arrays.class);
    Assert.assertFalse(instrumentationContext.getMatches().isEmpty());
    Match match = instrumentationContext.getMatches().values().iterator().next();
    Assert.assertNotNull(match);
    Assert.assertEquals(1, match.getMethods().size());
    Assert.assertTrue(match.getMethods().contains(new Method("asList", "([Ljava/lang/Object;)Ljava/util/List;")));
    Assert.assertEquals(1, match.getClassMatches().size());
}
Also used : InstrumentationContext(com.newrelic.agent.instrumentation.context.InstrumentationContext) ExactReturnTypeMethodMatcher(com.newrelic.agent.instrumentation.methodmatchers.ExactReturnTypeMethodMatcher) ClassMatchVisitorFactory(com.newrelic.agent.instrumentation.context.ClassMatchVisitorFactory) Method(org.objectweb.asm.commons.Method) Match(com.newrelic.agent.instrumentation.classmatchers.OptimizedClassMatcher.Match) Test(org.junit.Test)

Example 2 with ExactReturnTypeMethodMatcher

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

the class ClassesMatcherTest method getMatchingClasses_ReturnType.

@Trace
@Test
public void getMatchingClasses_ReturnType() {
    ClassMatchVisitorFactory matcher = OptimizedClassMatcherBuilder.newBuilder().addClassMethodMatcher(new DefaultClassAndMethodMatcher(new AllClassesMatcher(), new ExactReturnTypeMethodMatcher(Type.getType(List.class)))).build();
    Set<Class<?>> matchingClasses = ClassesMatcher.getMatchingClasses(Collections.singletonList(matcher), matcherHelper, Arrays.class, ArrayList.class, HashMap.class, TestClass.class, Proxy.class);
    assertEquals(3, matchingClasses.size());
    assertTrue(matchingClasses.contains(ArrayList.class));
    assertTrue(matchingClasses.contains(TestClass.class));
    assertTrue(matchingClasses.contains(Arrays.class));
}
Also used : ExactReturnTypeMethodMatcher(com.newrelic.agent.instrumentation.methodmatchers.ExactReturnTypeMethodMatcher) DefaultClassAndMethodMatcher(com.newrelic.agent.instrumentation.classmatchers.DefaultClassAndMethodMatcher) AllClassesMatcher(com.newrelic.agent.instrumentation.classmatchers.AllClassesMatcher) Trace(com.newrelic.api.agent.Trace) Test(org.junit.Test)

Example 3 with ExactReturnTypeMethodMatcher

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

the class MethodMatcherUtility method createMethodMatcher.

public static MethodMatcher createMethodMatcher(String className, Method method, Map<String, MethodMapper> classesToMethods, String extName) throws NoSuchMethodException, XmlException {
    if (method == null) {
        throw new XmlException("A method must be specified for a point cut in the extension.");
    }
    if (method.getReturnType() != null) {
        if (Utils.isPrimitiveType(method.getReturnType())) {
            throw new XmlException("The return type '" + method.getReturnType() + "' is not valid.  Primitive types are not allowed.");
        }
        Type returnType = Type.getObjectType(method.getReturnType().replace('.', '/'));
        if (!ExtensionConversionUtility.isReturnTypeOkay(returnType)) {
            throw new XmlException("The return type '" + returnType.getClassName() + "' is not valid.  Primitive types are not allowed.");
        }
        return new ExactReturnTypeMethodMatcher(returnType);
    }
    validateMethod(method, extName);
    String methodName = method.getName();
    if (methodName == null) {
        throw new XmlException("A method name must be specified for a point cut in the extension.");
    }
    methodName = methodName.trim();
    if (methodName.length() == 0) {
        throw new XmlException("A method must be specified for a point cut in the extension.");
    }
    Parameters mParams = method.getParameters();
    if (mParams == null || mParams.getType() == null) {
        if (!isDuplicateMethod(className, methodName, null, classesToMethods)) {
            return new NameMethodMatcher(methodName);
        } else {
            throw new NoSuchMethodException("Method " + methodName + " has already been added to a point cut and will " + "not be added again.");
        }
    } else {
        String descriptor = MethodParameters.getDescriptor(mParams);
        if (descriptor == null) {
            throw new XmlException("Descriptor not being calculated correctly.");
        }
        String mDescriptor = descriptor.trim();
        if (!isDuplicateMethod(className, methodName, mDescriptor, classesToMethods)) {
            return ExactParamsMethodMatcher.createExactParamsMethodMatcher(methodName, descriptor);
        } else {
            throw new NoSuchMethodException("Method " + methodName + " has already been added to a point cut and will " + "not be added again.");
        }
    }
}
Also used : ExactReturnTypeMethodMatcher(com.newrelic.agent.instrumentation.methodmatchers.ExactReturnTypeMethodMatcher) Type(org.objectweb.asm.Type) Parameters(com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut.Method.Parameters) MethodParameters(com.newrelic.agent.extension.beans.MethodParameters) NameMethodMatcher(com.newrelic.agent.instrumentation.methodmatchers.NameMethodMatcher)

Example 4 with ExactReturnTypeMethodMatcher

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

the class ExtensionDomParserTest method testSuperclassPointCut.

@Test
public void testSuperclassPointCut() throws Exception {
    Extension ext = ExtensionDomParser.readFile(getFile(SUPERCLASS_FILE_PATH));
    Instrumentation inst = ext.getInstrumentation();
    List<Pointcut> thePcs = inst.getPointcut();
    Assert.assertEquals(2, thePcs.size());
    Pointcut pc = thePcs.get(0);
    Assert.assertNotNull(pc.getClassName());
    Assert.assertEquals("test.SuperTest", pc.getClassName().getValue());
    List<ExtensionClassAndMethodMatcher> pcs = ExtensionConversionUtility.convertToPointCutsForValidation(ext);
    Assert.assertNotNull(pcs);
    Assert.assertEquals(2, pcs.size());
    ExtensionClassAndMethodMatcher actual = pcs.get(0);
    Assert.assertTrue(actual.getClassMatcher() instanceof ChildClassMatcher);
    ExtensionClassAndMethodMatcher returnMatcher = pcs.get(1);
    Assert.assertTrue(returnMatcher.getClassMatcher() instanceof ChildClassMatcher);
    Assert.assertTrue(returnMatcher.getMethodMatcher() instanceof ExactReturnTypeMethodMatcher);
    Assert.assertTrue(returnMatcher.getMethodMatcher().matches(Opcodes.ACC_PUBLIC, "bogus", "()Lcom/framework/Result;", com.google.common.collect.ImmutableSet.<String>of()));
    Assert.assertFalse(returnMatcher.getMethodMatcher().matches(Opcodes.ACC_PUBLIC, "test", "()[Lcom/framework/Result;", com.google.common.collect.ImmutableSet.<String>of()));
    Assert.assertFalse(returnMatcher.getMethodMatcher().matches(Opcodes.ACC_PUBLIC, "dude", "(Lcom/framework/Result;)V", com.google.common.collect.ImmutableSet.<String>of()));
}
Also used : Extension(com.newrelic.agent.extension.beans.Extension) Pointcut(com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut) ExactReturnTypeMethodMatcher(com.newrelic.agent.instrumentation.methodmatchers.ExactReturnTypeMethodMatcher) ExtensionClassAndMethodMatcher(com.newrelic.agent.instrumentation.custom.ExtensionClassAndMethodMatcher) Instrumentation(com.newrelic.agent.extension.beans.Extension.Instrumentation) ChildClassMatcher(com.newrelic.agent.instrumentation.classmatchers.ChildClassMatcher) Test(org.junit.Test)

Aggregations

ExactReturnTypeMethodMatcher (com.newrelic.agent.instrumentation.methodmatchers.ExactReturnTypeMethodMatcher)4 Test (org.junit.Test)3 Extension (com.newrelic.agent.extension.beans.Extension)1 Instrumentation (com.newrelic.agent.extension.beans.Extension.Instrumentation)1 Pointcut (com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut)1 Parameters (com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut.Method.Parameters)1 MethodParameters (com.newrelic.agent.extension.beans.MethodParameters)1 AllClassesMatcher (com.newrelic.agent.instrumentation.classmatchers.AllClassesMatcher)1 ChildClassMatcher (com.newrelic.agent.instrumentation.classmatchers.ChildClassMatcher)1 DefaultClassAndMethodMatcher (com.newrelic.agent.instrumentation.classmatchers.DefaultClassAndMethodMatcher)1 Match (com.newrelic.agent.instrumentation.classmatchers.OptimizedClassMatcher.Match)1 ClassMatchVisitorFactory (com.newrelic.agent.instrumentation.context.ClassMatchVisitorFactory)1 InstrumentationContext (com.newrelic.agent.instrumentation.context.InstrumentationContext)1 ExtensionClassAndMethodMatcher (com.newrelic.agent.instrumentation.custom.ExtensionClassAndMethodMatcher)1 NameMethodMatcher (com.newrelic.agent.instrumentation.methodmatchers.NameMethodMatcher)1 Trace (com.newrelic.api.agent.Trace)1 Type (org.objectweb.asm.Type)1 Method (org.objectweb.asm.commons.Method)1