|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. |
| 5 | + * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. |
| 6 | + */ |
| 7 | +package org.hibernate.property.access.internal; |
| 8 | + |
| 9 | +import java.lang.reflect.Field; |
| 10 | +import java.lang.reflect.Method; |
| 11 | + |
| 12 | +import org.hibernate.PropertyNotFoundException; |
| 13 | +import org.hibernate.bytecode.enhance.spi.EnhancerConstants; |
| 14 | +import org.hibernate.internal.util.ReflectHelper; |
| 15 | +import org.hibernate.property.access.spi.EnhancedGetterMethodImpl; |
| 16 | +import org.hibernate.property.access.spi.EnhancedSetterMethodImpl; |
| 17 | +import org.hibernate.property.access.spi.Getter; |
| 18 | +import org.hibernate.property.access.spi.GetterFieldImpl; |
| 19 | +import org.hibernate.property.access.spi.PropertyAccess; |
| 20 | +import org.hibernate.property.access.spi.PropertyAccessBuildingException; |
| 21 | +import org.hibernate.property.access.spi.PropertyAccessStrategy; |
| 22 | +import org.hibernate.property.access.spi.Setter; |
| 23 | +import org.hibernate.property.access.spi.SetterFieldImpl; |
| 24 | + |
| 25 | +/** |
| 26 | + * A PropertyAccess for byte code enhanced entities. Enhanced getter / setter methods ( if available ) are used for |
| 27 | + * field access. Regular getter / setter methods are used for property access. In both cases, delegates calls to |
| 28 | + * EnhancedMethodGetterImpl / EnhancedMethodGetterImpl. Based upon PropertyAccessMixedImpl. |
| 29 | + * |
| 30 | + * @author Steve Ebersole |
| 31 | + * @author Luis Barreiro |
| 32 | + */ |
| 33 | +public class PropertyAccessEnhancedImpl implements PropertyAccess { |
| 34 | + private final PropertyAccessStrategyEnhancedImpl strategy; |
| 35 | + |
| 36 | + private final Getter getter; |
| 37 | + private final Setter setter; |
| 38 | + |
| 39 | + public PropertyAccessEnhancedImpl( |
| 40 | + PropertyAccessStrategyEnhancedImpl strategy, |
| 41 | + Class containerJavaType, |
| 42 | + String propertyName) { |
| 43 | + this.strategy = strategy; |
| 44 | + |
| 45 | + final Field field = fieldOrNull( containerJavaType, propertyName ); |
| 46 | + final Method getterMethod = getterMethodOrNull( containerJavaType, propertyName ); |
| 47 | + |
| 48 | + final Class propertyJavaType; |
| 49 | + |
| 50 | + // need one of field or getterMethod to be non-null |
| 51 | + if ( field == null && getterMethod == null ) { |
| 52 | + String msg = String.format( "Could not locate field nor getter method for property named [%s#%s]", |
| 53 | + containerJavaType.getName(), |
| 54 | + propertyName ); |
| 55 | + throw new PropertyAccessBuildingException( msg ); |
| 56 | + } |
| 57 | + else if ( field != null ) { |
| 58 | + propertyJavaType = field.getType(); |
| 59 | + this.getter = resolveGetterForField( containerJavaType, propertyName, field ); |
| 60 | + } |
| 61 | + else { |
| 62 | + propertyJavaType = getterMethod.getReturnType(); |
| 63 | + this.getter = new EnhancedGetterMethodImpl( containerJavaType, propertyName, getterMethod ); |
| 64 | + } |
| 65 | + |
| 66 | + final Method setterMethod = setterMethodOrNull( containerJavaType, propertyName, propertyJavaType ); |
| 67 | + |
| 68 | + // need one of field or setterMethod to be non-null |
| 69 | + if ( field == null && setterMethod == null ) { |
| 70 | + String msg = String.format( "Could not locate field nor getter method for property named [%s#%s]", |
| 71 | + containerJavaType.getName(), |
| 72 | + propertyName ); |
| 73 | + throw new PropertyAccessBuildingException( msg ); |
| 74 | + } |
| 75 | + else if ( field != null ) { |
| 76 | + this.setter = resolveSetterForField( containerJavaType, propertyName, field ); |
| 77 | + } |
| 78 | + else { |
| 79 | + this.setter = new EnhancedSetterMethodImpl( containerJavaType, propertyName, setterMethod ); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private static Field fieldOrNull(Class containerJavaType, String propertyName) { |
| 84 | + try { |
| 85 | + return ReflectHelper.findField( containerJavaType, propertyName ); |
| 86 | + } |
| 87 | + catch (PropertyNotFoundException e) { |
| 88 | + return null; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private static Method getterMethodOrNull(Class containerJavaType, String propertyName) { |
| 93 | + try { |
| 94 | + return ReflectHelper.findGetterMethod( containerJavaType, propertyName ); |
| 95 | + } |
| 96 | + catch (PropertyNotFoundException e) { |
| 97 | + return null; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private static Method setterMethodOrNull(Class containerJavaType, String propertyName, Class propertyJavaType) { |
| 102 | + try { |
| 103 | + return ReflectHelper.findSetterMethod( containerJavaType, propertyName, propertyJavaType ); |
| 104 | + } |
| 105 | + catch (PropertyNotFoundException e) { |
| 106 | + return null; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + // |
| 111 | + |
| 112 | + private static Getter resolveGetterForField(Class<?> containerClass, String propertyName, Field field) { |
| 113 | + try { |
| 114 | + String enhancedGetterName = EnhancerConstants.PERSISTENT_FIELD_READER_PREFIX + propertyName; |
| 115 | + Method enhancedGetter = containerClass.getDeclaredMethod( enhancedGetterName ); |
| 116 | + enhancedGetter.setAccessible( true ); |
| 117 | + return new EnhancedGetterMethodImpl( containerClass, propertyName, enhancedGetter ); |
| 118 | + } |
| 119 | + catch (NoSuchMethodException e) { |
| 120 | + // enhancedGetter = null --- field not enhanced: fallback to reflection using the field |
| 121 | + return new GetterFieldImpl( containerClass, propertyName, field ); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private static Setter resolveSetterForField(Class<?> containerClass, String propertyName, Field field) { |
| 126 | + try { |
| 127 | + String enhancedSetterName = EnhancerConstants.PERSISTENT_FIELD_WRITER_PREFIX + propertyName; |
| 128 | + Method enhancedSetter = containerClass.getDeclaredMethod( enhancedSetterName, field.getType() ); |
| 129 | + enhancedSetter.setAccessible( true ); |
| 130 | + return new EnhancedSetterMethodImpl( containerClass, propertyName, enhancedSetter ); |
| 131 | + } |
| 132 | + catch (NoSuchMethodException e) { |
| 133 | + // enhancedSetter = null --- field not enhanced: fallback to reflection using the field |
| 134 | + return new SetterFieldImpl( containerClass, propertyName, field ); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public PropertyAccessStrategy getPropertyAccessStrategy() { |
| 140 | + return strategy; |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public Getter getGetter() { |
| 145 | + return getter; |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public Setter getSetter() { |
| 150 | + return setter; |
| 151 | + } |
| 152 | +} |
0 commit comments