KickJava   Java API By Example, From Geeks To Geeks.

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


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.javacore.jmiimpl.javamodel;
21
22 import java.util.*;
23 import javax.jmi.reflect.ConstraintViolationException;
24 import org.netbeans.jmi.javamodel.*;
25 import org.netbeans.lib.java.parser.*;
26 import org.netbeans.mdr.storagemodel.StorableObject;
27 import org.netbeans.modules.javacore.parser.*;
28 import org.openide.util.Utilities;
29
30 /**
31  *
32  * @author Pavel Flaska, Jan Becicka, Martin Matula
33  */

34 public abstract class TypeParameterImpl extends SemiPersistentElement implements TypeParameter {
35
36     protected ReferenceListWrapper interfaces;
37     private LightAttrList interfaceNames;
38     private MultipartId superClassName;
39     
40     private static final ElementInfo DEFAULT_INFO = new TypeParamInfo(null, TypeParamInfo.TYPEPARAM_TYPE, null, null);
41     
42     /** Creates a new instance of TypeParamImpl */
43     public TypeParameterImpl(StorableObject s) {
44         super(s);
45     }
46     
47     protected ElementInfo getDefaultInfo() {
48         return DEFAULT_INFO;
49     }
50     
51     public List getInterfaces() {
52         checkUpToDate();
53         if (interfaces == null) {
54             initInterfaces();
55         }
56         return interfaces;
57     }
58     
59     protected void matchPersistent(ElementInfo info) {
60         super.matchPersistent(info);
61         TypeParamInfo tpInfo = (TypeParamInfo)info;
62         
63         if (!isPersisted()) {
64             setPersisted(true);
65             persist();
66             setBoundsRef(tpInfo.bounds);
67         } else {
68             ArrayList ifcNames = new ArrayList(tpInfo.bounds.length);
69             TypeParamRef jclsRef = splitBounds(tpInfo.bounds, ifcNames);
70             if (!Utilities.compareObjects(jclsRef, getSuperclassRef())) {
71                 setSuperclassRef(jclsRef);
72             }
73             processMembers(getInterfaces(), ifcNames.toArray());
74         }
75     }
76     
77     public JavaClass getSuperClass() {
78         checkUpToDate();
79         return (JavaClass) resolveType(getSuperclassRef());
80     }
81
82     /**
83      * Sets the value of reference superClass. See {@link #getSuperClass} for
84      * description on the reference.
85      * @param newValue New value to be set.
86      */

87     public void setSuperClass(JavaClass newValue) {
88         TypeParamRef sc;
89         if (newValue == null) {
90             sc = NameRef.java_lang_Object;
91             newValue = (JavaClass) resolveType(sc);
92         } else {
93             sc = (TypeParamRef) typeToTypeRef(newValue);
94         }
95         _setSuperClass(sc, (MultipartId) typeRefToTypeReference(sc, 0));
96     }
97     
98     private void _setSuperClass(TypeParamRef superClass, MultipartId superClassName) {
99         if (!disableChanges) {
100             objectChanged(CHANGED_EXTENDS);
101             changeChild(getSuperClassName(), superClassName);
102             this.superClassName = superClassName;
103         }
104         setSuperclassRef(superClass);
105     }
106
107     public MultipartId getSuperClassName() {
108         checkUpToDate();
109         if (!childrenInited) {
110             initChildren();
111         }
112         return superClassName;
113     }
114
115     public void setSuperClassName(org.netbeans.jmi.javamodel.MultipartId newValue) {
116         TypeParamRef sc = (TypeParamRef) typeReferenceToTypeRef(newValue, 0);
117         if (sc == null) {
118             sc = NameRef.java_lang_Object;
119         }
120         _setSuperClass(sc, newValue);
121     }
122
123     public List getInterfaceNames() {
124         checkUpToDate();
125         if (!childrenInited) {
126             initChildren();
127         }
128         return interfaceNames;
129     }
130
131     private ReferenceListWrapper initInterfaces() {
132         List interfaceNames = getInterfaceRefs();
133         if (interfaceNames == null) {
134             interfaceNames = new ArrayList();
135         } else if (!(interfaceNames instanceof ArrayList)) {
136             interfaceNames = new ArrayList(interfaceNames);
137         }
138         // list of superinterfaces is transient
139
TypeList _interfaces = new TypeList(this, (ArrayList) interfaceNames) {
140             protected void updateParent() {
141                 setInterfaceRefs(innerList);
142             }
143         };
144         if (interfaces == null) {
145             ImplementsImpl implementsImpl = (ImplementsImpl)(((JavaModelPackage) refImmediatePackage()).getImplements());
146             interfaces = new ReferenceListWrapper(_getDelegate().getMdrStorage(), implementsImpl, this, "interfaces", this, CHANGED_IMPLEMENTS, _interfaces); // NOI18N
147
} else {
148             interfaces.setInnerList(_interfaces);
149         }
150         return interfaces;
151     }
152     
153     private TypeParamRef splitBounds(TypeParamRef[] interfaceNames, List ifcNames) {
154         if (interfaceNames == null)
155             return null;
156         TypeParamRef sc = null;
157         for (int i = 0; i < interfaceNames.length; i++) {
158             if (i == 0) {
159                 JavaClass cls = (JavaClass) resolveType(interfaceNames[i]);
160                 if (!cls.isInterface()) {
161                     sc = interfaceNames[i];
162                     continue;
163                 }
164             }
165             ifcNames.add(interfaceNames[i]);
166         }
167         return sc == null ? NameRef.java_lang_Object : sc;
168     }
169
170     protected void setData(MultipartId superClassName, List interfaceNames) {
171         changeChild(null, superClassName);
172         this.superClassName = superClassName;
173         this.interfaceNames = createChildrenList("interfaceNames", interfaceNames, CHANGED_IMPLEMENTS); // NOI18N
174
}
175     
176     protected void initChildren() {
177         childrenInited = false;
178         ASTree ifaceAST = null;
179         ASTree tree = getASTree();
180         
181         if (tree != null) {
182           ifaceAST = tree.getSubTrees()[1];
183         }
184         interfaceNames = createChildrenList(interfaceNames, "interfaceNames", ifaceAST, ASTreeTypes.BOUND_LIST, CHANGED_IMPLEMENTS, false); // NOI18N
185
if (!interfaceNames.isEmpty()) {
186             JavaClass cls = (JavaClass) ((MultipartId) interfaceNames.get(0)).getElement();
187             if (!cls.isInterface()) {
188                 MultipartId element = (MultipartId) interfaceNames.getInnerList().remove(0);
189                 changeChild(null, element);
190                 superClassName = element;
191             }
192         }
193         childrenInited = true;
194     }
195
196     protected void matchElementInfo(ElementInfo newInfo) {
197         super.matchElementInfo(newInfo);
198         resetChildren();
199     }
200
201     public List getChildren() {
202         ArrayList children = new ArrayList();
203         addIfNotNull(children, getSuperClassName());
204         children.addAll(getInterfaceNames());
205         return children;
206     }
207
208     protected void resetChildren() {
209         super.resetChildren();
210         if (childrenInited) {
211             if (superClassName != null) {
212                 MultipartId temp = superClassName;
213                 changeChild(superClassName, null);
214                 superClassName = null;
215                 temp.refDelete();
216             }
217             deleteChildren(interfaceNames);
218             interfaceNames = null;
219             childrenInited = false;
220         }
221     }
222     
223     protected void _delete() {
224         if (childrenInited) {
225             deleteChildren(interfaceNames);
226             deleteChild(superClassName);
227         }
228         super._delete();
229     }
230     
231     public String JavaDoc getSourceText() {
232         String JavaDoc origElem;
233         if ((origElem = checkChange()) != null)
234             return origElem;
235         
236         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
237         buf.append(getName());
238         boolean hasExtends = false;
239         if (getSuperClassName() != null) {
240             buf.append(" extends " + ((MultipartIdImpl) getSuperClassName()).getSourceText()); //NOI18N
241
hasExtends = true;
242         }
243         
244         for (Iterator it = getInterfaceNames().iterator(); it.hasNext();) {
245             if (!hasExtends) {
246                 buf.append(" extends "); // NOI18N
247
hasExtends = true;
248             } else {
249                 buf.append(" & "); // NOI18N
250
}
251             buf.append(((MetadataElement) it.next()).getSourceText());
252         }
253         
254         return buf.toString();
255     }
256     
257     public void getDiff(List diffList) {
258         ASTProvider parser = getParser();
259         ASTree tree = getASTree();
260         ASTree[] children = tree.getSubTrees();
261
262         // name
263
if (isChanged(CHANGED_NAME)) {
264             replaceNode(diffList, parser, children[0], getName(), 0, null);
265         }
266
267         int startOffset = children[1] == null ? parser.getToken(children[0].getLastToken()).getEndOffset() : parser.getToken(children[1].getFirstToken()).getStartOffset();
268
269         getCollectionDiff(diffList, parser, CHANGED_EXTENDS | CHANGED_IMPLEMENTS, children[1], ASTreeTypes.BOUND_LIST, getChildren(), startOffset, " & ", " extends "); // NOI18N
270
}
271     
272     public void throwReadOnly(String JavaDoc name) {
273         throw new ConstraintViolationException(null, null, "Cannot change " + name + " of a type parameter."); // NOI18N
274
}
275     
276     public void setModifiers(int mod) {
277         throwReadOnly("modifiers"); // NOI18N
278
}
279     
280     public int getModifiers() {
281         return 0;
282     }
283     
284     public ClassDefinition getDeclaringClass() {
285         return null;
286     }
287
288     public List getAnnotations() {
289         return Collections.EMPTY_LIST;
290     }
291     
292     public List getTypeParameters() {
293         return Collections.EMPTY_LIST;
294     }
295     
296     public List getContents() {
297         return Collections.EMPTY_LIST;
298     }
299     
300     public List getFeatures() {
301         return Collections.EMPTY_LIST;
302     }
303     
304     public JavaDoc getJavadoc() {
305         return null;
306     }
307     
308     public String JavaDoc getJavadocText() {
309         return null;
310     }
311
312     public boolean isInterface() {
313         return false;
314     }
315
316     public void setInterface(boolean newValue) {
317         throwReadOnly("isInterface"); // NOI18N
318
}
319     
320     public java.lang.String JavaDoc getSimpleName() {
321         return getName();
322     }
323
324     public void setSimpleName(java.lang.String JavaDoc newValue) {
325         setName(newValue);
326     }
327     
328     public java.util.Collection JavaDoc getImplementors() {
329         return Collections.EMPTY_LIST;
330     }
331     
332     public java.util.Collection JavaDoc getSubClasses() {
333         return Collections.EMPTY_LIST;
334     }
335     
336     public boolean isInner() {
337         return false;
338     }
339     
340     public boolean isDeprecated() {
341         return false;
342     }
343     
344     public void setDeprecated(boolean newValue) {
345         throwReadOnly("isDeprecated"); // NOI18N
346
}
347     
348     public Field getField(final String JavaDoc name, final boolean includeSupertypes) {
349         return (Field) doQuery(new Query() {
350             public Object JavaDoc query(JavaClass cls) {
351                 return cls.getField(name, includeSupertypes);
352             }
353         }, includeSupertypes);
354     }
355
356     public Method getMethod(final String JavaDoc name, final List parameters, final boolean includeSupertypes) {
357         return (Method) doQuery(new Query() {
358             public Object JavaDoc query(JavaClass cls) {
359                 return cls.getMethod(name, parameters, includeSupertypes);
360             }
361         }, includeSupertypes);
362     }
363
364     public JavaClass getInnerClass(final String JavaDoc simpleName, final boolean includeSupertypes) {
365         return (JavaClass) doQuery(new Query() {
366             public Object JavaDoc query(JavaClass cls) {
367                 return cls.getInnerClass(simpleName, includeSupertypes);
368             }
369         }, includeSupertypes);
370     }
371     
372     public Constructor getConstructor(java.util.List JavaDoc parameters, boolean includeSupertypes) {
373         return null;
374     }
375     
376     public boolean isSubTypeOf(ClassDefinition clazz) {
377         return ClassDefinitionImpl.isSubTypeOf(this, clazz);
378     }
379
380     private Object JavaDoc doQuery(Query query, boolean includeSupertypes) {
381         Object JavaDoc result = null;
382         if (includeSupertypes) {
383             result = query.query(getSuperClass());
384             for (Iterator it = getInterfaces().iterator(); it.hasNext() && result == null;) {
385                 result = query.query((JavaClass) it.next());
386             }
387         }
388         return result;
389     }
390     
391     private interface Query {
392         Object JavaDoc query(JavaClass cls);
393     }
394     
395     protected ASTree getPartTree(ElementPartKind part) {
396         if (ElementPartKindEnum.HEADER.equals(part)) {
397             return getASTree().getSubTrees()[0];
398         }
399         throw new IllegalArgumentException JavaDoc("Invalid part for this element: " + part); // NOI18N
400
}
401
402
403     void setBoundsRef(TypeParamRef[] bounds) {
404         List interfaces = new ArrayList(bounds == null ? 0 : bounds.length);
405         setSuperclassRef(splitBounds(bounds, interfaces));
406         setInterfaceRefs(interfaces);
407     }
408
409     /*
410     private List getBoundsRef() {
411         return (List) _getDelegate().getSlot1();
412     }
413      */

414     
415     public void setJavadocText(String JavaDoc text) {
416         throw new UnsupportedOperationException JavaDoc();
417     }
418     
419     protected void setSuperclassRef(TypeParamRef sc) {
420         _getDelegate().setSlot1(sc);
421     }
422     
423     private TypeParamRef getSuperclassRef() {
424         return (TypeParamRef) _getDelegate().getSlot1();
425     }
426     
427     protected void setInterfaceRefs(List ifcs) {
428         _getDelegate().setSlot2(ifcs);
429     }
430     
431     private List getInterfaceRefs() {
432         return (List) _getDelegate().getSlot2();
433     }
434     
435     protected abstract String JavaDoc super_getJavadocText();
436     protected abstract void super_setJavadocText(String JavaDoc text);
437
438     public boolean isPersisted() {
439         return super_getJavadocText() != null;
440     }
441     
442     public void setPersisted(boolean persisted) {
443         super_setJavadocText(persisted ? "" : null);
444     }
445     
446     public Collection findSubTypes(boolean recursively) {
447         return Collections.EMPTY_LIST;
448     }
449
450     public Element duplicate(JavaModelPackage targetExtent) {
451         return targetExtent.getTypeParameter().createTypeParameter(
452                 getName(),
453                 duplicateList(getAnnotations(), targetExtent),
454                 getModifiers(),
455                 null,
456                 (JavaDoc) duplicateElement(getJavadoc(), targetExtent),
457                 duplicateList(getContents(), targetExtent),
458                 (MultipartId) duplicateElement(getSuperClassName(), targetExtent),
459                 duplicateList(getInterfaceNames(), targetExtent),
460                 duplicateList(getTypeParameters(), targetExtent)
461                );
462     }
463 }
464
Popular Tags