Search in sources :

Example 1 with MethodParameters

use of com.newrelic.agent.extension.beans.MethodParameters 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 2 with MethodParameters

use of com.newrelic.agent.extension.beans.MethodParameters 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)

Example 3 with MethodParameters

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

the class MethodMatcherTests method createOrMethodMatcherExactOnly.

@Test
public void createOrMethodMatcherExactOnly() throws XmlException {
    Method m1 = new Method();
    m1.setName("m1");
    m1.setParameters(new MethodParameters(Arrays.asList("int")));
    Method m2 = new Method();
    m2.setName("m2");
    m2.setParameters(new MethodParameters(Arrays.asList("int[]")));
    Method m3 = new Method();
    m3.setName("m3");
    m3.setParameters(new MethodParameters(null));
    Method m4 = new Method();
    m4.setName("m4");
    m4.setParameters(new MethodParameters(Collections.EMPTY_LIST));
    MethodMatcher matcher = MethodMatcherUtility.createMethodMatcher("myclass", Arrays.asList(m1, m2, m3, m4), 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.assertFalse(matcher.matches(MethodMatcher.UNSPECIFIED_ACCESS, "m2", "()V", com.google.common.collect.ImmutableSet.<String>of()));
    Assert.assertFalse(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.assertTrue(matcher.matches(MethodMatcher.UNSPECIFIED_ACCESS, "m3", "()V", com.google.common.collect.ImmutableSet.<String>of()));
    Assert.assertFalse(matcher.matches(MethodMatcher.UNSPECIFIED_ACCESS, "m3", "(I)V", com.google.common.collect.ImmutableSet.<String>of()));
    Assert.assertTrue(matcher.matches(MethodMatcher.UNSPECIFIED_ACCESS, "m4", "()V", com.google.common.collect.ImmutableSet.<String>of()));
    Assert.assertFalse(matcher.matches(MethodMatcher.UNSPECIFIED_ACCESS, "m4", "(I)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)

Example 4 with MethodParameters

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

the class MethodMatcherTests method createMethodMatcherExactOnly.

@Test
public void createMethodMatcherExactOnly() throws XmlException {
    Method m1 = new Method();
    m1.setName("m1");
    m1.setParameters(new MethodParameters(Arrays.asList("int")));
    MethodMatcher matcher = MethodMatcherUtility.createMethodMatcher("myclass", Arrays.asList(m1), new HashMap<String, MethodMapper>(), "EXT");
    Assert.assertNotNull(matcher);
    Assert.assertTrue(matcher + " not of type ExactParamsMethodMatcher", matcher instanceof ExactParamsMethodMatcher);
    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.assertFalse(matcher.matches(MethodMatcher.UNSPECIFIED_ACCESS, "m2", "()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)

Example 5 with MethodParameters

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

the class OrExactParamMethodMatcherTest method testMatchingBasicEmptyParameterTypeMultiples.

@Test
public void testMatchingBasicEmptyParameterTypeMultiples() throws Exception {
    Method m1 = new Method();
    m1.setName("hello");
    List<String> pts = new ArrayList<>();
    m1.setParameters(new MethodParameters(pts));
    Method m2 = new Method();
    m2.setName("configure");
    m2.setParameters(new MethodParameters(null));
    List<Method> methods = new ArrayList<>();
    methods.add(m1);
    methods.add(m2);
    Map<String, MethodMapper> mapper = new HashMap<>();
    MethodMatcher matcher1 = MethodMatcherUtility.createMethodMatcher("classy", methods, mapper, "EXT");
    Assert.assertTrue(matcher1.matches(MethodMatcher.UNSPECIFIED_ACCESS, "hello", "()V", com.google.common.collect.ImmutableSet.<String>of()));
    Assert.assertTrue(matcher1.matches(MethodMatcher.UNSPECIFIED_ACCESS, "configure", "()V", com.google.common.collect.ImmutableSet.<String>of()));
    // different class and so this should be okay
    MethodMatcher matcher2 = MethodMatcherUtility.createMethodMatcher("classing", methods, mapper, "EXT");
    Assert.assertTrue(matcher2.matches(MethodMatcher.UNSPECIFIED_ACCESS, "hello", "()V", com.google.common.collect.ImmutableSet.<String>of()));
    Assert.assertTrue(matcher2.matches(MethodMatcher.UNSPECIFIED_ACCESS, "configure", "()V", com.google.common.collect.ImmutableSet.<String>of()));
    try {
        // we should fail here
        MethodMatcherUtility.createMethodMatcher("classing", methods, mapper, "EXT");
        Assert.fail("Should fail due to methods already being added.");
    } catch (Exception e) {
    // should get to here
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Method(com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut.Method) MethodParameters(com.newrelic.agent.extension.beans.MethodParameters) MethodMapper(com.newrelic.agent.extension.util.MethodMapper) XmlException(com.newrelic.agent.extension.util.XmlException) Test(org.junit.Test)

Aggregations

Method (com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut.Method)14 MethodParameters (com.newrelic.agent.extension.beans.MethodParameters)14 Test (org.junit.Test)14 HashMap (java.util.HashMap)11 MethodMapper (com.newrelic.agent.extension.util.MethodMapper)10 ArrayList (java.util.ArrayList)7 Extension (com.newrelic.agent.extension.beans.Extension)4 Instrumentation (com.newrelic.agent.extension.beans.Extension.Instrumentation)4 Pointcut (com.newrelic.agent.extension.beans.Extension.Instrumentation.Pointcut)4 HashSet (java.util.HashSet)4 XmlException (com.newrelic.agent.extension.util.XmlException)1