use of javassist.NotFoundException 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.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 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 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);
}
}
Aggregations