use of javassist.CannotCompileException 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.CannotCompileException in project hibernate-orm by hibernate.
the class FieldWriter method addWithModifiers.
private static void addWithModifiers(CtClass target, CtClass type, String name, int modifiers, Class<?>... annotations) {
try {
final CtField f = new CtField(type, name, target);
f.setModifiers(f.getModifiers() | modifiers);
addAnnotations(f.getFieldInfo(), annotations);
target.addField(f);
} catch (CannotCompileException cce) {
final String msg = String.format("Could not enhance class [%s] to add field [%s]", target.getName(), name);
throw new EnhancementException(msg, cce);
}
}
use of javassist.CannotCompileException in project hibernate-orm by hibernate.
the class MethodWriter method addSetter.
public static CtMethod addSetter(CtClass target, String field, String name) {
CtField actualField = null;
try {
actualField = target.getField(field);
log.debugf("Writing setter method [%s] into [%s] for field [%s]", name, target.getName(), field);
CtMethod method = CtNewMethod.setter(name, actualField);
target.addMethod(method);
return method;
} catch (CannotCompileException cce) {
try {
// Fall back to create a getter from delegation.
CtMethod method = CtNewMethod.delegator(CtNewMethod.setter(name, actualField), target);
target.addMethod(method);
return method;
} catch (CannotCompileException ignored) {
String msg = String.format("Could not enhance class [%s] to add method [%s] for field [%s]", target.getName(), name, field);
throw new EnhancementException(msg, cce);
}
} catch (NotFoundException nfe) {
String msg = String.format("Could not enhance class [%s] to add method [%s] for field [%s]", target.getName(), name, field);
throw new EnhancementException(msg, nfe);
}
}
use of javassist.CannotCompileException in project ignite by apache.
the class GridActivationCacheAbstractTestSuit method transform.
/**
* @param c Class to transform.
* @return Transformed class.
*/
private static Class transform(Class c) {
try {
ClassPool pool = ClassPool.getDefault();
pool.insertClassPath(new ClassClassPath(GridActivationCacheAbstractTestSuit.class));
String path = c.getProtectionDomain().getCodeSource().getLocation().getPath();
CtClass ct = pool.get(c.getName());
String name = c.getName() + SUFFIX;
CtClass pr = pool.get(GridActivateExtensionTest.class.getName());
pr.setName(name);
pr.setSuperclass(ct);
pr.writeFile(path);
return Class.forName(name);
} catch (IOException e) {
System.out.println("Io exception: " + e.getMessage());
throw new IgniteException(e);
} catch (CannotCompileException e) {
System.out.println("Cannot compile exception: " + e.getMessage());
throw new IgniteException(e);
} catch (NotFoundException e) {
System.out.println("Not found exception: " + e.getMessage());
throw new IgniteException(e);
} catch (ClassNotFoundException e) {
System.out.println("Class not found exception: " + e.getMessage());
throw new IgniteException(e);
}
}
use of javassist.CannotCompileException in project dubbo by alibaba.
the class ClassGenerator method toClass.
public Class<?> toClass(ClassLoader loader, ProtectionDomain pd) {
if (mCtc != null)
mCtc.detach();
long id = CLASS_NAME_COUNTER.getAndIncrement();
try {
CtClass ctcs = mSuperClass == null ? null : mPool.get(mSuperClass);
if (mClassName == null)
mClassName = (mSuperClass == null || javassist.Modifier.isPublic(ctcs.getModifiers()) ? ClassGenerator.class.getName() : mSuperClass + "$sc") + id;
mCtc = mPool.makeClass(mClassName);
if (mSuperClass != null)
mCtc.setSuperclass(ctcs);
// add dynamic class tag.
mCtc.addInterface(mPool.get(DC.class.getName()));
if (mInterfaces != null)
for (String cl : mInterfaces) mCtc.addInterface(mPool.get(cl));
if (mFields != null)
for (String code : mFields) mCtc.addField(CtField.make(code, mCtc));
if (mMethods != null) {
for (String code : mMethods) {
if (code.charAt(0) == ':')
mCtc.addMethod(CtNewMethod.copy(getCtMethod(mCopyMethods.get(code.substring(1))), code.substring(1, code.indexOf('(')), mCtc, null));
else
mCtc.addMethod(CtNewMethod.make(code, mCtc));
}
}
if (mDefaultConstructor)
mCtc.addConstructor(CtNewConstructor.defaultConstructor(mCtc));
if (mConstructors != null) {
for (String code : mConstructors) {
if (code.charAt(0) == ':') {
mCtc.addConstructor(CtNewConstructor.copy(getCtConstructor(mCopyConstructors.get(code.substring(1))), mCtc, null));
} else {
// inner class name include $.
String[] sn = mCtc.getSimpleName().split("\\$+");
mCtc.addConstructor(CtNewConstructor.make(code.replaceFirst(SIMPLE_NAME_TAG, sn[sn.length - 1]), mCtc));
}
}
}
return mCtc.toClass(loader, pd);
} catch (RuntimeException e) {
throw e;
} catch (NotFoundException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (CannotCompileException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
Aggregations