Search in sources :

Example 1 with AccessorDetails

use of com.navercorp.pinpoint.profiler.instrument.AccessorAnalyzer.AccessorDetails in project pinpoint by naver.

the class JavassistClass method addField0.

private void addField0(String accessorTypeName, String initValExp) throws InstrumentException {
    try {
        Class<?> accessorType = pluginContext.injectClass(classLoader, accessorTypeName);
        final AccessorAnalyzer accessorAnalyzer = new AccessorAnalyzer();
        final AccessorDetails accessorDetails = accessorAnalyzer.analyze(accessorType);
        Class<?> fieldType = accessorDetails.getFieldType();
        String fieldTypeName = JavaAssistUtils.javaClassNameToObjectName(fieldType.getName());
        final CtField newField = CtField.make("private " + fieldTypeName + " " + FIELD_PREFIX + JavaAssistUtils.javaClassNameToVariableName(accessorTypeName) + ";", ctClass);
        if (initValExp == null) {
            ctClass.addField(newField);
        } else {
            ctClass.addField(newField, initValExp);
        }
        final CtClass accessorInterface = getCtClass(accessorTypeName);
        ctClass.addInterface(accessorInterface);
        CtMethod getterMethod = CtNewMethod.getter(accessorDetails.getGetter().getName(), newField);
        ctClass.addMethod(getterMethod);
        CtMethod setterMethod = CtNewMethod.setter(accessorDetails.getSetter().getName(), newField);
        ctClass.addMethod(setterMethod);
    } catch (Exception e) {
        throw new InstrumentException("Failed to add field with accessor [" + accessorTypeName + "]. Cause:" + e.getMessage(), e);
    }
}
Also used : AccessorDetails(com.navercorp.pinpoint.profiler.instrument.AccessorAnalyzer.AccessorDetails) CtClass(javassist.CtClass) CtField(javassist.CtField) CtMethod(javassist.CtMethod) PinpointException(com.navercorp.pinpoint.exception.PinpointException) NotFoundException(javassist.NotFoundException) CannotCompileException(javassist.CannotCompileException) IOException(java.io.IOException)

Aggregations

PinpointException (com.navercorp.pinpoint.exception.PinpointException)1 AccessorDetails (com.navercorp.pinpoint.profiler.instrument.AccessorAnalyzer.AccessorDetails)1 IOException (java.io.IOException)1 CannotCompileException (javassist.CannotCompileException)1 CtClass (javassist.CtClass)1 CtField (javassist.CtField)1 CtMethod (javassist.CtMethod)1 NotFoundException (javassist.NotFoundException)1