use of com.newrelic.agent.instrumentation.context.ClassMatchVisitorFactory in project newrelic-java-agent by newrelic.
the class ClassWeaverService method loadExternalWeavePackages.
/**
* Load new instrumentation packages from disk and put any new matchers in the matchers collection.
*
* @param weaveExtensions
*/
private Collection<ClassMatchVisitorFactory> loadExternalWeavePackages(Collection<File> weaveExtensions) {
Collection<ClassMatchVisitorFactory> matchers = new HashSet<>();
for (File weaveExtension : weaveExtensions) {
try (JarInputStream stream = new JarInputStream(new FileInputStream(weaveExtension))) {
AgentConfig agentConfig = ServiceFactory.getConfigService().getDefaultAgentConfig();
WeavePackageConfig weaveConfig = createWeavePackageConfig(stream, weaveExtension.getAbsolutePath(), instrumentation, WeavePackageType.CUSTOM, agentConfig);
ClassTransformerConfig classTransformerConfig = agentConfig.getClassTransformerConfig();
String instrName = weaveConfig.getName();
if (weavePackageManager.isRegistered(instrName)) {
weavePackageManager.deregister(instrName);
this.externalWeavePackages.remove(weaveExtension.getAbsolutePath());
}
if (!classTransformerConfig.isWeavePackageEnabled(weaveConfig)) {
if (weaveConfig.isEnabled()) {
// Only log this if the module has been explicitly disabled (not if it is disabled by default)
LOG.log(Level.INFO, "Instrumentation {0} is disabled. Skipping.", instrName);
}
continue;
}
WeavePackage externalPackage = WeavePackage.createWeavePackage(stream, weaveConfig);
if (externalPackage.getPackageViolations().size() > 0) {
LOG.log(Level.FINER, "skip loading external weave package: {0}", instrName);
for (WeaveViolation violation : externalPackage.getPackageViolations()) {
LOG.log(Level.FINER, "\t{0}", violation);
}
} else {
weavePackageManager.register(externalPackage);
externalWeavePackages.put(weaveExtension.getAbsolutePath(), weaveConfig.getName());
}
} catch (Exception e) {
LOG.log(Level.FINE, e, "Error reading weave extension {0}", weaveExtension.getAbsolutePath());
}
}
return matchers;
}
use of com.newrelic.agent.instrumentation.context.ClassMatchVisitorFactory in project newrelic-java-agent by newrelic.
the class ApiImplementationUpdateTest method testMatcherMissingMethods.
@Test
public void testMatcherMissingMethods() throws Exception {
ArgumentCaptor<ClassMatchVisitorFactory> arg = ArgumentCaptor.forClass(ClassMatchVisitorFactory.class);
InstrumentationContext iInstrumentationContext = Mockito.mock(InstrumentationContext.class);
// remove required methods from Request implementation class
Class<?> clazz = RequestImpl.class;
Set<Method> methodsToRemove = new HashSet<>();
methodsToRemove.add(new Method("getHeaderType", "()Lcom/newrelic/api/agent/HeaderType;"));
byte[] classBytes = removeMethods(clazz, methodsToRemove);
// verify missing methods
RequireMethodsAdapter adapter = getRequireMethodsAdapter(clazz, Request.class, REQUEST_METHODS);
expectMissingMethods(adapter, classBytes);
ApiImplementationUpdate transformer = new ApiImplementationUpdate();
ClassMatchVisitorFactory matcher = transformer.getMatcher();
ClassReader reader = new ClassReader(new ByteArrayInputStream(classBytes));
ClassVisitor visitor = matcher.newClassMatchVisitor(clazz.getClassLoader(), null, reader, null, iInstrumentationContext);
reader.accept(visitor, ClassReader.SKIP_CODE);
Mockito.verify(iInstrumentationContext, Mockito.only()).putMatch(arg.capture(), Mockito.<Match>anyObject());
Assert.assertSame(matcher, arg.getValue());
}
use of com.newrelic.agent.instrumentation.context.ClassMatchVisitorFactory in project newrelic-java-agent by newrelic.
the class ApiImplementationUpdateTest method testMatcher.
@Test
public void testMatcher() throws Exception {
InstrumentationContext iInstrumentationContext = Mockito.mock(InstrumentationContext.class);
Class<?> clazz = RequestImpl.class;
Set<Method> methodsToRemove = new HashSet<>();
byte[] classBytes = removeMethods(clazz, methodsToRemove);
// verify no missing methods
RequireMethodsAdapter adapter = getRequireMethodsAdapter(clazz, Request.class, REQUEST_METHODS);
expectNoMissingMethods(adapter, classBytes);
ApiImplementationUpdate transformer = new ApiImplementationUpdate();
ClassMatchVisitorFactory matcher = transformer.getMatcher();
ClassReader reader = new ClassReader(new ByteArrayInputStream(classBytes));
ClassVisitor visitor = matcher.newClassMatchVisitor(clazz.getClassLoader(), null, reader, null, iInstrumentationContext);
reader.accept(visitor, ClassReader.SKIP_CODE);
Mockito.verify(iInstrumentationContext, Mockito.never()).putMatch(Mockito.<ClassMatchVisitorFactory>anyObject(), Mockito.<Match>anyObject());
}
use of com.newrelic.agent.instrumentation.context.ClassMatchVisitorFactory in project newrelic-java-agent by newrelic.
the class OptimizedClassMatcherTest method testSkipObjectMethods_exactMatch.
@Test
public void testSkipObjectMethods_exactMatch() throws IOException {
List<MethodMatcher> matchers = new ArrayList<>();
for (Method m : OBJECT_METHODS) {
matchers.add(new ExactMethodMatcher(m.getName(), m.getDescriptor()));
}
ClassMatchVisitorFactory matcher = OptimizedClassMatcherBuilder.newBuilder().addClassMethodMatcher(new DefaultClassAndMethodMatcher(new AllClassesMatcher(), OrMethodMatcher.getMethodMatcher(matchers))).build();
Match match = getMatch(matcher, MyObject.class);
Assert.assertNull(match);
}
use of com.newrelic.agent.instrumentation.context.ClassMatchVisitorFactory 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());
}
Aggregations