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);
}
}
use of javassist.CtClass in project voltdb by VoltDB.
the class JavassistTypeParameterMatcherGenerator method generate.
public static TypeParameterMatcher generate(Class<?> type, ClassLoader classLoader) {
final String typeName = typeName(type);
final String className = "io.netty.util.internal.__matchers__." + typeName + "Matcher";
try {
try {
return (TypeParameterMatcher) Class.forName(className, true, classLoader).newInstance();
} catch (Exception e) {
// Not defined in the specified class loader.
}
CtClass c = classPool.getAndRename(NoOpTypeParameterMatcher.class.getName(), className);
c.setModifiers(c.getModifiers() | Modifier.FINAL);
c.getDeclaredMethod("match").setBody("{ return $1 instanceof " + typeName + "; }");
byte[] byteCode = c.toBytecode();
c.detach();
Method method = ClassLoader.class.getDeclaredMethod("defineClass", String.class, byte[].class, int.class, int.class);
method.setAccessible(true);
Class<?> generated = (Class<?>) method.invoke(classLoader, className, byteCode, 0, byteCode.length);
if (type != Object.class) {
logger.debug("Generated: {}", generated.getName());
} else {
// Object.class is only used when checking if Javassist is available.
}
return (TypeParameterMatcher) generated.newInstance();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Aggregations