use of javassist.NotFoundException in project atlas by alibaba.
the class CodeInjectByJavassist method addField.
private static void addField(ClassPool pool, CtClass cc, String key, String value) throws NotFoundException, CannotCompileException {
if (StringUtils.isNotBlank(value)) {
try {
cc.removeField(cc.getField(key));
} catch (NotFoundException notfoundException) {
}
logger.info("inject key " + key + "->" + value);
CtClass ctClass = pool.get("java.lang.String");
CtField ctField = new CtField(ctClass, key, cc);
ctField.setModifiers(Modifier.STATIC | Modifier.PUBLIC);
CtField.Initializer initializer = CtField.Initializer.constant(value);
cc.addField(ctField, initializer);
}
}
use of javassist.NotFoundException in project ratpack by ratpack.
the class ClosureBackedHandler method describeTo.
@Override
public void describeTo(StringBuilder stringBuilder) {
ClosureUtil.SourceInfo sourceInfo = ClosureUtil.getSourceInfo(invoker.getClosure());
if (sourceInfo == null) {
ClassPool pool = ClassPool.getDefault();
try {
CtClass ctClass = pool.get(invoker.getClosure().getClass().getName());
CtMethod ctMethod = ctClass.getDeclaredMethod("doCall");
int lineNumber = ctMethod.getMethodInfo().getLineNumber(0);
ClassFile classFile = ctClass.getClassFile();
String sourceFile = classFile.getSourceFile();
if (lineNumber != -1 && sourceFile != null) {
stringBuilder.append("closure at line ").append(lineNumber).append(" of ").append(sourceFile);
} else {
stringBuilder.append("closure ").append(invoker.getClosure().getClass().getName());
}
} catch (NotFoundException e) {
stringBuilder.append(invoker.getClosure().getClass().getName());
}
} else {
stringBuilder.append("closure at line ").append(sourceInfo.getLineNumber()).append(" of ").append(sourceInfo.getUri());
}
}
use of javassist.NotFoundException in project pinpoint by naver.
the class JavassistClass method hasEnclosingMethod.
@Override
public boolean hasEnclosingMethod(String methodName, String... parameterTypes) {
CtBehavior behavior;
try {
behavior = ctClass.getEnclosingBehavior();
} catch (NotFoundException ignored) {
return false;
}
if (behavior == null) {
return false;
}
final MethodInfo methodInfo = behavior.getMethodInfo2();
if (!methodInfo.getName().equals(methodName)) {
return false;
}
final String jvmSignature = JavaAssistUtils.javaTypeToJvmSignature(parameterTypes);
if (methodInfo.getDescriptor().startsWith(jvmSignature)) {
return true;
}
return false;
}
use of javassist.NotFoundException in project pinpoint by naver.
the class JavassistClass method addDelegatorMethod.
@Override
public InstrumentMethod addDelegatorMethod(String methodName, String... paramTypes) throws InstrumentException {
if (getCtMethod0(ctClass, methodName, paramTypes) != null) {
throw new InstrumentException(getName() + "already have method(" + methodName + ").");
}
try {
final CtClass superClass = ctClass.getSuperclass();
CtMethod superMethod = getCtMethod0(superClass, methodName, paramTypes);
if (superMethod == null) {
throw new NotFoundInstrumentException(methodName + Arrays.toString(paramTypes) + " is not found in " + superClass.getName());
}
CtMethod delegatorMethod = CtNewMethod.delegator(superMethod, ctClass);
ctClass.addMethod(delegatorMethod);
return new JavassistMethod(objectBinderFactory, pluginContext, interceptorRegistryBinder, apiMetaDataService, this, delegatorMethod);
} catch (NotFoundException ex) {
throw new InstrumentException(getName() + "don't have super class(" + getSuperClass() + "). Cause:" + ex.getMessage(), ex);
} catch (CannotCompileException ex) {
throw new InstrumentException(methodName + " addDelegatorMethod fail. Cause:" + ex.getMessage(), ex);
}
}
use of javassist.NotFoundException in project pinpoint by naver.
the class LegacyProfilerPluginClassInjector method loadFromOtherClassLoader.
private Class<?> loadFromOtherClassLoader(ClassPool pool, ClassLoader classLoader, String className) throws NotFoundException, IOException, CannotCompileException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
Class<?> c = null;
try {
c = classLoader.loadClass(className);
} catch (ClassNotFoundException ignore) {
}
if (c != null) {
return c;
}
CtClass ct = pool.get(className);
if (ct == null) {
throw new NotFoundException(className);
}
CtClass superClass = ct.getSuperclass();
if (superClass != null) {
loadFromOtherClassLoader(pool, classLoader, superClass.getName());
}
CtClass[] interfaces = ct.getInterfaces();
for (CtClass i : interfaces) {
loadFromOtherClassLoader(pool, classLoader, i.getName());
}
Collection<String> refs = ct.getRefClasses();
for (String ref : refs) {
try {
loadFromOtherClassLoader(pool, classLoader, ref);
} catch (NotFoundException e) {
logger.warn("Skip a referenced class because of NotFoundException : ", e);
}
}
byte[] bytes = ct.toBytecode();
return (Class<?>) DEFINE_CLASS.invoke(classLoader, ct.getName(), bytes, 0, bytes.length);
}
Aggregations