This commit is contained in:
Alexey Kudravtsev
2018-06-20 12:46:58 +03:00
parent 567d0c52c5
commit 8db87053b7
7 changed files with 45 additions and 43 deletions
@@ -5,10 +5,7 @@ import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Comparing;
import com.intellij.psi.*;
import com.intellij.psi.util.*;
import com.intellij.util.ArrayUtil;
import com.intellij.util.Consumer;
import com.intellij.util.Processor;
import com.intellij.util.Processors;
import com.intellij.util.*;
import com.intellij.util.containers.ConcurrentFactoryMap;
import com.intellij.util.containers.ContainerUtil;
import gnu.trove.THashMap;
@@ -24,8 +21,6 @@ import java.lang.annotation.Target;
import java.lang.reflect.Proxy;
import java.util.*;
import static com.intellij.util.BitUtil.isSet;
/**
* @author max
*/
@@ -76,7 +71,7 @@ public class AnnotationUtil {
return skipExternal ? null : findNonCodeAnnotation(listOwner, annotationNames);
}
private static PsiAnnotation findOwnAnnotation(final PsiModifierListOwner listOwner, Collection<String> annotationNames) {
private static PsiAnnotation findOwnAnnotation(@NotNull final PsiModifierListOwner listOwner, @NotNull Collection<String> annotationNames) {
final PsiModifierList list = listOwner.getModifierList();
if (list == null) return null;
for (PsiAnnotation annotation : list.getAnnotations()) {
@@ -87,7 +82,7 @@ public class AnnotationUtil {
return null;
}
private static PsiAnnotation findNonCodeAnnotation(final PsiModifierListOwner listOwner, Collection<String> annotationNames) {
private static PsiAnnotation findNonCodeAnnotation(@NotNull PsiModifierListOwner listOwner, @NotNull Collection<String> annotationNames) {
if (listOwner instanceof PsiLocalVariable) {
// Non-code annotations for local variables are not supported: don't bother to search them
return null;
@@ -137,6 +132,7 @@ public class AnnotationUtil {
return result == null ? PsiAnnotation.EMPTY_ARRAY : result.toArray(PsiAnnotation.EMPTY_ARRAY);
}
@NotNull
public static <T extends PsiModifierListOwner> List<T> getSuperAnnotationOwners(@NotNull T element) {
return CachedValuesManager.getCachedValue(element, () -> {
Set<PsiModifierListOwner> result = ContainerUtil.newLinkedHashSet();
@@ -155,7 +151,8 @@ public class AnnotationUtil {
List<T> list;
if(result.isEmpty()) {
list = Collections.emptyList();
} else {
}
else {
PsiModifierListOwner[] array = result.toArray(new PsiModifierListOwner[0]);
//noinspection unchecked
list = Arrays.asList((T[])array);
@@ -179,7 +176,7 @@ public class AnnotationUtil {
return null;
}
private static void collectSuperParameters(@NotNull final Set<PsiModifierListOwner> result, @NotNull PsiParameter parameter) {
private static void collectSuperParameters(@NotNull final Set<? super PsiModifierListOwner> result, @NotNull PsiParameter parameter) {
PsiElement parent = parameter.getParent();
if (!(parent instanceof PsiParameterList)) {
return;
@@ -209,7 +206,7 @@ public class AnnotationUtil {
}
}
private static void collectSuperMethods(@NotNull Set<PsiModifierListOwner> result,
private static void collectSuperMethods(@NotNull Set<? super PsiModifierListOwner> result,
@NotNull HierarchicalMethodSignature signature,
@NotNull PsiElement place,
@NotNull PsiResolveHelper resolveHelper) {
@@ -238,7 +235,7 @@ public class AnnotationUtil {
return isAnnotated(listOwner, annotationFqn, flags, null);
}
private static boolean isAnnotated(PsiModifierListOwner listOwner, String annotationFQN, int flags, @Nullable Set<PsiMember> processed) {
private static boolean isAnnotated(@NotNull PsiModifierListOwner listOwner, @NotNull String annotationFQN, @Flags int flags, @Nullable Set<PsiMember> processed) {
if (!listOwner.isValid()) return false;
PsiModifierList modifierList = listOwner.getModifierList();
@@ -247,7 +244,7 @@ public class AnnotationUtil {
PsiAnnotation annotation = modifierList.findAnnotation(annotationFQN);
if (annotation != null) return true;
if (isSet(flags, CHECK_TYPE)) {
if (BitUtil.isSet(flags, CHECK_TYPE)) {
PsiType type = null;
if (listOwner instanceof PsiMethod) {
type = ((PsiMethod)listOwner).getReturnType();
@@ -260,21 +257,21 @@ public class AnnotationUtil {
}
}
if (isSet(flags, CHECK_EXTERNAL)) {
if (BitUtil.isSet(flags, CHECK_EXTERNAL)) {
Project project = listOwner.getProject();
if (ExternalAnnotationsManager.getInstance(project).findExternalAnnotation(listOwner, annotationFQN) != null) {
return true;
}
}
if (isSet(flags, CHECK_INFERRED)) {
if (BitUtil.isSet(flags, CHECK_INFERRED)) {
Project project = listOwner.getProject();
if (InferredAnnotationsManager.getInstance(project).findInferredAnnotation(listOwner, annotationFQN) != null) {
return true;
}
}
if (isSet(flags, CHECK_HIERARCHY)) {
if (BitUtil.isSet(flags, CHECK_HIERARCHY)) {
if (listOwner instanceof PsiMethod) {
PsiMethod method = (PsiMethod)listOwner;
if (processed == null) processed = new THashSet<>();
@@ -449,7 +446,7 @@ public class AnnotationUtil {
return annotations;
}
public static boolean isInsideAnnotation(PsiElement element) {
public static boolean isInsideAnnotation(@NotNull PsiElement element) {
for (int level = 0; level<4; level++) {
if (element instanceof PsiNameValuePair) return true;
element = element.getParent();
@@ -545,7 +542,7 @@ public class AnnotationUtil {
return true;
}
private static boolean fillValueMap(PsiAnnotationParameterList parameterList, Map<String, PsiAnnotationMemberValue> valueMap) {
private static boolean fillValueMap(@NotNull PsiAnnotationParameterList parameterList, @NotNull Map<String, PsiAnnotationMemberValue> valueMap) {
final PsiNameValuePair[] attributes1 = parameterList.getAttributes();
for (PsiNameValuePair attribute : attributes1) {
final PsiReference reference = attribute.getReference();
@@ -568,7 +565,7 @@ public class AnnotationUtil {
return true;
}
public static boolean equal(PsiAnnotationMemberValue value1, PsiAnnotationMemberValue value2) {
public static boolean equal(@Nullable PsiAnnotationMemberValue value1, @Nullable PsiAnnotationMemberValue value2) {
if (value1 instanceof PsiArrayInitializerMemberValue && value2 instanceof PsiArrayInitializerMemberValue) {
final PsiAnnotationMemberValue[] initializers1 = ((PsiArrayInitializerMemberValue)value1).getInitializers();
final PsiAnnotationMemberValue[] initializers2 = ((PsiArrayInitializerMemberValue)value2).getInitializers();
@@ -595,7 +592,7 @@ public class AnnotationUtil {
/**
* Ignores Override and SuppressWarnings annotations.
*/
public static boolean equal(PsiAnnotation[] annotations1, PsiAnnotation[] annotations2) {
public static boolean equal(@NotNull PsiAnnotation[] annotations1, @NotNull PsiAnnotation[] annotations2) {
final Map<String, PsiAnnotation> map1 = buildAnnotationMap(annotations1);
final Map<String, PsiAnnotation> map2 = buildAnnotationMap(annotations2);
if (map1.size() != map2.size()) {
@@ -609,7 +606,8 @@ public class AnnotationUtil {
return true;
}
private static Map<String, PsiAnnotation> buildAnnotationMap(PsiAnnotation[] annotations) {
@NotNull
private static Map<String, PsiAnnotation> buildAnnotationMap(@NotNull PsiAnnotation[] annotations) {
final Map<String, PsiAnnotation> map = new HashMap<>();
for (PsiAnnotation annotation : annotations) {
map.put(annotation.getQualifiedName(), annotation);
@@ -667,7 +665,8 @@ public class AnnotationUtil {
return isAnnotated(listOwner, annotationFQN, flags(checkHierarchy, skipExternal, skipExternal));
}
private static @Flags int flags(boolean checkHierarchy, boolean skipExternal, boolean skipInferred) {
@Flags
private static int flags(boolean checkHierarchy, boolean skipExternal, boolean skipInferred) {
int flags = CHECK_TYPE;
if (checkHierarchy) flags |= CHECK_HIERARCHY;
if (!skipExternal) flags |= CHECK_EXTERNAL;
@@ -41,14 +41,11 @@ public class ID<K, V> extends IndexId<K,V> {
try {
TObjectIntHashMap<String> nameToIdRegistry = new TObjectIntHashMap<>();
try (BufferedReader reader = new BufferedReader(new FileReader(indices))) {
int cnt = 0;
do {
cnt++;
for (int cnt = 1; ; cnt++) {
final String name = reader.readLine();
if (name == null) break;
nameToIdRegistry.put(name, cnt);
}
while (true);
}
synchronized (ourNameToIdRegistry) {
@@ -67,12 +64,13 @@ public class ID<K, V> extends IndexId<K,V> {
}
}
@NotNull
private static File getEnumFile() {
final File indexFolder = PathManager.getIndexRoot();
return new File(indexFolder, "indices.enum");
}
protected ID(String name) {
protected ID(@NotNull String name) {
super(name);
myUniqueId = stringToId(name);
@@ -80,14 +78,14 @@ public class ID<K, V> extends IndexId<K,V> {
assert old == null : "ID with name '" + name + "' is already registered";
}
private static short stringToId(String name) {
private static short stringToId(@NotNull String name) {
synchronized (ourNameToIdRegistry) {
if (ourNameToIdRegistry.containsKey(name)) {
return (short)ourNameToIdRegistry.get(name);
}
int n = ourNameToIdRegistry.size() + 1;
assert n <= MAX_NUMBER_OF_INDICES : "Number of indices exceeded";
assert n <= MAX_NUMBER_OF_INDICES : "Number of indices exceeded: "+n;
ourNameToIdRegistry.put(name, n);
writeEnumFile();
@@ -95,7 +93,7 @@ public class ID<K, V> extends IndexId<K,V> {
}
}
public static void reinitializeDiskStorage() {
static void reinitializeDiskStorage() {
synchronized (ourNameToIdRegistry) {
writeEnumFile();
}
@@ -126,7 +124,7 @@ public class ID<K, V> extends IndexId<K,V> {
@NotNull
public static <K, V> ID<K, V> create(@NonNls @NotNull String name) {
final ID<K, V> found = findByName(name);
return found != null ? found : new ID<>(name);
return found == null ? new ID<>(name) : found;
}
@Nullable
@@ -134,13 +132,15 @@ public class ID<K, V> extends IndexId<K,V> {
return (ID<K, V>)findById(stringToId(name));
}
@Override
public int hashCode() {
return (int)myUniqueId;
return myUniqueId;
}
/**
* Consider to use {@link ID#getName()} instead of this method
*/
@Override
public String toString() {
return getName();
}
@@ -47,24 +47,24 @@ public class SharedImplUtil {
return SourceTreeToPsiMap.treeElementToPsi(thisElement.getTreeParent());
}
public static PsiElement getFirstChild(ASTNode element) {
public static PsiElement getFirstChild(@NotNull ASTNode element) {
return SourceTreeToPsiMap.treeElementToPsi(element.getFirstChildNode());
}
@Nullable
public static PsiElement getLastChild(ASTNode element) {
public static PsiElement getLastChild(@NotNull ASTNode element) {
return SourceTreeToPsiMap.treeElementToPsi(element.getLastChildNode());
}
public static PsiElement getNextSibling(ASTNode thisElement) {
public static PsiElement getNextSibling(@NotNull ASTNode thisElement) {
return SourceTreeToPsiMap.treeElementToPsi(thisElement.getTreeNext());
}
public static PsiElement getPrevSibling(ASTNode thisElement) {
public static PsiElement getPrevSibling(@NotNull ASTNode thisElement) {
return SourceTreeToPsiMap.treeElementToPsi(thisElement.getTreePrev());
}
public static PsiFile getContainingFile(ASTNode thisElement) {
public static PsiFile getContainingFile(@NotNull ASTNode thisElement) {
FileASTNode node = findFileElement(thisElement);
PsiElement psi = node == null ? null : node.getPsi();
if (psi == null || psi instanceof PsiFile) return (PsiFile)psi;
@@ -186,7 +186,7 @@ public class SharedImplUtil {
return count;
}
public static void acceptChildren(PsiElementVisitor visitor, ASTNode root) {
public static void acceptChildren(@NotNull PsiElementVisitor visitor, @NotNull ASTNode root) {
ASTNode childNode = root.getFirstChildNode();
while (childNode != null) {
@@ -203,7 +203,7 @@ public class SharedImplUtil {
}
}
public static PsiElement doReplace(PsiElement psiElement, TreeElement treeElement, PsiElement newElement) {
public static PsiElement doReplace(@NotNull PsiElement psiElement, @NotNull TreeElement treeElement, @NotNull PsiElement newElement) {
CompositeElement treeParent = treeElement.getTreeParent();
LOG.assertTrue(treeParent != null);
CheckUtil.checkWritable(psiElement);
@@ -132,10 +132,10 @@ public class FindInProjectUtil {
VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByPath(path);
if (virtualFile == null || !virtualFile.isDirectory()) {
virtualFile = null;
@SuppressWarnings("deprecation") VirtualFileSystem[] fileSystems = ApplicationManager.getApplication().getComponents(VirtualFileSystem.class);
VirtualFileSystem[] fileSystems = ApplicationManager.getApplication().getComponents(VirtualFileSystem.class);
for (VirtualFileSystem fs : fileSystems) {
if (fs instanceof LocalFileProvider) {
@SuppressWarnings("deprecation") VirtualFile file = ((LocalFileProvider)fs).findLocalVirtualFileByPath(path);
VirtualFile file = ((LocalFileProvider)fs).findLocalVirtualFileByPath(path);
if (file != null && file.isDirectory()) {
if (file.getChildren().length > 0) {
virtualFile = file;
@@ -249,7 +249,7 @@ public class FindInProjectUtil {
@NotNull final PsiFile psiFile,
@NotNull int[] offsetRef,
int maxUsages,
@NotNull Processor<UsageInfo> consumer) {
@NotNull Processor<? super UsageInfo> consumer) {
int count = 0;
CharSequence text = document.getCharsSequence();
int textLength = document.getTextLength();
@@ -532,7 +532,7 @@ public class FindInProjectUtil {
private static void addSourceDirectoriesFromLibraries(@NotNull Project project,
@NotNull VirtualFile directory,
@NotNull Collection<VirtualFile> outSourceRoots) {
@NotNull Collection<? super VirtualFile> outSourceRoots) {
ProjectFileIndex index = ProjectFileIndex.SERVICE.getInstance(project);
// if we already are in the sources, search just in this directory only
if (!index.isInLibraryClasses(directory)) return;
@@ -44,6 +44,7 @@ public class PropertyListStubElementType extends ILightStubElementType<Propertie
return new PropertiesListStubImpl(parentStub);
}
@Override
@NotNull
public String getExternalId() {
return "properties.propertieslist";
@@ -48,6 +48,7 @@ public class PropertyStubElementType extends ILightStubElementType<PropertyStub,
return new PropertyStubImpl(parentStub, psi.getKey());
}
@Override
@NotNull
public String getExternalId() {
return "properties.prop";
@@ -43,6 +43,7 @@ public abstract class PyStubElementType<StubT extends StubElement, PsiT extends
public void indexStub(@NotNull final StubT stub, @NotNull final IndexSink sink) {
}
@Override
@NotNull
public String getExternalId() {
return "py." + super.toString();