Search in sources :

Example 1 with AnnotationMethodMatcher

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

the class ExtensionConversionUtility method createMethodMatcher.

/**
 * Creates the method matcher to be used in the point cut.
 *
 * @param cut The xml information about the methods contain in the point cut.
 * @param pExtName The name of this extension.
 * @param classesToMethods The class/method combos which have already appeared.
 * @return The matcher to be used for methods on the point cut.
 * @throws XmlException
 */
private static MethodMatcher createMethodMatcher(final Pointcut cut, final String pExtName, final Map<String, MethodMapper> classesToMethods) throws XmlException {
    List<Method> methods = cut.getMethod();
    List<String> traceReturnTypeDescriptors = cut.getTraceReturnTypeDescriptors();
    if (methods != null && !methods.isEmpty()) {
        return MethodMatcherUtility.createMethodMatcher(getClassName(cut), methods, classesToMethods, pExtName);
    } else if (cut.getMethodAnnotation() != null) {
        return new AnnotationMethodMatcher(Type.getObjectType(cut.getMethodAnnotation().replace('.', '/')));
    } else if (cut.isTraceLambda()) {
        return new LambdaMethodMatcher(cut.getPattern(), cut.getIncludeNonstatic());
    } else if (traceReturnTypeDescriptors != null && !traceReturnTypeDescriptors.isEmpty()) {
        return new ReturnTypeMethodMatcher(traceReturnTypeDescriptors);
    } else {
        throw new XmlException(MessageFormat.format(XmlParsingMessages.NO_METHOD, pExtName));
    }
}
Also used : ReturnTypeMethodMatcher(com.newrelic.agent.instrumentation.methodmatchers.ReturnTypeMethodMatcher) Method(com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut.Method) AnnotationMethodMatcher(com.newrelic.agent.instrumentation.methodmatchers.AnnotationMethodMatcher) LambdaMethodMatcher(com.newrelic.agent.instrumentation.methodmatchers.LambdaMethodMatcher)

Example 2 with AnnotationMethodMatcher

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

the class OptimizedClassMatcherBuilder method addClassMethodMatcher.

public OptimizedClassMatcherBuilder addClassMethodMatcher(ClassAndMethodMatcher... matchers) {
    for (ClassAndMethodMatcher matcher : matchers) {
        if (exactClassMatch && !matcher.getClassMatcher().isExactClassMatcher()) {
            exactClassMatch = false;
        } else {
            exactClassNames.addAll(matcher.getClassMatcher().getClassNames());
        }
        if (matcher.getMethodMatcher() instanceof AnnotationMethodMatcher) {
            methodAnnotationMatchers.add(((AnnotationMethodMatcher) matcher.getMethodMatcher()).getAnnotationType().getDescriptor());
        }
        Method[] exactMethods = matcher.getMethodMatcher().getExactMethods();
        if (exactMethods == null || exactMethods.length == 0) {
            methodMatchers.put(matcher.getMethodMatcher(), matcher);
        } else {
            for (Method m : exactMethods) {
                if (OptimizedClassMatcher.METHODS_WE_NEVER_INSTRUMENT.contains(m)) {
                    Agent.LOG.severe("Skipping method matcher for method " + m);
                    Agent.LOG.fine("Skipping matcher for class matcher " + matcher.getClassMatcher());
                } else {
                    if (OptimizedClassMatcher.DEFAULT_CONSTRUCTOR.equals(m)) {
                        Agent.LOG.severe("Instrumentation is matching a default constructor.  This may result in slow class loading times.");
                        Agent.LOG.debug("No arg constructor matcher: " + matcher.getClassMatcher());
                    }
                    methods.put(m, matcher);
                }
            }
        }
    }
    return this;
}
Also used : AnnotationMethodMatcher(com.newrelic.agent.instrumentation.methodmatchers.AnnotationMethodMatcher) Method(org.objectweb.asm.commons.Method)

Example 3 with AnnotationMethodMatcher

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

the class OptimizedClassMatcherTest method before.

@Before
public void before() {
    initializeServiceManager();
    OptimizedClassMatcherBuilder builder = OptimizedClassMatcherBuilder.newBuilder();
    match1 = new DefaultClassAndMethodMatcher(createInterfaceMatcher(Collection.class), new ExactMethodMatcher("size", "()I"));
    match2 = new DefaultClassAndMethodMatcher(createInterfaceMatcher(List.class), new ExactMethodMatcher("get", "(I)Ljava/lang/Object;"));
    match3 = new DefaultClassAndMethodMatcher(createInterfaceMatcher(InvocationHandler.class), new ExactMethodMatcher("invoke", "(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;"));
    match4 = new DefaultClassAndMethodMatcher(createInterfaceMatcher(Statement.class), new AllMethodsMatcher());
    match5 = new DefaultClassAndMethodMatcher(createInterfaceMatcher(Runnable.class), OrMethodMatcher.getMethodMatcher(new NameMethodMatcher("run"), new ExactMethodMatcher("bogus", "()V")));
    match6 = new DefaultClassAndMethodMatcher(createInterfaceMatcher(Map.class), new AccessMethodMatcher(Opcodes.ACC_PUBLIC));
    match7 = new DefaultClassAndMethodMatcher(createInterfaceMatcher(List.class), new ExactMethodMatcher("add", "(Ljava/lang/Object;)Z"));
    annotationMatch = new DefaultClassAndMethodMatcher(new AllClassesMatcher(), new AnnotationMethodMatcher(Type.getType(MyAnnotation.class)));
    // this is a bogus matcher to attempt to throw off match5
    builder.addClassMethodMatcher(new DefaultClassAndMethodMatcher(createInterfaceMatcher(PreparedStatement.class), new NameMethodMatcher("run")));
    builder.addClassMethodMatcher(match1);
    builder.addClassMethodMatcher(match2);
    builder.addClassMethodMatcher(match3);
    builder.addClassMethodMatcher(match4);
    builder.addClassMethodMatcher(match5);
    builder.addClassMethodMatcher(match6);
    builder.addClassMethodMatcher(match7);
    builder.addClassMethodMatcher(annotationMatch);
    this.matcher = builder.build();
}
Also used : NameMethodMatcher(com.newrelic.agent.instrumentation.methodmatchers.NameMethodMatcher) AccessMethodMatcher(com.newrelic.agent.instrumentation.methodmatchers.AccessMethodMatcher) AnnotationMethodMatcher(com.newrelic.agent.instrumentation.methodmatchers.AnnotationMethodMatcher) ExactMethodMatcher(com.newrelic.agent.instrumentation.methodmatchers.ExactMethodMatcher) AllMethodsMatcher(com.newrelic.agent.instrumentation.methodmatchers.AllMethodsMatcher) Before(org.junit.Before)

Example 4 with AnnotationMethodMatcher

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

the class ClassesMatcherTest method getMatchingClasses_Annotation.

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

Aggregations

AnnotationMethodMatcher (com.newrelic.agent.instrumentation.methodmatchers.AnnotationMethodMatcher)4 Method (com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut.Method)1 AllClassesMatcher (com.newrelic.agent.instrumentation.classmatchers.AllClassesMatcher)1 DefaultClassAndMethodMatcher (com.newrelic.agent.instrumentation.classmatchers.DefaultClassAndMethodMatcher)1 AccessMethodMatcher (com.newrelic.agent.instrumentation.methodmatchers.AccessMethodMatcher)1 AllMethodsMatcher (com.newrelic.agent.instrumentation.methodmatchers.AllMethodsMatcher)1 ExactMethodMatcher (com.newrelic.agent.instrumentation.methodmatchers.ExactMethodMatcher)1 LambdaMethodMatcher (com.newrelic.agent.instrumentation.methodmatchers.LambdaMethodMatcher)1 NameMethodMatcher (com.newrelic.agent.instrumentation.methodmatchers.NameMethodMatcher)1 ReturnTypeMethodMatcher (com.newrelic.agent.instrumentation.methodmatchers.ReturnTypeMethodMatcher)1 Trace (com.newrelic.api.agent.Trace)1 Before (org.junit.Before)1 Test (org.junit.Test)1 Method (org.objectweb.asm.commons.Method)1