use of javassist.CannotCompileException in project hibernate-orm by hibernate.
the class PersistentAttributesEnhancer method generateFieldWriter.
protected CtMethod generateFieldWriter(CtClass managedCtClass, CtField persistentField, AttributeTypeDescriptor typeDescriptor) {
String fieldName = persistentField.getName();
String readerName = EnhancerConstants.PERSISTENT_FIELD_READER_PREFIX + fieldName;
String writerName = EnhancerConstants.PERSISTENT_FIELD_WRITER_PREFIX + fieldName;
CtMethod tmpSuperReader = null;
CtMethod tmpSuperWriter = null;
CtMethod writer;
try {
boolean declared = persistentField.getDeclaringClass().equals(managedCtClass);
String declaredWriteFragment = "this." + fieldName + "=" + fieldName + ";";
String superWriteFragment = "super." + writerName + "(" + fieldName + ");";
if (!declared) {
// create a temporary setter on the supper entity to be able to compile our code
try {
persistentField.getDeclaringClass().getDeclaredMethod(readerName);
persistentField.getDeclaringClass().getDeclaredMethod(writerName);
} catch (NotFoundException nfe) {
tmpSuperReader = MethodWriter.addGetter(persistentField.getDeclaringClass(), persistentField.getName(), readerName);
tmpSuperWriter = MethodWriter.addSetter(persistentField.getDeclaringClass(), persistentField.getName(), writerName);
}
}
if (!enhancementContext.hasLazyLoadableAttributes(managedCtClass) || !enhancementContext.isLazyLoadable(persistentField)) {
writer = MethodWriter.write(managedCtClass, "public void %s(%s %s) {%n %s%n}", writerName, persistentField.getType().getName(), fieldName, declared ? declaredWriteFragment : superWriteFragment);
} else {
writer = MethodWriter.write(managedCtClass, "public void %s(%s %s) {%n%s%n}", writerName, persistentField.getType().getName(), fieldName, typeDescriptor.buildWriteInterceptionBodyFragment(fieldName));
}
if (enhancementContext.doDirtyCheckingInline(managedCtClass)) {
if (enhancementContext.isCompositeClass(managedCtClass)) {
writer.insertBefore(String.format(" if (%1$s != null) { %1$s.callOwner(\"\"); }%n", EnhancerConstants.TRACKER_COMPOSITE_FIELD_NAME));
} else {
writer.insertBefore(typeDescriptor.buildInLineDirtyCheckingBodyFragment(enhancementContext, persistentField));
}
handleCompositeField(managedCtClass, persistentField, writer);
}
if (enhancementContext.doBiDirectionalAssociationManagement(persistentField)) {
handleBiDirectionalAssociation(managedCtClass, persistentField, writer);
}
if (tmpSuperReader != null) {
persistentField.getDeclaringClass().removeMethod(tmpSuperReader);
}
if (tmpSuperWriter != null) {
persistentField.getDeclaringClass().removeMethod(tmpSuperWriter);
}
return writer;
} catch (CannotCompileException cce) {
final String msg = String.format("Could not enhance entity class [%s] to add field writer method [%s]", managedCtClass.getName(), writerName);
throw new EnhancementException(msg, cce);
} catch (NotFoundException nfe) {
final String msg = String.format("Could not enhance entity class [%s] to add field writer method [%s]", managedCtClass.getName(), writerName);
throw new EnhancementException(msg, nfe);
}
}
use of javassist.CannotCompileException in project afterburner by stephanenicolas.
the class AfterBurner method addOrInsertMethod.
/**
* Add/Inserts java instructions into a given method of a given class.
* @param insertableMethod contains all information to perform byte code injection.
* @throws CannotCompileException if the source contained in insertableMethod can't be compiled.
* @throws AfterBurnerImpossibleException if something else goes wrong, wraps other exceptions.
*/
public void addOrInsertMethod(InsertableMethod insertableMethod) throws CannotCompileException, AfterBurnerImpossibleException {
log.info("InsertableMethod : " + insertableMethod);
// create or complete onViewCreated
String targetMethodName = insertableMethod.getTargetMethodName();
CtClass classToTransform = insertableMethod.getClassToInsertInto();
CtMethod targetMethod = extractExistingMethod(classToTransform, targetMethodName);
log.info("Method : " + targetMethod);
if (targetMethod != null) {
InsertableMethodInjectorEditor injectorEditor = new InsertableMethodInjectorEditor(classToTransform, insertableMethod);
targetMethod.instrument(injectorEditor);
if (!injectorEditor.isSuccessful) {
throw new CannotCompileException("Transformation failed. Insertion method not found.: " + targetMethodName);
}
} else {
classToTransform.addMethod(CtNewMethod.make(insertableMethod.getFullMethod(), classToTransform));
}
}
use of javassist.CannotCompileException 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.CannotCompileException in project pinpoint by naver.
the class JavassistMethod method addInterceptor0.
private int addInterceptor0(String interceptorClassName, Object[] constructorArgs, InterceptorScope scope, ExecutionPolicy executionPolicy) throws InstrumentException {
try {
ScopeInfo scopeInfo = resolveScopeInfo(interceptorClassName, scope, executionPolicy);
Interceptor interceptor = createInterceptor(interceptorClassName, scopeInfo, constructorArgs);
int interceptorId = interceptorRegistryBinder.getInterceptorRegistryAdaptor().addInterceptor(interceptor);
addInterceptor0(interceptor, interceptorId);
return interceptorId;
} catch (CannotCompileException ccex) {
throw new InstrumentException("Failed to add interceptor " + interceptorClassName + " to " + behavior.getLongName(), ccex);
} catch (NotFoundException nex) {
throw new InstrumentException("Failed to add interceptor " + interceptorClassName + " to " + behavior.getLongName(), nex);
}
}
use of javassist.CannotCompileException in project pinpoint by naver.
the class JavassistMethod method insertCatch.
private void insertCatch(int from, String src, CtClass exceptionType, String exceptionName) throws CannotCompileException {
CtClass cc = behavior.getDeclaringClass();
ConstPool cp = behavior.getMethodInfo().getConstPool();
CodeAttribute ca = behavior.getMethodInfo().getCodeAttribute();
CodeIterator iterator = ca.iterator();
Bytecode b = new Bytecode(cp, ca.getMaxStack(), ca.getMaxLocals());
b.setStackDepth(1);
Javac jv = new Javac(b, cc);
try {
jv.recordParams(behavior.getParameterTypes(), Modifier.isStatic(getModifiers()));
jv.recordLocalVariables(ca, from);
int var = jv.recordVariable(exceptionType, exceptionName);
b.addAstore(var);
jv.compileStmnt(src);
int stack = b.getMaxStack();
int locals = b.getMaxLocals();
if (stack > ca.getMaxStack())
ca.setMaxStack(stack);
if (locals > ca.getMaxLocals())
ca.setMaxLocals(locals);
int len = iterator.getCodeLength();
int pos = iterator.append(b.get());
ca.getExceptionTable().add(from, len, len, cp.addClassInfo(exceptionType));
iterator.append(b.getExceptionTable(), pos);
behavior.getMethodInfo().rebuildStackMapIf6(cc.getClassPool(), cc.getClassFile2());
} catch (NotFoundException e) {
throw new CannotCompileException(e);
} catch (CompileError e) {
throw new CannotCompileException(e);
} catch (BadBytecode e) {
throw new CannotCompileException(e);
}
}
Aggregations