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 java.io.DataOutput;
045: import java.io.IOException;
046: import org.netbeans.modules.cnd.api.model.*;
047: import antlr.collections.AST;
048: import java.io.DataInput;
049: import org.netbeans.modules.cnd.modelimpl.parser.generated.CPPTokenTypes;
050: import org.netbeans.modules.cnd.modelimpl.csm.core.*;
051: import org.netbeans.modules.cnd.modelimpl.repository.PersistentUtils;
052: import org.netbeans.modules.cnd.modelimpl.textcache.NameCache;
053: import org.netbeans.modules.cnd.modelimpl.textcache.QualifiedNameCache;
054: import org.netbeans.modules.cnd.modelimpl.uid.UIDObjectFactory;
055: import org.netbeans.modules.cnd.utils.cache.CharSequenceKey;
056:
057: /**
058: *
059: * @author Vladimir Kvasihn
060: */
061: public class FriendClassImpl extends
062: OffsetableDeclarationBase<CsmFriendClass> implements
063: CsmFriendClass {
064: private final CharSequence name;
065: private final CharSequence[] nameParts;
066: private final CsmUID<CsmClass> parentUID;
067:
068: public FriendClassImpl(AST ast, FileImpl file, CsmClass parent) {
069: super (ast, file);
070: this .parentUID = parent.getUID();
071: AST qid = AstUtil.findSiblingOfType(ast,
072: CPPTokenTypes.CSM_QUALIFIED_ID);
073: name = (qid == null) ? CharSequenceKey.empty()
074: : QualifiedNameCache.getManager().getString(
075: AstRenderer.getQualifiedName(qid));
076: nameParts = initNameParts(qid);
077: registerInProject();
078: }
079:
080: private void registerInProject() {
081: CsmProject project = getContainingFile().getProject();
082: if (project instanceof ProjectBase) {
083: ((ProjectBase) project).registerDeclaration(this );
084: }
085: }
086:
087: public CsmClass getContainingClass() {
088: return parentUID.getObject();
089: }
090:
091: public CsmScope getScope() {
092: return getContainingClass();
093: }
094:
095: public CharSequence getName() {
096: return name;
097: }
098:
099: public CharSequence getQualifiedName() {
100: CsmClass cls = getContainingClass();
101: CharSequence clsQName = cls.getQualifiedName();
102: if (clsQName != null && clsQName.length() > 0) {
103: return CharSequenceKey.create(clsQName.toString() + "::"
104: + getQualifiedNamePostfix()); // NOI18N
105: }
106: return getName();
107: }
108:
109: public CsmDeclaration.Kind getKind() {
110: return CsmDeclaration.Kind.CLASS_FRIEND_DECLARATION;
111: }
112:
113: public CsmClass getReferencedClass() {
114: return getReferencedClass(null);
115: }
116:
117: public CsmClass getReferencedClass(Resolver resolver) {
118: CsmObject o = resolve(resolver);
119: return (o instanceof CsmClass) ? (CsmClass) o : (CsmClass) null;
120: }
121:
122: private String[] initNameParts(AST qid) {
123: if (qid != null) {
124: return AstRenderer.getNameTokens(qid);
125: }
126: return new String[0];
127: }
128:
129: private CsmObject resolve(Resolver resolver) {
130: return ResolverFactory.createResolver(this , resolver).resolve(
131: nameParts, Resolver.CLASSIFIER);
132: }
133:
134: @Override
135: public void dispose() {
136: super .dispose();
137: unregisterInProject();
138: }
139:
140: private void unregisterInProject() {
141: ((ProjectBase) getContainingFile().getProject())
142: .unregisterDeclaration(this );
143: this .cleanUID();
144: }
145:
146: ////////////////////////////////////////////////////////////////////////////
147: // iml of SelfPersistent
148:
149: @Override
150: public void write(DataOutput output) throws IOException {
151: super .write(output);
152: assert this .name != null;
153: output.writeUTF(this .name.toString());
154: PersistentUtils.writeStrings(this .nameParts, output);
155: UIDObjectFactory.getDefaultFactory().writeUID(this .parentUID,
156: output);
157: }
158:
159: public FriendClassImpl(DataInput input) throws IOException {
160: super(input);
161: this.name = QualifiedNameCache.getManager().getString(
162: input.readUTF());
163: assert this.name != null;
164: this.nameParts = PersistentUtils.readStrings(input, NameCache
165: .getManager());
166: this.parentUID = UIDObjectFactory.getDefaultFactory().readUID(
167: input);
168: }
169: }
|