1 package org.hibernate.cfg; 3 4 import java.beans.Introspector ; 5 import java.lang.reflect.AnnotatedElement ; 6 import java.lang.reflect.Field ; 7 import java.lang.reflect.Member ; 8 import java.lang.reflect.Method ; 9 import java.lang.reflect.Modifier ; 10 import java.lang.reflect.ParameterizedType ; 11 import java.lang.reflect.Type ; 12 13 import org.hibernate.MappingException; 14 15 24 public class PropertyInferredData { 25 private String defaultAccess; 26 private String propertyName; 27 private Class returnedClassOrElement; 28 private Class returnedClass; 29 private Class collectionType; 30 private String returnedClassOrElementName; 31 private String returnedClassName; 32 private final AnnotatedElement annotedElt; 33 private boolean processed; 34 private boolean annotable; 35 private boolean skip; 36 private boolean isArray; 37 38 public PropertyInferredData(String defaultAccess, String propertyName, Class returnedClass) { 39 this.defaultAccess = defaultAccess; 40 this.processed = true; 41 this.propertyName = propertyName; 42 this.returnedClass = returnedClass; 43 this.returnedClassOrElement = returnedClass; 44 this.returnedClassName = returnedClass == null ? null : returnedClass.getName(); 45 this.returnedClassOrElementName = returnedClassName; 46 annotedElt = null; 47 annotable = true; 48 skip = false; 49 isArray = false; 50 } 51 52 public boolean skip() { 53 execute(false); 54 return skip; 55 } 56 57 private static final String EXCEPTION_MESSAGE = 58 "The annoted inferred element should be a field or a getter"; 59 60 65 public PropertyInferredData(AnnotatedElement annotedElt) { 66 this.annotedElt = annotedElt; 67 } 68 69 76 private void execute(boolean throwException) throws MappingException { 77 if (! processed) { 78 82 skip = false; 83 annotable = true; 84 if ( ! (annotedElt instanceof Member ) ) { 85 throw new MappingException(EXCEPTION_MESSAGE); 86 } 87 propertyName = retrievePropertyNameFromMember( (Member ) annotedElt, throwException ); 88 if (annotedElt instanceof Field ) { 89 Field field = (Field ) annotedElt; 90 defaultAccess = "field"; 91 returnedClass = field.getType(); 92 collectionType = findCollectionType(field); 93 if ( field.isSynthetic() || 95 Modifier.isStatic( field.getModifiers() ) 96 ) { 97 skip = true; 98 } 99 } 100 else if (annotedElt instanceof Method ) { 101 Method method = (Method ) annotedElt; 102 defaultAccess = "property"; 103 returnedClass = method.getReturnType(); 104 collectionType = findCollectionType(method); 105 if ( method.isSynthetic() || 107 method.isBridge() || 108 Modifier.isStatic( method.getModifiers() ) || 109 method.getParameterTypes().length != 0 || void.class.equals( method.getReturnType() ) ) { 112 skip = true; 113 } 114 } 115 else { 116 throw new MappingException(EXCEPTION_MESSAGE); 117 } 118 if ( returnedClass.isArray() ) { 119 isArray = true; 120 returnedClassOrElement = returnedClass.getComponentType(); 121 collectionType = returnedClassOrElement; 122 } 123 else { 124 returnedClassOrElement = returnedClass; 125 } 126 returnedClassName = returnedClass.getName(); 127 returnedClassOrElementName = returnedClassOrElement.getName(); 128 if (annotable == false) skip = true; 129 processed = true; 130 } 131 } 132 133 private Class findCollectionType(Method method) { 134 Type t = method.getGenericReturnType(); 135 return extractType(t); 136 } 137 138 private Class findCollectionType(Field field) { 139 Type t = field.getGenericType(); 140 return extractType(t); 141 } 142 143 private Class extractType(Type t) { 144 if (t != null && t instanceof ParameterizedType ) { 145 ParameterizedType pt = (ParameterizedType ) t; 146 Type [] genTypes = pt.getActualTypeArguments(); 147 if (genTypes.length == 1 && genTypes[0] instanceof Class ) { 148 return (Class ) genTypes[0]; 149 } 150 else if (genTypes.length == 2 && genTypes[1] instanceof Class ) { 151 return (Class ) genTypes[1]; 153 } 154 } 155 return null; 156 } 157 158 166 private String retrievePropertyNameFromMember(Member member, boolean throwException) throws MappingException { 167 168 if (member instanceof Field ) { 169 return member.getName(); 170 } 171 else if (member instanceof Method ) { 172 final String methName = member.getName(); 173 if ( methName.startsWith( "get" ) ) { 174 175 return Introspector.decapitalize( methName.substring( "get".length() ) ); 176 } 177 else if ( methName.startsWith("is") ) { 178 return Introspector.decapitalize( methName.substring( "is".length() ) ); 179 } 180 else { 181 annotable = false; 182 if (throwException) { 183 throw new MappingException("Annotated Method is not a proper getter: " + methName); 184 } 185 else { 186 return methName; 187 } 188 } 189 } 190 else { 191 throw new MappingException(EXCEPTION_MESSAGE); 192 } 193 } 194 195 196 197 201 public String getDefaultAccess() throws MappingException { 202 execute(true); 203 return defaultAccess; 204 } 205 206 210 public String getPropertyName() throws MappingException { 211 execute(true); 212 return propertyName; 213 } 214 215 218 public Class getReturnedClassOrElement() throws MappingException { 219 execute(true); 220 return returnedClassOrElement; 221 } 222 223 226 public Class getReturnedClass() throws MappingException { 227 execute(true); 228 return returnedClass; 229 } 230 231 234 public String getReturnedClassOrElementName() throws MappingException { 235 execute(true); 236 return returnedClassOrElementName; 237 } 238 239 242 public String getReturnedClassName() throws MappingException { 243 execute(true); 244 return returnedClassName; 245 } 246 247 250 public Class getCollectionType() { 251 return collectionType; 252 } 253 254 public boolean isArray() { 255 return isArray; 256 } 257 } 258 | Popular Tags |