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());
}
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));
}
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.");
}
}
}
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()));
}
Aggregations