Search in sources :

Example 41 with TransformCallback

use of com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback in project pinpoint by naver.

the class VertxPlugin method addHandlerInterceptor.

private void addHandlerInterceptor(final List<String> basePackageNames) {
    // basepackageNames AND io.vertx.core.Handler
    final Matcher matcher = Matchers.newPackageBasedMatcher(basePackageNames, new InterfaceInternalNameMatcherOperand("io.vertx.core.Handler", true));
    transformTemplate.transform(matcher, new TransformCallback() {

        @Override
        public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException {
            final InstrumentClass target = instrumentor.getInstrumentClass(classLoader, className, classfileBuffer);
            if (!target.isInterceptable()) {
                return null;
            }
            target.addField(AsyncContextAccessor.class);
            final InstrumentMethod handleMethod = target.getDeclaredMethod("handle", "java.lang.Object");
            if (handleMethod != null) {
                handleMethod.addInterceptor(HandlerInterceptor.class);
            }
            return target.toBytecode();
        }
    });
}
Also used : InterfaceInternalNameMatcherOperand(com.navercorp.pinpoint.bootstrap.instrument.matcher.operand.InterfaceInternalNameMatcherOperand) ProtectionDomain(java.security.ProtectionDomain) InstrumentException(com.navercorp.pinpoint.bootstrap.instrument.InstrumentException) Matcher(com.navercorp.pinpoint.bootstrap.instrument.matcher.Matcher) Instrumentor(com.navercorp.pinpoint.bootstrap.instrument.Instrumentor) HandlerInterceptor(com.navercorp.pinpoint.plugin.vertx.interceptor.HandlerInterceptor) AsyncContextAccessor(com.navercorp.pinpoint.bootstrap.async.AsyncContextAccessor) TransformCallback(com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback) InstrumentClass(com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass) InstrumentMethod(com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod)

Example 42 with TransformCallback

use of com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback in project pinpoint by naver.

the class AccessorInjectionTest method testAddSetter.

@Test
public void testAddSetter() throws Exception {
    final TestClassLoader loader = getTestClassLoader();
    final String targetClassName = "com.navercorp.pinpoint.test.javasssit.mock.TestObject4";
    loader.addTransformer(targetClassName, new TransformCallback() {

        @Override
        public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException {
            try {
                logger.info("modify cl:{}", classLoader);
                InstrumentClass testClass = instrumentor.getInstrumentClass(classLoader, className, classfileBuffer);
                testClass.addSetter(IntSetter.class, "intValue");
                testClass.addSetter(IntArraySetter.class, "intValues");
                testClass.addSetter(IntegerArraySetter.class, "integerValues");
                return testClass.toBytecode();
            } catch (InstrumentException e) {
                throw new RuntimeException(e.getMessage(), e);
            }
        }
    });
    Object testObject = loader.loadClass(targetClassName).newInstance();
    Class<?> intSetter = loader.loadClass(IntSetter.class.getName());
    Class<?> intsSetter = loader.loadClass(IntArraySetter.class.getName());
    Class<?> integersSetter = loader.loadClass(IntegerArraySetter.class.getName());
    Assert.assertTrue(intSetter.isInstance(testObject));
    Assert.assertTrue(intsSetter.isInstance(testObject));
    Assert.assertTrue(integersSetter.isInstance(testObject));
    int intValue = 99;
    int[] intValues = { 99, 100 };
    Integer[] integerValues = { 99, 100 };
    Method setInt = intSetter.getMethod("_$PINPOINT$_setInt", int.class);
    setInt.invoke(testObject, intValue);
    Method getInt = testObject.getClass().getMethod("getIntValue");
    Assert.assertEquals(intValue, getInt.invoke(testObject));
    Method setInts = intsSetter.getMethod("_$PINPOINT$_setIntArray", int[].class);
    setInts.invoke(testObject, intValues);
    Method getInts = testObject.getClass().getMethod("getIntValues");
    Assert.assertEquals(intValues, getInts.invoke(testObject));
    Method setIntegers = integersSetter.getMethod("_$PINPOINT$_setIntegerArray", Integer[].class);
    // wrap due to vararg expansion
    Object[] wrappedIntegerValues = new Object[] { integerValues };
    setIntegers.invoke(testObject, wrappedIntegerValues);
    Method getIntegers = testObject.getClass().getMethod("getIntegerValues");
    Assert.assertEquals(integerValues, getIntegers.invoke(testObject));
}
Also used : ProtectionDomain(java.security.ProtectionDomain) InstrumentException(com.navercorp.pinpoint.bootstrap.instrument.InstrumentException) Instrumentor(com.navercorp.pinpoint.bootstrap.instrument.Instrumentor) Method(java.lang.reflect.Method) TransformCallback(com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback) InstrumentClass(com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass) TestClassLoader(com.navercorp.pinpoint.test.classloader.TestClassLoader) TestClassLoader(com.navercorp.pinpoint.test.classloader.TestClassLoader) Test(org.junit.Test)

Example 43 with TransformCallback

use of com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback in project pinpoint by naver.

the class AccessorInjectionTest method testAddGetter.

@Test
public void testAddGetter() throws Exception {
    final TestClassLoader loader = getTestClassLoader();
    final String targetClassName = "com.navercorp.pinpoint.test.javasssit.mock.TestObject3";
    loader.addTransformer(targetClassName, new TransformCallback() {

        @Override
        public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException {
            try {
                logger.info("modify cl:{}", classLoader);
                InstrumentClass aClass = instrumentor.getInstrumentClass(classLoader, className, classfileBuffer);
                aClass.addGetter(StringGetter.class, "value");
                aClass.addGetter(IntGetter.class, "intValue");
                aClass.addGetter(IntArrayGetter.class, "intValues");
                aClass.addGetter(IntegerArrayGetter.class, "integerValues");
                return aClass.toBytecode();
            } catch (InstrumentException e) {
                throw new RuntimeException(e.getMessage(), e);
            }
        }
    });
    Object testObject = loader.loadClass(targetClassName).newInstance();
    Class<?> stringGetter = loader.loadClass(StringGetter.class.getName());
    Class<?> intGetter = loader.loadClass(IntGetter.class.getName());
    Class<?> intsGetter = loader.loadClass(IntArrayGetter.class.getName());
    Class<?> integersGetter = loader.loadClass(IntegerArrayGetter.class.getName());
    Assert.assertTrue(stringGetter.isInstance(testObject));
    Assert.assertTrue(intGetter.isInstance(testObject));
    Assert.assertTrue(intsGetter.isInstance(testObject));
    Assert.assertTrue(integersGetter.isInstance(testObject));
    String value = "hehe";
    int intValue = 99;
    int[] intValues = { 99, 100 };
    Integer[] integerValues = { 99, 100 };
    Method method = testObject.getClass().getMethod("setValue", String.class);
    method.invoke(testObject, value);
    Method getString = stringGetter.getMethod("_$PINPOINT$_getString");
    Assert.assertEquals(value, getString.invoke(testObject));
    Method setIntValue = testObject.getClass().getMethod("setIntValue", int.class);
    setIntValue.invoke(testObject, intValue);
    Method getInt = intGetter.getMethod("_$PINPOINT$_getInt");
    Assert.assertEquals(intValue, getInt.invoke(testObject));
    Method setIntValues = testObject.getClass().getMethod("setIntValues", int[].class);
    setIntValues.invoke(testObject, intValues);
    Method getIntValues = intsGetter.getMethod("_$PINPOINT$_getIntArray");
    Assert.assertEquals(intValues, getIntValues.invoke(testObject));
    Method setIntegerValues = testObject.getClass().getMethod("setIntegerValues", Integer[].class);
    // wrap due to vararg expansion
    Object[] wrappedIntegerValues = new Object[] { integerValues };
    setIntegerValues.invoke(testObject, wrappedIntegerValues);
    Method getIntegerValues = integersGetter.getMethod("_$PINPOINT$_getIntegerArray");
    Assert.assertEquals(integerValues, getIntegerValues.invoke(testObject));
}
Also used : ProtectionDomain(java.security.ProtectionDomain) InstrumentException(com.navercorp.pinpoint.bootstrap.instrument.InstrumentException) Instrumentor(com.navercorp.pinpoint.bootstrap.instrument.Instrumentor) Method(java.lang.reflect.Method) TransformCallback(com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback) InstrumentClass(com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass) TestClassLoader(com.navercorp.pinpoint.test.classloader.TestClassLoader) TestClassLoader(com.navercorp.pinpoint.test.classloader.TestClassLoader) Test(org.junit.Test)

Example 44 with TransformCallback

use of com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback in project pinpoint by naver.

the class MatchableClassFileTransformerDelegate method transform.

@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
    Objects.requireNonNull(className, "className");
    final InstrumentContext instrumentContext = this.instrumentContext;
    final GuardInstrumentor guard = new GuardInstrumentor(this.profilerConfig, instrumentContext);
    try {
        // WARN external plugin api
        final TransformCallback transformCallback = transformCallbackProvider.getTransformCallback(instrumentContext, loader);
        return transformCallback.doInTransform(guard, loader, className, classBeingRedefined, protectionDomain, classfileBuffer);
    } catch (InstrumentException e) {
        throw new PinpointException(e);
    } finally {
        guard.close();
    }
}
Also used : GuardInstrumentor(com.navercorp.pinpoint.profiler.instrument.GuardInstrumentor) InstrumentException(com.navercorp.pinpoint.bootstrap.instrument.InstrumentException) PinpointException(com.navercorp.pinpoint.exception.PinpointException) InstrumentContext(com.navercorp.pinpoint.bootstrap.instrument.InstrumentContext) TransformCallback(com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback)

Example 45 with TransformCallback

use of com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback in project pinpoint by naver.

the class ClassFileTransformerDelegate method transform.

@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
    Objects.requireNonNull(className, "className");
    final InstrumentContext instrumentContext = this.instrumentContext;
    final GuardInstrumentor guard = new GuardInstrumentor(this.profilerConfig, instrumentContext);
    try {
        // WARN external plugin api
        final TransformCallback transformCallback = this.transformCallbackProvider.getTransformCallback(instrumentContext, loader);
        return transformCallback.doInTransform(guard, loader, className, classBeingRedefined, protectionDomain, classfileBuffer);
    } catch (InstrumentException e) {
        throw new PinpointException(e);
    } finally {
        guard.close();
    }
}
Also used : GuardInstrumentor(com.navercorp.pinpoint.profiler.instrument.GuardInstrumentor) InstrumentException(com.navercorp.pinpoint.bootstrap.instrument.InstrumentException) PinpointException(com.navercorp.pinpoint.exception.PinpointException) InstrumentContext(com.navercorp.pinpoint.bootstrap.instrument.InstrumentContext) TransformCallback(com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback)

Aggregations

TransformCallback (com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback)45 InstrumentClass (com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass)43 Instrumentor (com.navercorp.pinpoint.bootstrap.instrument.Instrumentor)43 ProtectionDomain (java.security.ProtectionDomain)43 InstrumentException (com.navercorp.pinpoint.bootstrap.instrument.InstrumentException)31 InstrumentMethod (com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod)21 TestClassLoader (com.navercorp.pinpoint.test.classloader.TestClassLoader)5 Method (java.lang.reflect.Method)5 Test (org.junit.Test)5 ProfilerConfig (com.navercorp.pinpoint.bootstrap.config.ProfilerConfig)2 InstrumentContext (com.navercorp.pinpoint.bootstrap.instrument.InstrumentContext)2 MethodFilter (com.navercorp.pinpoint.bootstrap.instrument.MethodFilter)2 Interceptor (com.navercorp.pinpoint.bootstrap.interceptor.Interceptor)2 ObjectFactory (com.navercorp.pinpoint.bootstrap.plugin.ObjectFactory)2 PreparedStatementBindingMethodFilter (com.navercorp.pinpoint.bootstrap.plugin.jdbc.PreparedStatementBindingMethodFilter)2 PinpointException (com.navercorp.pinpoint.exception.PinpointException)2 GuardInstrumentor (com.navercorp.pinpoint.profiler.instrument.GuardInstrumentor)2 List (java.util.List)2 AsyncContextAccessor (com.navercorp.pinpoint.bootstrap.async.AsyncContextAccessor)1 AsyncTraceIdAccessor (com.navercorp.pinpoint.bootstrap.async.AsyncTraceIdAccessor)1