Search in sources :

Example 1 with Instrumentation

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

the class ReinstrumentUtilsTest method checkInputMildCardClasses.

@Test
public void checkInputMildCardClasses() {
    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);
    // this should match
    Method m1 = new Method();
    m1.setName("yada");
    pc.getMethod().add(m1);
    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 2 with Instrumentation

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

the class ReinstrumentUtilsTest method checkInputMatchesClasses.

@Test
public void checkInputMatchesClasses() {
    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("getHello");
    MethodParameters params1 = new MethodParameters(new ArrayList<String>());
    m1.setParameters(params1);
    Method m2 = new Method();
    m2.setName("setHello");
    MethodParameters params2 = new MethodParameters(Arrays.asList("boolean"));
    m2.setParameters(params2);
    Method m3 = new Method();
    m3.setName("doTheWork");
    MethodParameters params3 = new MethodParameters(Arrays.asList("java.lang.String", "int"));
    m3.setParameters(params3);
    pc.getMethod().addAll(Arrays.asList(m1, m2, m3));
    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 3 with Instrumentation

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

the class XmlInstrumentValidator method validateInstrumentation.

/**
 * Validates the instrumentation.
 *
 * @param params The command line parameters.
 * @throws ClassNotFoundException Thrown if the class is not found.
 * @throws RuntimeException Thrown if a problem when converting the xml.
 * @throws IllegalArgumentException Thrown when the method or class can not be found on the class path.
 * @throws IOException Thrown if the file can not be read.
 * @throws SAXException Thrown if a problem parsing the document.
 */
protected static void validateInstrumentation(final XmlInstrumentParams params) throws Exception {
    // read in the file - DOM Exception potentially thrown
    Extension extension = ExtensionDomParser.readFile(params.getFile());
    if (params.isDebug()) {
        System.out.println("Xml was successfully read. Starting processing.");
    }
    // attempt to convert to point cuts - RuntimeException
    // potentially thrown
    List<ExtensionClassAndMethodMatcher> convertedPcs = ExtensionConversionUtility.convertToPointCutsForValidation(extension);
    Instrumentation inst = extension.getInstrumentation();
    // this really has already been checked
    if (inst == null) {
        throw new RuntimeException("The instrumentation propery must be set for the extension.");
    }
    List<Pointcut> origPcs = inst.getPointcut();
    if (convertedPcs.size() != origPcs.size()) {
        throw new IllegalArgumentException("The processed number of point cuts does not match the" + "original number of point cuts in the xml. Remove duplicates.");
    }
    for (int i = 0; i < convertedPcs.size(); i++) {
        MethodHolder holder = sortData(origPcs.get(i), params.isDebug());
        verifyPointCut(convertedPcs.get(i), holder);
        verifyAllMethodsAccounted(holder);
    }
}
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)

Example 4 with Instrumentation

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

the class ExtensionDomParserTest method testPrimitiveReturnType.

@Test
public void testPrimitiveReturnType() throws Exception {
    Extension ext = ExtensionDomParser.readFile(getFile(PRIMITIVE_RETURN_TYPE_FILE_PATH));
    Instrumentation inst = ext.getInstrumentation();
    List<Pointcut> thePcs = inst.getPointcut();
    Assert.assertEquals(1, thePcs.size());
    Pointcut pc = thePcs.get(0);
    Assert.assertNull(pc.getClassName());
    Assert.assertEquals("com.company.SomeInterface", pc.getInterfaceName());
    Assert.assertEquals(1, pc.getMethod().size());
    Assert.assertEquals("boolean", pc.getMethod().iterator().next().getReturnType());
    try {
        ExtensionConversionUtility.convertToPointCutsForValidation(ext);
        Assert.fail();
    } catch (XmlException ex) {
        Assert.assertEquals("The return type 'boolean' is not valid.  Primitive types are not allowed.", ex.getMessage());
    }
}
Also used : Extension(com.newrelic.agent.extension.beans.Extension) Pointcut(com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut) XmlException(com.newrelic.agent.extension.util.XmlException) Instrumentation(com.newrelic.agent.extension.beans.Extension.Instrumentation) Test(org.junit.Test)

Example 5 with Instrumentation

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

the class ExtensionDomParserTest method testInterfacePointCut.

@Test
public void testInterfacePointCut() throws Exception {
    Extension ext = ExtensionDomParser.readFile(getFile(INTERFACE_FILE_PATH));
    Instrumentation inst = ext.getInstrumentation();
    List<Pointcut> thePcs = inst.getPointcut();
    Assert.assertEquals(1, thePcs.size());
    Pointcut pc = thePcs.get(0);
    Assert.assertNull(pc.getClassName());
    Assert.assertEquals("javax.servlet.Filter", pc.getInterfaceName());
    // Assert.assertEquals(ClassType.INTERFACE_NAME, pc.getClassType());
    List<ExtensionClassAndMethodMatcher> pcs = ExtensionConversionUtility.convertToPointCutsForValidation(ext);
    Assert.assertNotNull(pcs);
    Assert.assertEquals(1, pcs.size());
    ExtensionClassAndMethodMatcher actual = pcs.get(0);
    Assert.assertTrue(actual.getClassMatcher() instanceof InterfaceMatcher);
}
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) InterfaceMatcher(com.newrelic.agent.instrumentation.classmatchers.InterfaceMatcher) Test(org.junit.Test)

Aggregations

Instrumentation (com.newrelic.agent.extension.beans.Extension.Instrumentation)18 Extension (com.newrelic.agent.extension.beans.Extension)17 Pointcut (com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut)17 Test (org.junit.Test)16 Method (com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut.Method)11 ExtensionClassAndMethodMatcher (com.newrelic.agent.instrumentation.custom.ExtensionClassAndMethodMatcher)8 HashMap (java.util.HashMap)8 HashSet (java.util.HashSet)7 MethodParameters (com.newrelic.agent.extension.beans.MethodParameters)4 AgentHelper.getFile (com.newrelic.agent.AgentHelper.getFile)1 XmlException (com.newrelic.agent.extension.util.XmlException)1 ChildClassMatcher (com.newrelic.agent.instrumentation.classmatchers.ChildClassMatcher)1 InterfaceMatcher (com.newrelic.agent.instrumentation.classmatchers.InterfaceMatcher)1 ExactReturnTypeMethodMatcher (com.newrelic.agent.instrumentation.methodmatchers.ExactReturnTypeMethodMatcher)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1