Search in sources :

Example 1 with InstrumentException

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

the class AccessorInjectionTest method addTraceValue.

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

        @Override
        public byte[] doInTransform(Instrumentor instrumentor, ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException {
            try {
                logger.info("modify cl:{}", loader);
                InstrumentClass aClass = instrumentor.getInstrumentClass(loader, javassistClassName, classfileBuffer);
                aClass.addField(ObjectTraceValue.class.getName());
                aClass.addField(IntTraceValue.class.getName());
                aClass.addField(IntArrayTraceValue.class.getName());
                aClass.addField(IntegerArrayTraceValue.class.getName());
                aClass.addField(DatabaseInfoTraceValue.class.getName());
                aClass.addField(BindValueTraceValue.class.getName());
                String methodName = "callA";
                aClass.getDeclaredMethod(methodName).addInterceptor("com.navercorp.pinpoint.test.javasssit.TestBeforeInterceptor");
                return aClass.toBytecode();
            } catch (InstrumentException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage(), e);
            }
        }
    });
    Class<?> testObjectClazz = loader.loadClass(javassistClassName);
    final String methodName = "callA";
    logger.info("class:{}", testObjectClazz.toString());
    final Object testObject = testObjectClazz.newInstance();
    Method callA = testObjectClazz.getMethod(methodName);
    callA.invoke(testObject);
    Class<?> objectTraceValue = loader.loadClass(ObjectTraceValue.class.getName());
    Assert.assertTrue("ObjectTraceValue implements fail", objectTraceValue.isInstance(testObject));
    objectTraceValue.getMethod("_$PINPOINT$_setTraceObject", Object.class).invoke(testObject, "a");
    Object get = objectTraceValue.getMethod("_$PINPOINT$_getTraceObject").invoke(testObject);
    Assert.assertEquals("a", get);
    Class<?> intTraceValue = loader.loadClass(IntTraceValue.class.getName());
    Assert.assertTrue("IntTraceValue implements fail", intTraceValue.isInstance(testObject));
    intTraceValue.getMethod("_$PINPOINT$_setTraceInt", int.class).invoke(testObject, 1);
    int a = (Integer) intTraceValue.getMethod("_$PINPOINT$_getTraceInt").invoke(testObject);
    Assert.assertEquals(1, a);
    Class<?> intArrayTraceValue = loader.loadClass(IntArrayTraceValue.class.getName());
    Assert.assertTrue("IntArrayTraceValue implements fail", intArrayTraceValue.isInstance(testObject));
    int[] expectedInts = { 1, 2, 3 };
    intArrayTraceValue.getMethod("_$PINPOINT$_setTraceIntArray", int[].class).invoke(testObject, expectedInts);
    int[] ints = (int[]) intArrayTraceValue.getMethod("_$PINPOINT$_getTraceIntArray").invoke(testObject);
    Assert.assertEquals(expectedInts, ints);
    Class<?> integerArrayTraceValue = loader.loadClass(IntegerArrayTraceValue.class.getName());
    Assert.assertTrue("IntegerArrayTraceValue implements fail", integerArrayTraceValue.isInstance(testObject));
    Integer[] expectedIntegers = { 1, 2 };
    // wrap due to vararg expansion
    Object[] wrappedExpectedIntegers = new Object[] { expectedIntegers };
    integerArrayTraceValue.getMethod("_$PINPOINT$_setTraceIntegerArray", Integer[].class).invoke(testObject, wrappedExpectedIntegers);
    Integer[] integers = (Integer[]) integerArrayTraceValue.getMethod("_$PINPOINT$_getTraceIntegerArray").invoke(testObject);
    Assert.assertArrayEquals(expectedIntegers, integers);
    Class<?> databaseTraceValue = loader.loadClass(DatabaseInfoTraceValue.class.getName());
    Assert.assertTrue("DatabaseInfoTraceValue implements fail", databaseTraceValue.isInstance(testObject));
    databaseTraceValue.getMethod("_$PINPOINT$_setTraceDatabaseInfo", DatabaseInfo.class).invoke(testObject, UnKnownDatabaseInfo.INSTANCE);
    Object databaseInfo = databaseTraceValue.getMethod("_$PINPOINT$_getTraceDatabaseInfo").invoke(testObject);
    Assert.assertSame(UnKnownDatabaseInfo.INSTANCE, databaseInfo);
}
Also used : ProtectionDomain(java.security.ProtectionDomain) Instrumentor(com.navercorp.pinpoint.bootstrap.instrument.Instrumentor) UnKnownDatabaseInfo(com.navercorp.pinpoint.bootstrap.plugin.jdbc.UnKnownDatabaseInfo) DatabaseInfo(com.navercorp.pinpoint.bootstrap.context.DatabaseInfo) TestClassLoader(com.navercorp.pinpoint.test.classloader.TestClassLoader) InstrumentException(com.navercorp.pinpoint.bootstrap.instrument.InstrumentException) 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) Test(org.junit.Test) JavassistClassTest(com.navercorp.pinpoint.test.javasssit.JavassistClassTest)

Example 2 with InstrumentException

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

the class ASMClass method addGetter.

@Override
public void addGetter(final String getterTypeName, final String fieldName) throws InstrumentException {
    try {
        final Class<?> getterType = this.pluginContext.injectClass(this.classLoader, getterTypeName);
        final GetterAnalyzer.GetterDetails getterDetails = new GetterAnalyzer().analyze(getterType);
        final ASMFieldNodeAdapter fieldNode = this.classNode.getField(fieldName, null);
        if (fieldNode == null) {
            throw new IllegalArgumentException("Not found field. name=" + fieldName);
        }
        final String fieldTypeName = JavaAssistUtils.javaClassNameToObjectName(getterDetails.getFieldType().getName());
        if (!fieldNode.getClassName().equals(fieldTypeName)) {
            throw new IllegalArgumentException("different return type. return=" + fieldTypeName + ", field=" + fieldNode.getClassName());
        }
        this.classNode.addGetterMethod(getterDetails.getGetter().getName(), fieldNode);
        this.classNode.addInterface(getterTypeName);
        setModified(true);
    } catch (Exception e) {
        throw new InstrumentException("Failed to add getter: " + getterTypeName, e);
    }
}
Also used : InstrumentException(com.navercorp.pinpoint.bootstrap.instrument.InstrumentException) NotFoundInstrumentException(com.navercorp.pinpoint.bootstrap.instrument.NotFoundInstrumentException) InstrumentException(com.navercorp.pinpoint.bootstrap.instrument.InstrumentException) PinpointException(com.navercorp.pinpoint.exception.PinpointException) NotFoundInstrumentException(com.navercorp.pinpoint.bootstrap.instrument.NotFoundInstrumentException)

Example 3 with InstrumentException

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

the class ASMClass method addSetter.

@Override
public void addSetter(String setterTypeName, String fieldName, boolean removeFinal) throws InstrumentException {
    try {
        final Class<?> setterType = this.pluginContext.injectClass(this.classLoader, setterTypeName);
        final SetterAnalyzer.SetterDetails setterDetails = new SetterAnalyzer().analyze(setterType);
        final ASMFieldNodeAdapter fieldNode = this.classNode.getField(fieldName, null);
        if (fieldNode == null) {
            throw new IllegalArgumentException("Not found field. name=" + fieldName);
        }
        final String fieldTypeName = JavaAssistUtils.javaClassNameToObjectName(setterDetails.getFieldType().getName());
        if (!fieldNode.getClassName().equals(fieldTypeName)) {
            throw new IllegalArgumentException("Argument type of the setter is different with the field type. setterMethod: " + fieldTypeName + ", fieldType: " + fieldNode.getClassName());
        }
        if (fieldNode.isStatic()) {
            throw new IllegalArgumentException("Cannot add setter to static fields. setterMethod: " + setterDetails.getSetter().getName() + ", fieldName: " + fieldName);
        }
        final int original = fieldNode.getAccess();
        boolean finalRemoved = false;
        if (fieldNode.isFinal()) {
            if (!removeFinal) {
                throw new IllegalArgumentException("Cannot add setter to final field. setterMethod: " + setterDetails.getSetter().getName() + ", fieldName: " + fieldName);
            } else {
                final int removed = original & ~Opcodes.ACC_FINAL;
                fieldNode.setAccess(removed);
                finalRemoved = true;
            }
        }
        try {
            this.classNode.addSetterMethod(setterDetails.getSetter().getName(), fieldNode);
            this.classNode.addInterface(setterTypeName);
            setModified(true);
        } catch (Exception e) {
            if (finalRemoved) {
                fieldNode.setAccess(original);
            }
            throw e;
        }
    } catch (Exception e) {
        throw new InstrumentException("Failed to add setter: " + setterTypeName, e);
    }
}
Also used : InstrumentException(com.navercorp.pinpoint.bootstrap.instrument.InstrumentException) NotFoundInstrumentException(com.navercorp.pinpoint.bootstrap.instrument.NotFoundInstrumentException) InstrumentException(com.navercorp.pinpoint.bootstrap.instrument.InstrumentException) PinpointException(com.navercorp.pinpoint.exception.PinpointException) NotFoundInstrumentException(com.navercorp.pinpoint.bootstrap.instrument.NotFoundInstrumentException)

Example 4 with InstrumentException

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

the class SpringBeansPlugin method addAbstractAutowireCapableBeanFactoryTransformer.

private void addAbstractAutowireCapableBeanFactoryTransformer(final ProfilerPluginSetupContext context) {
    final ProfilerConfig config = context.getConfig();
    final boolean errorMark = config.readBoolean(SPRING_BEANS_MARK_ERROR, false);
    transformTemplate.transform("org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory", new TransformCallback() {

        @Override
        public byte[] doInTransform(Instrumentor instrumentor, ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException {
            InstrumentClass target = instrumentor.getInstrumentClass(loader, className, classfileBuffer);
            final BeanMethodTransformer beanTransformer = new BeanMethodTransformer(errorMark);
            final ObjectFactory beanFilterFactory = ObjectFactory.byStaticFactory("com.navercorp.pinpoint.plugin.spring.beans.interceptor.TargetBeanFilter", "of", config);
            final InstrumentMethod createBeanInstance = target.getDeclaredMethod("createBeanInstance", "java.lang.String", "org.springframework.beans.factory.support.RootBeanDefinition", "java.lang.Object[]");
            createBeanInstance.addInterceptor("com.navercorp.pinpoint.plugin.spring.beans.interceptor.CreateBeanInstanceInterceptor", va(beanTransformer, beanFilterFactory));
            final InstrumentMethod postProcessor = target.getDeclaredMethod("applyBeanPostProcessorsBeforeInstantiation", "java.lang.Class", "java.lang.String");
            postProcessor.addInterceptor("com.navercorp.pinpoint.plugin.spring.beans.interceptor.PostProcessorInterceptor", va(beanTransformer, beanFilterFactory));
            return target.toBytecode();
        }
    });
}
Also used : ProtectionDomain(java.security.ProtectionDomain) InstrumentException(com.navercorp.pinpoint.bootstrap.instrument.InstrumentException) Instrumentor(com.navercorp.pinpoint.bootstrap.instrument.Instrumentor) ProfilerConfig(com.navercorp.pinpoint.bootstrap.config.ProfilerConfig) TransformCallback(com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback) InstrumentClass(com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass) InstrumentMethod(com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod) ObjectFactory(com.navercorp.pinpoint.bootstrap.plugin.ObjectFactory)

Example 5 with InstrumentException

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

the class SpringBeansPlugin method addClassPathDefinitionScannerTransformer.

private void addClassPathDefinitionScannerTransformer(final ProfilerPluginSetupContext context) {
    final ProfilerConfig config = context.getConfig();
    final boolean errorMark = config.readBoolean(SPRING_BEANS_MARK_ERROR, false);
    transformTemplate.transform("org.springframework.context.annotation.ClassPathBeanDefinitionScanner", new TransformCallback() {

        @Override
        public byte[] doInTransform(Instrumentor instrumentor, ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException {
            InstrumentClass target = instrumentor.getInstrumentClass(loader, className, classfileBuffer);
            final BeanMethodTransformer beanTransformer = new BeanMethodTransformer(errorMark);
            final ObjectFactory beanFilterFactory = ObjectFactory.byStaticFactory("com.navercorp.pinpoint.plugin.spring.beans.interceptor.TargetBeanFilter", "of", context.getConfig());
            final InstrumentMethod method = target.getDeclaredMethod("doScan", "java.lang.String[]");
            method.addInterceptor("com.navercorp.pinpoint.plugin.spring.beans.interceptor.ClassPathDefinitionScannerDoScanInterceptor", va(loader, beanTransformer, beanFilterFactory));
            return target.toBytecode();
        }
    });
}
Also used : ProtectionDomain(java.security.ProtectionDomain) InstrumentException(com.navercorp.pinpoint.bootstrap.instrument.InstrumentException) Instrumentor(com.navercorp.pinpoint.bootstrap.instrument.Instrumentor) ProfilerConfig(com.navercorp.pinpoint.bootstrap.config.ProfilerConfig) TransformCallback(com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback) InstrumentClass(com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass) InstrumentMethod(com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod) ObjectFactory(com.navercorp.pinpoint.bootstrap.plugin.ObjectFactory)

Aggregations

InstrumentException (com.navercorp.pinpoint.bootstrap.instrument.InstrumentException)35 InstrumentClass (com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass)29 Instrumentor (com.navercorp.pinpoint.bootstrap.instrument.Instrumentor)28 TransformCallback (com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback)28 ProtectionDomain (java.security.ProtectionDomain)28 InstrumentMethod (com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod)22 Method (java.lang.reflect.Method)6 Test (org.junit.Test)6 TestClassLoader (com.navercorp.pinpoint.test.classloader.TestClassLoader)5 NotFoundInstrumentException (com.navercorp.pinpoint.bootstrap.instrument.NotFoundInstrumentException)3 PinpointException (com.navercorp.pinpoint.exception.PinpointException)3 JavassistClassTest (com.navercorp.pinpoint.test.javasssit.JavassistClassTest)3 List (java.util.List)3 ProfilerConfig (com.navercorp.pinpoint.bootstrap.config.ProfilerConfig)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 AsyncTraceIdAccessor (com.navercorp.pinpoint.bootstrap.async.AsyncTraceIdAccessor)1 DatabaseInfo (com.navercorp.pinpoint.bootstrap.context.DatabaseInfo)1 PreparedStatementBindingMethodFilter (com.navercorp.pinpoint.bootstrap.plugin.jdbc.PreparedStatementBindingMethodFilter)1