From 3cddd7b1eeb0a5c83fa14b1729f121afdbb0d28e Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Wed, 19 Jun 2019 10:06:09 +0700 Subject: [PATCH] CallMatcher: purity annotations GitOrigin-RevId: edd1e90a4f0a7294a651f3595b78e5bb2068672b --- .../src/com/siyeh/ig/callMatcher/CallMatcher.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/callMatcher/CallMatcher.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/callMatcher/CallMatcher.java index 93a42fb0f571..42d85882545e 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/callMatcher/CallMatcher.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/callMatcher/CallMatcher.java @@ -47,7 +47,7 @@ public interface CallMatcher extends Predicate { * @param expression expression to test * @return true if the supplied expression matches this matcher */ - @Contract("null -> false") + @Contract(value = "null -> false", pure = true) default boolean matches(@Nullable PsiExpression expression) { expression = PsiUtil.skipParenthesizedExprDown(expression); return expression instanceof PsiMethodCallExpression && test((PsiMethodCallExpression)expression); @@ -110,6 +110,7 @@ public interface CallMatcher extends Predicate { * @param methodNames names of the methods * @return a new matcher */ + @Contract(pure = true) static Simple instanceCall(@NotNull String className, String... methodNames) { return new Simple(className, ContainerUtil.newTroveSet(methodNames), null, CallType.INSTANCE); } @@ -121,6 +122,7 @@ public interface CallMatcher extends Predicate { * @param methodNames names of the methods * @return a new matcher */ + @Contract(pure = true) static Simple exactInstanceCall(@NotNull String className, String... methodNames) { return new Simple(className, ContainerUtil.newTroveSet(methodNames), null, CallType.EXACT_INSTANCE); } @@ -132,6 +134,7 @@ public interface CallMatcher extends Predicate { * @param methodNames names of the methods * @return a new matcher */ + @Contract(pure = true) static Simple staticCall(@NotNull String className, String... methodNames) { return new Simple(className, ContainerUtil.newTroveSet(methodNames), null, CallType.STATIC); } @@ -151,6 +154,7 @@ public interface CallMatcher extends Predicate { * @return PsiReferenceExpression if match is successful, null otherwise */ @Nullable + @Contract(pure = true) default PsiReferenceExpression getReferenceIfMatched(PsiExpression expression) { if (expression instanceof PsiMethodReferenceExpression && methodReferenceMatches((PsiMethodReferenceExpression)expression)) { return (PsiReferenceExpression)expression; @@ -164,6 +168,7 @@ public interface CallMatcher extends Predicate { /** * @return call matcher with additional check before actual call matching */ + @Contract(pure = true) default CallMatcher withContextFilter(@NotNull Predicate filter) { return new CallMatcher() { @Override @@ -199,6 +204,7 @@ public interface CallMatcher extends Predicate { /** * @return call matcher, that matches element for file with given language level or higher */ + @Contract(pure = true) default CallMatcher withLanguageLevelAtLeast(@NotNull LanguageLevel level) { return withContextFilter(element -> PsiUtil.getLanguageLevel(element).isAtLeast(level)); } @@ -231,6 +237,7 @@ public interface CallMatcher extends Predicate { * @return a new matcher * @throws IllegalStateException if this matcher is already limited to parameters count or types */ + @Contract(pure = true) public Simple parameterCount(int count) { if (myParameters != null) { throw new IllegalStateException("Parameter count is already set to " + count); @@ -246,6 +253,7 @@ public interface CallMatcher extends Predicate { * @return a new matcher * @throws IllegalStateException if this matcher is already limited to parameters count or types */ + @Contract(pure = true) public Simple parameterTypes(@NotNull String... types) { if (myParameters != null) { throw new IllegalStateException("Parameters are already registered"); @@ -260,6 +268,7 @@ public interface CallMatcher extends Predicate { psiType instanceof PsiClassType && ((PsiClassType)psiType).rawType().equalsToText(type); } + @Contract(pure = true) @Override public boolean methodReferenceMatches(PsiMethodReferenceExpression methodRef) { if (methodRef == null) return false; @@ -271,6 +280,7 @@ public interface CallMatcher extends Predicate { return parametersMatch(parameterList); } + @Contract(pure = true) @Override public boolean test(PsiMethodCallExpression call) { if (call == null) return false; @@ -298,7 +308,7 @@ public interface CallMatcher extends Predicate { } @Override - @Contract("null -> false") + @Contract(value = "null -> false", pure = true) public boolean methodMatches(PsiMethod method) { if (method == null) return false; if (!myNames.contains(method.getName())) return false;