Cleanup (annotations; typos; formatting)

This commit is contained in:
Roman Shevchenko
2017-10-25 21:44:29 +02:00
parent 697c2df260
commit 085309a697
4 changed files with 83 additions and 119 deletions
@@ -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<String> TEST5_CONFIG_METHODS = Collections.unmodifiableList(Arrays.asList(BEFORE_EACH_ANNOTATION_NAME,
AFTER_EACH_ANNOTATION_NAME));
public static final Collection<String> 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<String> TEST5_STATIC_CONFIG_METHODS = Collections.unmodifiableList(Arrays.asList(BEFORE_ALL_ANNOTATION_NAME,
AFTER_ALL_ANNOTATION_NAME));
public static final Collection<String> TEST5_STATIC_CONFIG_METHODS = Collections.unmodifiableList(Arrays.asList(
BEFORE_ALL_ANNOTATION_NAME, AFTER_ALL_ANNOTATION_NAME));
private static final Collection<String> TEST_ANNOTATIONS = Collections.unmodifiableList(Arrays.asList(TEST_ANNOTATION,
TEST5_ANNOTATION,
TEST5_FACTORY_ANNOTATION));
public static final Collection<String> TEST5_ANNOTATIONS = Collections.unmodifiableList(Arrays.asList(TEST5_ANNOTATION,
TEST5_FACTORY_ANNOTATION,
CUSTOM_TESTABLE_ANNOTATION));
public static final Collection<String> TEST5_JUPITER_ANNOTATIONS = Collections.unmodifiableList(Arrays.asList(TEST5_ANNOTATION,
TEST5_FACTORY_ANNOTATION));
public static final Collection<String> TEST5_ANNOTATIONS = Collections.unmodifiableList(Arrays.asList(
TEST5_ANNOTATION, TEST5_FACTORY_ANNOTATION, CUSTOM_TESTABLE_ANNOTATION));
public static final Collection<String> TEST5_JUPITER_ANNOTATIONS = Collections.unmodifiableList(Arrays.asList(
TEST5_ANNOTATION, TEST5_FACTORY_ANNOTATION));
private static final List<String> INSTANCE_CONFIGS = Arrays.asList(BEFORE_ANNOTATION_NAME, AFTER_ANNOTATION_NAME);
private static final List<String> INSTANCE_5_CONFIGS = Arrays.asList(BEFORE_EACH_ANNOTATION_NAME, AFTER_EACH_ANNOTATION_NAME);
private static final List<String> STATIC_CONFIGS = Arrays.asList(
BEFORE_CLASS_ANNOTATION_NAME, AFTER_CLASS_ANNOTATION_NAME, PARAMETRIZED_PARAMETERS_ANNOTATION_NAME);
private static final List<String> STATIC_5_CONFIGS = Arrays.asList(BEFORE_ALL_ANNOTATION_NAME, AFTER_ALL_ANNOTATION_NAME);
private static final List<String> STATIC_CONFIGS = Arrays.asList(BEFORE_CLASS_ANNOTATION_NAME, AFTER_CLASS_ANNOTATION_NAME,
PARAMETRIZED_PARAMETERS_ANNOTATION_NAME);
private static final Collection<String> 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<String> 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));
}
}
}
}
@@ -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<PsiClass> HASHING_STRATEGY = new TObjectHashingStrategy<PsiClass>() {
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<PsiClass> getAnnotationTypesWithChildren(@NotNull final Module module,
final String annotationName,
final boolean includeTests) {
final Project project = module.getProject();
public static Collection<PsiClass> getAnnotationTypesWithChildren(@NotNull Module module, String annotationName, boolean includeTests) {
Project project = module.getProject();
Map<Pair<String, Boolean>, Collection<PsiClass>> map = CachedValuesManager.getManager(project).getCachedValue(module, () -> {
Map<Pair<String, Boolean>, Collection<PsiClass>> 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<PsiClass> getChildren(final PsiClass psiClass, final GlobalSearchScope scope) {
if (AnnotationTargetUtil.findAnnotationTarget(psiClass,
PsiAnnotation.TargetType.ANNOTATION_TYPE, PsiAnnotation.TargetType.TYPE) == null) {
public static Set<PsiClass> 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<PsiClass> result = new THashSet<>(HASHING_STRATEGY);
Set<PsiClass> 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<PsiClass> getAnnotatedTypes(final Module module,
final Key<CachedValue<Collection<PsiClass>>> key,
final String annotationName) {
public static Collection<PsiClass> getAnnotatedTypes(@NotNull Module module,
@NotNull Key<CachedValue<Collection<PsiClass>>> 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<PsiClass> classes;
Collection<PsiClass> classes;
if (psiClass == null || !psiClass.isAnnotationType()) {
classes = Collections.emptyList();
}
@@ -115,10 +116,8 @@ public class MetaAnnotationUtil {
@NotNull
private static Collection<PsiClass> getAnnotationTypesWithChildren(PsiClass annotationClass, GlobalSearchScope scope) {
final Set<PsiClass> classes = new THashSet<>(HASHING_STRATEGY);
Set<PsiClass> 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<PsiClass> classes, final GlobalSearchScope scope) {
private static void collectClassWithChildren(PsiClass psiClass, Set<PsiClass> 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<String> annotations) {
public static boolean isMetaAnnotated(@NotNull PsiModifierListOwner listOwner, @NotNull Collection<String> annotations) {
if (AnnotationUtil.isAnnotated(listOwner, annotations, 0)) {
return true;
}
final List<PsiClass> resolvedAnnotations = getResolvedClassesInAnnotationsList(listOwner);
List<PsiClass> 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<String, PsiAnnotation> cachedValue = CachedValuesManager.getCachedValue(subjectAnnotation, () -> {
ConcurrentMap<String, PsiAnnotation>
metaAnnotationsMap = ConcurrentFactoryMap.createMap(anno -> findMetaAnnotation(subjectAnnotation, anno, new HashSet<>()));
return CachedValuesManager.getCachedValue(subjectAnnotation, () -> {
ConcurrentMap<String, PsiAnnotation> 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<PsiClass> visited) {
private static PsiAnnotation findMetaAnnotation(PsiClass aClass, String annotation, Set<PsiClass> visited) {
PsiAnnotation directAnnotation = AnnotationUtil.findAnnotation(aClass, annotation);
if (directAnnotation != null) {
return directAnnotation;
}
List<PsiClass> resolvedAnnotations = getResolvedClassesInAnnotationsList(aClass);
for (PsiClass resolvedAnnotation : resolvedAnnotations) {
if (visited.add(resolvedAnnotation)) {
@@ -195,10 +194,8 @@ public class MetaAnnotationUtil {
return null;
}
@NotNull
public static Stream<PsiAnnotation> findMetaAnnotations(@NotNull PsiModifierListOwner listOwner,
@NotNull final Collection<String> annotations) {
public static Stream<PsiAnnotation> findMetaAnnotations(@NotNull PsiModifierListOwner listOwner, @NotNull Collection<String> annotations) {
Stream<PsiAnnotation> directAnnotations = Stream.of(AnnotationUtil.findAnnotations(listOwner, annotations));
Stream<PsiClass> lazyResolvedAnnotations =
@@ -214,7 +211,6 @@ public class MetaAnnotationUtil {
return Stream.concat(directAnnotations, metaAnnotations);
}
private static List<PsiClass> getResolvedClassesInAnnotationsList(PsiModifierListOwner owner) {
PsiModifierList modifierList = owner.getModifierList();
if (modifierList != null) {
@@ -226,4 +222,4 @@ public class MetaAnnotationUtil {
}
return Collections.emptyList();
}
}
}
@@ -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());
@@ -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();