Search in sources :

Example 1 with NameMethodMatcher

use of com.newrelic.agent.instrumentation.methodmatchers.NameMethodMatcher 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 2 with NameMethodMatcher

use of com.newrelic.agent.instrumentation.methodmatchers.NameMethodMatcher 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)

Aggregations

NameMethodMatcher (com.newrelic.agent.instrumentation.methodmatchers.NameMethodMatcher)2 Parameters (com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut.Method.Parameters)1 MethodParameters (com.newrelic.agent.extension.beans.MethodParameters)1 AccessMethodMatcher (com.newrelic.agent.instrumentation.methodmatchers.AccessMethodMatcher)1 AllMethodsMatcher (com.newrelic.agent.instrumentation.methodmatchers.AllMethodsMatcher)1 AnnotationMethodMatcher (com.newrelic.agent.instrumentation.methodmatchers.AnnotationMethodMatcher)1 ExactMethodMatcher (com.newrelic.agent.instrumentation.methodmatchers.ExactMethodMatcher)1 ExactReturnTypeMethodMatcher (com.newrelic.agent.instrumentation.methodmatchers.ExactReturnTypeMethodMatcher)1 Before (org.junit.Before)1 Type (org.objectweb.asm.Type)1