From db6c68001c07c20cb09bb40905bbf91dadd9b6d3 Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Tue, 28 Oct 2014 13:13:48 +0300 Subject: [PATCH] data races --- .../UnusedDeclarationPresentation.java | 7 +- .../ex/InspectionRVContentProvider.java | 5 +- .../ex/InspectionRVContentProviderImpl.java | 30 ++++--- .../ui/DefaultInspectionToolPresentation.java | 83 ++++++++----------- .../ui/InspectionToolPresentation.java | 4 +- .../ui/actions/ExportHTMLAction.java | 15 ++-- 6 files changed, 64 insertions(+), 80 deletions(-) diff --git a/java/java-impl/src/com/intellij/codeInspection/deadCode/UnusedDeclarationPresentation.java b/java/java-impl/src/com/intellij/codeInspection/deadCode/UnusedDeclarationPresentation.java index cdc8fdd15c7d..76602ca1c660 100644 --- a/java/java-impl/src/com/intellij/codeInspection/deadCode/UnusedDeclarationPresentation.java +++ b/java/java-impl/src/com/intellij/codeInspection/deadCode/UnusedDeclarationPresentation.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -54,7 +54,7 @@ import java.awt.event.KeyEvent; import java.util.*; public class UnusedDeclarationPresentation extends DefaultInspectionToolPresentation { - private Map> myPackageContents = new HashMap>(); + private final Map> myPackageContents = Collections.synchronizedMap(new HashMap>()); private Map> myOldPackageContents = null; @@ -335,7 +335,7 @@ public class UnusedDeclarationPresentation extends DefaultInspectionToolPresenta @Override public void updateContent() { getTool().checkForReachables(getContext()); - myPackageContents = new HashMap>(); + myPackageContents.clear(); getContext().getRefManager().iterate(new RefJavaVisitor() { @Override public void visitElement(@NotNull RefEntity refEntity) { if (!(refEntity instanceof RefJavaElement)) return;//dead code doesn't work with refModule | refPackage @@ -378,6 +378,7 @@ public class UnusedDeclarationPresentation extends DefaultInspectionToolPresenta return false; } + @NotNull @Override public Map> getContent() { return myPackageContents; diff --git a/platform/lang-impl/src/com/intellij/codeInspection/ex/InspectionRVContentProvider.java b/platform/lang-impl/src/com/intellij/codeInspection/ex/InspectionRVContentProvider.java index ec31a29b7f63..df797ef8115a 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/ex/InspectionRVContentProvider.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/ex/InspectionRVContentProvider.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -79,8 +79,7 @@ public abstract class InspectionRVContentProvider { InspectionToolPresentation presentation = context.getPresentation(wrapper); Map> content = presentation.getContent(); Map problems = presentation.getProblemElements(); - Map> contents = content == null ? new HashMap>() : content; - appendToolNodeContent(context, toolNode, parentNode, showStructure, contents, problems, null); + appendToolNodeContent(context, toolNode, parentNode, showStructure, content, problems, null); } public abstract void appendToolNodeContent(@NotNull GlobalInspectionContextImpl context, diff --git a/platform/lang-impl/src/com/intellij/codeInspection/ex/InspectionRVContentProviderImpl.java b/platform/lang-impl/src/com/intellij/codeInspection/ex/InspectionRVContentProviderImpl.java index 83c4885d9334..28d4861e5348 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/ex/InspectionRVContentProviderImpl.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/ex/InspectionRVContentProviderImpl.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -55,23 +55,21 @@ public class InspectionRVContentProviderImpl extends InspectionRVContentProvider final SearchScope searchScope = context.getCurrentScope().toSearchScope(); if (searchScope instanceof LocalSearchScope) { final Map> contents = presentation.getContent(); - if (contents != null) { - final Map problemElements = presentation.getProblemElements(); - for (Set entities : contents.values()) { - for (Iterator iterator = entities.iterator(); iterator.hasNext(); ) { - RefEntity entity = iterator.next(); - if (entity instanceof RefElement) { - final PsiElement element = ((RefElement)entity).getElement(); - if (element != null) { - final TextRange range = element.getTextRange(); - if (range != null && ((LocalSearchScope)searchScope).containsRange(element.getContainingFile(), range)) { - continue; - } + final Map problemElements = presentation.getProblemElements(); + for (Set entities : contents.values()) { + for (Iterator iterator = entities.iterator(); iterator.hasNext(); ) { + RefEntity entity = iterator.next(); + if (entity instanceof RefElement) { + final PsiElement element = ((RefElement)entity).getElement(); + if (element != null) { + final TextRange range = element.getTextRange(); + if (range != null && ((LocalSearchScope)searchScope).containsRange(element.getContainingFile(), range)) { + continue; } } - problemElements.remove(entity); - iterator.remove(); } + problemElements.remove(entity); + iterator.remove(); } } } @@ -106,7 +104,7 @@ public class InspectionRVContentProviderImpl extends InspectionRVContentProvider }; InspectionToolPresentation presentation = context.getPresentation(toolWrapper); final Set moduleProblems = presentation.getModuleProblems(); - if (moduleProblems != null && !moduleProblems.isEmpty()) { + if (!moduleProblems.isEmpty()) { Set entities = contents.get(""); if (entities == null) { entities = new HashSet(); diff --git a/platform/lang-impl/src/com/intellij/codeInspection/ui/DefaultInspectionToolPresentation.java b/platform/lang-impl/src/com/intellij/codeInspection/ui/DefaultInspectionToolPresentation.java index fe7c3ab22b81..131c8002af70 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/ui/DefaultInspectionToolPresentation.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/ui/DefaultInspectionToolPresentation.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -42,6 +42,7 @@ import com.intellij.util.containers.ContainerUtil; import com.intellij.util.containers.HashSet; import com.intellij.util.ui.UIUtil; import gnu.trove.THashMap; +import gnu.trove.THashSet; import org.jdom.Element; import org.jdom.IllegalDataException; import org.jetbrains.annotations.NonNls; @@ -63,13 +64,13 @@ public class DefaultInspectionToolPresentation implements ProblemDescriptionsPro protected InspectionNode myToolNode; private static final Object lock = new Object(); - private Map myProblemElements; - private Map> myContents = null; - private Set myModulesProblems = null; - private Map myProblemToElements; + private final Map myProblemElements = Collections.synchronizedMap(new THashMap()); + private final Map> myContents = Collections.synchronizedMap(new com.intellij.util.containers.HashMap>()); // keys can be null + private final Set myModulesProblems = Collections.synchronizedSet(new THashSet()); + private final Map myProblemToElements = Collections.synchronizedMap(new THashMap()); private DescriptorComposer myComposer; - private Map> myQuickFixActions; - private Map myIgnoredElements; + private final Map> myQuickFixActions = Collections.synchronizedMap(new com.intellij.util.containers.HashMap>()); + private final Map myIgnoredElements = Collections.synchronizedMap(new com.intellij.util.containers.HashMap()); private Map myOldProblemElements = null; protected static final Logger LOG = Logger.getInstance("#com.intellij.codeInspection.ex.DescriptorProviderInspection"); @@ -223,13 +224,16 @@ public class DefaultInspectionToolPresentation implements ProblemDescriptionsPro if (view == null || !(refElement instanceof RefElement)) { return; } - InspectionNode toolNode = myToolNode; - if (toolNode == null) { + final InspectionNode toolNode; + if (myToolNode == null) { final HighlightSeverity currentSeverity = getSeverity((RefElement)refElement); - view.addTool(myToolWrapper, HighlightDisplayLevel.find(currentSeverity), context.getUIOptions().GROUP_BY_SEVERITY); + toolNode = view.addTool(myToolWrapper, HighlightDisplayLevel.find(currentSeverity), context.getUIOptions().GROUP_BY_SEVERITY); } - else if (toolNode.isTooBigForOnlineRefresh()) { - return; + else { + toolNode = myToolNode; + if (toolNode.isTooBigForOnlineRefresh()) { + return; + } } final Map problems = new HashMap(); problems.put(refElement, descriptors); @@ -246,8 +250,8 @@ public class DefaultInspectionToolPresentation implements ProblemDescriptionsPro @Override public void run() { if (!isDisposed()) { - view.getProvider().appendToolNodeContent(context, myToolNode, - (InspectionTreeNode)myToolNode.getParent(), context.getUIOptions().SHOW_STRUCTURE, + view.getProvider().appendToolNodeContent(context, toolNode, + (InspectionTreeNode)toolNode.getParent(), context.getUIOptions().SHOW_STRUCTURE, contents, problems, (DefaultTreeModel)view.getTree().getModel()); context.addView(view); } @@ -275,7 +279,7 @@ public class DefaultInspectionToolPresentation implements ProblemDescriptionsPro final File file = new File(fileName); final CharArrayWriter writer = new CharArrayWriter(); if (!file.exists()) { - writer.append("<").append(InspectionsBundle.message("inspection.problems")).append(" " + GlobalInspectionContextImpl.LOCAL_TOOL_ATTRIBUTE + "=\"") + writer.append("<").append(InspectionsBundle.message("inspection.problems")).append(" " + GlobalInspectionContextBase.LOCAL_TOOL_ATTRIBUTE + "=\"") .append(Boolean.toString(myToolWrapper instanceof LocalInspectionToolWrapper)).append("\">\n"); } for (Object o : list) { @@ -393,14 +397,14 @@ public class DefaultInspectionToolPresentation implements ProblemDescriptionsPro myOldProblemElements = null; synchronized (lock) { - myProblemElements = null; - myProblemToElements = null; - myQuickFixActions = null; - myIgnoredElements = null; + myProblemElements.clear(); + myProblemToElements.clear(); + myQuickFixActions.clear(); + myIgnoredElements.clear(); + myContents.clear(); + myModulesProblems.clear(); } - myContents = null; - myModulesProblems = null; isDisposed = true; } @@ -525,8 +529,8 @@ public class DefaultInspectionToolPresentation implements ProblemDescriptionsPro @Override public void updateContent() { - myContents = new com.intellij.util.containers.HashMap>(); - myModulesProblems = new HashSet(); + myContents.clear(); + myModulesProblems.clear(); final Set elements = getProblemElements().keySet(); for (RefEntity element : elements) { if (getContext().getUIOptions().FILTER_RESOLVED_ITEMS && getIgnoredElements().containsKey(element)) continue; @@ -545,6 +549,7 @@ public class DefaultInspectionToolPresentation implements ProblemDescriptionsPro } } + @NotNull @Override public Map> getContent() { return myContents; @@ -553,8 +558,7 @@ public class DefaultInspectionToolPresentation implements ProblemDescriptionsPro @Override public Map> getOldContent() { if (myOldProblemElements == null) return null; - final com.intellij.util.containers.HashMap> - oldContents = new com.intellij.util.containers.HashMap>(); + final Map> oldContents = new com.intellij.util.containers.HashMap>(); final Set elements = myOldProblemElements.keySet(); for (RefEntity element : elements) { String groupName = element instanceof RefElement ? element.getRefManager().getGroupName((RefElement)element) : element.getName(); @@ -573,6 +577,7 @@ public class DefaultInspectionToolPresentation implements ProblemDescriptionsPro return oldContents; } + @NotNull @Override public Set getModuleProblems() { return myModulesProblems; @@ -719,12 +724,7 @@ public class DefaultInspectionToolPresentation implements ProblemDescriptionsPro @Override @NotNull public Map getProblemElements() { - synchronized (lock) { - if (myProblemElements == null) { - myProblemElements = Collections.synchronizedMap(new THashMap()); - } - return myProblemElements; - } + return myProblemElements; } @Override @@ -735,32 +735,17 @@ public class DefaultInspectionToolPresentation implements ProblemDescriptionsPro @NotNull private Map getProblemToElements() { - synchronized (lock) { - if (myProblemToElements == null) { - myProblemToElements = Collections.synchronizedMap(new THashMap()); - } - return myProblemToElements; - } + return myProblemToElements; } @NotNull private Map> getQuickFixActions() { - synchronized (lock) { - if (myQuickFixActions == null) { - myQuickFixActions = Collections.synchronizedMap(new com.intellij.util.containers.HashMap>()); - } - return myQuickFixActions; - } + return myQuickFixActions; } @NotNull private Map getIgnoredElements() { - synchronized (lock) { - if (myIgnoredElements == null) { - myIgnoredElements = Collections.synchronizedMap(new com.intellij.util.containers.HashMap()); - } - return myIgnoredElements; - } + return myIgnoredElements; } @NotNull diff --git a/platform/lang-impl/src/com/intellij/codeInspection/ui/InspectionToolPresentation.java b/platform/lang-impl/src/com/intellij/codeInspection/ui/InspectionToolPresentation.java index 87263fec882c..c7f632575815 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/ui/InspectionToolPresentation.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/ui/InspectionToolPresentation.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -45,6 +45,7 @@ public interface InspectionToolPresentation extends ProblemDescriptionsProcessor boolean hasReportedProblems(); + @NotNull Map> getContent(); Map> getOldContent(); @@ -63,6 +64,7 @@ public interface InspectionToolPresentation extends ProblemDescriptionsProcessor @NotNull HTMLComposerImpl getComposer(); void exportResults(@NotNull final Element parentNode, @NotNull RefEntity refEntity); + @NotNull Set getModuleProblems(); @Nullable QuickFixAction[] getQuickFixes(@NotNull final RefEntity[] refElements); diff --git a/platform/lang-impl/src/com/intellij/codeInspection/ui/actions/ExportHTMLAction.java b/platform/lang-impl/src/com/intellij/codeInspection/ui/actions/ExportHTMLAction.java index 4604de37b915..b633b8de98dc 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/ui/actions/ExportHTMLAction.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/ui/actions/ExportHTMLAction.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,7 +19,10 @@ package com.intellij.codeInspection.ui.actions; import com.intellij.codeEditor.printing.ExportToHTMLSettings; import com.intellij.codeInspection.InspectionApplication; import com.intellij.codeInspection.InspectionsBundle; -import com.intellij.codeInspection.ex.*; +import com.intellij.codeInspection.ex.GlobalInspectionContextImpl; +import com.intellij.codeInspection.ex.InspectionToolWrapper; +import com.intellij.codeInspection.ex.ScopeToolState; +import com.intellij.codeInspection.ex.Tools; import com.intellij.codeInspection.export.ExportToHTMLDialog; import com.intellij.codeInspection.export.HTMLExportFrameMaker; import com.intellij.codeInspection.export.HTMLExportUtil; @@ -247,9 +250,7 @@ public class ExportHTMLAction extends AnAction implements DumbAware { for (InspectionToolWrapper toolWrapper : toolWrappers) { InspectionToolPresentation presentation = myView.getGlobalInspectionContext().getPresentation(toolWrapper); final Map> toolContent = presentation.getContent(); - if (toolContent != null) { - content.putAll(toolContent); - } + content.putAll(toolContent); } final Set defaultPackageEntities = content.remove(null); @@ -285,9 +286,7 @@ public class ExportHTMLAction extends AnAction implements DumbAware { for (InspectionToolWrapper toolWrapper : toolWrappers) { InspectionToolPresentation presentation = myView.getGlobalInspectionContext().getPresentation(toolWrapper); final Set problems = presentation.getModuleProblems(); - if (problems != null) { - modules.addAll(problems); - } + modules.addAll(problems); } final List sortedModules = new ArrayList(modules);