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);
}
}
Aggregations