Search in sources :

Example 1 with SetterDetails

use of com.navercorp.pinpoint.profiler.instrument.SetterAnalyzer.SetterDetails in project pinpoint by naver.

the class JavassistClass method addSetter.

@Override
public void addSetter(String setterTypeName, String fieldName, boolean removeFinalFlag) throws InstrumentException {
    try {
        Class<?> setterType = pluginContext.injectClass(classLoader, setterTypeName);
        SetterAnalyzer setterAnalyzer = new SetterAnalyzer();
        SetterDetails setterDetails = setterAnalyzer.analyze(setterType);
        CtField field = ctClass.getField(fieldName);
        String fieldTypeName = JavaAssistUtils.javaClassNameToObjectName(setterDetails.getFieldType().getName());
        if (!field.getType().getName().equals(fieldTypeName)) {
            throw new IllegalArgumentException("Argument type of the setter is different with the field type. setterMethod: " + setterDetails.getSetter() + ", fieldType: " + field.getType().getName());
        }
        final int originalModifiers = field.getModifiers();
        if (Modifier.isStatic(originalModifiers)) {
            throw new IllegalArgumentException("Cannot add setter to static fields. setterMethod: " + setterDetails.getSetter().getName() + ", fieldName: " + fieldName);
        }
        boolean finalRemoved = false;
        if (Modifier.isFinal(originalModifiers)) {
            if (!removeFinalFlag) {
                throw new IllegalArgumentException("Cannot add setter to final field. setterMethod: " + setterDetails.getSetter().getName() + ", fieldName: " + fieldName);
            } else {
                final int modifiersWithFinalRemoved = Modifier.clear(originalModifiers, Modifier.FINAL);
                field.setModifiers(modifiersWithFinalRemoved);
                finalRemoved = true;
            }
        }
        try {
            CtMethod setterMethod = CtNewMethod.setter(setterDetails.getSetter().getName(), field);
            if (setterMethod.getDeclaringClass() != ctClass) {
                setterMethod = CtNewMethod.copy(setterMethod, ctClass, null);
            }
            ctClass.addMethod(setterMethod);
            CtClass ctInterface = getCtClass(setterTypeName);
            ctClass.addInterface(ctInterface);
        } catch (Exception e) {
            if (finalRemoved) {
                field.setModifiers(originalModifiers);
            }
            throw e;
        }
    } catch (Exception e) {
        throw new InstrumentException("Failed to add setter: " + setterTypeName, e);
    }
}
Also used : CtClass(javassist.CtClass) SetterDetails(com.navercorp.pinpoint.profiler.instrument.SetterAnalyzer.SetterDetails) 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 SetterDetails (com.navercorp.pinpoint.profiler.instrument.SetterAnalyzer.SetterDetails)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