mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-18 09:34:34 +07:00
IDEA-138521 Infer contracts for anonymous class methods resolved to from outside
This commit is contained in:
+12
-2
@@ -44,8 +44,18 @@ public class InferenceFromSourceUtil {
|
||||
|
||||
private static boolean isUnusedInAnonymousClass(@NotNull PsiMethod method) {
|
||||
PsiClass containingClass = method.getContainingClass();
|
||||
return containingClass instanceof PsiAnonymousClass &&
|
||||
MethodReferencesSearch.search(method, new LocalSearchScope(containingClass), false).findFirst() == null;
|
||||
if (!(containingClass instanceof PsiAnonymousClass)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (containingClass.getParent() instanceof PsiNewExpression &&
|
||||
containingClass.getParent().getParent() instanceof PsiVariable &&
|
||||
!method.getHierarchicalMethodSignature().getSuperSignatures().isEmpty()) {
|
||||
// references outside anonymous class can still resolve to this method, see com.intellij.psi.scope.util.PsiScopesUtil.setupAndRunProcessor()
|
||||
return false;
|
||||
}
|
||||
|
||||
return MethodReferencesSearch.search(method, new LocalSearchScope(containingClass), false).findFirst() == null;
|
||||
}
|
||||
|
||||
private static boolean isLibraryCode(@NotNull PsiMethod method) {
|
||||
|
||||
+12
@@ -507,6 +507,18 @@ class Foo {{
|
||||
assert ContractInference.inferContracts(method).collect { it as String } == [' -> null']
|
||||
}
|
||||
|
||||
public void "test anonymous class methods potentially used from outside"() {
|
||||
def method = PsiTreeUtil.findChildOfType(myFixture.addClass("""
|
||||
class Foo {{
|
||||
Runnable r = new Runnable() {
|
||||
public void run() {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
};
|
||||
}}"""), PsiAnonymousClass).methods[0]
|
||||
assert ContractInference.inferContracts(method).collect { it as String } == [' -> fail']
|
||||
}
|
||||
|
||||
private String inferContract(String method) {
|
||||
return assertOneElement(inferContracts(method))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user