@Override @SuppressWarnings({"unchecked", "rawtypes"}) public ResourceInformation build(final Class<?> resourceClass) { String resourceType = getResourceType(resourceClass); MetaDataObject meta = metaProvider.discoverMeta(resourceClass).asDataObject(); DefaultResourceInstanceBuilder instanceBuilder = new DefaultResourceInstanceBuilder(resourceClass); List<ResourceField> fields = getResourceFields(resourceClass); Class<?> superclass = resourceClass.getSuperclass(); String superResourceType = superclass != Object.class && superclass.getAnnotation(MappedSuperclass.class) == null ? context.getResourceType(superclass) : null; TypeParser typeParser = context.getTypeParser(); return new JpaResourceInformation(typeParser, meta, resourceClass, resourceType, superResourceType, instanceBuilder, fields); }
@Override public String getResourceType(Class<?> entityClass) { JpaResource annotation = entityClass.getAnnotation(JpaResource.class); if (annotation != null) { return annotation.type(); } if (entityClass.getAnnotation(MappedSuperclass.class) != null) { return null; // super classes do not have a document type } String name = entityClass.getSimpleName(); if (name.endsWith(ENTITY_NAME_SUFFIX)) { name = name.substring(0, name.length() - ENTITY_NAME_SUFFIX.length()); } return Character.toLowerCase(name.charAt(0)) + name.substring(1); }
private void buildHierarchyColumnOverride(XClass element) { XClass current = element; Map<String, Column[]> columnOverride = new HashMap<String, Column[]>(); Map<String, JoinColumn[]> joinColumnOverride = new HashMap<String, JoinColumn[]>(); Map<String, JoinTable> joinTableOverride = new HashMap<String, JoinTable>(); while ( current != null && !mappings.getReflectionManager().toXClass( Object.class ).equals( current ) ) { if ( current.isAnnotationPresent( Entity.class ) || current.isAnnotationPresent( MappedSuperclass.class ) || current.isAnnotationPresent( Embeddable.class ) ) { //FIXME is embeddable override? Map<String, Column[]> currentOverride = buildColumnOverride( current, getPath() ); Map<String, JoinColumn[]> currentJoinOverride = buildJoinColumnOverride( current, getPath() ); Map<String, JoinTable> currentJoinTableOverride = buildJoinTableOverride( current, getPath() ); currentOverride.putAll( columnOverride ); //subclasses have precedence over superclasses currentJoinOverride.putAll( joinColumnOverride ); //subclasses have precedence over superclasses currentJoinTableOverride.putAll( joinTableOverride ); //subclasses have precedence over superclasses columnOverride = currentOverride; joinColumnOverride = currentJoinOverride; joinTableOverride = currentJoinTableOverride; } current = current.getSuperclass(); } holderColumnOverride = columnOverride.size() > 0 ? columnOverride : null; holderJoinColumnOverride = joinColumnOverride.size() > 0 ? joinColumnOverride : null; holderJoinTableOverride = joinTableOverride.size() > 0 ? joinTableOverride : null; }
protected List<String> getClasses(Resource[] resources) { List<String> classNames = new ArrayList<>(); for (Resource resource : resources) { if (resource.isReadable()) { MetadataReader metadataReader; try { metadataReader = metadataReaderFactory.getMetadataReader(resource); } catch (IOException e) { throw new RuntimeException("Unable to read metadata resource", e); } AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata(); if (annotationMetadata.isAnnotated(com.haulmont.chile.core.annotations.MetaClass.class.getName()) || annotationMetadata.isAnnotated(MappedSuperclass.class.getName()) || annotationMetadata.isAnnotated(Entity.class.getName())) { ClassMetadata classMetadata = metadataReader.getClassMetadata(); classNames.add(classMetadata.getClassName()); } } } return classNames; }
/** * Fills the {@link #properties}. * * @param c * the currently inspected class * @param stopClass * the class in the hierarchy to stop inspecting */ private void buildProperties(final Class<? super E> c, final Class<? super E> stopClass) { // Fill properties of super classes (at least until we find the joined parent class) if (c.getSuperclass() != null && c.getSuperclass() != stopClass) { buildProperties(c.getSuperclass(), stopClass); } // And now fill the properties of this class if (c.isAnnotationPresent(MappedSuperclass.class) || c.isAnnotationPresent(Entity.class)) { for (final AttributeAccessor field : this.accessStyle.getDeclaredAttributes(c, this.entityClass)) { if (!field.isAnnotationPresent(EmbeddedId.class) && !field.isAnnotationPresent(Id.class)) { final Property<E, ?> property = buildProperty(field, getColumnAnnotation(field), this.associationOverrides.get(field.getName())); if (property != null) { this.properties.put(field.getName(), property); this.allProperties.add(property); if (property instanceof SingularProperty) { buildUniqueProperty((SingularProperty<E, ?>) property); } } } } } }
@Test public void testStaticWeaving() { // first, scan for all files on the classpath with an @Entity or @MappedSuperClass annotation Reflections reflections = new Reflections( DocumentAttachment.class.getPackage().getName(), DocumentBase.class.getPackage().getName(), MaintenanceLock.class.getPackage().getName(), Message.class.getPackage().getName()); Set<Class<?>> entityTypes = reflections.getTypesAnnotatedWith(Entity.class); Set<Class<?>> superTypes = reflections.getTypesAnnotatedWith(MappedSuperclass.class); Set<Class<?>> embeddableTypes = reflections.getTypesAnnotatedWith(Embeddable.class); // next, let's assert that they have been statically weaved assertStaticWeaved(entityTypes, superTypes, embeddableTypes); }
/** * A partir du nom du fieldname, retorune le field * @param cl * @param fieldName * @return * @throws FieldNotFound */ private static Field getField(final Class cl, final String fieldName) throws FieldNotFound { Class tClass = cl; boolean found = false; while (!found && (tClass.isAnnotationPresent(Entity.class) || tClass.isAnnotationPresent(MappedSuperclass.class))) { try { Field field = tClass.getDeclaredField(fieldName); if (field.isAnnotationPresent(Lob.class)) { throw new FieldNotFound(); } return field; } catch (NoSuchFieldException ex) { // on verifie dans la hierarchie aussi tClass = tClass.getSuperclass(); } } throw new FieldNotFound(); }
public static boolean isJpaAnnotated(Class<?> clazz) { if (clazz == null) { return false; } if (!cache.containsKey(clazz.getName())) { if (clazz.getName().contains("EnhancerByCGLIB")) { try { // Strip a proxy if found clazz = Class.forName(clazz.getName().substring(0, clazz.getName().indexOf("$$EnhancerByCGLIB"))); } catch (Exception e) { LOG.error(e.getMessage(), e); } } synchronized (cache) { cache.put(clazz.getName(), new Boolean(clazz.isAnnotationPresent(Entity.class) || clazz.isAnnotationPresent(MappedSuperclass.class))); } } return cache.get(clazz.getName()).booleanValue(); }
/** * Verifies that all class that are annotated with {@link Entity} observe the rules for JPA entities. * * <ul> * <li>The class must have a public or protected, no-argument constructor. The class may have other * constructors.</li> * <li>The class must not be declared final.</li> * <li>No methods or persistent instance variables must be declared final.</li> * <li>Persistent instance variables must be declared private, protected, or package-private.</li> * </ul> * * @return Self. */ public JandexAssert hasOnlyValidJpaEntities() { // Precondition isNotNull(); final List<AnnotationInstance> annotations = new ArrayList<>(); annotations.addAll(actual.getAnnotations(DotName.createSimple(Entity.class.getName()))); annotations.addAll(actual.getAnnotations(DotName.createSimple(MappedSuperclass.class.getName()))); for (final AnnotationInstance ai : annotations) { final AnnotationTarget target = ai.target(); final ClassInfo info = target.asClass(); final AssertionRules<ClassInfo> rules = new AssertionRules<ClassInfo>( new RulePublicOrProtectedNoArgConstructor(), new RuleClassNotFinal(), new RuleClassHasNoFinalMethods(), new RulePersistentInstanceFieldVisibility()); final AssertionResult result = rules.verify(info); if (!result.isValid()) { failWithMessage(result.getErrorMessage()); } } return this; }
private void extractInheritanceType() { XAnnotatedElement element = getClazz(); Inheritance inhAnn = element.getAnnotation( Inheritance.class ); MappedSuperclass mappedSuperClass = element.getAnnotation( MappedSuperclass.class ); if ( mappedSuperClass != null ) { setEmbeddableSuperclass( true ); setType( inhAnn == null ? null : inhAnn.strategy() ); } else { setType( inhAnn == null ? InheritanceType.SINGLE_TABLE : inhAnn.strategy() ); } }
private void addMappedSuperClassInMetadata(PersistentClass persistentClass) { //add @MappedSuperclass in the metadata // classes from 0 to n-1 are @MappedSuperclass and should be linked org.hibernate.mapping.MappedSuperclass mappedSuperclass = null; final InheritanceState superEntityState = InheritanceState.getInheritanceStateOfSuperEntity( clazz, inheritanceStatePerClass ); PersistentClass superEntity = superEntityState != null ? mappings.getClass( superEntityState.getClazz().getName() ) : null; final int lastMappedSuperclass = classesToProcessForMappedSuperclass.size() - 1; for ( int index = 0; index < lastMappedSuperclass; index++ ) { org.hibernate.mapping.MappedSuperclass parentSuperclass = mappedSuperclass; final Class<?> type = mappings.getReflectionManager() .toClass( classesToProcessForMappedSuperclass.get( index ) ); //add MAppedSuperclass if not already there mappedSuperclass = mappings.getMappedSuperclass( type ); if ( mappedSuperclass == null ) { mappedSuperclass = new org.hibernate.mapping.MappedSuperclass( parentSuperclass, superEntity ); mappedSuperclass.setMappedClass( type ); mappings.addMappedSuperclass( type, mappedSuperclass ); } } if ( mappedSuperclass != null ) { persistentClass.setSuperMappedSuperclass( mappedSuperclass ); } }
private MappedSuperclass getMappedSuperclass(Element tree, XMLContext.Default defaults) { if ( tree == null ) { return defaults.canUseJavaAnnotations() ? getPhysicalAnnotation( MappedSuperclass.class ) : null; } else { if ( "mapped-superclass".equals( tree.getName() ) ) { AnnotationDescriptor entity = new AnnotationDescriptor( MappedSuperclass.class ); return AnnotationFactory.create( entity ); } else { return null; //this is not an entity } } }
@Nonnull @SuppressWarnings("unchecked") public static Set<Class<?>> getManagedJpaClasses() { return getMainClasses(Predicates.or( withAnnotation(Entity.class), withAnnotation(Embeddable.class), withAnnotation(MappedSuperclass.class))); }
private Class<?> getJpaSuperclass(Class<?> resourceClass) { Class<?> superclass = resourceClass.getSuperclass(); while(superclass != Object.class){ if(superclass.getAnnotation(Entity.class) != null || superclass.getAnnotation(MappedSuperclass.class) != null){ return superclass; } superclass = superclass.getSuperclass(); } return null; }
public String getParent() { MetaClass ancestor = meta.getAncestor(); if (ancestor == null || !ancestor.getName().contains("$") || ancestor.getJavaClass().isAnnotationPresent(MappedSuperclass.class)) return ""; if (!readPermitted(ancestor)) { return null; } return "Parent is " + asHref(ancestor.getName()); }
protected Set<String> getDescendants(Set<String> relatedTypes) { if (relatedTypes == null) return null; Set<String> newRelatedTypes = new HashSet<>(); relatedTypes.forEach(type -> { newRelatedTypes.add(type); MetaClass metaClass = metadata.getClassNN(type); if (metaClass.getDescendants() != null) { Set<String> descendants = metaClass.getDescendants().stream() .filter(it -> it.getJavaClass() != null && !it.getJavaClass().isAnnotationPresent(MappedSuperclass.class)) .map(MetadataObject::getName).collect(Collectors.toSet()); newRelatedTypes.addAll(descendants); } }); return newRelatedTypes; }
/** * Determines if a given annotation set contains annotations that correspond to ones that someone would expect to appear * in a persistence.xml * * @param annotations * @return */ protected boolean containsTypeLevelPersistenceAnnotation(Annotation[] annotations) { for (Annotation annotation : annotations) { if (annotation.getTypeName().equals(Entity.class.getName()) || annotation.getTypeName().equals(Embeddable.class.getName()) || annotation.getTypeName().equals(MappedSuperclass.class.getName())) { return true; } } return false; }
@Test public void testStaticWeaving() { // first, scan for all files on the classpath with an @Entity or @MappedSuperClass annotation Reflections reflections = new Reflections( DocumentAttachment.class.getPackage().getName(), DocumentBase.class.getPackage().getName(), MaintenanceLock.class.getPackage().getName(), Message.class.getPackage().getName()); Set<Class<?>> entityTypes = reflections.getTypesAnnotatedWith(Entity.class, true); Set<Class<?>> superTypes = reflections.getTypesAnnotatedWith(MappedSuperclass.class, true); Set<Class<?>> embeddableTypes = reflections.getTypesAnnotatedWith(Embeddable.class, true); // next, let's assert that they have been statically weaved assertStaticWeaved(entityTypes, superTypes, embeddableTypes); }
@Test public void testStaticWeaving() { // first, scan for all files on the classpath with an @Entity or @MappedSuperClass annotation Reflections reflections = new Reflections("org.kuali.rice.krad"); Set<Class<?>> entityTypes = reflections.getTypesAnnotatedWith(Entity.class, true); Set<Class<?>> superTypes = reflections.getTypesAnnotatedWith(MappedSuperclass.class, true); Set<Class<?>> embeddableTypes = reflections.getTypesAnnotatedWith(Embeddable.class, true); // next, let's assert that they have been statically weaved assertStaticWeaved(entityTypes, superTypes, embeddableTypes); }
@Test public void testStaticWeaving() { // first, scan for all files on the classpath with an @Entity or @MappedSuperClass annotation Reflections reflections = new Reflections(getClass().getPackage().getName()); Set<Class<?>> entityTypes = reflections.getTypesAnnotatedWith(Entity.class, true); Set<Class<?>> superTypes = reflections.getTypesAnnotatedWith(MappedSuperclass.class, true); Set<Class<?>> embeddableTypes = reflections.getTypesAnnotatedWith(Embeddable.class, true); // next, let's assert that they have been statically weaved assertStaticWeaved(entityTypes, superTypes, embeddableTypes); }
@Test public void testStaticWeaving() { // first, scan for all files on the classpath with an @Entity or @MappedSuperClass annotation Reflections reflections = new Reflections( PersistableBusinessObjectBase.class.getPackage().getName()); Set<Class<?>> entityTypes = reflections.getTypesAnnotatedWith(Entity.class, true); Set<Class<?>> superTypes = reflections.getTypesAnnotatedWith(MappedSuperclass.class, true); Set<Class<?>> embeddableTypes = reflections.getTypesAnnotatedWith(Embeddable.class, true); // next, let's assert that they have been statically weaved assertStaticWeaved(entityTypes, superTypes, embeddableTypes); }
@Test public void testStaticWeaving() { // first, scan for all files on the classpath with an @Entity or @MappedSuperClass annotation Reflections reflections = new Reflections("org.kuali.rice.kew", "org.kuali.rice.kim", "org.kuali.rice.kcb", "org.kuali.rice.ken"); Set<Class<?>> entityTypes = reflections.getTypesAnnotatedWith(Entity.class, true); Set<Class<?>> superTypes = reflections.getTypesAnnotatedWith(MappedSuperclass.class, true); Set<Class<?>> embeddableTypes = reflections.getTypesAnnotatedWith(Embeddable.class, true); // next, let's assert that they have been statically weaved assertStaticWeaved(entityTypes, superTypes, embeddableTypes); }
public boolean isRootClass(Class<?> theClass) { final boolean notMappedSuperclassAndNotEmbeddable = theClass .getAnnotation(MappedSuperclass.class) == null && theClass.getAnnotation(Embeddable.class) == null; if (theClass.getSuperclass() != null) { return notMappedSuperclassAndNotEmbeddable && !isSelfOrAncestorRootClass(theClass.getSuperclass()); } else { return notMappedSuperclassAndNotEmbeddable; } }
public boolean isSelfOrAncestorRootClass(Class<?> theClass) { if (isRootClass(theClass)) { return true; } else if (theClass.getSuperclass() != null) { return isSelfOrAncestorRootClass(theClass.getSuperclass()); } else { return theClass.getAnnotation(MappedSuperclass.class) == null && theClass.getAnnotation(Embeddable.class) == null; } }
/** * Scans for *.orm.xml and adds Entites from classpath. * * @param pui * the pui */ @Override public void postProcessPersistenceUnitInfo( MutablePersistenceUnitInfo pui ) { _Log.info( "Scanning for JPA orm.xml files" ); for ( File ormFile : getListORMFiles( ) ) { String ormAbsolutePath = ormFile.getAbsolutePath( ); _Log.info( "Found ORM file : " + ormAbsolutePath ); pui.addMappingFileName( ormAbsolutePath.substring( ormAbsolutePath.indexOf( CLASSPATH_PATH_IDENTIFIER ) ) ); } _Log.info( "Scanning for JPA entities..." ); Set<String> entityClasses = AnnotationUtil.find( Entity.class.getName( ) ); entityClasses.addAll( AnnotationUtil.find( Embeddable.class.getName( ) ) ); entityClasses.addAll( AnnotationUtil.find( MappedSuperclass.class.getName( ) ) ); for ( String strClass : entityClasses ) { _Log.info( "Found entity class : " + strClass ); if ( !pui.getManagedClassNames( ).contains( strClass ) ) { pui.addManagedClassName( strClass ); } } if ( _Log.isDebugEnabled( ) ) { dumpPersistenceUnitInfo( pui ); } }
/** * Fills the {@link #idProperty}. * * @param c * the currently inspected class */ private void buildIdProperty(final Class<? super E> c) { // TODO (Issue #2) Support @IdClass // Find ID properties of super classes if (c.getSuperclass() != null) { buildIdProperty(c.getSuperclass()); } // Find the Entity / MappedSuperclass annotation if (c.isAnnotationPresent(Entity.class) || c.isAnnotationPresent(MappedSuperclass.class)) { // Determine the access type if (this.accessStyle == null) { final Access accessType = c.getAnnotation(Access.class); if (accessType != null) { this.accessStyle = AccessStyle.getStyle(accessType.value()); } } // And now find the id property of this class if (this.accessStyle == null) { if (findIdProperty(AccessStyle.FIELD.getDeclaredAttributes(c, this.entityClass))) { this.accessStyle = AccessStyle.FIELD; } else if (findIdProperty(AccessStyle.METHOD.getDeclaredAttributes(c, this.entityClass))) { this.accessStyle = AccessStyle.METHOD; } } else { findIdProperty(this.accessStyle.getDeclaredAttributes(c, this.entityClass)); } } }
@Test public void testStaticWeaving() { // first, scan for all files on the classpath with an @Entity or @MappedSuperClass annotation Reflections reflections = new Reflections("org.kuali.rice.krad"); Set<Class<?>> entityTypes = reflections.getTypesAnnotatedWith(Entity.class); Set<Class<?>> superTypes = reflections.getTypesAnnotatedWith(MappedSuperclass.class); Set<Class<?>> embeddableTypes = reflections.getTypesAnnotatedWith(Embeddable.class); // next, let's assert that they have been statically weaved assertStaticWeaved(entityTypes, superTypes, embeddableTypes); }
@Test public void testStaticWeaving() { // first, scan for all files on the classpath with an @Entity or @MappedSuperClass annotation Reflections reflections = new Reflections(getClass().getPackage().getName()); Set<Class<?>> entityTypes = reflections.getTypesAnnotatedWith(Entity.class); Set<Class<?>> superTypes = reflections.getTypesAnnotatedWith(MappedSuperclass.class); Set<Class<?>> embeddableTypes = reflections.getTypesAnnotatedWith(Embeddable.class); // next, let's assert that they have been statically weaved assertStaticWeaved(entityTypes, superTypes, embeddableTypes); }
@Test public void testStaticWeaving() { // first, scan for all files on the classpath with an @Entity or @MappedSuperClass annotation Reflections reflections = new Reflections( PersistableBusinessObjectBase.class.getPackage().getName()); Set<Class<?>> entityTypes = reflections.getTypesAnnotatedWith(Entity.class); Set<Class<?>> superTypes = reflections.getTypesAnnotatedWith(MappedSuperclass.class); Set<Class<?>> embeddableTypes = reflections.getTypesAnnotatedWith(Embeddable.class); // next, let's assert that they have been statically weaved assertStaticWeaved(entityTypes, superTypes, embeddableTypes); }