下面列出了怎么用javax.persistence.AccessType的API类实例代码及写法,或者点击链接到github查看源代码。
private boolean isProcessingId(XMLContext.Default defaults) {
boolean isExplicit = defaults.getAccess() != null;
boolean correctAccess =
( PropertyType.PROPERTY.equals( propertyType ) && AccessType.PROPERTY.equals( defaults.getAccess() ) )
|| ( PropertyType.FIELD.equals( propertyType ) && AccessType.FIELD
.equals( defaults.getAccess() ) );
boolean hasId = defaults.canUseJavaAnnotations()
&& ( isPhysicalAnnotationPresent( Id.class ) || isPhysicalAnnotationPresent( EmbeddedId.class ) );
//if ( properAccessOnMetadataComplete || properOverridingOnMetadataNonComplete ) {
boolean mirrorAttributeIsId = defaults.canUseJavaAnnotations() &&
( mirroredAttribute != null &&
( mirroredAttribute.isAnnotationPresent( Id.class )
|| mirroredAttribute.isAnnotationPresent( EmbeddedId.class ) ) );
boolean propertyIsDefault = PropertyType.PROPERTY.equals( propertyType )
&& !mirrorAttributeIsId;
return correctAccess || ( !isExplicit && hasId ) || ( !isExplicit && propertyIsDefault );
}
private void getAccessType(List<Annotation> annotationList, Element element) {
if ( element == null ) {
return;
}
String access = element.attributeValue( "access" );
if ( access != null ) {
AnnotationDescriptor ad = new AnnotationDescriptor( Access.class );
AccessType type;
try {
type = AccessType.valueOf( access );
}
catch ( IllegalArgumentException e ) {
throw new AnnotationException( access + " is not a valid access type. Check you xml confguration." );
}
if ( ( AccessType.PROPERTY.equals( type ) && this.element instanceof Method ) ||
( AccessType.FIELD.equals( type ) && this.element instanceof Field ) ) {
return;
}
ad.setValue( "value", type );
annotationList.add( AnnotationFactory.create( ad ) );
}
}
protected static AccessType getAccessType(Class<?> containerJavaType, String propertyName) {
Field field = fieldOrNull( containerJavaType, propertyName );
AccessType fieldAccessType = getAccessTypeOrNull( field );
if ( fieldAccessType != null ) {
return fieldAccessType;
}
AccessType methodAccessType = getAccessTypeOrNull( getterMethodOrNull( containerJavaType, propertyName ) );
if ( methodAccessType != null ) {
return methodAccessType;
}
// No @Access on property or field; check to see if containerJavaType has an explicit @Access
AccessType classAccessType = getAccessTypeOrNull( containerJavaType );
if ( classAccessType != null ) {
return classAccessType;
}
return field != null ? AccessType.FIELD : AccessType.PROPERTY;
}
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;
}
/**
* 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;
}
private void setAccess( String access, Default defaultType) {
AccessType type;
if ( access != null ) {
try {
type = AccessType.valueOf( access );
}
catch ( IllegalArgumentException e ) {
throw new AnnotationException( "Invalid access type " + access + " (check your xml configuration)" );
}
defaultType.setAccess( type );
}
}
public PropertyAccessMixedImpl(
PropertyAccessStrategy strategy,
Class containerJavaType,
String propertyName) {
this.strategy = strategy;
AccessType propertyAccessType = getAccessType( containerJavaType, propertyName );
switch ( propertyAccessType ) {
case FIELD: {
Field field = fieldOrNull( containerJavaType, propertyName );
if ( field == null ) {
throw new PropertyAccessBuildingException(
"Could not locate field for property named [" + containerJavaType.getName() + "#" + propertyName + "]"
);
}
this.getter = fieldGetter( containerJavaType, propertyName, field );
this.setter = fieldSetter( containerJavaType, propertyName, field );
break;
}
case PROPERTY: {
Method getterMethod = getterMethodOrNull( containerJavaType, propertyName );
if ( getterMethod == null ) {
throw new PropertyAccessBuildingException(
"Could not locate getter for property named [" + containerJavaType.getName() + "#" + propertyName + "]"
);
}
Method setterMethod = setterMethodOrNull( containerJavaType, propertyName, getterMethod.getReturnType() );
this.getter = propertyGetter( containerJavaType, propertyName, getterMethod );
this.setter = propertySetter( containerJavaType, propertyName, setterMethod );
break;
}
default: {
throw new PropertyAccessBuildingException(
"Invalid access type " + propertyAccessType + " for property named [" + containerJavaType.getName() + "#" + propertyName + "]"
);
}
}
}
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 TypeDescription.Generic target(FieldDescription persistentField) {
AnnotationDescription.Loadable<Access> access = persistentField.getDeclaringType().asErasure().getDeclaredAnnotations().ofType( Access.class );
if ( access != null && access.loadSilent().value() == AccessType.FIELD ) {
return persistentField.getType();
}
else {
MethodDescription getter = EnhancerImpl.getterOf( persistentField );
if ( getter == null ) {
return persistentField.getType();
}
else {
return getter.getReturnType();
}
}
}
@Override
@OneToMany(targetEntity = MCRCategoryImpl.class,
cascade = {
CascadeType.ALL },
mappedBy = "parent")
@OrderColumn(name = "positionInParent")
@Access(AccessType.FIELD)
public List<MCRCategory> getChildren() {
return super.getChildren();
}
@Access(AccessType.PROPERTY)
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
public long getId() {
return super.getId();
}
@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;
}
@SuppressWarnings("unused")
@Access(AccessType.PROPERTY)
@Column(name = Table.Column.DATE_RANGE_TYPE)
@JsonIgnore
public Integer getDateRangeInt() {
return dateRange == null ? null : dateRange.getId();
}
public XAnnotation<javax.persistence.Access> createAccess(String access) {
return access == null ? null
: new XAnnotation<javax.persistence.Access>(
javax.persistence.Access.class, AnnotationUtils.create(
"value", AccessType.valueOf(access)));
}
@Column(length = 1024)
@Access(AccessType.PROPERTY)
public String getMagFilmeAsString() {
StringBuffer res = new StringBuffer();
Set<Filme> set = getMagFilme();
if (set != null) {
for (Filme f : set) {
if (res.length() > 0) {
res.append(",");
}
res.append(f.name());
}
}
return res.toString();
}
@Column(length = 1024)
@Access(AccessType.PROPERTY)
public String getMagFilmeAsString() {
StringBuffer res = new StringBuffer();
Set<Filme> set = getMagFilme();
if (set != null) {
for (Filme f : set) {
if (res.length() > 0) {
res.append(",");
}
res.append(f.name());
}
}
return res.toString();
}
public AccessType getAccess() {
return access;
}
protected void setAccess(AccessType access) {
this.access = access;
}
private static AccessType getAccessTypeOrNull(CtMember ctMember) {
Access access = getAnnotationOrNull( ctMember, Access.class );
return access == null ? null : access.value();
}
@ManyToOne(optional = true, targetEntity = MCRCategoryImpl.class)
@JoinColumn(name = "parentID")
@Access(AccessType.FIELD)
public MCRCategory getParent() {
return super.getParent();
}
@Access(AccessType.PROPERTY)
@Column(name = "allocation_state")
@Enumerated(value = EnumType.STRING)
public AllocationState getAllocationState() {
return super.getAllocationState();
}
@Access(AccessType.PROPERTY)
@Column(name = "description")
public String getDescription() {
return super.getDescription();
}
@Access(AccessType.PROPERTY)
@Column(name = "dhcp_provider")
public String getDhcpProvider() {
return super.getDhcpProvider();
}
@Access(AccessType.PROPERTY)
@Column(name = "dns1")
public String getDns1() {
return super.getDns1();
}
@Access(AccessType.PROPERTY)
@Column(name = "dns2")
public String getDns2() {
return super.getDns2();
}
@Access(AccessType.PROPERTY)
@Column(name = "dns_provider")
public String getDnsProvider() {
return super.getDnsProvider();
}
@Access(AccessType.PROPERTY)
@Column(name = "domain_id")
public Long getDomainId() {
return super.getDomainId();
}
@Access(AccessType.PROPERTY)
@Column(name = "domain")
public String getDomain() {
return super.getDomain();
}
@Access(AccessType.PROPERTY)
@Column(name = "firewall_provider")
public String getFirewallProvider() {
return super.getFirewallProvider();
}