mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Unsafe VFS recursion inspection
This commit is contained in:
+76
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright 2000-2012 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.
|
||||
*/
|
||||
package com.intellij.codeInspection.internal;
|
||||
|
||||
import com.intellij.codeInspection.ProblemsHolder;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Ref;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.psi.util.InheritanceUtil;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class UnsafeVfsRecursionInspection extends InternalInspection {
|
||||
private static final String VIRTUAL_FILE_CLASS_NAME = VirtualFile.class.getName();
|
||||
private static final String GET_CHILDREN_METHOD_NAME = "getChildren";
|
||||
|
||||
private static final String MESSAGE =
|
||||
"VirtualFile.getChildren() is called from a recursive method. " +
|
||||
"This may cause an endless loop on cyclic symlinks. " +
|
||||
"Please use VfsUtilCore.visitChildrenRecursively() instead.";
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public PsiElementVisitor buildInternalVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
|
||||
return new JavaElementVisitor() {
|
||||
@Override
|
||||
public void visitMethodCallExpression(final PsiMethodCallExpression expression) {
|
||||
final Project project = expression.getProject();
|
||||
final JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
|
||||
|
||||
final PsiReferenceExpression methodRef = expression.getMethodExpression();
|
||||
if (!GET_CHILDREN_METHOD_NAME.equals(methodRef.getReferenceName())) return;
|
||||
final PsiElement methodElement = methodRef.resolve();
|
||||
if (!(methodElement instanceof PsiMethod)) return;
|
||||
final PsiMethod method = (PsiMethod)methodElement;
|
||||
final PsiClass aClass = method.getContainingClass();
|
||||
final PsiClass virtualFileClass = facade.findClass(VIRTUAL_FILE_CLASS_NAME, GlobalSearchScope.allScope(project));
|
||||
if (!InheritanceUtil.isInheritorOrSelf(aClass, virtualFileClass, true)) return;
|
||||
|
||||
final PsiMethod containingMethod = PsiTreeUtil.getParentOfType(expression, PsiMethod.class);
|
||||
if (containingMethod == null) return;
|
||||
final String containingMethodName = containingMethod.getName();
|
||||
final Ref<Boolean> result = Ref.create();
|
||||
containingMethod.accept(new JavaRecursiveElementVisitor() {
|
||||
@Override
|
||||
public void visitMethodCallExpression(final PsiMethodCallExpression expression2) {
|
||||
if (expression2 != expression &&
|
||||
containingMethodName.equals(expression2.getMethodExpression().getReferenceName()) &&
|
||||
expression2.resolveMethod() == containingMethod) {
|
||||
result.set(Boolean.TRUE);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (!result.isNull()) {
|
||||
holder.registerProblem(expression, MESSAGE);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -192,7 +192,7 @@ public class VfsUtilCore {
|
||||
}
|
||||
|
||||
if (visitChildren) {
|
||||
VirtualFile[] children = file.getChildren();
|
||||
@SuppressWarnings("UnsafeVfsRecursion") VirtualFile[] children = file.getChildren();
|
||||
for (VirtualFile child : children) {
|
||||
visitChildrenRecursively(child, visitor, visitedSymLinks);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
<html>
|
||||
<body>
|
||||
This inspection detects calls of VirtualFile.getChildren() inside recursive methods.
|
||||
Such a code may cause dead loops when iterating over cyclic symlinks -
|
||||
so please use VfsUtilCore.visitChildrenRecursively() instead.
|
||||
<p><small>Internal inspection - has no effect outside of IntelliJ IDEA project.</small></p>
|
||||
</body>
|
||||
</html>
|
||||
@@ -211,6 +211,9 @@
|
||||
<localInspection language="JAVA" shortName="GtkPreferredJComboBoxRenderer" displayName="Preferred JComboBox renderer"
|
||||
groupName="IDEA Platform Inspections" enabledByDefault="true" level="WARNING"
|
||||
implementationClass="com.intellij.codeInspection.internal.GtkPreferredJComboBoxRendererInspection"/>
|
||||
<localInspection language="JAVA" shortName="UnsafeVfsRecursion" displayName="Unsafe VFS recursion"
|
||||
groupName="IDEA Platform Inspections" enabledByDefault="true" level="WARNING"
|
||||
implementationClass="com.intellij.codeInspection.internal.UnsafeVfsRecursionInspection"/>
|
||||
|
||||
<fileTypeFactory implementation="com.intellij.openapi.fileTypes.impl.JavaFileTypeFactory"/>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user