Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2012 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2012 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.netbeans.modules.java.hints.onsave; |
43 |
|
44 |
import java.io.IOException; |
45 |
import java.util.ArrayList; |
46 |
import java.util.Collection; |
47 |
import java.util.HashMap; |
48 |
import java.util.HashSet; |
49 |
import java.util.List; |
50 |
import java.util.Map; |
51 |
import java.util.Map.Entry; |
52 |
import java.util.Set; |
53 |
import java.util.concurrent.atomic.AtomicBoolean; |
54 |
import java.util.prefs.Preferences; |
55 |
import javax.swing.undo.UndoableEdit; |
56 |
import org.netbeans.api.editor.mimelookup.MimeLookup; |
57 |
import org.netbeans.api.editor.mimelookup.MimePath; |
58 |
import org.netbeans.api.editor.mimelookup.MimeRegistration; |
59 |
import org.netbeans.api.java.source.CompilationController; |
60 |
import org.netbeans.api.java.source.JavaSource; |
61 |
import org.netbeans.api.java.source.ModificationResult; |
62 |
import org.netbeans.api.java.source.Task; |
63 |
import org.netbeans.modules.java.hints.providers.spi.HintDescription; |
64 |
import org.netbeans.modules.java.hints.providers.spi.HintMetadata; |
65 |
import org.netbeans.modules.java.hints.spiimpl.MessageImpl; |
66 |
import org.netbeans.modules.java.hints.spiimpl.RulesManager; |
67 |
import org.netbeans.modules.java.hints.spiimpl.batch.BatchSearch; |
68 |
import org.netbeans.modules.java.hints.spiimpl.batch.BatchSearch.BatchResult; |
69 |
import org.netbeans.modules.java.hints.spiimpl.batch.BatchSearch.Folder; |
70 |
import org.netbeans.modules.java.hints.spiimpl.batch.BatchUtilities; |
71 |
import org.netbeans.modules.java.hints.spiimpl.batch.ProgressHandleWrapper; |
72 |
import org.netbeans.modules.java.hints.spiimpl.batch.Scopes; |
73 |
import org.netbeans.modules.refactoring.spi.RefactoringElementImplementation; |
74 |
import org.netbeans.spi.editor.document.OnSaveTask; |
75 |
import org.openide.filesystems.FileObject; |
76 |
import org.openide.util.Exceptions; |
77 |
|
78 |
/** |
79 |
* |
80 |
* @author lahvac |
81 |
*/ |
82 |
public class RemoveUnusedAfterSave implements OnSaveTask { |
83 |
|
84 |
public static final String KEY_SETTINGS_PREFIX = "on-save."; |
85 |
|
86 |
private final Context c; |
87 |
private final JavaSource javaSource; |
88 |
private final Set<String> toRun; |
89 |
private final AtomicBoolean cancel = new AtomicBoolean(); |
90 |
|
91 |
public RemoveUnusedAfterSave(Context c, JavaSource javaSource, Set<String> toRun) { |
92 |
this.c = c; |
93 |
this.javaSource = javaSource; |
94 |
this.toRun = toRun; |
95 |
} |
96 |
|
97 |
@Override |
98 |
public void performTask(UndoableEdit undoEdit) { |
99 |
try { |
100 |
List<HintDescription> hints = new ArrayList<HintDescription>(); |
101 |
for (Entry<HintMetadata, ? extends Collection<? extends HintDescription>> e : RulesManager.getInstance().readHints(null, null, cancel).entrySet()) { |
102 |
if (toRun.contains(e.getKey().id)) { |
103 |
hints.addAll(e.getValue()); |
104 |
} |
105 |
} |
106 |
FileObject file = javaSource.getFileObjects().iterator().next(); |
107 |
BatchResult batchResult = BatchSearch.findOccurrences(hints, Scopes.specifiedFoldersScope(Folder.convert(file))); |
108 |
for (ModificationResult mr : BatchUtilities.applyFixes(batchResult, new ProgressHandleWrapper(1), cancel, new ArrayList<RefactoringElementImplementation>(), null, true, new ArrayList<MessageImpl>())) { |
109 |
mr.commit(); |
110 |
} |
111 |
} catch (IOException ex) { |
112 |
Exceptions.printStackTrace(ex); |
113 |
} |
114 |
} |
115 |
|
116 |
@Override |
117 |
public void runLocked(final Runnable run) { |
118 |
try { |
119 |
javaSource.runUserActionTask(new Task<CompilationController>() { |
120 |
@Override public void run(CompilationController parameter) throws Exception { |
121 |
run.run(); |
122 |
} |
123 |
}, true); |
124 |
} catch (IOException ex) { |
125 |
throw new IllegalStateException(ex); |
126 |
} |
127 |
} |
128 |
|
129 |
@MimeRegistration(mimeType="text/x-java", service=Factory.class) |
130 |
public static class TaskFactory implements Factory { |
131 |
@Override public OnSaveTask createTask(Context context) { |
132 |
JavaSource javaSource = JavaSource.forDocument(context.getDocument()); |
133 |
|
134 |
if (javaSource == null) return NOOP; |
135 |
|
136 |
Set<String> toRun = new HashSet<String>(); |
137 |
Preferences settings = MimeLookup.getLookup(MimePath.get("text/x-java")).lookup(Preferences.class); |
138 |
|
139 |
for (Entry<String, Boolean> e : id2DefaultState.entrySet()) { |
140 |
if (settings.getBoolean(KEY_SETTINGS_PREFIX + e.getKey(), e.getValue())) toRun.add(e.getKey()); |
141 |
} |
142 |
|
143 |
if (toRun.isEmpty()) return NOOP; |
144 |
|
145 |
return new RemoveUnusedAfterSave(context, javaSource, toRun); |
146 |
} |
147 |
} |
148 |
|
149 |
private static final Map<String, Boolean> id2DefaultState = new HashMap<String, Boolean>(); |
150 |
|
151 |
static { |
152 |
id2DefaultState.put("Imports_UNUSED", false); |
153 |
id2DefaultState.put("org.netbeans.modules.java.hints.OrganizeImports", false); |
154 |
} |
155 |
|
156 |
static boolean getValue(Preferences settings, String id) { |
157 |
return settings.getBoolean(KEY_SETTINGS_PREFIX + id, id2DefaultState.get(id)); |
158 |
} |
159 |
|
160 |
private static final OnSaveTask NOOP = new OnSaveTask() { |
161 |
@Override public void performTask(UndoableEdit undoEdit) { } |
162 |
@Override public void runLocked(Runnable run) { |
163 |
run.run(); |
164 |
} |
165 |
}; |
166 |
} |