KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
22 import org.netbeans.jmi.javamodel.*;
23 import org.netbeans.lib.java.parser.ASTree;
24 import org.netbeans.lib.java.parser.ASTreeTypes;
25 import org.netbeans.lib.java.parser.ParserTokens;
26 import org.netbeans.mdr.storagemodel.StorableObject;
27 import org.netbeans.modules.javacore.internalapi.JavaModelUtil;
28 import org.netbeans.modules.javacore.parser.MDRParser;
29
30 /**
31  *
32  * @author Martin Matula
33  */

34 public abstract class MultipartIdImpl extends TypeReferenceImpl implements MultipartId {
35     private LightAttrList typeArguments;
36
37     /** Creates a new instance of MultipartIdImpl */
38     public MultipartIdImpl(StorableObject o) {
39         super(o);
40     }
41
42     void setData(String JavaDoc name, MultipartId parent, List typeArguments) {
43         super.setData(name, parent);
44         this.typeArguments = createChildrenList("typeArguments", typeArguments, CHANGED_TYPE_ARGUMENTS); // NOI18N
45
}
46
47     protected void initChildren() {
48         childrenInited = false;
49         ASTree tree = getASTree();
50         ASTree[] children = tree.getSubTrees();
51         typeArguments = createChildrenList(typeArguments, "typeArguments", children == null || children.length < 3 ? null : children[2], ASTreeTypes.TYPE_ARGUMENTS, CHANGED_TYPE_ARGUMENTS, false); // NOI18N
52
super.initChildren();
53     }
54
55     protected ASTree getNameAST() {
56         ASTree tree = getASTree();
57         if (tree.getType() == ASTreeTypes.MULTI_PART_ID) {
58             tree = tree.getSubTrees()[1];
59         }
60         return tree;
61     }
62
63     public List getTypeArguments() {
64         if (!childrenInited) {
65             initChildren();
66         }
67         return typeArguments;
68     }
69
70     public String JavaDoc getSourceText() {
71         return getRawText();
72     }
73     
74     String JavaDoc getRawText() {
75         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
76         MetadataElement parent = (MetadataElement) getParent();
77         if (parent != null) {
78             buf.append(parent.getSourceText() + '.');
79         }
80         buf.append(getName());
81         generateNewTypeArguments(buf);
82         return buf.toString();
83     }
84
85     private void generateNewTypeArguments(StringBuffer JavaDoc buf) {
86         if (!typeArguments.isEmpty()) {
87             buf.append('<');
88             Iterator it = typeArguments.iterator();
89             while (it.hasNext()) {
90                 buf.append(((MetadataElement) it.next()).getSourceText());
91                 if (it.hasNext()) {
92                     formatElementPart(MetadataElement.COMMA, buf);
93                 }
94             }
95             buf.append('>');
96         }
97     }
98
99     public void getDiff(List diff) {
100         MDRParser parser = getParser();
101         ASTree tree = getASTree();
102         ASTree[] children = tree.getSubTrees();
103         ASTree nameAST = getNameAST();
104         ASTree parentAST = null;
105         
106         if (tree.getType() == ASTreeTypes.MULTI_PART_ID && children != null && children.length > 0) {
107             parentAST = children[0];
108         }
109         
110         if (isChanged(CHANGED_PARENT) && parentAST != null && getParent()==null) {
111             // parent was there, but is not here any more
112
int start = getStartOffset(parser, children[0], false);
113             int end = getEndOffset(parser,children[0]);
114             diff.add(new DiffElement(start, end + 1, ""));
115         } else {
116             getChildDiff(diff, parser, parentAST, (MetadataElement) getParent(), CHANGED_PARENT, getStartOffset(parser, tree, false), "");
117         }
118
119         if (isChanged(CHANGED_NAME)) {
120             replaceNode(diff, parser, nameAST, getName(), 0, null);
121         }
122
123         if (children == null || children.length < 3 || children[2] == null) {
124             if (isChanged(CHANGED_TYPE_ARGUMENTS)) {
125                 StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
126                 generateNewTypeArguments(buf);
127                 int endOffset = getEndOffset(parser, getNameAST());
128                 diff.add(new DiffElement(endOffset, endOffset, buf.toString()));
129             }
130         } else if (getTypeArguments().isEmpty()) {
131             if (isChanged(CHANGED_TYPE_ARGUMENTS)) {
132                 replaceNode(diff, parser, children[2], "", 0, null);
133             }
134         } else {
135             getCollectionDiff(diff, parser, CHANGED_TYPE_ARGUMENTS, children[2], ASTreeTypes.TYPE_ARGUMENTS, getTypeArguments(), parser.getToken(children[2].getLastToken()).getStartOffset(), ", "); // NOI18N
136
}
137     }
138
139     public NamedElement getElement() {
140         ASTree tree = getASTree();
141         if (tree == null) {
142             // [PENDING] remove this branch when semantic info for new elements is fixed
143
TypeClass typeClass = ((JavaModelPackage) refImmediatePackage()).getType();
144             NamedElement result = typeClass.resolve(getName());
145             if (result instanceof UnresolvedClass) {
146                 JavaPackage pkg = ((JavaModelPackage) refImmediatePackage()).getJavaPackage().resolvePackage(getName());
147                 if (pkg != null) {
148                     return pkg;
149                 }
150             }
151             return result;
152         } else {
153             MDRParser parser = (MDRParser) tree.getASTContext();
154             switch (tree.getType()) {
155                 case ASTreeTypes.MULTI_PART_ID:
156                     return (NamedElement) parser.getSemanticInfo(tree.getSubTrees()[1], this);
157                 case ParserTokens.IDENTIFIER:
158                     return (NamedElement) parser.getSemanticInfo(tree, this);
159                 case ASTreeTypes.PRIMITIVE_TYPE:
160                     return ((JavaModelPackage) refImmediatePackage()).getType().resolve(getName());
161                 default:
162                     throw new IllegalArgumentException JavaDoc("Illegal tree type " + tree.getType() + "."); // NOI18N
163
}
164         }
165     }
166
167     /**
168      * Creates a copy of this MultipartId instance
169      */

170     public Element duplicate(JavaModelPackage targetExtent) {
171         return targetExtent.getMultipartId().createMultipartId(
172             getName(),
173             (MultipartId) duplicateElement(getParent(), targetExtent),
174             duplicateList(getTypeArguments(), targetExtent)
175         );
176     }
177
178     protected void _delete() {
179         // --- delete components -------------------------------------------
180
if (childrenInited) {
181             deleteChildren(typeArguments);
182         }
183         // --- delete links -----------------------------------------------
184
// no links to delete
185
// --- call super ---------------------------------------
186
super._delete();
187     }
188
189     public void replaceChild(Element oldElement,Element newElement) {
190         if (childrenInited) {
191             if (replaceObject(getTypeArguments(),oldElement,newElement)) return;
192             super.replaceChild(oldElement,newElement);
193         }
194     }
195
196     public List getChildren() {
197         List list = super.getChildren();
198         list.addAll(getTypeArguments());
199         return list;
200     }
201
202     public void fixImports(Element scope, Element original) {
203         MultipartId mpi=(MultipartId)original;
204         TypeReference resolved=JavaModelUtil.resolveImportsForType(scope,(Type)mpi.getElement());
205         
206         if (resolved!=null) {
207             setName(resolved.getName());
208             setParent(null);
209         }
210         fixImports(scope,getTypeArguments(),((MultipartId)original).getTypeArguments());
211     }
212
213 }
214
Popular Tags