1 19 20 package org.apache.cayenne.jpa.map; 21 22 import java.io.Serializable ; 23 import java.lang.reflect.AnnotatedElement ; 24 import java.lang.reflect.Field ; 25 import java.lang.reflect.Method ; 26 import java.lang.reflect.ParameterizedType ; 27 import java.lang.reflect.Type ; 28 import java.util.Collection ; 29 30 import org.apache.cayenne.jpa.JpaProviderException; 31 32 37 public class JpaPropertyDescriptor { 38 39 protected AnnotatedElement member; 40 protected String name; 41 protected Class type; 42 protected Type genericType; 43 protected Class targetEntityType; 44 45 public JpaPropertyDescriptor(Field field) { 46 this.member = field; 47 this.name = field.getName(); 48 this.type = field.getType(); 49 this.genericType = field.getGenericType(); 50 } 51 52 public JpaPropertyDescriptor(Method getter, String name) { 53 54 if (JpaClassDescriptor.propertyNameForGetter(getter.getName()) == null) { 55 throw new JpaProviderException("Invalid property getter name: " 56 + getter.getName()); 57 } 58 59 this.member = getter; 60 this.name = name; 61 this.type = getter.getReturnType(); 62 this.genericType = getter.getGenericReturnType(); 63 } 64 65 protected void processTargetEntityType() { 66 67 this.targetEntityType = Void.TYPE; 68 69 if (Collection .class.isAssignableFrom(type)) { 70 if (genericType instanceof ParameterizedType ) { 71 ParameterizedType pType = (ParameterizedType ) genericType; 72 Type [] bounds = pType.getActualTypeArguments(); 73 for (int i = bounds.length - 1; i >= 0; i--) { 74 if (bounds[i] instanceof Class ) { 75 this.targetEntityType = (Class ) bounds[i]; 76 return; 77 } 78 } 79 } 80 } 81 else { 82 targetEntityType = type; 83 } 84 } 85 86 public AnnotatedElement getMember() { 87 return member; 88 } 89 90 public String getName() { 91 return name; 92 } 93 94 public Class getType() { 95 return type; 96 } 97 98 public Class getTargetEntityType() { 99 if (targetEntityType == null) { 100 processTargetEntityType(); 101 } 102 103 return Void.TYPE.equals(targetEntityType) ? null : targetEntityType; 104 } 105 106 public boolean isStringType() { 107 return String .class.isAssignableFrom(type); 108 } 109 110 121 public boolean isDefaultNonRelationalType() { 122 return isDefaultNonRelationalType(getTargetEntityType()); 123 } 124 125 boolean isDefaultNonRelationalType(Class type) { 126 127 if (type.isPrimitive() || type.isEnum()) { 128 return true; 129 } 130 131 if (type.isArray()) { 132 return isDefaultNonRelationalType(type.getComponentType()); 133 } 134 135 return Serializable .class.isAssignableFrom(type); 138 } 139 } 140 | Popular Tags |