KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > jpa > model > JPAHelper


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.modules.j2ee.jpa.model;
22
23 import java.util.Collection JavaDoc;
24 import javax.lang.model.element.AnnotationMirror;
25 import javax.lang.model.element.AnnotationValue;
26 import javax.lang.model.element.Element;
27 import javax.lang.model.element.ElementKind;
28 import javax.lang.model.element.TypeElement;
29 import javax.lang.model.util.ElementFilter;
30 import org.netbeans.modules.j2ee.jpa.verification.common.Utilities;
31
32 /**
33  * Utility methods for discovering various facts
34  * about JPA model
35  *
36  * @author Sanjeeb.Sahoo@Sun.COM
37  * @author Tomasz.Slota@Sun.COM
38  */

39 public class JPAHelper {
40     
41     /**
42      * Utility method to find out if any member is annotated as Id or
43      * EmbeddedId in this class? It does not check any of the inheritted
44      * members.
45      *
46      * @param javaClass JavaClass whose members will be inspected.
47      * @return returns true if atleast one member is annotated as Id or EmbeddedId
48      */

49     public static boolean isAnyMemberAnnotatedAsIdOrEmbeddedId(TypeElement javaClass) {
50         for (Element classElement : javaClass.getEnclosedElements()) {
51             
52             if (Utilities.findAnnotation(classElement, JPAAnnotations.ID) != null){
53                 return true;
54             }
55             
56             if (Utilities.findAnnotation(classElement, JPAAnnotations.EMBEDDED_ID) != null){
57                 return true;
58             }
59         }
60         return false;
61     }
62     
63     /**
64      * @return name of the primary table that will be mapped to given entity class
65      */

66     public static String JavaDoc getPrimaryTableName(TypeElement entityClass){
67         String JavaDoc name = null;
68         AnnotationMirror annTable = Utilities.findAnnotation(entityClass, JPAAnnotations.TABLE);
69         AnnotationValue nameAttrValue = Utilities.getAnnotationAttrValue(annTable, JPAAnnotations.NAME_ATTR);
70         
71         if (nameAttrValue != null){
72             name = nameAttrValue.getValue().toString();
73         }
74         else {
75             AnnotationMirror annEntity = Utilities.findAnnotation(entityClass, JPAAnnotations.ENTITY);
76             nameAttrValue = Utilities.getAnnotationAttrValue(annEntity, JPAAnnotations.NAME_ATTR);
77             
78             if (nameAttrValue == null){
79                 name = entityClass.getSimpleName().toString();
80             } else{
81                 name = nameAttrValue.getValue().toString();
82             }
83         }
84         
85         assert name != null;
86         
87         return name;
88     }
89     
90     public static AnnotationMirror getFirstAnnotationFromGivenSet(Element element,
91             Collection JavaDoc<String JavaDoc> searchedAnnotations){
92         
93         for (AnnotationMirror ann : element.getAnnotationMirrors()){
94             String JavaDoc annType = ann.getAnnotationType().toString();
95             
96             if (searchedAnnotations.contains(annType)){
97                 return ann;
98             }
99         }
100         
101         return null;
102     }
103     
104     /**
105      *
106      */

107     public static AccessType findAccessType(TypeElement entityClass){
108         AccessType accessType = AccessType.INDETERMINED;
109         
110         // look for the first element annotated with a JPA field annotation
111
for (Element element : entityClass.getEnclosedElements()){
112             if (element.getKind() == ElementKind.FIELD || element.getKind() == ElementKind.METHOD){
113                 AnnotationMirror ann = getFirstAnnotationFromGivenSet(element, JPAAnnotations.MEMBER_LEVEL);
114                 
115                 if (ann != null){
116                     accessType = element.getKind() == ElementKind.FIELD ?
117                         AccessType.FIELD : AccessType.PROPERTY;
118                     
119                     break;
120                 }
121             }
122         }
123         
124         if (accessType.isDetermined()){
125             // check if access type is consistent
126
Collection JavaDoc<? extends Element> otherElems = null;
127             
128             if (accessType == AccessType.FIELD){
129                 otherElems = ElementFilter.methodsIn(entityClass.getEnclosedElements());
130             }
131             else{
132                 otherElems = ElementFilter.fieldsIn(entityClass.getEnclosedElements());
133             }
134             
135             for (Element element : otherElems){
136                 AnnotationMirror ann = getFirstAnnotationFromGivenSet(element, JPAAnnotations.MEMBER_LEVEL);
137                 
138                 if (ann != null){
139                     accessType = AccessType.INCONSISTENT;
140                     break;
141                 }
142             }
143         }
144         
145         return accessType;
146     }
147 }
148
Popular Tags