KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > jpa > map > JpaPropertyDescriptor


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.jpa.map;
21
22 import java.io.Serializable JavaDoc;
23 import java.lang.reflect.AnnotatedElement JavaDoc;
24 import java.lang.reflect.Field JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.lang.reflect.ParameterizedType JavaDoc;
27 import java.lang.reflect.Type JavaDoc;
28 import java.util.Collection JavaDoc;
29
30 import org.apache.cayenne.jpa.JpaProviderException;
31
32 /**
33  * A descriptor of a class property that may or may not be persistent.
34  *
35  * @author Andrus Adamchik
36  */

37 public class JpaPropertyDescriptor {
38
39     protected AnnotatedElement JavaDoc member;
40     protected String JavaDoc name;
41     protected Class JavaDoc type;
42     protected Type JavaDoc genericType;
43     protected Class JavaDoc targetEntityType;
44
45     public JpaPropertyDescriptor(Field JavaDoc 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 JavaDoc getter, String JavaDoc 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 JavaDoc.class.isAssignableFrom(type)) {
70             if (genericType instanceof ParameterizedType JavaDoc) {
71                 ParameterizedType JavaDoc pType = (ParameterizedType JavaDoc) genericType;
72                 Type JavaDoc[] bounds = pType.getActualTypeArguments();
73                 for (int i = bounds.length - 1; i >= 0; i--) {
74                     if (bounds[i] instanceof Class JavaDoc) {
75                         this.targetEntityType = (Class JavaDoc) bounds[i];
76                         return;
77                     }
78                 }
79             }
80         }
81         else {
82             targetEntityType = type;
83         }
84     }
85
86     public AnnotatedElement JavaDoc getMember() {
87         return member;
88     }
89
90     public String JavaDoc getName() {
91         return name;
92     }
93
94     public Class JavaDoc getType() {
95         return type;
96     }
97
98     public Class JavaDoc 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 JavaDoc.class.isAssignableFrom(type);
108     }
109
110     /**
111      * Returns true if the property is a default simple attribute.
112      * <h3>JPA Spec, 2.1.6:</h3>
113      * If the type of the field or property is one of the following, it is mapped in the
114      * same way as it would if it were annotated as Basic: Java primitive types, wrappers
115      * of the primitive types, java.lang.String, java.math.BigInteger,
116      * java.math.BigDecimal, java.util.Date, java.util.Calendar, java.sql.Date,
117      * java.sql.Time, java.sql.Timestamp, byte[], Byte[], char[], Character[], enums, any
118      * other type that implements Serializable. See Sections 9.1.16 through 9.1.19. It is
119      * an error if no annotation is present and none of the above rules apply.
120      */

121     public boolean isDefaultNonRelationalType() {
122         return isDefaultNonRelationalType(getTargetEntityType());
123     }
124
125     boolean isDefaultNonRelationalType(Class JavaDoc 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         // it is sufficient to check serializability as all the types mentioned in the
136
// spec are serializable
137
return Serializable JavaDoc.class.isAssignableFrom(type);
138     }
139 }
140
Popular Tags