001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.cnd.modelimpl.csm;
043:
044: import org.netbeans.modules.cnd.modelimpl.parser.generated.CPPTokenTypes;
045: import java.util.*;
046:
047: import antlr.collections.AST;
048: import java.io.DataInput;
049: import java.io.DataOutput;
050: import java.io.IOException;
051: import org.netbeans.modules.cnd.api.model.*;
052: import org.netbeans.modules.cnd.modelimpl.csm.core.*;
053: import org.netbeans.modules.cnd.modelimpl.debug.TraceFlags;
054: import org.netbeans.modules.cnd.modelimpl.repository.RepositoryUtils;
055: import org.netbeans.modules.cnd.modelimpl.uid.UIDCsmConverter;
056: import org.netbeans.modules.cnd.modelimpl.uid.UIDObjectFactory;
057: import org.netbeans.modules.cnd.modelimpl.textcache.NameCache;
058:
059: /**
060: * Implements CsmNamespaceDefinition
061: * @author Vladimir Kvasihn
062: */
063: public final class NamespaceDefinitionImpl extends
064: OffsetableDeclarationBase<CsmNamespaceDefinition> implements
065: CsmNamespaceDefinition, MutableDeclarationsContainer,
066: Disposable {
067:
068: private List<CsmUID<CsmOffsetableDeclaration>> declarations = Collections
069: .synchronizedList(new ArrayList<CsmUID<CsmOffsetableDeclaration>>());
070:
071: private final CharSequence name;
072:
073: // only one of namespaceRef/namespaceUID must be used (based on USE_REPOSITORY/USE_UID_TO_CONTAINER)
074: private/*final*/NamespaceImpl namespaceRef;// can be set in onDispose or contstructor only
075: private final CsmUID<CsmNamespace> namespaceUID;
076:
077: public NamespaceDefinitionImpl(AST ast, CsmFile file,
078: NamespaceImpl parent) {
079: super (ast, file);
080: assert ast.getType() == CPPTokenTypes.CSM_NAMESPACE_DECLARATION;
081: name = NameCache.getManager().getString(ast.getText());
082: NamespaceImpl nsImpl = ((ProjectBase) file.getProject())
083: .findNamespaceCreateIfNeeded(parent, name);
084:
085: // set parent ns, do it in constructor to have final fields
086: namespaceUID = UIDCsmConverter.namespaceToUID(nsImpl);
087: assert namespaceUID != null;
088: this .namespaceRef = null;
089:
090: if (nsImpl instanceof NamespaceImpl) {
091: nsImpl.addNamespaceDefinition(this );
092: }
093: }
094:
095: public CsmDeclaration.Kind getKind() {
096: return CsmDeclaration.Kind.NAMESPACE_DEFINITION;
097: }
098:
099: public Collection<CsmOffsetableDeclaration> getDeclarations() {
100: Collection<CsmOffsetableDeclaration> decls;
101: synchronized (declarations) {
102: decls = UIDCsmConverter.UIDsToDeclarations(declarations);
103: }
104: return decls;
105: }
106:
107: public void addDeclaration(CsmOffsetableDeclaration decl) {
108: CsmUID<CsmOffsetableDeclaration> uid = RepositoryUtils
109: .put(decl);
110: assert uid != null;
111: declarations.add(uid);
112: // update repository
113: RepositoryUtils.put(this );
114: }
115:
116: public void removeDeclaration(CsmOffsetableDeclaration declaration) {
117: CsmUID<CsmOffsetableDeclaration> uid = UIDCsmConverter
118: .declarationToUID(declaration);
119: assert uid != null;
120: boolean res = declarations.remove(uid);
121: assert res;
122: RepositoryUtils.remove(uid);
123: // update repository
124: RepositoryUtils.put(this );
125: }
126:
127: public CharSequence getQualifiedName() {
128: return getNamespace().getQualifiedName();
129: }
130:
131: // Moved to OffsetableDeclarationBase
132: // public String getUniqueName() {
133: // return getQualifiedName();
134: // }
135:
136: public CsmNamespace getNamespace() {
137: return _getNamespaceImpl();
138: }
139:
140: public CharSequence getName() {
141: return name;
142: }
143:
144: public CsmScope getScope() {
145: return getContainingFile();
146: }
147:
148: @Override
149: public void dispose() {
150: super .dispose();
151: onDispose();
152: //NB: we're copying declarations, because dispose can invoke this.removeDeclaration
153: Collection<CsmOffsetableDeclaration> decls;
154: List<CsmUID<CsmOffsetableDeclaration>> uids;
155: synchronized (declarations) {
156: decls = getDeclarations();
157: uids = declarations;
158: declarations = Collections
159: .synchronizedList(new ArrayList<CsmUID<CsmOffsetableDeclaration>>());
160: }
161: Utils.disposeAll(decls);
162: RepositoryUtils.remove(uids);
163: NamespaceImpl ns = _getNamespaceImpl();
164: assert ns != null;
165: ns.removeNamespaceDefinition(this );
166: }
167:
168: private void onDispose() {
169: if (TraceFlags.RESTORE_CONTAINER_FROM_UID) {
170: // restore container from it's UID
171: this .namespaceRef = (NamespaceImpl) UIDCsmConverter
172: .UIDtoNamespace(this .namespaceUID);
173: assert this .namespaceRef != null
174: || this .namespaceUID == null : "no object for UID "
175: + this .namespaceUID;
176: }
177: }
178:
179: private NamespaceImpl _getNamespaceImpl() {
180: NamespaceImpl impl = this .namespaceRef;
181: if (impl == null) {
182: impl = (NamespaceImpl) UIDCsmConverter
183: .UIDtoNamespace(this .namespaceUID);
184: assert impl != null || this .namespaceUID == null : "null object for UID "
185: + this .namespaceUID;
186: }
187: return impl;
188: }
189:
190: ////////////////////////////////////////////////////////////////////////////
191: // impl of SelfPersistent
192:
193: @Override
194: public void write(DataOutput output) throws IOException {
195: super .write(output);
196: UIDObjectFactory factory = UIDObjectFactory.getDefaultFactory();
197: factory.writeUIDCollection(this .declarations, output, true);
198:
199: // not null
200: assert this .namespaceUID != null;
201: factory.writeUID(this .namespaceUID, output);
202:
203: assert this .name != null;
204: output.writeUTF(this .name.toString());
205: }
206:
207: public NamespaceDefinitionImpl(DataInput input) throws IOException {
208: super (input);
209: UIDObjectFactory factory = UIDObjectFactory.getDefaultFactory();
210: this .declarations = factory
211: .readUIDCollection(
212: Collections
213: .synchronizedList(new ArrayList<CsmUID<CsmOffsetableDeclaration>>()),
214: input);
215:
216: this .namespaceUID = factory.readUID(input);
217: // not null UID
218: assert this.namespaceUID != null;
219: this.namespaceRef = null;
220:
221: this.name = NameCache.getManager().getString(input.readUTF());
222: assert this.name != null;
223: }
224: }
|