KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > jmiimpl > javamodel > AnnotationImpl


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 package org.netbeans.modules.javacore.jmiimpl.javamodel;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import javax.jmi.model.Association;
27 import javax.jmi.reflect.RefAssociation;
28 import javax.jmi.reflect.RefObject;
29 import org.netbeans.api.mdr.events.AssociationEvent;
30 import org.netbeans.api.mdr.events.AttributeEvent;
31 import org.netbeans.jmi.javamodel.AnnotationType;
32 import org.netbeans.jmi.javamodel.Extends;
33 import org.netbeans.jmi.javamodel.IsOfAnnotationType;
34 import org.netbeans.jmi.javamodel.Type;
35 import org.netbeans.mdr.handlers.AttrListWrapper;
36 import org.netbeans.modules.javacore.internalapi.JavaModelUtil;
37 import org.netbeans.modules.javacore.parser.ASTProvider;
38 import org.netbeans.modules.javacore.parser.AnnotationInfo;
39 import org.netbeans.modules.javacore.parser.ElementInfo;
40 import org.netbeans.jmi.javamodel.Annotation;
41 import org.netbeans.jmi.javamodel.Element;
42 import org.netbeans.jmi.javamodel.JavaModelPackage;
43 import org.netbeans.jmi.javamodel.MultipartId;
44 import org.netbeans.jmi.javamodel.TypeReference;
45 import org.netbeans.lib.java.parser.ASTree;
46 import org.netbeans.lib.java.parser.ASTreeTypes;
47 import org.netbeans.mdr.storagemodel.StorableObject;
48 import org.netbeans.modules.javacore.parser.TypeRef;
49 import org.openide.util.Utilities;
50
51
52 /**
53  * Represents an annotation usage.
54  *
55  * @author Pavel Flaska
56  * @author Tomas Hurka
57  */

58 public abstract class AnnotationImpl extends SemiPersistentElement implements Annotation {
59     private static final ElementInfo DEFAULT_INFO = new AnnotationInfo(null, AnnotationInfo.ANNOTATION_TYPE, null, null);
60
61     private LightAttrList attributeValues;
62     private MultipartId typeName = null;
63     private String JavaDoc typeRef = null;
64     protected boolean elementsInited = false;
65
66     /** Creates a new instance of AnnotationImpl */
67     public AnnotationImpl(StorableObject s) {
68         super(s);
69     }
70
71     protected void matchPersistent(ElementInfo newInfo) {
72         super.matchPersistent(newInfo);
73         
74         AnnotationInfo info = (AnnotationInfo) newInfo;
75         
76         if (!isPersisted()) {
77             setPersisted(true);
78             persist();
79             setTypeRef(info.type);
80             persistChildren(getPersistentList("attributeValues", super_getAttributeValues()), info.values);
81         } else {
82             if (!Utilities.compareObjects(info.type, getTypeRef())) {
83                 Type type = resolveType(info.type);
84                 Type old = resolveType(getTypeRef());
85                 _setType(type);
86                 fireAssocChange("type",old ,type); //NOI18N
87
}
88             processMembers(getAttributeValues(), info.values);
89         }
90     }
91
92     protected abstract List JavaDoc super_getAttributeValues();
93
94     protected void matchElementInfo(ElementInfo newInfo) {
95         super.matchElementInfo(newInfo);
96         resetASTElements();
97     }
98     
99     protected ElementInfo getDefaultInfo() {
100         return DEFAULT_INFO;
101     }
102     
103     protected void resetASTElements() {
104         deleteChild(typeName);
105         typeName = null;
106         elementsInited = false;
107     }
108     
109     protected void resetChildren() {
110         super.resetChildren();
111         if (attributeValues != null) attributeValues.setInnerList(getPersistentList("attributeValues", super_getAttributeValues()));
112         if (childrenInited) {
113             resetASTElements();
114             initChildren();
115         }
116     }
117     
118     /**
119      * Returns the value of reference type.
120      * @return Value of reference type.
121      */

122     public AnnotationType getType() {
123         Type type;
124         
125         checkUpToDate();
126         type = resolveType(getTypeRef());
127         if (type instanceof AnnotationType)
128             return (AnnotationType)type;
129         return null;
130     }
131
132     /**
133      * Sets the value of reference type. See {@link #getType} for description
134      * on the reference.
135      * @param newValue New value to be set.
136      */

137     public void setType(AnnotationType newValue) {
138         _setType(newValue);
139     }
140
141     private void _setType(Type newValue) {
142         TypeRef tr = typeToTypeRef(newValue);
143         MultipartId typeReference = null;
144         if (!disableChanges) {
145             typeReference = (MultipartId) typeRefToTypeReference(tr, 0);
146         }
147         fireTypeNameChange(typeReference);
148         _setTypeName(typeReference, tr);
149     }
150     
151     protected final void fireAssocChange(String JavaDoc endName, Object JavaDoc oldValue, Object JavaDoc newValue) {
152         fireAssocChange(endName, oldValue, newValue, AssociationEvent.POSITION_NONE);
153     }
154     
155     protected final void fireAssocChange(String JavaDoc endName, Object JavaDoc oldValue, Object JavaDoc newValue, int position) {
156         IsOfAnnotationType source = ((JavaModelPackage) refImmediatePackage()).getIsOfAnnotationType();
157         if (_getMdrStorage().eventsEnabled()) {
158             AssociationEvent event = new AssociationEvent(
159                 source,
160                 AssociationEvent.EVENT_ASSOCIATION_SET,
161                 this,
162                 endName,
163                 (RefObject) oldValue,
164                 (RefObject) newValue,
165                 position
166             );
167             _getMdrStorage().getEventNotifier().ASSOCIATION.firePlannedChange(source, event);
168         }
169     }
170
171     
172     public MultipartId getTypeName() {
173         checkUpToDate();
174         if (!elementsInited) {
175             initASTElements();
176         }
177         return typeName;
178     }
179
180     public void setTypeName(MultipartId typeName) {
181         _setTypeName(typeName, typeReferenceToTypeRef(typeName, 0));
182     }
183     
184     private void _setTypeName(MultipartId typeName, TypeRef typeRef) {
185         if (!disableChanges) {
186             objectChanged(CHANGED_TYPE);
187             changeChild(getTypeName(), typeName);
188             this.typeName = typeName;
189         }
190         setTypeRef(typeRef);
191     }
192
193     private void fireTypeNameChange(TypeReference typeReference) {
194         Object JavaDoc oldValue = null;
195         Object JavaDoc newValue = null;
196         if (elementsInited && !disableChanges) {
197             oldValue = getTypeName();
198             newValue = typeReference;
199         }
200         fireAttrChange("typeName", oldValue, newValue); // NOI18N
201
}
202
203     public void replaceChild(Element oldElement, Element newElement) {
204         if (childrenInited) {
205             if (elementsInited) {
206                 if (oldElement.equals(typeName)) {
207                     setTypeName((MultipartId) newElement);
208                     return;
209                 }
210             }
211             if (replaceObject(getAttributeValues(), oldElement, newElement)) return;
212         }
213         super.replaceChild(oldElement, newElement);
214     }
215
216     public List JavaDoc getChildren() {
217         List JavaDoc list = new ArrayList JavaDoc(getAttributeValues().size() + 1);
218         addIfNotNull(list, getTypeName());
219         list.addAll(getAttributeValues());
220         return list;
221     }
222
223     public void fixImports(Element scope, Element original) {
224         Annotation ann=(Annotation)original;
225         AnnotationType type=ann.getType();
226         if (type!=null) {
227             setTypeName(JavaModelUtil.resolveImportsForClass(scope,type));
228         }
229         fixImports(scope,getAttributeValues(),ann.getAttributeValues());
230     }
231
232     public List JavaDoc getInitedChildren() {
233         if (childrenInited) {
234             List JavaDoc list = new ArrayList JavaDoc(10);
235             if (elementsInited) {
236                 addIfNotNull(list, typeName);
237             }
238             list.addAll(getAttributeValues());
239             return list;
240         }
241         return Collections.EMPTY_LIST;
242     }
243
244     public List JavaDoc getAttributeValues() {
245        checkUpToDate();
246         if (attributeValues == null) {
247             attributeValues = createChildrenList("attributeValues", (AttrListWrapper) super_getAttributeValues(), null, CHANGED_ANNOTATION);
248         }
249         return attributeValues;
250     }
251     
252     String JavaDoc getRawText() {
253         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
254         buf.append('@').append(((MultipartIdImpl) getTypeName()).getSourceText());
255         List JavaDoc attributes = getAttributeValues();
256         if (!attributes.isEmpty()) {
257             buf.append('(');
258             Iterator JavaDoc aIt = attributes.iterator();
259             buf.append(((AttributeValueImpl) aIt.next()).getSourceText());
260             while (aIt.hasNext()) {
261                 formatElementPart(COMMA, buf);
262                 buf.append(((AttributeValueImpl) aIt.next()).getSourceText());
263             }
264             buf.append(')');
265         };
266         return buf.toString();
267     }
268     
269     public void getDiff(List JavaDoc diffList) {
270         ASTProvider parser = getParser();
271         ASTree[] children = getASTree().getSubTrees();
272         AnnotationInfo astInfo = (AnnotationInfo) getElementInfo();
273         getChildDiff(diffList, parser, children[0], (MetadataElement) getTypeName(), CHANGED_TYPE);
274         if (astInfo.values.length == 0) {
275             if (isChanged(CHANGED_ANNOTATION)) {
276                 StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
277                 Collection JavaDoc values = getAttributeValues();
278                 if (!values.isEmpty()) {
279                     buf.append('(');
280                     Iterator JavaDoc it = values.iterator();
281                     while (it.hasNext()) {
282                         buf.append(((AttributeValueImpl) it.next()).getSourceText());
283                         if (it.hasNext()) {
284                             formatElementPart(COMMA, buf);
285                         }
286                     }
287                     buf.append(')');
288                 }
289                 int endOffset = getEndOffset(parser, children[0]);
290                 diffList.add(new DiffElement(endOffset, endOffset, buf.toString()));
291             }
292         } else if (getAttributeValues().isEmpty()) {
293             if (isChanged(CHANGED_ANNOTATION)) {
294                 replaceNode(diffList, parser, children[1], "", 0, null);
295             }
296         } else {
297             getCollectionDiff(diffList, parser, CHANGED_ANNOTATION, astInfo.values, getAttributeValues(), parser.getToken(children[1].getLastToken()).getStartOffset(), ", "); // NOI18N
298
}
299     }
300     ////////////////////////////////////////////////////////////////////////////
301
protected void initChildren() {
302         childrenInited = false;
303         AnnotationInfo info = (AnnotationInfo) getElementInfo();
304         attributeValues = createChildrenList(attributeValues, "attributeValues", (AttrListWrapper) super_getAttributeValues(), info.values, CHANGED_ANNOTATION);
305         childrenInited = true;
306         
307         if (elementsInited) {
308             initASTElements();
309         }
310     }
311
312     protected void initASTElements() {
313         elementsInited = false;
314         if (!childrenInited) {
315             initChildren();
316         }
317         AnnotationInfo info = (AnnotationInfo) getElementInfo();
318         ASTree tree = info.getTypeAST(this);
319         typeName = (MultipartId) initOrCreate(typeName, tree);
320         elementsInited = true;
321     }
322     
323     protected void setData(MultipartId typeName, List JavaDoc attributeValues) {
324         changeChild(null, typeName);
325         this.typeName = typeName;
326         setTypeRef(typeReferenceToTypeRef(typeName, 0));
327         this.attributeValues = createChildrenList("attributeValues", (AttrListWrapper) super_getAttributeValues(), attributeValues, CHANGED_ANNOTATION); // NOI18N
328
childrenInited = true;
329         elementsInited = true;
330     }
331     
332     protected void setTypeRef(TypeRef type) {
333         _getDelegate().setSlot1(type);
334     }
335     
336     public TypeRef getTypeRef() {
337         return (TypeRef) _getDelegate().getSlot1();
338     }
339     
340     public boolean isPersisted() {
341         return _getDelegate().getSlot2() != null;
342     }
343     
344     public void setPersisted(boolean persisted) {
345         _getDelegate().setSlot2(persisted ? "" : null);
346     }
347     
348     void childChanged(MetadataElement mpi) {
349         super.childChanged(mpi);
350         if (elementsInited) {
351             if (mpi == typeName) {
352                 setTypeName((MultipartId) mpi);
353             }
354         }
355     }
356     
357     protected void _delete() {
358         if (elementsInited) {
359             deleteChild(typeName);
360         }
361         deleteChildren("attributeValues", (AttrListWrapper) super_getAttributeValues());
362         super._delete();
363     }
364     
365     public Element duplicate(JavaModelPackage targetExtent) {
366         return targetExtent.getAnnotation().createAnnotation(
367                    (MultipartId) ((MetadataElement) getTypeName()).duplicate(targetExtent),
368                    duplicateList(getAttributeValues(), targetExtent)
369                );
370     }
371     
372     public String JavaDoc getName() {
373         throw new UnsupportedOperationException JavaDoc();
374     }
375
376     protected void matchName(ElementInfo info) {
377     }
378 }
379
Popular Tags