Search in sources :

Example 1 with Method

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

the class ExtensionConversionUtility method getParameterAttributeNames.

private static List<ParameterAttributeName> getParameterAttributeNames(List<Method> methods) {
    List<ParameterAttributeName> reportedParams = new ArrayList<>();
    for (Method m : methods) {
        if (m.getParameters() != null && m.getParameters().getType() != null) {
            for (int i = 0; i < m.getParameters().getType().size(); i++) {
                com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut.Method.Parameters.Type t = m.getParameters().getType().get(i);
                if (t.getAttributeName() != null) {
                    try {
                        MethodMatcher methodMatcher = MethodMatcherUtility.createMethodMatcher("DummyClassName", m, new HashMap<String, MethodMapper>(), "");
                        ParameterAttributeName reportedParam = new ParameterAttributeName(i, t.getAttributeName(), methodMatcher);
                        reportedParams.add(reportedParam);
                    } catch (Exception e) {
                        Agent.LOG.log(Level.FINEST, e, e.getMessage());
                    }
                }
            }
        }
    }
    return reportedParams;
}
Also used : ArrayList(java.util.ArrayList) Method(com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut.Method) Pointcut(com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut) Extension(com.newrelic.agent.extension.beans.Extension) ParameterAttributeName(com.newrelic.agent.instrumentation.tracing.ParameterAttributeName) AnnotationMethodMatcher(com.newrelic.agent.instrumentation.methodmatchers.AnnotationMethodMatcher) LambdaMethodMatcher(com.newrelic.agent.instrumentation.methodmatchers.LambdaMethodMatcher) ExtensionClassAndMethodMatcher(com.newrelic.agent.instrumentation.custom.ExtensionClassAndMethodMatcher) ReturnTypeMethodMatcher(com.newrelic.agent.instrumentation.methodmatchers.ReturnTypeMethodMatcher) MethodMatcher(com.newrelic.agent.instrumentation.methodmatchers.MethodMatcher)

Example 2 with Method

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

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

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

the class MethodMatcherTests method createOrMethodMatcherNameOnly.

@Test
public void createOrMethodMatcherNameOnly() throws XmlException {
    Method m1 = new Method();
    m1.setName("m1");
    Method m2 = new Method();
    m2.setName("m2");
    MethodMatcher matcher = MethodMatcherUtility.createMethodMatcher("myclass", Arrays.asList(m1, m2), new HashMap<String, MethodMapper>(), "EXT");
    Assert.assertNotNull(matcher);
    Assert.assertTrue(matcher + " not of type OrMethodMatcher", matcher instanceof OrMethodMatcher);
    Assert.assertTrue(matcher.matches(MethodMatcher.UNSPECIFIED_ACCESS, "m1", "()V", com.google.common.collect.ImmutableSet.<String>of()));
    Assert.assertTrue(matcher.matches(MethodMatcher.UNSPECIFIED_ACCESS, "m1", "(I)V", com.google.common.collect.ImmutableSet.<String>of()));
    Assert.assertTrue(matcher.matches(MethodMatcher.UNSPECIFIED_ACCESS, "m1", "([I)V", com.google.common.collect.ImmutableSet.<String>of()));
    Assert.assertTrue(matcher.matches(MethodMatcher.UNSPECIFIED_ACCESS, "m2", "()V", com.google.common.collect.ImmutableSet.<String>of()));
    Assert.assertTrue(matcher.matches(MethodMatcher.UNSPECIFIED_ACCESS, "m2", "(I)V", com.google.common.collect.ImmutableSet.<String>of()));
    Assert.assertTrue(matcher.matches(MethodMatcher.UNSPECIFIED_ACCESS, "m2", "([I)V", com.google.common.collect.ImmutableSet.<String>of()));
    Assert.assertFalse(matcher.matches(MethodMatcher.UNSPECIFIED_ACCESS, "m3", "()V", com.google.common.collect.ImmutableSet.<String>of()));
}
Also used : Method(com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut.Method) MethodMapper(com.newrelic.agent.extension.util.MethodMapper) Test(org.junit.Test)

Example 5 with Method

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

the class MethodMatcherTests method createOrMethodMatcherMix.

@Test
public void createOrMethodMatcherMix() throws XmlException {
    Method m1 = new Method();
    m1.setName("m1");
    m1.setParameters(new MethodParameters(Arrays.asList("int")));
    Method m2 = new Method();
    m2.setName("m2");
    MethodMatcher matcher = MethodMatcherUtility.createMethodMatcher("myclass", Arrays.asList(m1, m2), new HashMap<String, MethodMapper>(), "EXT");
    Assert.assertNotNull(matcher);
    Assert.assertTrue(matcher + " not of type OrMethodMatcher", matcher instanceof OrMethodMatcher);
    Assert.assertFalse(matcher.matches(MethodMatcher.UNSPECIFIED_ACCESS, "m1", "()V", com.google.common.collect.ImmutableSet.<String>of()));
    Assert.assertTrue(matcher.matches(MethodMatcher.UNSPECIFIED_ACCESS, "m1", "(I)V", com.google.common.collect.ImmutableSet.<String>of()));
    Assert.assertFalse(matcher.matches(MethodMatcher.UNSPECIFIED_ACCESS, "m1", "([I)V", com.google.common.collect.ImmutableSet.<String>of()));
    Assert.assertTrue(matcher.matches(MethodMatcher.UNSPECIFIED_ACCESS, "m2", "()V", com.google.common.collect.ImmutableSet.<String>of()));
    Assert.assertTrue(matcher.matches(MethodMatcher.UNSPECIFIED_ACCESS, "m2", "(I)V", com.google.common.collect.ImmutableSet.<String>of()));
    Assert.assertTrue(matcher.matches(MethodMatcher.UNSPECIFIED_ACCESS, "m2", "([I)V", com.google.common.collect.ImmutableSet.<String>of()));
    Assert.assertFalse(matcher.matches(MethodMatcher.UNSPECIFIED_ACCESS, "m3", "()V", com.google.common.collect.ImmutableSet.<String>of()));
}
Also used : Method(com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut.Method) MethodParameters(com.newrelic.agent.extension.beans.MethodParameters) MethodMapper(com.newrelic.agent.extension.util.MethodMapper) 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