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-2006 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: package org.netbeans.modules.java.source.usages;
042:
043: import java.io.IOException;
044: import javax.lang.model.element.ElementKind;
045: import javax.lang.model.element.TypeElement;
046: import org.netbeans.api.java.source.ElementHandle;
047: import org.netbeans.modules.java.source.ElementHandleAccessor;
048: import org.netbeans.modules.java.source.parsing.FileObjects;
049: import org.openide.filesystems.FileObject;
050: import org.openide.util.Exceptions;
051:
052: /**
053: *
054: * @author Tomas Zezula
055: */
056: public abstract class ResultConvertor<T> {
057:
058: public abstract T convert(ElementKind kind, String value);
059:
060: public static ResultConvertor<FileObject> fileObjectConvertor(
061: final FileObject... roots) {
062: assert roots != null;
063: return new FileObjectConvertor(roots);
064: }
065:
066: public static ResultConvertor<ElementHandle<TypeElement>> elementHandleConvertor() {
067: return new ElementHandleConvertor();
068: }
069:
070: public static ResultConvertor<String> identityConvertor() {
071: return new IdentityConvertor();
072: }
073:
074: private static class FileObjectConvertor extends
075: ResultConvertor<FileObject> {
076:
077: private FileObject[] roots;
078:
079: private FileObjectConvertor(final FileObject... roots) {
080: this .roots = roots;
081: }
082:
083: public FileObject convert(ElementKind kind, String value) {
084: for (FileObject root : roots) {
085: FileObject result = resolveFile(root, value);
086: if (result != null) {
087: return result;
088: }
089: }
090: ClassIndexManager cim = ClassIndexManager.getDefault();
091: for (FileObject root : roots) {
092: try {
093: ClassIndexImpl impl = cim.getUsagesQuery(root
094: .getURL());
095: if (impl != null) {
096: String sourceName = impl.getSourceName(value);
097: if (sourceName != null) {
098: FileObject result = root
099: .getFileObject(sourceName);
100: if (result != null) {
101: return result;
102: }
103: }
104: }
105: } catch (IOException e) {
106: Exceptions.printStackTrace(e);
107: }
108: }
109: return null;
110: }
111:
112: private static FileObject resolveFile(final FileObject root,
113: String classBinaryName) {
114: assert classBinaryName != null;
115: classBinaryName = classBinaryName.replace('.', '/'); //NOI18N
116: int index = classBinaryName.lastIndexOf('/'); //NOI18N
117: FileObject folder;
118: String name;
119: if (index < 0) {
120: folder = root;
121: name = classBinaryName;
122: } else {
123: assert index > 0 : classBinaryName;
124: assert index < classBinaryName.length() - 1 : classBinaryName;
125: folder = root.getFileObject(classBinaryName.substring(
126: 0, index));
127: name = classBinaryName.substring(index + 1);
128: }
129: if (folder == null) {
130: return null;
131: }
132: index = name.indexOf('$'); //NOI18N
133: if (index > 0) {
134: name = name.substring(0, index);
135: }
136: for (FileObject child : folder.getChildren()) {
137: if (FileObjects.JAVA.equalsIgnoreCase(child.getExt())
138: && name.equals(child.getName())) {
139: return child;
140: }
141: }
142: return null;
143: }
144: }
145:
146: private static class ElementHandleConvertor extends
147: ResultConvertor<ElementHandle<TypeElement>> {
148:
149: public ElementHandle<TypeElement> convert(
150: final ElementKind kind, final String value) {
151: return createTypeHandle(kind, value);
152: }
153:
154: @SuppressWarnings("unchecked")
155: // NOI18N
156: private static ElementHandle<TypeElement> createTypeHandle(
157: final ElementKind kind, final String binaryName) {
158: assert binaryName != null;
159: return ElementHandleAccessor.INSTANCE.create(kind,
160: binaryName);
161: }
162:
163: }
164:
165: private static class IdentityConvertor extends
166: ResultConvertor<String> {
167:
168: public String convert(ElementKind kind, String value) {
169: return value;
170: }
171: }
172: }
|