Search in sources :

Example 1 with GetterDetails

use of com.navercorp.pinpoint.profiler.instrument.GetterAnalyzer.GetterDetails in project pinpoint by naver.

the class JavassistClass method addGetter.

@Override
public void addGetter(String getterTypeName, String fieldName) throws InstrumentException {
    try {
        Class<?> getterType = pluginContext.injectClass(classLoader, getterTypeName);
        GetterAnalyzer getterAnalyzer = new GetterAnalyzer();
        GetterDetails getterDetails = getterAnalyzer.analyze(getterType);
        CtField field = ctClass.getField(fieldName);
        String fieldTypeName = JavaAssistUtils.javaClassNameToObjectName(getterDetails.getFieldType().getName());
        if (!field.getType().getName().equals(fieldTypeName)) {
            throw new IllegalArgumentException("Return type of the getter is different with the field type. getterMethod: " + getterDetails.getGetter() + ", fieldType: " + field.getType().getName());
        }
        CtMethod getterMethod = CtNewMethod.getter(getterDetails.getGetter().getName(), field);
        if (getterMethod.getDeclaringClass() != ctClass) {
            getterMethod = CtNewMethod.copy(getterMethod, ctClass, null);
        }
        ctClass.addMethod(getterMethod);
        CtClass ctInterface = getCtClass(getterTypeName);
        ctClass.addInterface(ctInterface);
    } catch (Exception e) {
        throw new InstrumentException("Failed to add getter: " + getterTypeName, e);
    }
}
Also used : CtClass(javassist.CtClass) CtField(javassist.CtField) GetterDetails(com.navercorp.pinpoint.profiler.instrument.GetterAnalyzer.GetterDetails) 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 GetterDetails (com.navercorp.pinpoint.profiler.instrument.GetterAnalyzer.GetterDetails)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