下面列出了怎么用org.hibernate.property.access.spi.PropertyAccessStrategy的API类实例代码及写法,或者点击链接到github查看源代码。
public ExtendPropertyAccess(PropertyAccessStrategy strategy,
Class containerJavaType, String propertyName) {
this.strategy = strategy;
PropertyDescriptor desc = BeanUtils
.getPropertyDescriptor(containerJavaType, propertyName);
Class<?> returnType = null;
MetaEntity entity = MetaEntity.localEntity(containerJavaType.getName());
MetaProperty property = entity == null ? null
: entity.getProperty(propertyName);
if (property != null) {
returnType = EntityUtil.getPropertyType(property.getType());
}
if (returnType == null) {
returnType = Object.class;
}
this.getter = new DynamicPropertyGetterSetter(propertyName, returnType,
desc == null ? null : desc.getReadMethod());
this.setter = new DynamicPropertyGetterSetter(propertyName, returnType,
desc == null ? null : desc.getWriteMethod());
}
public DynamicPropertyAccess(PropertyAccessStrategy strategy,
Class containerJavaType, String propertyName) {
this.strategy = strategy;
PropertyDescriptor desc = BeanUtils
.getPropertyDescriptor(containerJavaType, propertyName);
Class<?> returnType = null;
MetaEntity entity = MetaEntity.localEntity(containerJavaType.getName());
MetaProperty property = entity == null ? null
: entity.getProperty(propertyName);
if (property != null) {
returnType = EntityUtil.getPropertyType(property.getType());
}
if (returnType == null) {
returnType = Object.class;
}
this.getter = new DynamicPropertyGetterSetter(propertyName, returnType,
desc == null ? null : desc.getReadMethod());
this.setter = new DynamicPropertyGetterSetter(propertyName, returnType,
desc == null ? null : desc.getWriteMethod());
}
private void initialize(String aliases[]) {
PropertyAccessStrategyChainedImpl propertyAccessStrategy = new PropertyAccessStrategyChainedImpl(new PropertyAccessStrategy[]{PropertyAccessStrategyBasicImpl.INSTANCE, PropertyAccessStrategyFieldImpl.INSTANCE});
this.aliases = new String[aliases.length];
this.setters = new Setter[aliases.length];
for (int i = 0; i < aliases.length; i++) {
String alias = aliases[i];
if (alias != null) {
this.aliases[i] = alias;
alias = CamelUtils.underlineToCamel(alias);
try {
this.setters[i] = propertyAccessStrategy.buildPropertyAccess(this.resultClass, alias).getSetter();
} catch (Exception e) {
this.setters[i] = null;
}
}
}
isInitialized = true;
}
public PropertyAccessStrategy getPropertyAccessStrategy(Class clazz) throws MappingException {
String accessName = getPropertyAccessorName();
if ( accessName == null ) {
if ( clazz == null || java.util.Map.class.equals( clazz ) ) {
accessName = "map";
}
else {
accessName = "property";
}
}
final EntityMode entityMode = clazz == null || java.util.Map.class.equals( clazz )
? EntityMode.MAP
: EntityMode.POJO;
return resolveServiceRegistry().getService( PropertyAccessStrategyResolver.class ).resolvePropertyAccessStrategy(
clazz,
accessName,
entityMode
);
}
@Override
public PropertyAccessStrategy resolvePropertyAccessStrategy(
Class containerClass,
String explicitAccessStrategyName,
EntityMode entityMode) {
if ( BuiltInPropertyAccessStrategies.BASIC.getExternalName().equals( explicitAccessStrategyName )
|| BuiltInPropertyAccessStrategies.FIELD.getExternalName().equals( explicitAccessStrategyName )
|| BuiltInPropertyAccessStrategies.MIXED.getExternalName().equals( explicitAccessStrategyName ) ) {
if ( Managed.class.isAssignableFrom( containerClass ) ) {
// PROPERTY (BASIC) and MIXED are not valid for bytecode enhanced entities...
return PropertyAccessStrategyEnhancedImpl.INSTANCE;
}
}
if ( StringHelper.isNotEmpty( explicitAccessStrategyName ) ) {
return resolveExplicitlyNamedPropertyAccessStrategy( explicitAccessStrategyName );
}
if ( entityMode == EntityMode.MAP ) {
return BuiltInPropertyAccessStrategies.MAP.getStrategy();
}
else {
return BuiltInPropertyAccessStrategies.BASIC.getStrategy();
}
}
private static Getter getGetter(Property mappingProperty) {
if ( mappingProperty == null || !mappingProperty.getPersistentClass().hasPojoRepresentation() ) {
return null;
}
final PropertyAccessStrategyResolver propertyAccessStrategyResolver =
mappingProperty.getPersistentClass().getServiceRegistry().getService( PropertyAccessStrategyResolver.class );
final PropertyAccessStrategy propertyAccessStrategy = propertyAccessStrategyResolver.resolvePropertyAccessStrategy(
mappingProperty.getClass(),
mappingProperty.getPropertyAccessorName(),
EntityMode.POJO
);
final PropertyAccess propertyAccess = propertyAccessStrategy.buildPropertyAccess(
mappingProperty.getPersistentClass().getMappedClass(),
mappingProperty.getName()
);
return propertyAccess.getGetter();
}
@Override
public PropertyAccessStrategy getPropertyAccessStrategy(Class clazz) throws MappingException {
if ( propertyAccessStrategy == null ) {
propertyAccessStrategy = new PropertyAccessStrategyBackRefImpl( collectionRole, entityName );
}
return propertyAccessStrategy;
}
@Override
public PropertyAccessStrategy getPropertyAccessStrategy(Class clazz) throws MappingException {
if ( accessStrategy == null ) {
accessStrategy = new PropertyAccessStrategyIndexBackRefImpl( collectionRole, entityName );
}
return accessStrategy;
}
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 + "]"
);
}
}
}
protected PropertyAccessStrategy resolveExplicitlyNamedPropertyAccessStrategy(String explicitAccessStrategyName) {
final BuiltInPropertyAccessStrategies builtInStrategyEnum = BuiltInPropertyAccessStrategies.interpret(
explicitAccessStrategyName
);
if ( builtInStrategyEnum != null ) {
return builtInStrategyEnum.getStrategy();
}
return strategySelectorService().resolveStrategy( PropertyAccessStrategy.class, explicitAccessStrategyName );
}
@Override
public PropertyAccess buildPropertyAccess(Class containerJavaType, String propertyName) {
for ( PropertyAccessStrategy candidate : chain ) {
try {
return candidate.buildPropertyAccess( containerJavaType, propertyName );
}
catch (Exception ignore) {
// ignore
}
}
throw new PropertyNotFoundException( "Could not resolve PropertyAccess for " + propertyName + " on " + containerJavaType );
}
@Override
public PropertyAccessStrategy getPropertyAccessStrategy() {
return strategy;
}
@Override
public PropertyAccessStrategy getPropertyAccessStrategy() {
return strategy;
}
@Override
public PropertyAccessStrategy getPropertyAccessStrategy() {
return strategy;
}
@Override
public PropertyAccessStrategy getPropertyAccessStrategy() {
return strategy;
}
@Override
public PropertyAccessStrategy getPropertyAccessStrategy() {
return strategy;
}
@Override
public PropertyAccessStrategy getPropertyAccessStrategy() {
return strategy;
}
public PropertyAccessStrategyChainedImpl(PropertyAccessStrategy... chain) {
this.chain = chain;
}
public PropertyAccessEnhancedImpl(
PropertyAccessStrategy strategy,
Class containerJavaType,
String propertyName) {
super( strategy, containerJavaType, propertyName );
}
@Override
public PropertyAccessStrategy getPropertyAccessStrategy() {
return strategy;
}
@Override
public PropertyAccessStrategy getPropertyAccessStrategy() {
return PropertyAccessStrategyNoopImpl.INSTANCE;
}
@Override
public PropertyAccessStrategy getPropertyAccessStrategy() {
return strategy;
}
@Override
public PropertyAccessStrategy getPropertyAccessStrategy() {
return strategy;
}