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.java.hints.errors;
043:
044: import com.sun.source.tree.CompilationUnitTree;
045: import com.sun.source.tree.MethodInvocationTree;
046: import com.sun.source.tree.Tree.Kind;
047: import com.sun.source.util.TreePath;
048: import java.io.IOException;
049: import java.util.ArrayList;
050: import java.util.Arrays;
051: import java.util.Collections;
052: import java.util.HashMap;
053: import java.util.HashSet;
054: import java.util.List;
055: import java.util.Map;
056: import java.util.Set;
057: import java.util.logging.Level;
058: import java.util.logging.Logger;
059: import javax.lang.model.element.TypeElement;
060: import org.netbeans.api.java.source.Task;
061: import org.netbeans.api.java.source.CompilationInfo;
062: import org.netbeans.api.java.source.JavaSource;
063: import org.netbeans.api.java.source.JavaSource.Phase;
064: import org.netbeans.api.java.source.SourceUtils;
065: import org.netbeans.api.java.source.WorkingCopy;
066: import org.netbeans.api.lexer.Token;
067: import org.netbeans.modules.editor.java.Utilities;
068: import org.netbeans.modules.java.editor.imports.ComputeImports;
069: import org.netbeans.modules.java.editor.imports.JavaFixAllImports;
070: import org.netbeans.modules.java.hints.errors.ImportClass.ImportCandidatesHolder;
071: import org.netbeans.modules.java.hints.infrastructure.CreatorBasedLazyFixList;
072: import org.netbeans.modules.java.hints.infrastructure.ErrorHintsProvider;
073: import org.netbeans.modules.java.hints.infrastructure.Pair;
074: import org.netbeans.modules.java.hints.spi.ErrorRule;
075: import org.netbeans.spi.editor.hints.ChangeInfo;
076: import org.netbeans.spi.editor.hints.EnhancedFix;
077: import org.netbeans.spi.editor.hints.Fix;
078: import org.openide.ErrorManager;
079: import org.openide.filesystems.FileObject;
080: import org.openide.util.Exceptions;
081: import org.openide.util.NbBundle;
082: import org.openide.util.RequestProcessor;
083:
084: /**
085: *
086: * @author Jan Lahoda
087: */
088: public final class ImportClass implements
089: ErrorRule<ImportCandidatesHolder> {
090:
091: static RequestProcessor WORKER = new RequestProcessor(
092: "ImportClassEnabler", 1);
093:
094: public ImportClass() {
095: }
096:
097: public Set<String> getCodes() {
098: return new HashSet<String>(Arrays.asList(
099: "compiler.err.cant.resolve",
100: "compiler.err.cant.resolve.location",
101: "compiler.err.doesnt.exist", "compiler.err.not.stmt"));
102: }
103:
104: public List<Fix> run(final CompilationInfo info,
105: String diagnosticKey, final int offset, TreePath treePath,
106: Data<ImportCandidatesHolder> data) {
107: resume();
108:
109: int errorPosition = offset + 1; //TODO: +1 required to work OK, rethink
110:
111: if (errorPosition == (-1)) {
112: ErrorHintsProvider.LOG.log(Level.FINE,
113: "ImportClassEnabler.create errorPosition=-1"); //NOI18N
114:
115: return Collections.<Fix> emptyList();
116: }
117:
118: TreePath path = info.getTreeUtilities().pathFor(errorPosition);
119:
120: if (path.getParentPath() != null
121: && path.getParentPath().getLeaf().getKind() == Kind.METHOD_INVOCATION) {
122: //#86313:
123: //if the error is in the type parameter, import should be proposed:
124: MethodInvocationTree mit = (MethodInvocationTree) path
125: .getParentPath().getLeaf();
126:
127: if (!mit.getTypeArguments().contains(path.getLeaf())) {
128: return Collections.<Fix> emptyList();
129: }
130: }
131:
132: Token ident = null;
133:
134: try {
135: ident = ErrorHintsProvider.findUnresolvedElementToken(info,
136: offset);
137: } catch (IOException e) {
138: Exceptions.printStackTrace(e);
139: }
140:
141: ErrorHintsProvider.LOG.log(Level.FINE,
142: "ImportClassEnabler.create ident={0}", ident); //NOI18N
143:
144: if (ident == null) {
145: return Collections.<Fix> emptyList();
146: }
147:
148: FileObject file = info.getFileObject();
149: String simpleName = ident.text().toString();
150: Pair<List<String>, List<String>> candidates = getCandidateFQNs(
151: info, file, simpleName, data);
152:
153: if (isCancelled() || candidates == null) {
154: ErrorHintsProvider.LOG.log(Level.FINE,
155: "ImportClassEnabler.cancelled."); //NOI18N
156:
157: return CreatorBasedLazyFixList.CANCELLED;
158: }
159:
160: List<String> filtered = candidates.getA();
161: List<String> unfiltered = candidates.getB();
162: List<Fix> fixes = new ArrayList<Fix>();
163:
164: if (unfiltered != null && filtered != null) {
165: for (String fqn : unfiltered) {
166: StringBuilder sort = new StringBuilder();
167:
168: sort.append("0001#");
169:
170: boolean prefered = filtered.contains(fqn);
171:
172: if (prefered)
173: sort.append("A#");
174: else
175: sort.append("Z#");
176:
177: int order = Utilities.getImportanceLevel(fqn);
178: String orderString = Integer.toHexString(order);
179:
180: sort.append("00000000".substring(0, 8 - orderString
181: .length()));
182: sort.append(orderString);
183: sort.append('#');
184: sort.append(fqn);
185:
186: fixes.add(new FixImport(file, fqn, sort.toString(),
187: prefered));
188: }
189: }
190:
191: ErrorHintsProvider.LOG.log(Level.FINE,
192: "ImportClassEnabler.create finished."); //NOI18N
193:
194: return fixes;
195: }
196:
197: public synchronized void cancel() {
198: ErrorHintsProvider.LOG.log(Level.FINE,
199: "ImportClassEnabler.cancel called."); //NOI18N
200:
201: cancelled = true;
202:
203: if (compImports != null) {
204: compImports.cancel();
205: }
206: }
207:
208: public String getId() {
209: return ImportClass.class.getName();
210: }
211:
212: public String getDisplayName() {
213: return "Add Import Fix";
214: }
215:
216: public String getDescription() {
217: return "Add Import Fix";
218: }
219:
220: private synchronized void resume() {
221: ErrorHintsProvider.LOG.log(Level.FINE,
222: "ImportClassEnabler.resume called."); //NOI18N
223:
224: cancelled = false;
225: }
226:
227: private synchronized boolean isCancelled() {
228: return cancelled;
229: }
230:
231: private boolean cancelled;
232: private ComputeImports compImports;
233:
234: private synchronized void setComputeImports(
235: ComputeImports compImports) {
236: this .compImports = compImports;
237: }
238:
239: public Pair<List<String>, List<String>> getCandidateFQNs(
240: CompilationInfo info, FileObject file, String simpleName,
241: Data<ImportCandidatesHolder> data) {
242: ImportCandidatesHolder holder = data.getData();
243:
244: if (holder == null) {
245: data.setData(holder = new ImportCandidatesHolder());
246: }
247:
248: Pair<Map<String, List<String>>, Map<String, List<String>>> result = holder
249: .getCandidates();
250:
251: if (result == null || result.getA() == null
252: || result.getB() == null) {
253: //compute imports:
254: Map<String, List<String>> candidates = new HashMap<String, List<String>>();
255: ComputeImports imp = new ComputeImports();
256:
257: setComputeImports(imp);
258:
259: ComputeImports.Pair<Map<String, List<TypeElement>>, Map<String, List<TypeElement>>> rawCandidates = imp
260: .computeCandidates(info);
261:
262: setComputeImports(null);
263:
264: if (isCancelled() || rawCandidates == null) {
265: ErrorHintsProvider.LOG
266: .log(Level.FINE,
267: "ImportClassEnabler.getCandidateFQNs cancelled, returning."); //NOI18N
268:
269: return null;
270: }
271:
272: for (String sn : rawCandidates.a.keySet()) {
273: List<String> c = new ArrayList<String>();
274:
275: for (TypeElement te : rawCandidates.a.get(sn)) {
276: c.add(te.getQualifiedName().toString());
277: }
278:
279: candidates.put(sn, c);
280: }
281:
282: Map<String, List<String>> notFilteredCandidates = new HashMap<String, List<String>>();
283:
284: for (String sn : rawCandidates.b.keySet()) {
285: List<String> c = new ArrayList<String>();
286:
287: for (TypeElement te : rawCandidates.b.get(sn)) {
288: c.add(te.getQualifiedName().toString());
289: }
290:
291: notFilteredCandidates.put(sn, c);
292: }
293:
294: result = new Pair(candidates, notFilteredCandidates);
295:
296: holder.setCandidates(result);
297: }
298:
299: List<String> candList = result.getA().get(simpleName);
300: List<String> notFilteredCandList = result.getB()
301: .get(simpleName);
302:
303: return new Pair(candList, notFilteredCandList);
304: }
305:
306: public static class ImportCandidatesHolder {
307: private Pair<Map<String, List<String>>, Map<String, List<String>>> candidates;
308:
309: public Pair<Map<String, List<String>>, Map<String, List<String>>> getCandidates() {
310: return candidates;
311: }
312:
313: public void setCandidates(
314: Pair<Map<String, List<String>>, Map<String, List<String>>> candidates) {
315: this .candidates = candidates;
316: }
317: }
318:
319: static final class FixImport implements EnhancedFix {
320:
321: private FileObject file;
322: private String fqn;
323: private String sortText;
324: private boolean isValid;
325:
326: public FixImport(FileObject file, String fqn, String sortText,
327: boolean isValid) {
328: this .file = file;
329: this .fqn = fqn;
330: this .sortText = sortText;
331: this .isValid = isValid;
332: }
333:
334: public String getText() {
335: if (isValid)
336: return NbBundle.getMessage(ImportClass.class,
337: "Add_import_for_X", new Object[] { fqn });
338: else
339: return "<html><font color='#808080'><s>"
340: + NbBundle.getMessage(ImportClass.class,
341: "Add_import_for_X",
342: new Object[] { fqn });
343: }
344:
345: public ChangeInfo implement() throws IOException {
346: JavaSource js = JavaSource.forFileObject(file);
347:
348: Task task = new Task<WorkingCopy>() {
349: public void run(WorkingCopy copy) throws Exception {
350: if (copy.toPhase(Phase.RESOLVED).compareTo(
351: Phase.RESOLVED) < 0)
352: return;
353:
354: TypeElement te = copy.getElements().getTypeElement(
355: fqn);
356:
357: if (te == null) {
358: Logger
359: .getAnonymousLogger()
360: .warning(
361: String
362: .format(
363: "Attempt to fix import for FQN: %s, which does not have a TypeElement in currect context",
364: fqn));
365: return;
366: }
367:
368: CompilationUnitTree cut = JavaFixAllImports
369: .addImports(copy.getCompilationUnit(),
370: Collections.singletonList(te
371: .getQualifiedName()
372: .toString()), copy
373: .getTreeMaker());
374: copy.rewrite(copy.getCompilationUnit(), cut);
375: }
376:
377: };
378:
379: js.runModificationTask(task).commit();
380: return null;
381: }
382:
383: @Override
384: public int hashCode() {
385: return fqn.hashCode();
386: }
387:
388: @Override
389: public boolean equals(Object o) {
390: if (o instanceof FixImport) {
391: return fqn.equals(((FixImport) o).fqn);
392: }
393:
394: return false;
395: }
396:
397: public CharSequence getSortText() {
398: return sortText;
399: }
400: }
401: }
|