use of javassist.CtClass in project hibernate-orm by hibernate.
the class EnhancerTestUtils method enhanceAndDecompile.
/**
* method that performs the enhancement of a class
* also checks the signature of enhanced entities methods using 'javap' decompiler
*/
public static Class<?> enhanceAndDecompile(Class<?> classToEnhance, ClassLoader cl) throws Exception {
CtClass entityCtClass = generateCtClassForAnEntity(classToEnhance);
byte[] original = entityCtClass.toBytecode();
byte[] enhanced = Environment.getBytecodeProvider().getEnhancer(new EnhancerTestContext()).enhance(entityCtClass.getName(), original);
assertFalse("entity was not enhanced", enhanced == null);
log.infof("enhanced entity [%s]", entityCtClass.getName());
ClassPool cp = new ClassPool(false);
cp.appendClassPath(new LoaderClassPath(cl));
CtClass enhancedCtClass = cp.makeClass(new ByteArrayInputStream(enhanced));
enhancedCtClass.debugWriteFile(workingDir);
DecompileUtils.decompileDumpedClass(workingDir, classToEnhance.getName());
Class<?> enhancedClass = enhancedCtClass.toClass(cl, EnhancerTestUtils.class.getProtectionDomain());
assertNotNull(enhancedClass);
return enhancedClass;
}
use of javassist.CtClass in project pinpoint by naver.
the class JavassistClass method getNestedClasses.
@Override
public List<InstrumentClass> getNestedClasses(ClassFilter filter) {
List<InstrumentClass> list = new ArrayList<InstrumentClass>();
CtClass[] nestedClasses;
try {
nestedClasses = ctClass.getNestedClasses();
} catch (NotFoundException ex) {
return list;
}
if (nestedClasses == null || nestedClasses.length == 0) {
return list;
}
for (CtClass nested : nestedClasses) {
final InstrumentClass clazz = new JavassistClass(objectBinderFactory, pluginContext, interceptorRegistryBinder, apiMetaDataService, classLoader, nested);
if (filter.accept(clazz)) {
list.add(clazz);
}
}
return list;
}
use of javassist.CtClass in project pinpoint by naver.
the class JavassistClass method weave.
@Override
public void weave(String adviceClassName) throws InstrumentException {
pluginContext.injectClass(classLoader, adviceClassName);
CtClass adviceClass;
try {
adviceClass = getCtClass(adviceClassName);
} catch (NotFoundException e) {
throw new NotFoundInstrumentException(adviceClassName + " not found. Caused:" + e.getMessage(), e);
}
try {
AspectWeaverClass weaverClass = new AspectWeaverClass();
weaverClass.weaving(ctClass, adviceClass);
} catch (CannotCompileException e) {
throw new InstrumentException("weaving fail. sourceClassName:" + ctClass.getName() + " adviceClassName:" + adviceClassName + " Caused:" + e.getMessage(), e);
} catch (NotFoundException e) {
throw new InstrumentException("weaving fail. sourceClassName:" + ctClass.getName() + " adviceClassName:" + adviceClassName + " Caused:" + e.getMessage(), e);
}
}
use of javassist.CtClass 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);
}
}
use of javassist.CtClass 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