@SuppressWarnings("unchecked") private void putEntityListeners(AnnotationInfo ai, EntityListeners entityListeners) { Class[] entityListenerClasses = entityListeners.value(); if (entityListenerClasses == null) return; Map<Class, List<ClassMethodEntry>> listeners = ai.getEntityListeners(); List<Class<? extends Annotation>> annotations = Arrays.asList( PrePersist.class, PreUpdate.class, PreRemove.class, PostLoad.class, PostPersist.class, PostUpdate.class, PostRemove.class); // TODO: More than one listener per event cannot be handled like this... for (Class clazz : entityListenerClasses) { // System.out.println("class=" + clazz); for (Method method : clazz.getMethods()) { // System.out.println("method=" + method.getName()); for (Class<? extends Annotation> annotationClass : annotations) { Annotation annotation = method.getAnnotation(annotationClass); addListener(listeners, clazz, method, annotation, annotationClass); } } } }
private void putTableDeclaration(AnnotationInfo ai, Class<?> c) { Table table = c.getAnnotation(Table.class); if (table != null) { if (table.name() == null) throw new PersistenceException("You must specify a name= for @Table on " + c.getName()); ai.setDomainName(table.name()); } }
private void parseProperty(AnnotationInfo ai, Class c, Field field) { // TODO add support for OneToOne if (!field.isAnnotationPresent(Transient.class) && (field.isAnnotationPresent(ManyToMany.class) || field.isAnnotationPresent(OneToMany.class) || field.isAnnotationPresent(ManyToOne.class) || field.isAnnotationPresent(Id.class))) { ai.addField(field); } }
/** * Gets all the annotation info for a particular class and puts it in our annotation info cache. * * @param c * @return */ public AnnotationInfo putAnnotationInfo(Class c) { { Entity entity = (Entity) c.getAnnotation(Entity.class); if (entity == null) { throw new PersistenceException("Class not marked as an @Entity: " + c.getName()); } } AnnotationInfo ai = new AnnotationInfo(); ai.setClassAnnotations(c.getAnnotations()); ai.setMainClass(c); Class superClass = c; Class rootClass = null; while ((superClass = superClass.getSuperclass()) != null) { MappedSuperclass mappedSuperclass = (MappedSuperclass) superClass.getAnnotation(MappedSuperclass.class); Entity entity = (Entity) superClass.getAnnotation(Entity.class); Inheritance inheritance = (Inheritance) superClass.getAnnotation(Inheritance.class); if (mappedSuperclass != null || entity != null) { putProperties(ai, superClass); putMethods(ai, superClass); if (entity != null) { rootClass = superClass; } putEntityListeners(ai, superClass); } } if (rootClass != null) { ai.setRootClass(rootClass); DiscriminatorValue dv = (DiscriminatorValue) c.getAnnotation(DiscriminatorValue.class); String discriminatorValue; if (dv != null) { discriminatorValue = dv.value(); if (discriminatorValue == null) { throw new PersistenceException( "You must specify a value= for @DiscriminatorValue on " + c.getName()); } } else { discriminatorValue = c.getSimpleName(); } ai.setDiscriminatorValue(discriminatorValue); discriminatorMap.put(discriminatorValue, ai); } else { ai.setRootClass(c); } putTableDeclaration(ai, c); putProperties(ai, c); putMethods(ai, c); if (ai.getIdMethod() == null) { throw new PersistenceException("No ID method specified for: " + c.getName()); } putEntityListeners(ai, c); getAnnotationMap().put(c.getName(), ai); return ai; }
private void putMethods(AnnotationInfo ai, Class c) { Method[] methods = c.getDeclaredMethods(); for (Method method : methods) { // logger.fine("method=" + method.getName()); String methodName = method.getName(); if (!methodName.startsWith("get")) continue; // System.out.println("method=" + methodName); if (config.isGroovyBeans() && (methodName.equals("getProperty") || methodName.equals("getMetaClass"))) continue; Transient transientM = method.getAnnotation(Transient.class); if (transientM != null) continue; // we don't save this one ai.addGetter(method); } }