Inspections Enumeration moved to offline pass
[fedora-idea.git] / inspections / impl / com / intellij / codeInspection / ex / InspectionApplication.java
blobae9968bbcb3c0a198185099204acf2097239a056
1 package com.intellij.codeInspection.ex;
3 import com.intellij.analysis.AnalysisScope;
4 import com.intellij.codeInspection.*;
5 import com.intellij.openapi.application.PathManager;
6 import com.intellij.openapi.application.ex.ApplicationEx;
7 import com.intellij.openapi.application.ex.ApplicationManagerEx;
8 import com.intellij.openapi.diagnostic.Logger;
9 import com.intellij.openapi.progress.ProgressManager;
10 import com.intellij.openapi.progress.util.ProgressIndicatorBase;
11 import com.intellij.openapi.project.Project;
12 import com.intellij.openapi.project.ex.ProjectManagerEx;
13 import com.intellij.openapi.vfs.LocalFileSystem;
14 import com.intellij.openapi.vfs.VirtualFile;
15 import com.intellij.profile.Profile;
16 import com.intellij.profile.codeInspection.InspectionProfileManager;
17 import com.intellij.psi.PsiClass;
18 import com.intellij.psi.PsiDirectory;
19 import com.intellij.psi.PsiManager;
20 import com.intellij.util.ResourceUtil;
21 import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
23 import javax.swing.*;
24 import java.io.File;
25 import java.io.FileWriter;
26 import java.io.IOException;
27 import java.net.URL;
28 import java.util.HashMap;
29 import java.util.HashSet;
30 import java.util.Map;
31 import java.util.Set;
33 /**
34 * @author max
36 public class InspectionApplication {
37 private static final Logger LOG = Logger.getInstance("#com.intellij.codeInspection.ex.InspectionApplication");
39 public String myProjectPath = null;
40 public String myOutPath = null;
41 public String mySourceDirectory = null;
42 public String myProfileName = null;
43 public boolean myRunWithEditorSettings = false;
44 private Project myProject;
45 private int myVerboseLevel = 0;
47 public void startup() {
48 if (myProjectPath == null || myOutPath == null || myProfileName == null) {
49 InspectionMain.printHelp();
52 SwingUtilities.invokeLater(new Runnable() {
53 public void run() {
54 ApplicationEx application = ApplicationManagerEx.getApplicationEx();
55 try {
56 logMessage(1, InspectionsBundle.message("inspection.application.starting.up"));
57 application.doNotSave();
58 application.load(PathManager.getOptionsPath());
59 logMessageLn(1, InspectionsBundle.message("inspection.done"));
61 InspectionApplication.this.run();
63 catch (Exception e) {
64 LOG.error(e);
66 finally {
67 application.exit(true);
70 });
73 public void run() {
74 try {
75 myProjectPath = myProjectPath.replace(File.separatorChar, '/');
76 VirtualFile vfsProject = LocalFileSystem.getInstance().findFileByPath(myProjectPath);
77 if (vfsProject == null) {
78 logError(InspectionsBundle.message("inspection.application.file.cannot.be.found", myProjectPath));
79 InspectionMain.printHelp();
82 final Profile inspectionProfile = InspectionProfileManager.getInstance().loadProfile(myProfileName);
83 if (inspectionProfile == null) {
84 logError(InspectionsBundle.message("inspection.application.file.cannot.be.found", myProfileName));
85 InspectionMain.printHelp();
89 logMessage(1, InspectionsBundle.message("inspection.application.opening.project"));
90 myProject = ProjectManagerEx.getInstanceEx().loadAndOpenProject(myProjectPath);
91 logMessageLn(1, InspectionsBundle.message("inspection.done"));
92 logMessage(1, InspectionsBundle.message("inspection.application.initializing.project"));
94 final InspectionManagerEx im = (InspectionManagerEx)InspectionManager.getInstance(myProject);
95 final AnalysisScope scope;
97 final GlobalInspectionContextImpl inspectionContext = im.createNewGlobalContext(true);
98 inspectionContext.setExternalProfile((InspectionProfile)inspectionProfile);
99 im.setProfile(inspectionProfile.getName());
101 if (mySourceDirectory == null) {
102 scope = new AnalysisScope(myProject);
104 else {
105 mySourceDirectory = mySourceDirectory.replace(File.separatorChar, '/');
107 VirtualFile vfsDir = LocalFileSystem.getInstance().findFileByPath(mySourceDirectory);
108 if (vfsDir == null) {
109 logError(InspectionsBundle.message("inspection.application.directory.cannot.be.found", mySourceDirectory));
110 InspectionMain.printHelp();
113 PsiDirectory psiDirectory = PsiManager.getInstance(myProject).findDirectory(vfsDir);
114 scope = new AnalysisScope(psiDirectory);
117 logMessageLn(1, InspectionsBundle.message("inspection.done"));
119 ProgressManager.getInstance().runProcess(new Runnable() {
120 public void run() {
121 PsiClass psiObjectClass = PsiManager.getInstance(myProject).findClass("java.lang.Object");
122 if (psiObjectClass == null) {
123 logError(InspectionsBundle.message("inspection.no.jdk.error.message"));
124 System.exit(1);
125 return;
127 inspectionContext.launchInspectionsOffline(scope, myOutPath, myRunWithEditorSettings, im);
128 logMessageLn(1, "\n" +
129 InspectionsBundle.message("inspection.capitalized.done") +
130 "\n");
132 }, new ProgressIndicatorBase() {
133 private String lastPrefix = "";
135 public void setText(String text) {
136 if (myVerboseLevel == 0) return;
138 if (myVerboseLevel == 1) {
139 //noinspection HardCodedStringLiteral
140 int idx = text.indexOf(" in ");
141 if (idx == -1) {
142 //noinspection HardCodedStringLiteral
143 idx = text.indexOf(" of ");
146 if (idx == -1) return;
147 String prefix = text.substring(0, idx);
148 if (prefix.equals(lastPrefix)) {
149 logMessage(1, ".");
150 return;
152 lastPrefix = prefix;
153 logMessageLn(1, "");
154 logMessageLn(1, prefix);
155 return;
158 logMessageLn(2, text);
161 describeInspections(myOutPath + File.separatorChar + ".descriptions.xml");
163 catch (IOException e) {
164 LOG.error(e);
165 InspectionMain.printHelp();
167 catch (Throwable e) {
168 LOG.error(e);
169 System.exit(1);
173 public void setVerboseLevel(int verboseLevel) {
174 myVerboseLevel = verboseLevel;
177 private void logMessage(int minVerboseLevel, String message) {
178 if (myVerboseLevel >= minVerboseLevel) {
179 System.out.print(message);
183 private void logError(String message) {
184 System.err.println(message);
187 private void logMessageLn(int minVerboseLevel, String message) {
188 if (myVerboseLevel >= minVerboseLevel) {
189 System.out.println(message);
193 private void describeInspections(String myOutputPath) throws IOException {
194 final InspectionProfileEntry[] profileEntries = InspectionProfileImpl.DEFAULT_PROFILE.getInspectionTools();
195 final Map<String, Set<InspectionProfileEntry>> map = new HashMap<String, Set<InspectionProfileEntry>>();
196 for (InspectionProfileEntry entry : profileEntries) {
197 final String groupName = entry.getGroupDisplayName();
198 Set<InspectionProfileEntry> groupInspections = map.get(groupName);
199 if (groupInspections == null) {
200 groupInspections = new HashSet<InspectionProfileEntry>();
201 map.put(groupName, groupInspections);
203 groupInspections.add(entry);
206 FileWriter fw = null;
207 try {
208 fw = new FileWriter(myOutputPath);
209 final PrettyPrintWriter xmlWriter = new PrettyPrintWriter(fw);
210 xmlWriter.startNode("inspections");
211 for (String groupName : map.keySet()) {
212 xmlWriter.startNode("group");
213 xmlWriter.addAttribute("group_name", groupName);
214 final Set<InspectionProfileEntry> entries = map.get(groupName);
215 for (InspectionProfileEntry entry : entries) {
216 xmlWriter.startNode("inspection");
217 xmlWriter.addAttribute("short_name", entry.getShortName());
218 xmlWriter.addAttribute("display_name", entry.getDisplayName());
219 final URL descriptionUrl = InspectionToolRegistrar.getDescriptionUrl(entry);
220 if (descriptionUrl!=null) {
221 final String description = ResourceUtil.loadText(descriptionUrl);
222 xmlWriter.setValue(description);
223 } else {
224 LOG.error(entry.getShortName() + " descriptionUrl==null");
226 xmlWriter.endNode();
228 xmlWriter.endNode();
230 xmlWriter.endNode();
232 finally {
233 if (fw != null) {
234 fw.close();