use of javassist.NotFoundException in project hibernate-orm by hibernate.
the class AttributeTypeDescriptor method buildInLineDirtyCheckingBodyFragment.
public String buildInLineDirtyCheckingBodyFragment(JavassistEnhancementContext context, CtField currentValue) {
StringBuilder builder = new StringBuilder();
try {
// should ignore primary keys
if (PersistentAttributesHelper.hasAnnotation(currentValue, Id.class) || PersistentAttributesHelper.hasAnnotation(currentValue, EmbeddedId.class)) {
return "";
}
String readFragment = inheritanceMetadata.isInherited() && !inheritanceMetadata.isVisible() ? "super." + inheritanceMetadata.getReaderName() + "()" : "this." + currentValue.getName();
if (currentValue.getType().isPrimitive() || currentValue.getType().isEnum()) {
// primitives || enums
builder.append(String.format(" if ( %s != $1 )", readFragment));
} else {
// if the field is a collection we return since we handle that in a separate method
for (CtClass ctClass : currentValue.getType().getInterfaces()) {
if (ctClass.getName().equals(Collection.class.getName())) {
// if the collection is not managed we should write it to the tracker
if (context.isMappedCollection(currentValue)) {
return "";
}
}
}
builder.append(String.format(" if ( !%s.areEqual( %s, $1 ) )", EqualsHelper.class.getName(), readFragment));
}
builder.append(String.format(" { %s(\"%s\"); }", EnhancerConstants.TRACKER_CHANGER_NAME, currentValue.getName()));
} catch (NotFoundException ignore) {
}
return builder.toString();
}
use of javassist.NotFoundException in project hibernate-orm by hibernate.
the class EntityEnhancer method collectInheritCollectionFields.
private Collection<CtField> collectInheritCollectionFields(CtClass managedCtClass) {
if (managedCtClass == null || Object.class.getName().equals(managedCtClass.getName())) {
return Collections.emptyList();
}
try {
CtClass managedCtSuperclass = managedCtClass.getSuperclass();
if (!enhancementContext.isMappedSuperclassClass(managedCtSuperclass)) {
return collectInheritCollectionFields(managedCtSuperclass);
}
List<CtField> collectionList = new ArrayList<CtField>();
for (CtField ctField : managedCtSuperclass.getDeclaredFields()) {
if (!Modifier.isStatic(ctField.getModifiers()) && enhancementContext.isPersistentField(ctField)) {
if (PersistentAttributesHelper.isAssignable(ctField, Collection.class.getName()) || PersistentAttributesHelper.isAssignable(ctField, Map.class.getName())) {
collectionList.add(ctField);
}
}
}
collectionList.addAll(collectInheritCollectionFields(managedCtSuperclass));
return collectionList;
} catch (NotFoundException nfe) {
return Collections.emptyList();
}
}
use of javassist.NotFoundException in project hibernate-orm by hibernate.
the class MethodWriter method addGetter.
/* --- */
public static CtMethod addGetter(CtClass target, String field, String name) {
CtField actualField = null;
try {
actualField = target.getField(field);
log.debugf("Writing getter method [%s] into [%s] for field [%s]", name, target.getName(), field);
CtMethod method = CtNewMethod.getter(name, target.getField(field));
target.addMethod(method);
return method;
} catch (CannotCompileException cce) {
try {
// Fall back to create a getter from delegation.
CtMethod method = CtNewMethod.delegator(CtNewMethod.getter(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.NotFoundException in project hibernate-orm by hibernate.
the class PersistentAttributesEnhancer method generateFieldReader.
protected CtMethod generateFieldReader(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 reader = null;
try {
boolean declared = persistentField.getDeclaringClass().equals(managedCtClass);
String declaredReadFragment = "this." + fieldName + "";
String superReadFragment = "super." + readerName + "()";
if (!declared) {
// create a temporary getter 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);
}
}
// so if the field is not enabled as lazy-loadable return a plain simple getter as the reader
if (!enhancementContext.hasLazyLoadableAttributes(managedCtClass) || !enhancementContext.isLazyLoadable(persistentField)) {
reader = MethodWriter.write(managedCtClass, "public %s %s() { return %s;%n}", persistentField.getType().getName(), readerName, declared ? declaredReadFragment : superReadFragment);
} else {
reader = MethodWriter.write(managedCtClass, "public %s %s() {%n%s%n return %s;%n}", persistentField.getType().getName(), readerName, typeDescriptor.buildReadInterceptionBodyFragment(fieldName), declared ? declaredReadFragment : superReadFragment);
}
if (tmpSuperReader != null) {
persistentField.getDeclaringClass().removeMethod(tmpSuperReader);
}
if (tmpSuperWriter != null) {
persistentField.getDeclaringClass().removeMethod(tmpSuperWriter);
}
return reader;
} catch (CannotCompileException cce) {
final String msg = String.format("Could not enhance entity class [%s] to add field reader method [%s]", managedCtClass.getName(), readerName);
throw new EnhancementException(msg, cce);
} catch (NotFoundException nfe) {
final String msg = String.format("Could not enhance entity class [%s] to add field reader method [%s]", managedCtClass.getName(), readerName);
throw new EnhancementException(msg, nfe);
}
}
use of javassist.NotFoundException in project hibernate-orm by hibernate.
the class PersistentAttributesEnhancer method collectInheritPersistentFields.
private Collection<CtField> collectInheritPersistentFields(CtClass managedCtClass) {
if (managedCtClass == null || Object.class.getName().equals(managedCtClass.getName())) {
return Collections.emptyList();
}
try {
CtClass managedCtSuperclass = managedCtClass.getSuperclass();
if (!enhancementContext.isMappedSuperclassClass(managedCtSuperclass)) {
return collectInheritPersistentFields(managedCtSuperclass);
}
log.debugf("Found @MappedSuperclass %s to collectPersistenceFields", managedCtSuperclass.getName());
List<CtField> persistentFieldList = new ArrayList<CtField>();
for (CtField ctField : managedCtSuperclass.getDeclaredFields()) {
if (ctField.getName().startsWith("$$_hibernate_") || "this$0".equals(ctField.getName())) {
continue;
}
if (!Modifier.isStatic(ctField.getModifiers()) && enhancementContext.isPersistentField(ctField)) {
persistentFieldList.add(ctField);
}
}
persistentFieldList.addAll(collectInheritPersistentFields(managedCtSuperclass));
return persistentFieldList;
} catch (NotFoundException nfe) {
log.warnf("Could not find the superclass of %s", managedCtClass);
return Collections.emptyList();
}
}
Aggregations