Search in sources :

Example 6 with EnhancementException

use of org.hibernate.bytecode.enhance.spi.EnhancementException 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 7 with EnhancementException

use of org.hibernate.bytecode.enhance.spi.EnhancementException 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 8 with EnhancementException

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

Aggregations

EnhancementException (org.hibernate.bytecode.enhance.spi.EnhancementException)8 CannotCompileException (javassist.CannotCompileException)5 NotFoundException (javassist.NotFoundException)5 CtMethod (javassist.CtMethod)4 CtField (javassist.CtField)3 ClassPool (javassist.ClassPool)2 BadBytecode (javassist.bytecode.BadBytecode)2 CodeIterator (javassist.bytecode.CodeIterator)2 ConstPool (javassist.bytecode.ConstPool)2 MethodInfo (javassist.bytecode.MethodInfo)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 UncheckedIOException (java.io.UncheckedIOException)1 CtClass (javassist.CtClass)1