private static AccessType getAccessTypeOrNull(AnnotatedElement element) { if ( element == null ) { return null; } Access elementAccess = element.getAnnotation( Access.class ); return elementAccess == null ? null : elementAccess.value(); }
private static AccessType getAccessTypeOrNull(CtMember ctMember) { Access access = getAnnotationOrNull( ctMember, Access.class ); return access == null ? null : access.value(); }
@Override public VisitorConfig getConfig(TypeElement e, List<? extends Element> elements) { Access access = e.getAnnotation(Access.class); if (access != null) { if (access.value() == AccessType.FIELD) { return VisitorConfig.FIELDS_ONLY; } else { return VisitorConfig.METHODS_ONLY; } } boolean fields = false, methods = false; for (Element element : elements) { if (hasRelevantAnnotation(element)) { fields |= element.getKind().equals(ElementKind.FIELD); methods |= element.getKind().equals(ElementKind.METHOD); } } return VisitorConfig.get(fields, methods, VisitorConfig.ALL); }
public AccessType getDefaultAccess() throws MappingException { AccessType accessType = defaultAccess; AccessType hibernateAccessType = AccessType.DEFAULT; AccessType jpaAccessType = AccessType.DEFAULT; org.hibernate.annotations.AccessType accessTypeAnnotation = property.getAnnotation( org.hibernate.annotations.AccessType.class ); if ( accessTypeAnnotation != null ) { hibernateAccessType = AccessType.getAccessStrategy( accessTypeAnnotation.value() ); } Access access = property.getAnnotation( Access.class ); if ( access != null ) { jpaAccessType = AccessType.getAccessStrategy( access.value() ); } if ( hibernateAccessType != AccessType.DEFAULT && jpaAccessType != AccessType.DEFAULT && hibernateAccessType != jpaAccessType ) { StringBuilder builder = new StringBuilder(); builder.append( property.toString() ); builder.append( " defines @AccessType and @Access with contradicting values. Use of @Access only is recommended." ); throw new MappingException( builder.toString() ); } if ( hibernateAccessType != AccessType.DEFAULT ) { accessType = hibernateAccessType; } else if ( jpaAccessType != AccessType.DEFAULT ) { accessType = jpaAccessType; } return accessType; }
final Access localAccessAnnotation = xProperty.getAnnotation( Access.class ); if ( localAccessAnnotation == null || localAccessAnnotation.value() != javax.persistence.AccessType.FIELD ) { continue; final Access localAccessAnnotation = xProperty.getAnnotation( Access.class ); if ( localAccessAnnotation == null || localAccessAnnotation.value() != javax.persistence.AccessType.PROPERTY ) { continue;
private static AccessType getAccessTypeOrNull(CtClass ctClass) { try { if ( ctClass.hasAnnotation( Access.class ) ) { return ( (Access) ctClass.getAnnotation( Access.class ) ).value(); } else { CtClass extendsClass = ctClass.getSuperclass(); return extendsClass == null ? null : getAccessTypeOrNull( extendsClass ); } } catch (ClassNotFoundException e) { return null; } catch (NotFoundException e) { return null; } }
private AnnotationList doGetAnnotations() { AnnotationDescription.Loadable<Access> access = fieldDescription.getDeclaringType().asErasure() .getDeclaredAnnotations().ofType( Access.class ); if ( access != null && access.loadSilent().value() == AccessType.PROPERTY ) { Optional<MethodDescription> getter = getGetter(); if ( getter.isPresent() ) { return getter.get().getDeclaredAnnotations(); } else { return fieldDescription.getDeclaredAnnotations(); } } else if ( access != null && access.loadSilent().value() == AccessType.FIELD ) { return fieldDescription.getDeclaredAnnotations(); } else { Optional<MethodDescription> getter = getGetter(); // Note that the order here is important List<AnnotationDescription> annotationDescriptions = new ArrayList<>(); if ( getter.isPresent() ) { annotationDescriptions.addAll( getter.get().getDeclaredAnnotations() ); } annotationDescriptions.addAll( fieldDescription.getDeclaredAnnotations() ); return fieldDescription.getDeclaredAnnotations(); } } }
private static TypeDescription.Generic target(AnnotatedFieldDescription persistentField) { AnnotationDescription.Loadable<Access> access = persistentField.getDeclaringType().asErasure().getDeclaredAnnotations().ofType( Access.class ); if ( access != null && access.loadSilent().value() == AccessType.FIELD ) { return persistentField.getType(); } else { Optional<MethodDescription> getter = persistentField.getGetter(); if ( getter.isPresent() ) { return getter.get().getReturnType(); } else { return persistentField.getType(); } } }
jpaAccessType = AccessType.getAccessStrategy( access.value() );
/** * Looks up both Spring Data's and JPA's access type definition annotations on the property or type level to determine * the access type to be used. Will consider property-level annotations over type-level ones, favoring the Spring Data * ones over the JPA ones if found on the same level. Returns {@literal null} if no explicit annotation can be found * falling back to the defaults implemented in the super class. * * @return */ @Nullable private Boolean detectPropertyAccess() { org.springframework.data.annotation.AccessType accessType = findAnnotation( org.springframework.data.annotation.AccessType.class); if (accessType != null) { return Type.PROPERTY.equals(accessType.value()); } Access access = findAnnotation(Access.class); if (access != null) { return AccessType.PROPERTY.equals(access.value()); } accessType = findPropertyOrOwnerAnnotation(org.springframework.data.annotation.AccessType.class); if (accessType != null) { return Type.PROPERTY.equals(accessType.value()); } access = findPropertyOrOwnerAnnotation(Access.class); if (access != null) { return AccessType.PROPERTY.equals(access.value()); } return null; }
private AccessType determineLocalClassDefinedAccessStrategy() { AccessType classDefinedAccessType; AccessType hibernateDefinedAccessType = AccessType.DEFAULT; AccessType jpaDefinedAccessType = AccessType.DEFAULT; org.hibernate.annotations.AccessType accessType = xClass.getAnnotation( org.hibernate.annotations.AccessType.class ); if ( accessType != null ) { hibernateDefinedAccessType = AccessType.getAccessStrategy( accessType.value() ); } Access access = xClass.getAnnotation( Access.class ); if ( access != null ) { jpaDefinedAccessType = AccessType.getAccessStrategy( access.value() ); } if ( hibernateDefinedAccessType != AccessType.DEFAULT && jpaDefinedAccessType != AccessType.DEFAULT && hibernateDefinedAccessType != jpaDefinedAccessType ) { throw new MappingException( "@AccessType and @Access specified with contradicting values. Use of @Access only is recommended. " ); } if ( hibernateDefinedAccessType != AccessType.DEFAULT ) { classDefinedAccessType = hibernateDefinedAccessType; } else { classDefinedAccessType = jpaDefinedAccessType; } return classDefinedAccessType; }
private AccessType determineDefaultAccessType() { for (XClass xclass = clazz; xclass != null; xclass = xclass.getSuperclass()) { if ( ( xclass.getSuperclass() == null || Object.class.getName().equals( xclass.getSuperclass().getName() ) ) && ( xclass.isAnnotationPresent( Entity.class ) || xclass.isAnnotationPresent( MappedSuperclass.class ) ) && xclass.isAnnotationPresent( Access.class ) ) { return AccessType.getAccessStrategy( xclass.getAnnotation( Access.class ).value() ); } } // Guess from identifier. // FIX: Shouldn't this be determined by the first attribute (i.e., field or property) with annotations, but without an // explicit Access annotation, according to JPA 2.0 spec 2.3.1: Default Access Type? for (XClass xclass = clazz; xclass != null && !Object.class.getName().equals(xclass.getName()); xclass = xclass.getSuperclass()) { if ( xclass.isAnnotationPresent( Entity.class ) || xclass.isAnnotationPresent( MappedSuperclass.class ) ) { for ( XProperty prop : xclass.getDeclaredProperties( AccessType.PROPERTY.getType() ) ) { final boolean isEmbeddedId = prop.isAnnotationPresent( EmbeddedId.class ); if ( prop.isAnnotationPresent( Id.class ) || isEmbeddedId ) { return AccessType.PROPERTY; } } for ( XProperty prop : xclass.getDeclaredProperties( AccessType.FIELD.getType() ) ) { final boolean isEmbeddedId = prop.isAnnotationPresent( EmbeddedId.class ); if ( prop.isAnnotationPresent( Id.class ) || isEmbeddedId ) { return AccessType.FIELD; } } } } throw new AnnotationException( "No identifier specified for entity: " + clazz ); }
AccessType accessType = AccessType.getAccessStrategy( access.value() ); if ( accessType == AccessType.PROPERTY ) { log.warn( "Placing @Access(AccessType.PROPERTY) on a field does not have any effect." ); AccessType accessType = AccessType.getAccessStrategy( access.value() ); if ( accessType == AccessType.FIELD ) { log.warn( "Placing @Access(AccessType.FIELD) on a field does not have any effect." );
@Test public void testAllAttributes() throws Exception { reader = getReader( Entity1.class, "field1", "many-to-one.orm6.xml" ); assertAnnotationPresent( ManyToOne.class ); assertAnnotationNotPresent( JoinColumn.class ); assertAnnotationNotPresent( JoinColumns.class ); assertAnnotationNotPresent( JoinTable.class ); assertAnnotationPresent( Id.class ); assertAnnotationPresent( MapsId.class ); assertAnnotationPresent( Access.class ); ManyToOne relAnno = reader.getAnnotation( ManyToOne.class ); assertEquals( 0, relAnno.cascade().length ); assertEquals( FetchType.LAZY, relAnno.fetch() ); assertFalse( relAnno.optional() ); assertEquals( Entity3.class, relAnno.targetEntity() ); assertEquals( "col1", reader.getAnnotation( MapsId.class ).value() ); assertEquals( AccessType.PROPERTY, reader.getAnnotation( Access.class ) .value() ); }
public AccessType getDefaultAccess() throws MappingException { AccessType accessType = defaultAccess; AccessType hibernateAccessType = AccessType.DEFAULT; AccessType jpaAccessType = AccessType.DEFAULT; org.hibernate.annotations.AccessType accessTypeAnnotation = property.getAnnotation( org.hibernate.annotations.AccessType.class ); if ( accessTypeAnnotation != null ) { hibernateAccessType = AccessType.getAccessStrategy( accessTypeAnnotation.value() ); } Access access = property.getAnnotation( Access.class ); if ( access != null ) { jpaAccessType = AccessType.getAccessStrategy( access.value() ); } if ( hibernateAccessType != AccessType.DEFAULT && jpaAccessType != AccessType.DEFAULT && hibernateAccessType != jpaAccessType ) { StringBuilder builder = new StringBuilder(); builder.append( property.toString() ); builder.append( " defines @AccessType and @Access with contradicting values. Use of @Access only is recommended." ); throw new MappingException( builder.toString() ); } if ( hibernateAccessType != AccessType.DEFAULT ) { accessType = hibernateAccessType; } else if ( jpaAccessType != AccessType.DEFAULT ) { accessType = jpaAccessType; } return accessType; }
@Test public void testAllAttributes() throws Exception { reader = getReader( Entity2.class, "field1", "many-to-many.orm21.xml" ); assertAnnotationPresent( ManyToMany.class ); assertAnnotationNotPresent( OrderBy.class ); assertAnnotationNotPresent( OrderColumn.class ); assertAnnotationNotPresent( MapKey.class ); assertAnnotationNotPresent( MapKeyClass.class ); assertAnnotationNotPresent( MapKeyTemporal.class ); assertAnnotationNotPresent( MapKeyEnumerated.class ); assertAnnotationNotPresent( MapKeyColumn.class ); assertAnnotationNotPresent( MapKeyJoinColumns.class ); assertAnnotationNotPresent( MapKeyJoinColumn.class ); assertAnnotationNotPresent( JoinTable.class ); assertAnnotationPresent( Access.class ); ManyToMany relAnno = reader.getAnnotation( ManyToMany.class ); assertEquals( 0, relAnno.cascade().length ); assertEquals( FetchType.EAGER, relAnno.fetch() ); assertEquals( "field2", relAnno.mappedBy() ); assertEquals( Entity3.class, relAnno.targetEntity() ); assertEquals( AccessType.PROPERTY, reader.getAnnotation( Access.class ) .value() ); }
assertEquals( AccessType.PROPERTY, reader.getAnnotation( Access.class ) .value() );
jpaAccessType = AccessType.getAccessStrategy( access.value() );
private AccessType determineClassDefinedAccessStrategy() { AccessType classDefinedAccessType; AccessType hibernateDefinedAccessType = AccessType.DEFAULT; AccessType jpaDefinedAccessType = AccessType.DEFAULT; org.hibernate.annotations.AccessType accessType = xClass.getAnnotation( org.hibernate.annotations.AccessType.class ); if ( accessType != null ) { hibernateDefinedAccessType = AccessType.getAccessStrategy( accessType.value() ); } Access access = xClass.getAnnotation( Access.class ); if ( access != null ) { jpaDefinedAccessType = AccessType.getAccessStrategy( access.value() ); } if ( hibernateDefinedAccessType != AccessType.DEFAULT && jpaDefinedAccessType != AccessType.DEFAULT && hibernateDefinedAccessType != jpaDefinedAccessType ) { throw new MappingException( "@AccessType and @Access specified with contradicting values. Use of @Access only is recommended. " ); } if ( hibernateDefinedAccessType != AccessType.DEFAULT ) { classDefinedAccessType = hibernateDefinedAccessType; } else { classDefinedAccessType = jpaDefinedAccessType; } return classDefinedAccessType; }
public boolean includes(AnnotatedElement obj) { Access access = obj.getAnnotation(Access.class); return access != null && access.value().equals(target); } }