|
4 | 4 | */ |
5 | 5 | package org.hibernate.property.access.internal; |
6 | 6 |
|
7 | | -import java.beans.Introspector; |
8 | 7 | import java.lang.reflect.AnnotatedElement; |
9 | 8 | import java.lang.reflect.Field; |
10 | 9 | import java.lang.reflect.Method; |
11 | 10 | import java.lang.reflect.Modifier; |
12 | | -import java.util.Locale; |
13 | 11 |
|
14 | | -import org.hibernate.MappingException; |
15 | 12 | import org.hibernate.PropertyNotFoundException; |
16 | 13 | import org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor; |
17 | 14 | import org.hibernate.engine.spi.CompositeOwner; |
|
31 | 28 | import static org.hibernate.engine.internal.ManagedTypeHelper.isPersistentAttributeInterceptableType; |
32 | 29 | import static org.hibernate.internal.util.ReflectHelper.NO_PARAM_SIGNATURE; |
33 | 30 | import static org.hibernate.internal.util.ReflectHelper.findField; |
| 31 | +import static org.hibernate.internal.util.ReflectHelper.getterMethodOrNull; |
34 | 32 | import static org.hibernate.internal.util.ReflectHelper.isRecord; |
35 | 33 |
|
36 | 34 | /** |
@@ -82,92 +80,12 @@ public static AccessType getAccessType(Class<?> containerJavaType, String proper |
82 | 80 | && field.isAnnotationPresent( Access.class ) |
83 | 81 | && !field.isAnnotationPresent( Transient.class ) |
84 | 82 | && !Modifier.isStatic( field.getModifiers() ) ) { |
85 | | - return AccessType.FIELD; |
| 83 | + return NullnessUtil.castNonNull( field.getAnnotation( Access.class ) ).value(); |
86 | 84 | } |
87 | 85 |
|
88 | | - for ( Method method : containerClass.getDeclaredMethods() ) { |
89 | | - // if the method has parameters, skip it |
90 | | - if ( method.getParameterCount() != 0 ) { |
91 | | - continue; |
92 | | - } |
93 | | - |
94 | | - // if the method is a "bridge", skip it |
95 | | - if ( method.isBridge() ) { |
96 | | - continue; |
97 | | - } |
98 | | - |
99 | | - if ( method.isAnnotationPresent( Transient.class ) ) { |
100 | | - continue; |
101 | | - } |
102 | | - |
103 | | - if ( Modifier.isStatic( method.getModifiers() ) ) { |
104 | | - continue; |
105 | | - } |
106 | | - |
107 | | - final String methodName = method.getName(); |
108 | | - |
109 | | - // try "get" |
110 | | - if ( methodName.startsWith( "get" ) ) { |
111 | | - final String stemName = methodName.substring( 3 ); |
112 | | - final String decapitalizedStemName = Introspector.decapitalize( stemName ); |
113 | | - if ( stemName.equals( propertyName ) || decapitalizedStemName.equals( propertyName ) ) { |
114 | | - if ( method.isAnnotationPresent( Access.class ) ) { |
115 | | - return AccessType.PROPERTY; |
116 | | - } |
117 | | - else { |
118 | | - checkIsMethodVariant( containerClass, propertyName, method, stemName ); |
119 | | - } |
120 | | - } |
121 | | - } |
122 | | - |
123 | | - // if not "get", then try "is" |
124 | | - if ( methodName.startsWith( "is" ) ) { |
125 | | - final String stemName = methodName.substring( 2 ); |
126 | | - String decapitalizedStemName = Introspector.decapitalize( stemName ); |
127 | | - if ( stemName.equals( propertyName ) || decapitalizedStemName.equals( propertyName ) ) { |
128 | | - if ( method.isAnnotationPresent( Access.class ) ) { |
129 | | - return AccessType.PROPERTY; |
130 | | - } |
131 | | - } |
132 | | - } |
133 | | - } |
134 | | - |
135 | | - return null; |
136 | | - } |
137 | | - |
138 | | - private static void checkIsMethodVariant( |
139 | | - Class<?> containerClass, |
140 | | - String propertyName, |
141 | | - Method method, |
142 | | - String stemName) { |
143 | | - final Method isMethodVariant = findIsMethodVariant( containerClass, stemName ); |
144 | | - if ( isMethodVariant == null ) { |
145 | | - return; |
146 | | - } |
147 | | - |
148 | | - if ( !isMethodVariant.isAnnotationPresent( Access.class ) ) { |
149 | | - throw new MappingException( |
150 | | - String.format( |
151 | | - Locale.ROOT, |
152 | | - "Class '%s' declares both 'get' [%s] and 'is' [%s] variants of getter for property '%s'", |
153 | | - containerClass.getName(), |
154 | | - method.toString(), |
155 | | - isMethodVariant, |
156 | | - propertyName |
157 | | - ) |
158 | | - ); |
159 | | - } |
160 | | - } |
161 | | - |
162 | | - public static @Nullable Method findIsMethodVariant(Class<?> containerClass, String stemName) { |
163 | | - // verify that the Class does not also define a method with the same stem name with 'is' |
164 | | - try { |
165 | | - final Method isMethod = containerClass.getDeclaredMethod( "is" + stemName ); |
166 | | - if ( !Modifier.isStatic( isMethod.getModifiers() ) && isMethod.getAnnotation( Transient.class ) == null ) { |
167 | | - return isMethod; |
168 | | - } |
169 | | - } |
170 | | - catch (NoSuchMethodException ignore) { |
| 86 | + final Method getter = getterMethodOrNull( containerClass, propertyName ); |
| 87 | + if ( getter != null && getter.isAnnotationPresent( Access.class ) ) { |
| 88 | + return NullnessUtil.castNonNull( getter.getAnnotation( Access.class ) ).value(); |
171 | 89 | } |
172 | 90 |
|
173 | 91 | return null; |
|
0 commit comments