Search in sources :

Example 16 with Method

use of com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut.Method in project newrelic-java-agent by newrelic.

the class ReinstrumentUtilsTest method checkInputClassesNullPointers3.

@Test
public void checkInputClassesNullPointers3() {
    SampleObject obj = new SampleObject();
    ReinstrumentResult result = new ReinstrumentResult();
    obj.getClass().getClassLoader();
    Set<ClassLoader> loaders = new HashSet<>();
    loaders.add(this.getClass().getClassLoader());
    Extension ext = new Extension();
    Instrumentation inst = new Instrumentation();
    ext.setInstrumentation(inst);
    Pointcut pc = new Pointcut();
    inst.getPointcut().add(pc);
    // no class name set
    Method m1 = new Method();
    m1.setName("falala");
    MethodParameters params1 = new MethodParameters(new ArrayList<String>());
    m1.setParameters(params1);
    Map<String, Class<?>> daClasses = new HashMap<>();
    daClasses.put(SampleObject.class.getName(), SampleObject.class);
    ReinstrumentUtils.checkInputClasses(result, loaders, ext, daClasses);
    Object actual = result.getStatusMap().get(ReinstrumentResult.ERROR_KEY);
    Assert.assertNull("There should not have been an error. Errors: " + actual, actual);
}
Also used : HashMap(java.util.HashMap) Instrumentation(com.newrelic.agent.extension.beans.Extension.Instrumentation) Method(com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut.Method) Extension(com.newrelic.agent.extension.beans.Extension) Pointcut(com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut) MethodParameters(com.newrelic.agent.extension.beans.MethodParameters) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 17 with Method

use of com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut.Method in project newrelic-java-agent by newrelic.

the class ReinstrumentUtilsTest method checkInputClassesNullPointers4.

@Test
public void checkInputClassesNullPointers4() {
    SampleObject obj = new SampleObject();
    ReinstrumentResult result = new ReinstrumentResult();
    obj.getClass().getClassLoader();
    Set<ClassLoader> loaders = new HashSet<>();
    loaders.add(this.getClass().getClassLoader());
    Extension ext = new Extension();
    Instrumentation inst = new Instrumentation();
    ext.setInstrumentation(inst);
    Pointcut pc = new Pointcut();
    inst.getPointcut().add(pc);
    setClassName(pc, SampleObject.class);
    Method m1 = new Method();
    // no method name
    MethodParameters params1 = new MethodParameters(new ArrayList<String>());
    m1.setParameters(params1);
    Map<String, Class<?>> daClasses = new HashMap<>();
    daClasses.put(SampleObject.class.getName(), SampleObject.class);
    ReinstrumentUtils.checkInputClasses(result, loaders, ext, daClasses);
    Object actual = result.getStatusMap().get(ReinstrumentResult.ERROR_KEY);
    Assert.assertNull("There should not have been an error. Errors: " + actual, actual);
}
Also used : HashMap(java.util.HashMap) Instrumentation(com.newrelic.agent.extension.beans.Extension.Instrumentation) Method(com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut.Method) Extension(com.newrelic.agent.extension.beans.Extension) Pointcut(com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut) MethodParameters(com.newrelic.agent.extension.beans.MethodParameters) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 18 with Method

use of com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut.Method in project newrelic-java-agent by newrelic.

the class ReinstrumentUtilsTest method checkInputClassesNullPointers5.

@Test
public void checkInputClassesNullPointers5() {
    SampleObject obj = new SampleObject();
    ReinstrumentResult result = new ReinstrumentResult();
    obj.getClass().getClassLoader();
    Set<ClassLoader> loaders = new HashSet<>();
    loaders.add(this.getClass().getClassLoader());
    Extension ext = new Extension();
    Instrumentation inst = new Instrumentation();
    ext.setInstrumentation(inst);
    Pointcut pc = new Pointcut();
    inst.getPointcut().add(pc);
    setClassName(pc, SampleObject.class);
    Method m1 = new Method();
    m1.setName("falala");
    // no method parameters set
    Map<String, Class<?>> daClasses = new HashMap<>();
    daClasses.put(SampleObject.class.getName(), SampleObject.class);
    ReinstrumentUtils.checkInputClasses(result, loaders, ext, daClasses);
    Object actual = result.getStatusMap().get(ReinstrumentResult.ERROR_KEY);
    Assert.assertNull("There should not have been an error. Errors: " + actual, actual);
}
Also used : HashMap(java.util.HashMap) Instrumentation(com.newrelic.agent.extension.beans.Extension.Instrumentation) Method(com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut.Method) Extension(com.newrelic.agent.extension.beans.Extension) Pointcut(com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 19 with Method

use of com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut.Method 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 20 with Method

use of com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut.Method in project newrelic-java-agent by newrelic.

the class ExtensionDomParserTest method testDomOnePointCutWithReadString.

@Test
public void testDomOnePointCutWithReadString() throws Exception {
    File file = getFile(FILE_PATH_1);
    Extension ext = ExtensionDomParser.readStringCatchException(readInFile(file));
    Assert.assertNotNull(ext);
    // ext attributes
    Assert.assertEquals("test1", ext.getName());
    Assert.assertEquals(1.0, ext.getVersion(), .001);
    Assert.assertTrue(ext.isEnabled());
    Instrumentation inst = ext.getInstrumentation();
    Assert.assertEquals("PREFIX", inst.getMetricPrefix());
    List<Pointcut> thePcs = inst.getPointcut();
    Assert.assertEquals(1, thePcs.size());
    Pointcut pc = thePcs.get(0);
    Assert.assertTrue(pc.isTransactionStartPoint());
    Assert.assertFalse(pc.isIgnoreTransaction());
    Assert.assertFalse(pc.isExcludeFromTransactionTrace());
    Assert.assertNull(pc.getMetricNameFormat());
    Assert.assertEquals("test.CustomExampleTest", pc.getClassName().getValue());
    List<Method> methods = pc.getMethod();
    Assert.assertEquals(2, methods.size());
    Assert.assertEquals("run", methods.get(0).getName());
    Assert.assertEquals("finish", methods.get(1).getName());
    Assert.assertEquals("(Ljava/lang/String;Ljava/lang/String;)", MethodParameters.getDescriptor(methods.get(0).getParameters()));
    Assert.assertEquals("(F)", MethodParameters.getDescriptor(methods.get(1).getParameters()));
    List<ExtensionClassAndMethodMatcher> pcs = ExtensionConversionUtility.convertToPointCutsForValidation(ext);
    Assert.assertNotNull(pcs);
    Assert.assertEquals(1, pcs.size());
    ExtensionClassAndMethodMatcher actual = pcs.get(0);
    // test method matching
    Assert.assertTrue(actual.getMethodMatcher().matches(MethodMatcher.UNSPECIFIED_ACCESS, "run", "(Ljava/lang/String;Ljava/lang/String;)V", com.google.common.collect.ImmutableSet.<String>of()));
    Assert.assertFalse(actual.getMethodMatcher().matches(MethodMatcher.UNSPECIFIED_ACCESS, "finish", "(Ljava/lang/String;Ljava/lang/String;)V", com.google.common.collect.ImmutableSet.<String>of()));
    Assert.assertTrue(actual.getMethodMatcher().matches(MethodMatcher.UNSPECIFIED_ACCESS, "finish", "(F)V", com.google.common.collect.ImmutableSet.<String>of()));
    Assert.assertFalse(actual.getMethodMatcher().matches(MethodMatcher.UNSPECIFIED_ACCESS, "run", "(F)V", com.google.common.collect.ImmutableSet.<String>of()));
    Assert.assertFalse(actual.getMethodMatcher().matches(MethodMatcher.UNSPECIFIED_ACCESS, "finish", "(B)V", com.google.common.collect.ImmutableSet.<String>of()));
    // test class matching
    Assert.assertEquals(1, actual.getClassMatcher().getClassNames().size());
    Assert.assertEquals("test/CustomExampleTest", actual.getClassMatcher().getClassNames().toArray()[0]);
}
Also used : Extension(com.newrelic.agent.extension.beans.Extension) Pointcut(com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut) ExtensionClassAndMethodMatcher(com.newrelic.agent.instrumentation.custom.ExtensionClassAndMethodMatcher) Instrumentation(com.newrelic.agent.extension.beans.Extension.Instrumentation) Method(com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut.Method) AgentHelper.getFile(com.newrelic.agent.AgentHelper.getFile) File(java.io.File) Test(org.junit.Test)

Aggregations

Method (com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut.Method)27 Test (org.junit.Test)25 MethodParameters (com.newrelic.agent.extension.beans.MethodParameters)14 HashMap (java.util.HashMap)14 Pointcut (com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut)13 MethodMapper (com.newrelic.agent.extension.util.MethodMapper)13 Extension (com.newrelic.agent.extension.beans.Extension)12 Instrumentation (com.newrelic.agent.extension.beans.Extension.Instrumentation)11 ArrayList (java.util.ArrayList)9 HashSet (java.util.HashSet)6 ExtensionClassAndMethodMatcher (com.newrelic.agent.instrumentation.custom.ExtensionClassAndMethodMatcher)5 AnnotationMethodMatcher (com.newrelic.agent.instrumentation.methodmatchers.AnnotationMethodMatcher)2 LambdaMethodMatcher (com.newrelic.agent.instrumentation.methodmatchers.LambdaMethodMatcher)2 ReturnTypeMethodMatcher (com.newrelic.agent.instrumentation.methodmatchers.ReturnTypeMethodMatcher)2 AgentHelper.getFile (com.newrelic.agent.AgentHelper.getFile)1 XmlException (com.newrelic.agent.extension.util.XmlException)1 MethodMatcher (com.newrelic.agent.instrumentation.methodmatchers.MethodMatcher)1 ParameterAttributeName (com.newrelic.agent.instrumentation.tracing.ParameterAttributeName)1 File (java.io.File)1