KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > refactoring > EntityAssociationResolver


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.refactoring;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.List JavaDoc;
26 import org.netbeans.jmi.javamodel.Annotation;
27 import org.netbeans.jmi.javamodel.AttributeValue;
28 import org.netbeans.jmi.javamodel.Feature;
29 import org.netbeans.jmi.javamodel.Field;
30 import org.netbeans.jmi.javamodel.JavaClass;
31 import org.netbeans.jmi.javamodel.Method;
32 import org.netbeans.jmi.javamodel.ParameterizedType;
33 import org.netbeans.jmi.javamodel.StringLiteral;
34 import org.netbeans.jmi.javamodel.Type;
35 import org.netbeans.modules.j2ee.common.JMIUtils;
36 import org.netbeans.modules.javacore.api.JavaModel;
37
38 /**
39  * This class resolves associations between entities. Entities might be associated
40  * via annotations such as OneToOne/OneToMany/ManyToMany. Note that currently this
41  * class can only find annotated associations.
42  *
43  * @author Erno Mononen
44  */

45 public class EntityAssociationResolver {
46     
47     /**
48      * The entity whose associations are resolved.
49      */

50     private JavaClass entity;
51     /**
52      * List of all possible entities to whom <code>entity</code> might be associated,
53      * for example all entities in a project.
54      */

55     private List JavaDoc<JavaClass> allEntities;
56     
57     // supported annotations
58
private static final List JavaDoc<String JavaDoc> ANNOTATIONS = Arrays.asList(
59             new String JavaDoc[]{"OneToOne", "OneToMany", "ManyToOne", "ManyToMany",});
60     
61     private static final String JavaDoc MAPPED_BY = "mappedBy";
62     private static final String JavaDoc TARGET_ENTITY = "targetEntity";
63     
64     
65     /**
66      * Creates a new instance of EntityAssociationResolver
67      * @param entity the entity whose associations are resolved. Must be an annotated
68      * entity and must not be null.
69      * @param allEntities list of all possible entities to whom <code>entity</code> might be associated,
70      * for example all entities in a project. Must not be null.
71      */

72     public EntityAssociationResolver(JavaClass entity, List JavaDoc<JavaClass> allEntities) {
73         assert entity != null;
74         assert allEntities != null;
75         this.entity = entity;
76         this.allEntities = allEntities;
77     }
78     
79     /**
80      * Gets all annotation references that refer to given <code>property</code> via <tt>mappedBy</tt>
81      * element.
82      * @param property the property of our entity (field or method) whose references are to be looked
83      * for.
84      * @return associations that refer to given <code>property</code> via 'mappedBy' element.
85      */

86     public List JavaDoc<EntityAnnotationReference> getMappedByReferences(Feature property){
87         
88         if (!(property instanceof Method || property instanceof Field)){
89             return Collections.emptyList();
90         }
91         
92         JavaClass reference = findReferencesOfType(resolveType(property));
93         if (reference == null){
94             return Collections.emptyList();
95         }
96         return getReferences(reference, MAPPED_BY, Utility.getPropertyName(property.getName()));
97     }
98     
99     
100     private List JavaDoc<Annotation> getFeatureAnnotations(JavaClass javaClass){
101         List JavaDoc<Annotation> result = new ArrayList JavaDoc<Annotation>();
102         for (Object JavaDoc elem : javaClass.getFeatures()) {
103             Feature feature = (Feature) elem;
104             for (Object JavaDoc elem2 : feature.getAnnotations()) {
105                 Annotation annotation = (Annotation) elem2;
106                 result.add(annotation);
107             }
108         }
109         return result;
110     }
111     
112     /**
113      * Gets the name of f the given feature's type, i.e. return type's name for
114      * method and for field its type's name. For a method that returns a generic collection or for
115      * a field whose type is a generic collection this method will
116      * return the type of its elements.
117      * @param feature the feature whose type's name is to be resolved.
118      * @return name of the given feature's type.
119      */

120     private String JavaDoc resolveType(Feature feature){
121         Type returnType = null;
122         if (feature instanceof Method){
123             Method method = (Method) feature;
124             returnType = method.getType();
125         } else if (feature instanceof Field){
126             Field field = (Field) feature;
127             returnType = field.getType();
128         }
129         if (returnType instanceof ParameterizedType){
130             ParameterizedType parametrizedType = (ParameterizedType) returnType;
131             List JavaDoc list = parametrizedType.getParameters();
132             if (!list.isEmpty()){
133                 returnType = (Type) list.get(0);
134             }
135         }
136         return returnType != null ? returnType.getName() : null;
137     }
138     
139     /**
140      * Gets references from our entity to given javaClass.
141      * TODO: document me.
142      * @param javaClass the
143      * @param annotationElementName the name of the annotation whose references we're
144      * looking for.
145      * @param propertyName the name of the property.
146      * @return list of associations.
147      */

148     private List JavaDoc<EntityAnnotationReference> getReferences(JavaClass javaClass,
149             String JavaDoc annotationElementName, String JavaDoc propertyName){
150         
151         List JavaDoc<EntityAnnotationReference> result = new ArrayList JavaDoc<EntityAnnotationReference>();
152         
153         for (Object JavaDoc elem : javaClass.getFeatures()) {
154             Feature feature = (Feature) elem;
155             
156             if (!entity.getName().equals(resolveType(feature))){
157                 continue;
158             }
159             
160             for (Object JavaDoc elem2 : feature.getAnnotations()){
161                 Annotation annotation = (Annotation) elem2;
162                 
163                 if (ANNOTATIONS.contains(annotation.getTypeName().getName())){
164                     
165                     for (Object JavaDoc elem3 : annotation.getAttributeValues()) {
166                         AttributeValue attributeValue = (AttributeValue) elem3;
167                         
168                         if (attributeValue.getName().equals(annotationElementName)
169                         && propertyName.equals(getStringValue(attributeValue))){
170                             
171                             result.add(new EntityAnnotationReference(entity, javaClass, feature, annotation, attributeValue));
172                         }
173                     }
174                 }
175             }
176         }
177         return result;
178     }
179     
180     private String JavaDoc getStringValue(AttributeValue attributeValue){
181         return ((StringLiteral) attributeValue.getValue()).getValue();
182     }
183     
184     private JavaClass findReferencesOfType(String JavaDoc clazz){
185         for (JavaClass each : allEntities) {
186             if (each.getName().equals(clazz)){
187                 return each;
188             }
189         }
190         return null;
191     }
192     
193 }
194
Popular Tags