From 21a30b344d3a082c589e3d9b320cca2fefbf76f8 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Mon, 10 Apr 2017 16:06:22 +0700 Subject: [PATCH] CreateFromUsageUtils: WriteAction/ReadAction.compute used (IDEA-CR-20153) --- .../impl/quickfix/CreateFromUsageUtils.java | 30 ++++++++----------- 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateFromUsageUtils.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateFromUsageUtils.java index 9611625d7d53..f8bd27d96569 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateFromUsageUtils.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateFromUsageUtils.java @@ -30,6 +30,7 @@ import com.intellij.ide.fileTemplates.FileTemplateUtil; import com.intellij.ide.fileTemplates.JavaTemplateUtil; import com.intellij.lang.java.JavaLanguage; import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.application.ReadAction; import com.intellij.openapi.application.WriteAction; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.editor.Editor; @@ -45,7 +46,6 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.DialogWrapper; import com.intellij.openapi.ui.Messages; import com.intellij.openapi.util.Comparing; -import com.intellij.openapi.util.Computable; import com.intellij.openapi.util.Pair; import com.intellij.openapi.util.TextRange; import com.intellij.openapi.util.text.StringUtil; @@ -388,8 +388,7 @@ public class CreateFromUsageUtils { final JavaPsiFacade facade = JavaPsiFacade.getInstance(manager.getProject()); final PsiElementFactory factory = facade.getElementFactory(); - return ApplicationManager.getApplication().runWriteAction( - (Computable)() -> { + return WriteAction.compute(() -> { try { PsiClass targetClass; if (directory != null) { @@ -585,8 +584,8 @@ public class CreateFromUsageUtils { Comparator comparator = expectedTypesComparator; if (expressionList != null) { int argCount = expressionList.getExpressions().length; - Comparator mostSuitableMethodComparator = Comparator - .comparingInt((ExpectedTypeInfo eti) -> eti.getCalledMethod().getParameterList().getParametersCount() == argCount ? 0 : 1); + Comparator mostSuitableMethodComparator = + Comparator.comparingInt(typeInfo -> typeInfo.getCalledMethod().getParameterList().getParametersCount() == argCount ? 0 : 1); comparator = mostSuitableMethodComparator.thenComparing(comparator); } Arrays.sort(someExpectedTypes, comparator); @@ -885,8 +884,7 @@ public class CreateFromUsageUtils { final Module moduleForFile = ModuleUtilCore.findModuleForPsiElement(file); if (moduleForFile == null) return; - final GlobalSearchScope searchScope = - ApplicationManager.getApplication().runReadAction((Computable)file::getResolveScope); + final GlobalSearchScope searchScope = ReadAction.compute(file::getResolveScope); GlobalSearchScope descendantsSearchScope = GlobalSearchScope.moduleWithDependenciesScope(moduleForFile); final JavaPsiFacade facade = JavaPsiFacade.getInstance(project); final PsiShortNamesCache cache = PsiShortNamesCache.getInstance(project); @@ -895,10 +893,8 @@ public class CreateFromUsageUtils { return; } - final PsiMember[] members = ApplicationManager.getApplication().runReadAction( - (Computable)() -> method - ? cache.getMethodsByName(memberName, searchScope) - : cache.getFieldsByName(memberName, searchScope)); + final PsiMember[] members = ReadAction.compute( + () -> method ? cache.getMethodsByName(memberName, searchScope) : cache.getFieldsByName(memberName, searchScope)); for (int i = 0; i < members.length; ++i) { final PsiMember member = members[i]; @@ -924,7 +920,7 @@ public class CreateFromUsageUtils { private static boolean handleObjectMethod(Set possibleClassNames, final JavaPsiFacade facade, final GlobalSearchScope searchScope, final boolean method, final String memberName, final boolean staticAccess, boolean addInheritors) { final PsiShortNamesCache cache = PsiShortNamesCache.getInstance(facade.getProject()); final boolean[] allClasses = {false}; - ApplicationManager.getApplication().runReadAction(() -> { + ReadAction.run(() -> { final PsiClass objectClass = facade.findClass(CommonClassNames.JAVA_LANG_OBJECT, searchScope); if (objectClass != null) { if (method && objectClass.findMethodsByName(memberName, false).length > 0) { @@ -945,10 +941,9 @@ public class CreateFromUsageUtils { return true; } - final String[] strings = ApplicationManager.getApplication().runReadAction((Computable)cache::getAllClassNames); + final String[] strings = ReadAction.compute(cache::getAllClassNames); for (final String className : strings) { - final PsiClass[] classes = ApplicationManager.getApplication().runReadAction( - (Computable)() -> cache.getClassesByName(className, searchScope)); + final PsiClass[] classes = ReadAction.compute(() -> cache.getClassesByName(className, searchScope)); for (final PsiClass aClass : classes) { final String qname = getQualifiedName(aClass); ContainerUtil.addIfNotNull(possibleClassNames, qname); @@ -961,7 +956,7 @@ public class CreateFromUsageUtils { @Nullable private static String getQualifiedName(final PsiClass aClass) { - return ApplicationManager.getApplication().runReadAction((Computable)aClass::getQualifiedName); + return ReadAction.compute(aClass::getQualifiedName); } private static boolean hasCorrectModifiers(@Nullable final PsiMember member, final boolean staticAccess) { @@ -969,8 +964,7 @@ public class CreateFromUsageUtils { return false; } - return ApplicationManager.getApplication().runReadAction( - (Computable)() -> !member.hasModifierProperty(PsiModifier.PRIVATE) && + return ReadAction.compute(() -> !member.hasModifierProperty(PsiModifier.PRIVATE) && member.hasModifierProperty(PsiModifier.STATIC) == staticAccess).booleanValue(); }