offline inspections: iterate by project once to export all results

This commit is contained in:
anna
2012-04-17 16:26:19 +02:00
parent 41c4d586c4
commit 4ed975f255
6 changed files with 94 additions and 71 deletions
@@ -47,7 +47,9 @@ public class DummyEntryPointsTool extends FilteringInspectionTool {
public void runInspection(@NotNull AnalysisScope scope, @NotNull final InspectionManager manager) {}
public void exportResults(@NotNull Element parentNode) {}
@Override
public void exportResults(@NotNull Element parentNode, RefEntity refEntity) {
}
@NotNull
public JobDescriptor[] getJobDescriptors(GlobalInspectionContext globalInspectionContext) {
@@ -631,45 +631,43 @@ public class UnusedDeclarationInspection extends FilteringInspectionTool {
return myComposer;
}
public void exportResults(@NotNull final Element parentNode) {
@Override
public void exportResults(@NotNull final Element parentNode, RefEntity refEntity) {
if (!(refEntity instanceof RefJavaElement)) return;
final WeakUnreferencedFilter filter = new WeakUnreferencedFilter(this);
getRefManager().iterate(new RefJavaVisitor() {
@Override public void visitElement(RefEntity refEntity) {
if (!(refEntity instanceof RefJavaElement)) return;
if (!getIgnoredRefElements().contains(refEntity) && filter.accepts((RefJavaElement)refEntity)) {
if (refEntity instanceof RefImplicitConstructor) refEntity = ((RefImplicitConstructor)refEntity).getOwnerClass();
Element element = refEntity.getRefManager().export(refEntity, parentNode, -1);
@NonNls Element problemClassElement = new Element(InspectionsBundle.message("inspection.export.results.problem.element.tag"));
if (!getIgnoredRefElements().contains(refEntity) && filter.accepts((RefJavaElement)refEntity)) {
if (refEntity instanceof RefImplicitConstructor) refEntity = ((RefImplicitConstructor)refEntity).getOwnerClass();
Element element = refEntity.getRefManager().export(refEntity, parentNode, -1);
@NonNls Element problemClassElement = new Element(InspectionsBundle.message("inspection.export.results.problem.element.tag"));
if (refEntity instanceof RefElement) {
final RefElement refElement = (RefElement)refEntity;
final HighlightSeverity severity = getCurrentSeverity(refElement);
final String attributeKey = getTextAttributeKey(refElement.getElement().getProject(), severity, ProblemHighlightType.LIKE_UNUSED_SYMBOL);
problemClassElement.setAttribute("severity", severity.myName);
problemClassElement.setAttribute("attribute_key", attributeKey);
}
problemClassElement.addContent(InspectionsBundle.message("inspection.export.results.dead.code"));
element.addContent(problemClassElement);
@NonNls Element hintsElement = new Element("hints");
for (String hint : HINTS) {
@NonNls Element hintElement = new Element("hint");
hintElement.setAttribute("value", hint);
hintsElement.addContent(hintElement);
}
element.addContent(hintsElement);
Element descriptionElement = new Element(InspectionsBundle.message("inspection.export.results.description.tag"));
StringBuffer buf = new StringBuffer();
DeadHTMLComposer.appendProblemSynopsis((RefElement)refEntity, buf);
descriptionElement.addContent(buf.toString());
element.addContent(descriptionElement);
}
if (refEntity instanceof RefElement) {
final RefElement refElement = (RefElement)refEntity;
final HighlightSeverity severity = getCurrentSeverity(refElement);
final String attributeKey =
getTextAttributeKey(refElement.getElement().getProject(), severity, ProblemHighlightType.LIKE_UNUSED_SYMBOL);
problemClassElement.setAttribute("severity", severity.myName);
problemClassElement.setAttribute("attribute_key", attributeKey);
}
});
problemClassElement.addContent(InspectionsBundle.message("inspection.export.results.dead.code"));
element.addContent(problemClassElement);
@NonNls Element hintsElement = new Element("hints");
for (String hint : HINTS) {
@NonNls Element hintElement = new Element("hint");
hintElement.setAttribute("value", hint);
hintsElement.addContent(hintElement);
}
element.addContent(hintsElement);
Element descriptionElement = new Element(InspectionsBundle.message("inspection.export.results.description.tag"));
StringBuffer buf = new StringBuffer();
DeadHTMLComposer.appendProblemSynopsis((RefElement)refEntity, buf);
descriptionElement.addContent(buf.toString());
element.addContent(descriptionElement);
}
}
public QuickFixAction[] getQuickFixes(final RefEntity[] refElements) {
@@ -20,6 +20,7 @@ import com.intellij.codeInspection.GlobalInspectionContext;
import com.intellij.codeInspection.InspectionEP;
import com.intellij.codeInspection.InspectionManager;
import com.intellij.codeInspection.reference.RefEntity;
import com.intellij.codeInspection.reference.RefVisitor;
import com.intellij.codeInspection.ui.InspectionNode;
import com.intellij.codeInspection.ui.InspectionTreeNode;
import org.jdom.Element;
@@ -111,7 +112,7 @@ public class CommonInspectionToolWrapper extends InspectionToolWrapper<Inspectio
}
@Override
public void exportResults(@NotNull Element parentNode) {
getTool().exportResults(parentNode);
public void exportResults(@NotNull Element parentNode, RefEntity refEntity) {
getTool().exportResults(parentNode, refEntity);
}
}
@@ -20,7 +20,6 @@ import com.intellij.codeInspection.*;
import com.intellij.codeInspection.reference.RefElement;
import com.intellij.codeInspection.reference.RefEntity;
import com.intellij.codeInspection.reference.RefModule;
import com.intellij.codeInspection.reference.RefVisitor;
import com.intellij.codeInspection.ui.ProblemDescriptionNode;
import com.intellij.lang.annotation.HighlightSeverity;
import com.intellij.openapi.components.PathMacroManager;
@@ -105,7 +104,7 @@ public abstract class DescriptorProviderInspection extends InspectionTool implem
final File file = new File(fileName);
final CharArrayWriter writer = new CharArrayWriter();
if (!file.exists()) {
writer.append("<").append(InspectionsBundle.message("inspection.problems")).append(" is_local_tool=\"")
writer.append("<").append(InspectionsBundle.message("inspection.problems")).append(" " + GlobalInspectionContextImpl.LOCAL_TOOL_ATTRIBUTE + "=\"")
.append(Boolean.toString(this instanceof LocalInspectionToolWrapper)).append("\">\n");
}
for (Object o : list) {
@@ -263,19 +262,16 @@ public abstract class DescriptorProviderInspection extends InspectionTool implem
return myComposer;
}
public void exportResults(@NotNull final Element parentNode) {
getRefManager().iterate(new RefVisitor() {
@Override public void visitElement(final RefEntity refEntity) {
synchronized (lock) {
if (getProblemElements().containsKey(refEntity)) {
CommonProblemDescriptor[] descriptions = getDescriptions(refEntity);
if (descriptions != null) {
exportResults(descriptions, refEntity, parentNode);
}
}
@Override
public void exportResults(final @NotNull Element parentNode, RefEntity refEntity) {
synchronized (lock) {
if (getProblemElements().containsKey(refEntity)) {
CommonProblemDescriptor[] descriptions = getDescriptions(refEntity);
if (descriptions != null) {
exportResults(descriptions, refEntity, parentNode);
}
}
});
}
}
private void exportResults(@NotNull final CommonProblemDescriptor[] descriptions, final RefEntity refEntity, final Element parentNode) {
@@ -105,6 +105,7 @@ public class GlobalInspectionContextImpl extends UserDataHolderBase implements G
private final Map<String, Tools> myTools = new THashMap<String, Tools>();
private AnalysisUIOptions myUIOptions;
@NonNls static final String LOCAL_TOOL_ATTRIBUTE = "is_local_tool";
public GlobalInspectionContextImpl(Project project, NotNullLazyValue<ContentManager> contentManager) {
myProject = project;
@@ -296,44 +297,63 @@ public class GlobalInspectionContextImpl extends UserDataHolderBase implements G
public void run() {
performInspectionsWithProgress(scope, manager);
@NonNls final String ext = ".xml";
final Map<Element, Tools> globalTools = new HashMap<Element, Tools>();
for (Map.Entry<String,Tools> stringSetEntry : myTools.entrySet()) {
final Element root = new Element(InspectionsBundle.message("inspection.problems"));
final Document doc = new Document(root);
final Tools sameTools = stringSetEntry.getValue();
boolean hasProblems = false;
boolean isLocalTool = false;
String toolName = stringSetEntry.getKey();
if (sameTools != null) {
for (ScopeToolState toolDescr : sameTools.getTools()) {
final InspectionTool tool = (InspectionTool)toolDescr.getTool();
if (tool instanceof LocalInspectionToolWrapper) {
hasProblems = new File(outputPath, toolName + ext).exists();
isLocalTool = true;
}
else {
tool.updateContent();
if (tool.hasReportedProblems()) {
hasProblems = true;
tool.exportResults(root);
final Element root = new Element(InspectionsBundle.message("inspection.problems"));
globalTools.put(root, sameTools);
LOG.assertTrue(!hasProblems, toolName);
break;
}
}
}
}
if (!hasProblems) continue;
@NonNls final String isLocalToolAttribute = "is_local_tool";
root.setAttribute(isLocalToolAttribute, String.valueOf(isLocalTool));
try {
new File(outputPath).mkdirs();
final File file = new File(outputPath, toolName + ext);
inspectionsResults.add(file);
if (isLocalTool) {
FileUtil.writeToFile(file, ("</" + InspectionsBundle.message("inspection.problems") + ">").getBytes("UTF-8"), true);
}
else {
PathMacroManager.getInstance(getProject()).collapsePaths(doc.getRootElement());
JDOMUtil.writeDocument(doc, file, "\n");
FileUtil.writeToFile(file, ("</" + InspectionsBundle.message("inspection.problems") + ">").getBytes("UTF-8"), true);
}
catch (IOException e) {
LOG.error(e);
}
}
getRefManager().iterate(new RefVisitor() {
@Override
public void visitElement(final RefEntity refEntity) {
for (Element element : globalTools.keySet()) {
final Tools tools = globalTools.get(element);
for (ScopeToolState state : tools.getTools()) {
((InspectionTool)state.getTool()).exportResults(element, refEntity);
}
}
}
});
for (Element element : globalTools.keySet()) {
final String toolName = globalTools.get(element).getShortName();
element.setAttribute(LOCAL_TOOL_ATTRIBUTE, Boolean.toString(false));
final Document doc = new Document(element);
PathMacroManager.getInstance(getProject()).collapsePaths(doc.getRootElement());
try {
new File(outputPath).mkdirs();
final File file = new File(outputPath, toolName + ext);
inspectionsResults.add(file);
JDOMUtil.writeDocument(doc, file, "\n");
}
catch (IOException e) {
LOG.error(e);
}
@@ -29,10 +29,7 @@ import com.intellij.codeInsight.daemon.impl.HighlightInfoType;
import com.intellij.codeInsight.daemon.impl.SeverityRegistrar;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInspection.*;
import com.intellij.codeInspection.reference.RefElement;
import com.intellij.codeInspection.reference.RefEntity;
import com.intellij.codeInspection.reference.RefManager;
import com.intellij.codeInspection.reference.RefModule;
import com.intellij.codeInspection.reference.*;
import com.intellij.codeInspection.ui.InspectionNode;
import com.intellij.codeInspection.ui.InspectionTreeNode;
import com.intellij.lang.annotation.HighlightSeverity;
@@ -69,7 +66,16 @@ public abstract class InspectionTool extends InspectionProfileEntry {
public abstract void runInspection(@NotNull AnalysisScope scope, @NotNull InspectionManager manager);
public abstract void exportResults(@NotNull Element parentNode);
public void exportResults(@NotNull final Element parentNode) {
getRefManager().iterate(new RefVisitor(){
@Override
public void visitElement(RefEntity elem) {
exportResults(parentNode, elem);
}
});
}
public abstract void exportResults(@NotNull Element parentNode, RefEntity refEntity);
public abstract boolean isGraphNeeded();
@Nullable