Search in sources :

Example 1 with XMethod

use of org.hibernate.annotations.common.reflection.XMethod in project hibernate-orm by hibernate.

the class CallbackBuilderLegacyImpl method resolveCallbacks.

public Callback[] resolveCallbacks(XClass beanClass, CallbackType callbackType, ReflectionManager reflectionManager) {
    List<Callback> callbacks = new ArrayList<Callback>();
    //used to track overridden methods
    List<String> callbacksMethodNames = new ArrayList<String>();
    List<Class> orderedListeners = new ArrayList<Class>();
    XClass currentClazz = beanClass;
    boolean stopListeners = false;
    boolean stopDefaultListeners = false;
    do {
        Callback callback = null;
        List<XMethod> methods = currentClazz.getDeclaredMethods();
        for (final XMethod xMethod : methods) {
            if (xMethod.isAnnotationPresent(callbackType.getCallbackAnnotation())) {
                Method method = reflectionManager.toMethod(xMethod);
                final String methodName = method.getName();
                if (!callbacksMethodNames.contains(methodName)) {
                    //overridden method, remove the superclass overridden method
                    if (callback == null) {
                        callback = new EntityCallback(method, callbackType);
                        Class returnType = method.getReturnType();
                        Class[] args = method.getParameterTypes();
                        if (returnType != Void.TYPE || args.length != 0) {
                            throw new RuntimeException("Callback methods annotated on the bean class must return void and take no arguments: " + callbackType.getCallbackAnnotation().getName() + " - " + xMethod);
                        }
                        method.setAccessible(true);
                        log.debugf("Adding %s as %s callback for entity %s", methodName, callbackType.getCallbackAnnotation().getSimpleName(), beanClass.getName());
                        //superclass first
                        callbacks.add(0, callback);
                        callbacksMethodNames.add(0, methodName);
                    } else {
                        throw new PersistenceException("You can only annotate one callback method with " + callbackType.getCallbackAnnotation().getName() + " in bean class: " + beanClass.getName());
                    }
                }
            }
        }
        if (!stopListeners) {
            getListeners(currentClazz, orderedListeners);
            stopListeners = currentClazz.isAnnotationPresent(ExcludeSuperclassListeners.class);
            stopDefaultListeners = currentClazz.isAnnotationPresent(ExcludeDefaultListeners.class);
        }
        do {
            currentClazz = currentClazz.getSuperclass();
        } while (currentClazz != null && !(currentClazz.isAnnotationPresent(Entity.class) || currentClazz.isAnnotationPresent(MappedSuperclass.class)));
    } while (currentClazz != null);
    //handle default listeners
    if (!stopDefaultListeners) {
        List<Class> defaultListeners = (List<Class>) reflectionManager.getDefaults().get(EntityListeners.class);
        if (defaultListeners != null) {
            int defaultListenerSize = defaultListeners.size();
            for (int i = defaultListenerSize - 1; i >= 0; i--) {
                orderedListeners.add(defaultListeners.get(i));
            }
        }
    }
    for (Class listener : orderedListeners) {
        Callback callback = null;
        if (listener != null) {
            XClass xListener = reflectionManager.toXClass(listener);
            callbacksMethodNames = new ArrayList<String>();
            List<XMethod> methods = xListener.getDeclaredMethods();
            for (final XMethod xMethod : methods) {
                if (xMethod.isAnnotationPresent(callbackType.getCallbackAnnotation())) {
                    final Method method = reflectionManager.toMethod(xMethod);
                    final String methodName = method.getName();
                    if (!callbacksMethodNames.contains(methodName)) {
                        //overridden method, remove the superclass overridden method
                        if (callback == null) {
                            callback = new ListenerCallback(jpaListenerFactory.buildListener(listener), method, callbackType);
                            Class returnType = method.getReturnType();
                            Class[] args = method.getParameterTypes();
                            if (returnType != Void.TYPE || args.length != 1) {
                                throw new PersistenceException("Callback methods annotated in a listener bean class must return void and take one argument: " + callbackType.getCallbackAnnotation().getName() + " - " + method);
                            }
                            if (!method.isAccessible()) {
                                method.setAccessible(true);
                            }
                            log.debugf("Adding %s as %s callback for entity %s", methodName, callbackType.getCallbackAnnotation().getSimpleName(), beanClass.getName());
                            // listeners first
                            callbacks.add(0, callback);
                        } else {
                            throw new PersistenceException("You can only annotate one callback method with " + callbackType.getCallbackAnnotation().getName() + " in bean class: " + beanClass.getName() + " and callback listener: " + listener.getName());
                        }
                    }
                }
            }
        }
    }
    return callbacks.toArray(new Callback[callbacks.size()]);
}
Also used : ArrayList(java.util.ArrayList) XMethod(org.hibernate.annotations.common.reflection.XMethod) Method(java.lang.reflect.Method) XClass(org.hibernate.annotations.common.reflection.XClass) EntityListeners(javax.persistence.EntityListeners) Callback(org.hibernate.jpa.event.spi.jpa.Callback) XMethod(org.hibernate.annotations.common.reflection.XMethod) ExcludeDefaultListeners(javax.persistence.ExcludeDefaultListeners) PersistenceException(javax.persistence.PersistenceException) XClass(org.hibernate.annotations.common.reflection.XClass) ArrayList(java.util.ArrayList) List(java.util.List) ExcludeSuperclassListeners(javax.persistence.ExcludeSuperclassListeners)

Example 2 with XMethod

use of org.hibernate.annotations.common.reflection.XMethod in project hibernate-orm by hibernate.

the class AnnotationBinder method hasAnnotationsOnIdClass.

private static boolean hasAnnotationsOnIdClass(XClass idClass) {
    //		if(idClass.getAnnotation(Embeddable.class) != null)
    //			return true;
    List<XProperty> properties = idClass.getDeclaredProperties(XClass.ACCESS_FIELD);
    for (XProperty property : properties) {
        if (property.isAnnotationPresent(Column.class) || property.isAnnotationPresent(OneToMany.class) || property.isAnnotationPresent(ManyToOne.class) || property.isAnnotationPresent(Id.class) || property.isAnnotationPresent(GeneratedValue.class) || property.isAnnotationPresent(OneToOne.class) || property.isAnnotationPresent(ManyToMany.class)) {
            return true;
        }
    }
    List<XMethod> methods = idClass.getDeclaredMethods();
    for (XMethod method : methods) {
        if (method.isAnnotationPresent(Column.class) || method.isAnnotationPresent(OneToMany.class) || method.isAnnotationPresent(ManyToOne.class) || method.isAnnotationPresent(Id.class) || method.isAnnotationPresent(GeneratedValue.class) || method.isAnnotationPresent(OneToOne.class) || method.isAnnotationPresent(ManyToMany.class)) {
            return true;
        }
    }
    return false;
}
Also used : GeneratedValue(javax.persistence.GeneratedValue) XProperty(org.hibernate.annotations.common.reflection.XProperty) XMethod(org.hibernate.annotations.common.reflection.XMethod) ManyToMany(javax.persistence.ManyToMany) ManyToOne(javax.persistence.ManyToOne)

Aggregations

XMethod (org.hibernate.annotations.common.reflection.XMethod)2 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 EntityListeners (javax.persistence.EntityListeners)1 ExcludeDefaultListeners (javax.persistence.ExcludeDefaultListeners)1 ExcludeSuperclassListeners (javax.persistence.ExcludeSuperclassListeners)1 GeneratedValue (javax.persistence.GeneratedValue)1 ManyToMany (javax.persistence.ManyToMany)1 ManyToOne (javax.persistence.ManyToOne)1 PersistenceException (javax.persistence.PersistenceException)1 XClass (org.hibernate.annotations.common.reflection.XClass)1 XProperty (org.hibernate.annotations.common.reflection.XProperty)1 Callback (org.hibernate.jpa.event.spi.jpa.Callback)1