Search in sources :

Example 16 with NotFoundException

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();
}
Also used : CtClass(javassist.CtClass) EmbeddedId(javax.persistence.EmbeddedId) Collection(java.util.Collection) NotFoundException(javassist.NotFoundException) EmbeddedId(javax.persistence.EmbeddedId) Id(javax.persistence.Id)

Example 17 with NotFoundException

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();
    }
}
Also used : CtClass(javassist.CtClass) CtField(javassist.CtField) ArrayList(java.util.ArrayList) NotFoundException(javassist.NotFoundException)

Example 18 with NotFoundException

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);
    }
}
Also used : CtField(javassist.CtField) EnhancementException(org.hibernate.bytecode.enhance.spi.EnhancementException) NotFoundException(javassist.NotFoundException) CannotCompileException(javassist.CannotCompileException) CtMethod(javassist.CtMethod)

Example 19 with NotFoundException

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);
    }
}
Also used : EnhancementException(org.hibernate.bytecode.enhance.spi.EnhancementException) NotFoundException(javassist.NotFoundException) CannotCompileException(javassist.CannotCompileException) CtMethod(javassist.CtMethod)

Example 20 with NotFoundException

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();
    }
}
Also used : CtClass(javassist.CtClass) CtField(javassist.CtField) ArrayList(java.util.ArrayList) NotFoundException(javassist.NotFoundException)

Aggregations

NotFoundException (javassist.NotFoundException)35 CtClass (javassist.CtClass)26 CannotCompileException (javassist.CannotCompileException)19 CtMethod (javassist.CtMethod)15 ClassPool (javassist.ClassPool)12 IOException (java.io.IOException)5 CtField (javassist.CtField)5 EnhancementException (org.hibernate.bytecode.enhance.spi.EnhancementException)5 FileNotFoundException (java.io.FileNotFoundException)4 BadBytecode (javassist.bytecode.BadBytecode)4 CodeIterator (javassist.bytecode.CodeIterator)4 File (java.io.File)3 ArrayList (java.util.ArrayList)3 Bytecode (javassist.bytecode.Bytecode)3 CodeAttribute (javassist.bytecode.CodeAttribute)3 CompileError (javassist.compiler.CompileError)3 Javac (javassist.compiler.Javac)3 FileFilter (java.io.FileFilter)2 CtBehavior (javassist.CtBehavior)2 CtConstructor (javassist.CtConstructor)2