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.uid;
043:
044: import java.util.ArrayList;
045: import java.util.Collection;
046: import org.netbeans.modules.cnd.api.model.CsmClass;
047: import org.netbeans.modules.cnd.api.model.CsmDeclaration;
048: import org.netbeans.modules.cnd.api.model.CsmFile;
049: import org.netbeans.modules.cnd.api.model.CsmIdentifiable;
050: import org.netbeans.modules.cnd.api.model.CsmInclude;
051: import org.netbeans.modules.cnd.api.model.CsmMacro;
052: import org.netbeans.modules.cnd.api.model.CsmNamespace;
053: import org.netbeans.modules.cnd.api.model.CsmObject;
054: import org.netbeans.modules.cnd.api.model.CsmProject;
055: import org.netbeans.modules.cnd.api.model.CsmScope;
056: import org.netbeans.modules.cnd.api.model.CsmUID;
057: import org.netbeans.modules.cnd.modelimpl.debug.DiagnosticExceptoins;
058: import org.netbeans.modules.cnd.modelimpl.debug.TraceFlags;
059:
060: /**
061: * utilities to convert between CsmUID and CsmObjects
062: * @author Vladimir Voskresensky
063: */
064: public class UIDCsmConverter {
065:
066: private UIDCsmConverter() {
067: }
068:
069: ////////////////////////////////////////////////////////////////////////////
070: // UID -> Object
071:
072: private static int lastHash = 0;
073:
074: public static CsmFile UIDtoFile(CsmUID<CsmFile> uid) {
075: try {
076: CsmFile result = uid == null ? null : uid.getObject();
077: // if( result != null ) {
078: // if( uid.toString().indexOf("unresolved") == -1 ) {
079: // if( lastHash != 0 && result.hashCode() != lastHash ) {
080: // System.err.printf("SHIT: got another file for %s\n", uid.toString());
081: // CsmFile res2 = uid.getObject();
082: // CsmFile res3 = uid.getObject();
083: // CsmFile res4 = uid.getObject();
084: // CsmFile res5 = uid.getObject();
085: // }
086: // lastHash = result.hashCode();
087: // }
088: // }
089: return result;
090: } catch (StackOverflowError ex) {
091: // needed to analyze IZ99230; it's fixed!
092: Exception ex2 = new Exception("StackOverflowError for UID "
093: + uid);
094: ex2.setStackTrace(ex.getStackTrace());
095: DiagnosticExceptoins.register(ex2);
096: return null;
097: }
098: }
099:
100: public static CsmObject UIDtoCsmObject(CsmUID uid) {
101: return uid == null ? null : (CsmObject) uid.getObject();
102: }
103:
104: public static CsmNamespace UIDtoNamespace(CsmUID<CsmNamespace> uid) {
105: return uid == null ? null : uid.getObject();
106: }
107:
108: public static CsmProject UIDtoProject(CsmUID<CsmProject> uid) {
109: return uid == null ? null : uid.getObject();
110: }
111:
112: public static <T extends CsmDeclaration> T UIDtoDeclaration(
113: CsmUID<T> uid) {
114: return uid == null ? null : uid.getObject();
115: }
116:
117: public static CsmScope UIDtoScope(CsmUID<CsmScope> uid) {
118: return uid == null ? null : uid.getObject();
119: }
120:
121: public static CsmClass UIDtoClass(CsmUID<CsmClass> uid) {
122: return uid == null ? null : uid.getObject();
123: }
124:
125: public static <T extends CsmNamespace> Collection<T> UIDsToNamespaces(
126: Collection<CsmUID<T>> uids) {
127: Collection<T> out = UIDsToList(uids, false);
128: return out;
129: }
130:
131: public static <T extends CsmDeclaration> Collection<T> UIDsToDeclarations(
132: Collection<CsmUID<T>> uids) {
133: Collection<T> out = UIDsToList(uids, false);
134: return out;
135: }
136:
137: public static <T extends CsmDeclaration> Collection<T> UIDsToDeclarationsUnsafe(
138: Collection<CsmUID<T>> uids) {
139: Collection<T> out = UIDsToList(uids, true);
140: return out;
141: }
142:
143: public static <T extends CsmMacro> Collection<T> UIDsToMacros(
144: Collection<CsmUID<T>> uids) {
145: Collection<T> out = UIDsToList(uids, false);
146: return out;
147: }
148:
149: public static <T extends CsmInclude> Collection<T> UIDsToIncludes(
150: Collection<CsmUID<T>> uids) {
151: Collection<T> out = UIDsToList(uids, false);
152: return out;
153: }
154:
155: private static <T extends CsmIdentifiable> Collection<T> UIDsToList(
156: Collection<CsmUID<T>> uids, boolean allowNullsAndSkip) {
157: allowNullsAndSkip |= TraceFlags.SAFE_UID_ACCESS;
158: return new LazyCsmCollection<T>(new ArrayList<CsmUID<T>>(uids),
159: allowNullsAndSkip);
160: // List<T> out = new ArrayList<T>(uids.size());
161: // for (CsmUID<T> uid : uids) {
162: // assert uid != null;
163: // T decl = UIDCsmConverter.UIDtoIdentifiable(uid);
164: // if (!allowNullsAndSkip || decl != null) {
165: // assert decl != null : "no object for UID " + uid;
166: // out.add(decl);
167: // }
168: // }
169: // return out;
170: }
171:
172: public static <T extends CsmIdentifiable> T UIDtoIdentifiable(
173: CsmUID<T> uid) {
174: return uid == null ? null : uid.getObject();
175: }
176:
177: ////////////////////////////////////////////////////////////////////////////
178: // Object -> UID
179:
180: public static CsmUID<CsmFile> fileToUID(CsmFile file) {
181: return file == null ? null : file.getUID();
182: }
183:
184: public static CsmUID<CsmNamespace> namespaceToUID(CsmNamespace ns) {
185: return ns == null ? null : ns.getUID();
186: }
187:
188: public static CsmUID<CsmProject> projectToUID(CsmProject project) {
189: return project == null ? null : project.getUID();
190: }
191:
192: public static <T extends CsmDeclaration> CsmUID<T> declarationToUID(
193: T decl) {
194: return decl == null ? null : decl.getUID();
195: }
196:
197: public static CsmUID<CsmScope> scopeToUID(CsmScope scope) {
198: return scope == null ? null : ((CsmIdentifiable) scope)
199: .getUID();
200: }
201:
202: public static <T extends CsmIdentifiable> CsmUID<T> identifiableToUID(
203: CsmIdentifiable<T> obj) {
204: return obj == null ? null : obj.getUID();
205: }
206: }
|