use of com.newrelic.agent.instrumentation.InstrumentedMethod in project newrelic-java-agent by newrelic.
the class WeaveTest method testTraceUtilityClass.
@Test
public void testTraceUtilityClass() throws NoSuchMethodException, SecurityException, ClassNotFoundException {
Class<?> clazz = Class.forName("com.nr.instrumentation.instrumentation.weaver.NotWeaved");
AgentBridge.instrumentation.retransformUninstrumentedClass(clazz);
Method method = clazz.getDeclaredMethod("notWeavedButStillTraced");
InstrumentedMethod annotation = method.getAnnotation(InstrumentedMethod.class);
Assert.assertNotNull(annotation);
Assert.assertEquals(InstrumentationType.TracedWeaveInstrumentation, annotation.instrumentationTypes()[0]);
Assert.assertEquals("Weave Test", annotation.instrumentationNames()[0]);
}
use of com.newrelic.agent.instrumentation.InstrumentedMethod in project newrelic-java-agent by newrelic.
the class FinalClassTransformer method addModifiedMethodAnnotation.
private ClassVisitor addModifiedMethodAnnotation(ClassVisitor cv, final InstrumentationContext context, final ClassLoader loader) {
return new ClassVisitor(WeaveUtils.ASM_API_LEVEL, cv) {
private String className;
@Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
className = name;
super.visit(version, access, name, signature, superName, interfaces);
}
/**
* @see InstrumentedMethod#instrumentationTypes()
* @see InstrumentedMethod#instrumentationNames()
* @see InstrumentedMethod#dispatcher()
*/
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
Method method = new Method(name, desc);
if (context.isModified(method)) {
if (loader != null) {
TraceDetails traceDetails = context.getTraceInformation().getTraceAnnotations().get(method);
boolean dispatcher = false;
if (traceDetails != null) {
dispatcher = traceDetails.dispatcher();
}
AnnotationVisitor av = mv.visitAnnotation(Type.getDescriptor(InstrumentedMethod.class), true);
av.visit("dispatcher", dispatcher);
List<String> instrumentationNames = new ArrayList<>();
List<InstrumentationType> instrumentationTypes = new ArrayList<>();
Level logLevel = Level.FINER;
if (traceDetails != null) {
if (traceDetails.instrumentationSourceNames() != null) {
instrumentationNames.addAll(traceDetails.instrumentationSourceNames());
}
if (traceDetails.instrumentationTypes() != null) {
for (InstrumentationType type : traceDetails.instrumentationTypes()) {
instrumentationTypes.add(type);
}
}
if (traceDetails.isCustom()) {
logLevel = Level.FINE;
}
}
PointCut pointCut = context.getOldStylePointCut(method);
if (pointCut != null) {
instrumentationNames.add(pointCut.getClass().getName());
instrumentationTypes.add(InstrumentationType.Pointcut);
}
Collection<String> instrumentationPackages = context.getMergeInstrumentationPackages(method);
if (instrumentationPackages != null && !instrumentationPackages.isEmpty()) {
for (String current : instrumentationPackages) {
instrumentationNames.add(current);
instrumentationTypes.add(InstrumentationType.WeaveInstrumentation);
}
}
if (instrumentationNames.size() == 0) {
instrumentationNames.add("Unknown");
Agent.LOG.finest("Unknown instrumentation source for " + className + '.' + method);
}
if (instrumentationTypes.size() == 0) {
instrumentationTypes.add(InstrumentationType.Unknown);
Agent.LOG.finest("Unknown instrumentation type for " + className + '.' + method);
}
AnnotationVisitor visitArrayName = av.visitArray("instrumentationNames");
for (String current : instrumentationNames) {
// the key on this is ignored
visitArrayName.visit("", current);
}
visitArrayName.visitEnd();
AnnotationVisitor visitArrayType = av.visitArray("instrumentationTypes");
for (InstrumentationType type : instrumentationTypes) {
// the key on this is ignored
visitArrayType.visitEnum("", Type.getDescriptor(InstrumentationType.class), type.toString());
}
visitArrayType.visitEnd();
av.visitEnd();
if (Agent.LOG.isLoggable(logLevel)) {
Agent.LOG.log(logLevel, "Instrumented " + Type.getObjectType(className).getClassName() + '.' + method + ", " + instrumentationTypes + ", " + instrumentationNames);
}
}
}
return mv;
}
};
}
use of com.newrelic.agent.instrumentation.InstrumentedMethod in project newrelic-java-agent by newrelic.
the class AgentTest method testAnnotations.
@Test
public void testAnnotations() throws SecurityException, NoSuchMethodException {
InstrumentedClass annotation = HttpServlet.class.getAnnotation(InstrumentedClass.class);
Assert.assertNotNull(annotation);
Assert.assertFalse(annotation.legacy());
Method m = HttpServlet.class.getMethod("service", ServletRequest.class, ServletResponse.class);
Assert.assertNotNull(m);
InstrumentedMethod methodAnnotation = m.getAnnotation(InstrumentedMethod.class);
Assert.assertNotNull(methodAnnotation);
Assert.assertEquals(InstrumentationType.TracedWeaveInstrumentation, methodAnnotation.instrumentationTypes()[0]);
// Assert.assertEquals(HttpServletPointCut.class.getName(), methodAnnotation.instrumentationName());
// m = HttpServlet.class.getMethod("service", HttpServletRequest.class, HttpServletResponse.class);
// annotations = m.getAnnotations();
// Assert.assertTrue(annotations.length == 0);
// Assert.assertTrue(Arrays.asList(annotations).toString(), annotations[0] instanceof Instrumented);
}
use of com.newrelic.agent.instrumentation.InstrumentedMethod in project newrelic-java-agent by newrelic.
the class WeaveTest method testTraceNoImpl.
@Test
public void testTraceNoImpl() throws NoSuchMethodException, SecurityException {
Method method = TestClass.class.getDeclaredMethod("fooBar");
InstrumentedMethod annotation = method.getAnnotation(InstrumentedMethod.class);
Assert.assertNotNull(annotation);
Assert.assertEquals(InstrumentationType.TracedWeaveInstrumentation, annotation.instrumentationTypes()[0]);
Assert.assertEquals("Weave Test", annotation.instrumentationNames()[0]);
}
use of com.newrelic.agent.instrumentation.InstrumentedMethod in project newrelic-java-agent by newrelic.
the class WeaveTest method annotationNotTraced.
@Test
public void annotationNotTraced() throws NoSuchMethodException, SecurityException {
Method method = TestClass.class.getDeclaredMethod("integerTest");
InstrumentedMethod annotation = method.getAnnotation(InstrumentedMethod.class);
Assert.assertNotNull(annotation);
Assert.assertEquals(InstrumentationType.WeaveInstrumentation, annotation.instrumentationTypes()[0]);
Assert.assertEquals("Weave Test", annotation.instrumentationNames()[0]);
}
Aggregations