From 085309a69708fa7487e82ead16430a94b611b04d Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Wed, 25 Oct 2017 21:44:29 +0200 Subject: [PATCH] Cleanup (annotations; typos; formatting) --- .../intellij/execution/junit/JUnitUtil.java | 90 +++++++++---------- .../codeInsight/MetaAnnotationUtil.java | 76 ++++++++-------- ...ctAllInDirectoryConfigurationProducer.java | 18 +--- ...ractAllInPackageConfigurationProducer.java | 18 +--- 4 files changed, 83 insertions(+), 119 deletions(-) diff --git a/java/execution/impl/src/com/intellij/execution/junit/JUnitUtil.java b/java/execution/impl/src/com/intellij/execution/junit/JUnitUtil.java index 500989b0fd23..13d8e7e0f0a6 100644 --- a/java/execution/impl/src/com/intellij/execution/junit/JUnitUtil.java +++ b/java/execution/impl/src/com/intellij/execution/junit/JUnitUtil.java @@ -20,7 +20,6 @@ import com.intellij.psi.util.*; import com.intellij.testIntegration.JavaTestFramework; import com.intellij.testIntegration.TestFramework; import com.siyeh.ig.psiutils.TestUtils; -import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -30,18 +29,18 @@ import static com.intellij.codeInsight.AnnotationUtil.CHECK_HIERARCHY; @SuppressWarnings({"UtilityClassWithoutPrivateConstructor"}) public class JUnitUtil { - @NonNls public static final String TESTCASE_CLASS = "junit.framework.TestCase"; - @NonNls private static final String TEST_INTERFACE = "junit.framework.Test"; - @NonNls private static final String TESTSUITE_CLASS = "junit.framework.TestSuite"; - @NonNls public static final String TEST_ANNOTATION = "org.junit.Test"; - @NonNls public static final String TEST5_PACKAGE_FQN = "org.junit.jupiter.api"; - @NonNls public static final String TEST5_ANNOTATION = "org.junit.jupiter.api.Test"; - @NonNls public static final String CUSTOM_TESTABLE_ANNOTATION = "org.junit.platform.commons.annotation.Testable"; - @NonNls public static final String TEST5_FACTORY_ANNOTATION = "org.junit.jupiter.api.TestFactory"; - @NonNls public static final String IGNORE_ANNOTATION = "org.junit.Ignore"; - @NonNls public static final String RUN_WITH = "org.junit.runner.RunWith"; - @NonNls public static final String DATA_POINT = "org.junit.experimental.theories.DataPoint"; - @NonNls public static final String SUITE_METHOD_NAME = "suite"; + public static final String TEST_CASE_CLASS = "junit.framework.TestCase"; + private static final String TEST_INTERFACE = "junit.framework.Test"; + private static final String TEST_SUITE_CLASS = "junit.framework.TestSuite"; + public static final String TEST_ANNOTATION = "org.junit.Test"; + public static final String TEST5_PACKAGE_FQN = "org.junit.jupiter.api"; + public static final String TEST5_ANNOTATION = "org.junit.jupiter.api.Test"; + public static final String CUSTOM_TESTABLE_ANNOTATION = "org.junit.platform.commons.annotation.Testable"; + public static final String TEST5_FACTORY_ANNOTATION = "org.junit.jupiter.api.TestFactory"; + public static final String IGNORE_ANNOTATION = "org.junit.Ignore"; + public static final String RUN_WITH = "org.junit.runner.RunWith"; + public static final String DATA_POINT = "org.junit.experimental.theories.DataPoint"; + public static final String SUITE_METHOD_NAME = "suite"; public static final String BEFORE_ANNOTATION_NAME = "org.junit.Before"; public static final String AFTER_ANNOTATION_NAME = "org.junit.After"; @@ -54,51 +53,48 @@ public class JUnitUtil { public static final String AFTER_CLASS_ANNOTATION_NAME = "org.junit.AfterClass"; public static final String BEFORE_CLASS_ANNOTATION_NAME = "org.junit.BeforeClass"; - public static final Collection TEST5_CONFIG_METHODS = Collections.unmodifiableList(Arrays.asList(BEFORE_EACH_ANNOTATION_NAME, - AFTER_EACH_ANNOTATION_NAME)); + public static final Collection TEST5_CONFIG_METHODS = Collections.unmodifiableList(Arrays.asList( + BEFORE_EACH_ANNOTATION_NAME, AFTER_EACH_ANNOTATION_NAME)); public static final String BEFORE_ALL_ANNOTATION_NAME = "org.junit.jupiter.api.BeforeAll"; public static final String AFTER_ALL_ANNOTATION_NAME = "org.junit.jupiter.api.AfterAll"; - public static final Collection TEST5_STATIC_CONFIG_METHODS = Collections.unmodifiableList(Arrays.asList(BEFORE_ALL_ANNOTATION_NAME, - AFTER_ALL_ANNOTATION_NAME)); + public static final Collection TEST5_STATIC_CONFIG_METHODS = Collections.unmodifiableList(Arrays.asList( + BEFORE_ALL_ANNOTATION_NAME, AFTER_ALL_ANNOTATION_NAME)); - private static final Collection TEST_ANNOTATIONS = Collections.unmodifiableList(Arrays.asList(TEST_ANNOTATION, - TEST5_ANNOTATION, - TEST5_FACTORY_ANNOTATION)); - public static final Collection TEST5_ANNOTATIONS = Collections.unmodifiableList(Arrays.asList(TEST5_ANNOTATION, - TEST5_FACTORY_ANNOTATION, - CUSTOM_TESTABLE_ANNOTATION)); - public static final Collection TEST5_JUPITER_ANNOTATIONS = Collections.unmodifiableList(Arrays.asList(TEST5_ANNOTATION, - TEST5_FACTORY_ANNOTATION)); + public static final Collection TEST5_ANNOTATIONS = Collections.unmodifiableList(Arrays.asList( + TEST5_ANNOTATION, TEST5_FACTORY_ANNOTATION, CUSTOM_TESTABLE_ANNOTATION)); + public static final Collection TEST5_JUPITER_ANNOTATIONS = Collections.unmodifiableList(Arrays.asList( + TEST5_ANNOTATION, TEST5_FACTORY_ANNOTATION)); private static final List INSTANCE_CONFIGS = Arrays.asList(BEFORE_ANNOTATION_NAME, AFTER_ANNOTATION_NAME); private static final List INSTANCE_5_CONFIGS = Arrays.asList(BEFORE_EACH_ANNOTATION_NAME, AFTER_EACH_ANNOTATION_NAME); + private static final List STATIC_CONFIGS = Arrays.asList( + BEFORE_CLASS_ANNOTATION_NAME, AFTER_CLASS_ANNOTATION_NAME, PARAMETRIZED_PARAMETERS_ANNOTATION_NAME); private static final List STATIC_5_CONFIGS = Arrays.asList(BEFORE_ALL_ANNOTATION_NAME, AFTER_ALL_ANNOTATION_NAME); - private static final List STATIC_CONFIGS = Arrays.asList(BEFORE_CLASS_ANNOTATION_NAME, AFTER_CLASS_ANNOTATION_NAME, - PARAMETRIZED_PARAMETERS_ANNOTATION_NAME); - private static final Collection CONFIGURATIONS_ANNOTATION_NAME = Collections.unmodifiableList( - Arrays.asList(DATA_POINT, AFTER_ANNOTATION_NAME, BEFORE_ANNOTATION_NAME, AFTER_CLASS_ANNOTATION_NAME, BEFORE_CLASS_ANNOTATION_NAME, - BEFORE_ALL_ANNOTATION_NAME, AFTER_ALL_ANNOTATION_NAME)); + private static final Collection CONFIGURATIONS_ANNOTATION_NAME = Collections.unmodifiableList(Arrays.asList( + DATA_POINT, AFTER_ANNOTATION_NAME, BEFORE_ANNOTATION_NAME, AFTER_CLASS_ANNOTATION_NAME, BEFORE_CLASS_ANNOTATION_NAME, + BEFORE_ALL_ANNOTATION_NAME, AFTER_ALL_ANNOTATION_NAME)); - @NonNls public static final String PARAMETERIZED_CLASS_NAME = "org.junit.runners.Parameterized"; - @NonNls public static final String SUITE_CLASS_NAME = "org.junit.runners.Suite"; + public static final String PARAMETERIZED_CLASS_NAME = "org.junit.runners.Parameterized"; + public static final String SUITE_CLASS_NAME = "org.junit.runners.Suite"; public static final String JUNIT5_NESTED = "org.junit.jupiter.api.Nested"; + private static final String[] RUNNERS_UNAWARE_OF_INNER_CLASSES = { - "org.junit.runners.Parameterized", - "org.junit.runners.BlockJUnit4ClassRunner", - "org.junit.runners.JUnit4", - "org.junit.internal.runners.JUnit38ClassRunner", - "org.junit.internal.runners.JUnit4ClassRunner", - "org.junit.runners.Suite" + "org.junit.runners.Parameterized", + "org.junit.runners.BlockJUnit4ClassRunner", + "org.junit.runners.JUnit4", + "org.junit.internal.runners.JUnit38ClassRunner", + "org.junit.internal.runners.JUnit4ClassRunner", + "org.junit.runners.Suite" }; private static final String[] RUNNERS_REQUIRE_ANNOTATION_ON_TEST_METHOD = { - "org.junit.runners.Parameterized", - "org.junit.runners.BlockJUnit4ClassRunner", - "org.junit.runners.JUnit4", - "org.junit.internal.runners.JUnit4ClassRunner" + "org.junit.runners.Parameterized", + "org.junit.runners.BlockJUnit4ClassRunner", + "org.junit.runners.JUnit4", + "org.junit.internal.runners.JUnit4ClassRunner" }; public static boolean isSuiteMethod(@NotNull PsiMethod psiMethod) { @@ -108,8 +104,8 @@ public class JUnitUtil { if (psiMethod.getParameterList().getParametersCount() > 0) return false; final PsiType returnType = psiMethod.getReturnType(); if (returnType == null || returnType instanceof PsiPrimitiveType) return false; - return returnType.equalsToText(TEST_INTERFACE)|| - returnType.equalsToText(TESTSUITE_CLASS) || + return returnType.equalsToText(TEST_INTERFACE) || + returnType.equalsToText(TEST_SUITE_CLASS) || InheritanceUtil.isInheritor(returnType, TEST_INTERFACE); } @@ -179,6 +175,7 @@ public class JUnitUtil { } } } + if (!PsiClassUtil.isRunnableClass(psiClass, true, checkAbstract)) return false; if (AnnotationUtil.isAnnotated(psiClass, RUN_WITH, CHECK_HIERARCHY)) return true; @@ -344,7 +341,7 @@ public class JUnitUtil { @Nullable private static PsiClass getTestCaseClassOrNull(final GlobalSearchScope scope, final Project project) { - return JavaPsiFacade.getInstance(project).findClass(TESTCASE_CLASS, scope); + return JavaPsiFacade.getInstance(project).findClass(TEST_CASE_CLASS, scope); } public static boolean isTestMethodOrConfig(@NotNull PsiMethod psiMethod) { @@ -353,7 +350,6 @@ public class JUnitUtil { return false; } if (isTestMethod(PsiLocation.fromPsiElement(psiMethod), false)) { - assert containingClass != null : psiMethod + "; " + psiMethod.getClass() + "; " + psiMethod.getParent(); if (containingClass.hasModifierProperty(PsiModifier.ABSTRACT)) { final boolean[] foundNonAbstractInheritor = new boolean[1]; ClassInheritorsSearch.search(containingClass).forEach(psiClass -> { @@ -511,4 +507,4 @@ public class JUnitUtil { super(ExecutionBundle.message("no.junit.in.scope.error.message", message)); } } -} +} \ No newline at end of file diff --git a/java/openapi/src/com/intellij/codeInsight/MetaAnnotationUtil.java b/java/openapi/src/com/intellij/codeInsight/MetaAnnotationUtil.java index 3bcbf5273be5..823a17430e0e 100644 --- a/java/openapi/src/com/intellij/codeInsight/MetaAnnotationUtil.java +++ b/java/openapi/src/com/intellij/codeInsight/MetaAnnotationUtil.java @@ -35,25 +35,27 @@ import java.util.*; import java.util.concurrent.ConcurrentMap; import java.util.stream.Stream; +import static com.intellij.openapi.util.Pair.pair; + /** * @since 2016.3 */ public class MetaAnnotationUtil { private static final TObjectHashingStrategy HASHING_STRATEGY = new TObjectHashingStrategy() { - public int computeHashCode(final PsiClass object) { - final String qualifiedName = object.getQualifiedName(); + @Override + public int computeHashCode(PsiClass object) { + String qualifiedName = object.getQualifiedName(); return qualifiedName == null ? 0 : qualifiedName.hashCode(); } - public boolean equals(final PsiClass o1, final PsiClass o2) { + @Override + public boolean equals(PsiClass o1, PsiClass o2) { return Comparing.equal(o1.getQualifiedName(), o2.getQualifiedName()); } }; - public static Collection getAnnotationTypesWithChildren(@NotNull final Module module, - final String annotationName, - final boolean includeTests) { - final Project project = module.getProject(); + public static Collection getAnnotationTypesWithChildren(@NotNull Module module, String annotationName, boolean includeTests) { + Project project = module.getProject(); Map, Collection> map = CachedValuesManager.getManager(project).getCachedValue(module, () -> { Map, Collection> factoryMap = ConcurrentFactoryMap.createMap(key -> { @@ -71,19 +73,18 @@ public class MetaAnnotationUtil { return CachedValueProvider.Result.create(factoryMap, PsiModificationTracker.JAVA_STRUCTURE_MODIFICATION_COUNT); }); - return map.get(Pair.create(annotationName, includeTests)); + return map.get(pair(annotationName, includeTests)); } - public static Set getChildren(final PsiClass psiClass, final GlobalSearchScope scope) { - if (AnnotationTargetUtil.findAnnotationTarget(psiClass, - PsiAnnotation.TargetType.ANNOTATION_TYPE, PsiAnnotation.TargetType.TYPE) == null) { + public static Set getChildren(@NotNull PsiClass psiClass, @NotNull GlobalSearchScope scope) { + if (AnnotationTargetUtil.findAnnotationTarget(psiClass, PsiAnnotation.TargetType.ANNOTATION_TYPE, PsiAnnotation.TargetType.TYPE) == null) { return Collections.emptySet(); } - final String name = psiClass.getQualifiedName(); + String name = psiClass.getQualifiedName(); if (name == null) return Collections.emptySet(); - final Set result = new THashSet<>(HASHING_STRATEGY); + Set result = new THashSet<>(HASHING_STRATEGY); AnnotatedElementsSearch.searchPsiClasses(psiClass, scope).forEach(processorResult -> { if (processorResult.isAnnotationType()) { @@ -95,14 +96,14 @@ public class MetaAnnotationUtil { return result; } - public static Collection getAnnotatedTypes(final Module module, - final Key>> key, - final String annotationName) { + public static Collection getAnnotatedTypes(@NotNull Module module, + @NotNull Key>> key, + @NotNull String annotationName) { return CachedValuesManager.getManager(module.getProject()).getCachedValue(module, key, () -> { - final GlobalSearchScope scope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module, false); - final PsiClass psiClass = JavaPsiFacade.getInstance(module.getProject()).findClass(annotationName, scope); + GlobalSearchScope scope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module, false); + PsiClass psiClass = JavaPsiFacade.getInstance(module.getProject()).findClass(annotationName, scope); - final Collection classes; + Collection classes; if (psiClass == null || !psiClass.isAnnotationType()) { classes = Collections.emptyList(); } @@ -115,10 +116,8 @@ public class MetaAnnotationUtil { @NotNull private static Collection getAnnotationTypesWithChildren(PsiClass annotationClass, GlobalSearchScope scope) { - final Set classes = new THashSet<>(HASHING_STRATEGY); - + Set classes = new THashSet<>(HASHING_STRATEGY); collectClassWithChildren(annotationClass, classes, scope); - return classes; } @@ -132,13 +131,13 @@ public class MetaAnnotationUtil { return true; }); } - return CachedValueProvider.Result - .createSingleDependency(GlobalSearchScope.filesWithLibrariesScope(project, allAnnotationFiles), - PsiModificationTracker.JAVA_STRUCTURE_MODIFICATION_COUNT); + + scope = GlobalSearchScope.filesWithLibrariesScope(project, allAnnotationFiles); + return CachedValueProvider.Result.createSingleDependency(scope, PsiModificationTracker.JAVA_STRUCTURE_MODIFICATION_COUNT); }); } - private static void collectClassWithChildren(final PsiClass psiClass, final Set classes, final GlobalSearchScope scope) { + private static void collectClassWithChildren(PsiClass psiClass, Set classes, GlobalSearchScope scope) { classes.add(psiClass); for (PsiClass aClass : getChildren(psiClass, scope)) { @@ -149,14 +148,14 @@ public class MetaAnnotationUtil { } /** - * Check if listOwner is annotated with annotations or listOwner's annotations contain given annotations + * Checks if listOwner is annotated with annotations or listOwner's annotations contain given annotations. */ - public static boolean isMetaAnnotated(@NotNull PsiModifierListOwner listOwner, @NotNull final Collection annotations) { + public static boolean isMetaAnnotated(@NotNull PsiModifierListOwner listOwner, @NotNull Collection annotations) { if (AnnotationUtil.isAnnotated(listOwner, annotations, 0)) { return true; } - final List resolvedAnnotations = getResolvedClassesInAnnotationsList(listOwner); + List resolvedAnnotations = getResolvedClassesInAnnotationsList(listOwner); for (String annotationFQN : annotations) { for (PsiClass resolvedAnnotation : resolvedAnnotations) { if (metaAnnotationCached(resolvedAnnotation, annotationFQN) != null) return true; @@ -168,20 +167,20 @@ public class MetaAnnotationUtil { @Nullable private static PsiAnnotation metaAnnotationCached(PsiClass subjectAnnotation, String annotationToFind) { - ConcurrentMap cachedValue = CachedValuesManager.getCachedValue(subjectAnnotation, () -> { - ConcurrentMap - metaAnnotationsMap = ConcurrentFactoryMap.createMap(anno -> findMetaAnnotation(subjectAnnotation, anno, new HashSet<>())); + return CachedValuesManager.getCachedValue(subjectAnnotation, () -> { + ConcurrentMap metaAnnotationsMap = ConcurrentFactoryMap.createMap( + anno -> findMetaAnnotation(subjectAnnotation, anno, new HashSet<>())); return new CachedValueProvider.Result<>(metaAnnotationsMap, PsiModificationTracker.JAVA_STRUCTURE_MODIFICATION_COUNT); - }); - return cachedValue.get(annotationToFind); + }).get(annotationToFind); } @Nullable - private static PsiAnnotation findMetaAnnotation(PsiClass aClass, final String annotation, final Set visited) { + private static PsiAnnotation findMetaAnnotation(PsiClass aClass, String annotation, Set visited) { PsiAnnotation directAnnotation = AnnotationUtil.findAnnotation(aClass, annotation); if (directAnnotation != null) { return directAnnotation; } + List resolvedAnnotations = getResolvedClassesInAnnotationsList(aClass); for (PsiClass resolvedAnnotation : resolvedAnnotations) { if (visited.add(resolvedAnnotation)) { @@ -195,10 +194,8 @@ public class MetaAnnotationUtil { return null; } - @NotNull - public static Stream findMetaAnnotations(@NotNull PsiModifierListOwner listOwner, - @NotNull final Collection annotations) { + public static Stream findMetaAnnotations(@NotNull PsiModifierListOwner listOwner, @NotNull Collection annotations) { Stream directAnnotations = Stream.of(AnnotationUtil.findAnnotations(listOwner, annotations)); Stream lazyResolvedAnnotations = @@ -214,7 +211,6 @@ public class MetaAnnotationUtil { return Stream.concat(directAnnotations, metaAnnotations); } - private static List getResolvedClassesInAnnotationsList(PsiModifierListOwner owner) { PsiModifierList modifierList = owner.getModifierList(); if (modifierList != null) { @@ -226,4 +222,4 @@ public class MetaAnnotationUtil { } return Collections.emptyList(); } -} +} \ No newline at end of file diff --git a/plugins/junit/src/com/intellij/execution/junit/AbstractAllInDirectoryConfigurationProducer.java b/plugins/junit/src/com/intellij/execution/junit/AbstractAllInDirectoryConfigurationProducer.java index a4967379a675..59d70201b352 100644 --- a/plugins/junit/src/com/intellij/execution/junit/AbstractAllInDirectoryConfigurationProducer.java +++ b/plugins/junit/src/com/intellij/execution/junit/AbstractAllInDirectoryConfigurationProducer.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2013 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.execution.junit; @@ -52,7 +38,7 @@ public abstract class AbstractAllInDirectoryConfigurationProducer extends JUnitC if (!ModuleRootManager.getInstance(module).getFileIndex().isInTestSourceContent(virtualFile)) return false; int testRootCount = ModuleRootManager.getInstance(module).getSourceRoots(JavaSourceRootType.TEST_SOURCE).size(); if (testRootCount < 2) return false; - if (!LocationUtil.isJarAttached(context.getLocation(), aPackage, JUnitUtil.TESTCASE_CLASS)) return false; + if (!LocationUtil.isJarAttached(context.getLocation(), aPackage, JUnitUtil.TEST_CASE_CLASS)) return false; setupConfigurationModule(context, configuration); final JUnitConfiguration.Data data = configuration.getPersistentData(); data.setDirName(virtualFile.getPath()); diff --git a/plugins/junit/src/com/intellij/execution/junit/AbstractAllInPackageConfigurationProducer.java b/plugins/junit/src/com/intellij/execution/junit/AbstractAllInPackageConfigurationProducer.java index 7acc17cc9ba3..690eec10b812 100644 --- a/plugins/junit/src/com/intellij/execution/junit/AbstractAllInPackageConfigurationProducer.java +++ b/plugins/junit/src/com/intellij/execution/junit/AbstractAllInPackageConfigurationProducer.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2009 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.execution.junit; @@ -39,7 +25,7 @@ public abstract class AbstractAllInPackageConfigurationProducer extends JUnitCon PsiPackage psiPackage = JavaRuntimeConfigurationProducerBase.checkPackage(context.getPsiLocation()); if (psiPackage == null) return false; sourceElement.set(psiPackage); - if (!LocationUtil.isJarAttached(context.getLocation(), psiPackage, JUnitUtil.TESTCASE_CLASS, JUnitUtil.TEST5_ANNOTATION, + if (!LocationUtil.isJarAttached(context.getLocation(), psiPackage, JUnitUtil.TEST_CASE_CLASS, JUnitUtil.TEST5_ANNOTATION, JUnitCommonClassNames.ORG_JUNIT_PLATFORM_ENGINE_TEST_ENGINE)) return false; final JUnitConfiguration.Data data = configuration.getPersistentData(); data.PACKAGE_NAME = psiPackage.getQualifiedName();