Search in sources :

Example 41 with CtMethod

use of javassist.CtMethod in project hibernate-orm by hibernate.

the class PersistentAttributesHelper method inferTypeName.

/**
	 * Consistent with hasAnnotation()
	 */
private static String inferTypeName(CtClass ctClass, String attributeName) {
    AccessType classAccessType = getAccessTypeOrNull(ctClass);
    CtField field = findFieldOrNull(ctClass, attributeName);
    CtMethod getter = findGetterOrNull(ctClass, attributeName);
    if (classAccessType == AccessType.FIELD || (field != null && getAccessTypeOrNull(field) == AccessType.FIELD)) {
        return field == null ? null : inferFieldTypeName(field);
    }
    if (classAccessType == AccessType.PROPERTY || (getter != null && getAccessTypeOrNull(getter) == AccessType.PROPERTY)) {
        return getter == null ? null : inferMethodTypeName(getter);
    }
    String found = (getter == null ? null : inferMethodTypeName(getter));
    if (found == null && field != null) {
        return inferFieldTypeName(field);
    }
    return found;
}
Also used : CtField(javassist.CtField) AccessType(javax.persistence.AccessType) CtMethod(javassist.CtMethod)

Example 42 with CtMethod

use of javassist.CtMethod in project hibernate-orm by hibernate.

the class PersistentAttributesHelper method getAnnotation.

public static <T extends Annotation> T getAnnotation(CtClass ctClass, String attributeName, Class<T> annotation) {
    AccessType classAccessType = getAccessTypeOrNull(ctClass);
    CtField field = findFieldOrNull(ctClass, attributeName);
    CtMethod getter = findGetterOrNull(ctClass, attributeName);
    if (classAccessType == AccessType.FIELD || (field != null && getAccessTypeOrNull(field) == AccessType.FIELD)) {
        return field == null ? null : getAnnotationOrNull(field, annotation);
    }
    if (classAccessType == AccessType.PROPERTY || (getter != null && getAccessTypeOrNull(getter) == AccessType.PROPERTY)) {
        return getter == null ? null : getAnnotationOrNull(getter, annotation);
    }
    T found = (getter == null ? null : getAnnotationOrNull(getter, annotation));
    if (found == null && field != null) {
        return getAnnotationOrNull(field, annotation);
    }
    return found;
}
Also used : CtField(javassist.CtField) AccessType(javax.persistence.AccessType) CtMethod(javassist.CtMethod)

Example 43 with CtMethod

use of javassist.CtMethod 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 44 with CtMethod

use of javassist.CtMethod 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 45 with CtMethod

use of javassist.CtMethod 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

CtMethod (javassist.CtMethod)76 CtClass (javassist.CtClass)46 NotFoundException (javassist.NotFoundException)23 CannotCompileException (javassist.CannotCompileException)20 ClassPool (javassist.ClassPool)18 CtField (javassist.CtField)14 Test (org.junit.Test)12 IOException (java.io.IOException)10 InitMethod (com.googlecode.gwt.test.patchers.InitMethod)6 Method (java.lang.reflect.Method)5 CtConstructor (javassist.CtConstructor)5 EnhancementException (org.hibernate.bytecode.enhance.spi.EnhancementException)4 GwtTestPatchException (com.googlecode.gwt.test.exceptions.GwtTestPatchException)3 PinpointException (com.navercorp.pinpoint.exception.PinpointException)3 File (java.io.File)3 FileNotFoundException (java.io.FileNotFoundException)3 ArrayList (java.util.ArrayList)3 PatchMethod (com.googlecode.gwt.test.patchers.PatchMethod)2 FileFilter (java.io.FileFilter)2 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)2