private static boolean isR2Expression(UElement node) { UElement parentNode = node.getUastParent(); if (parentNode == null) { return false; } String text = node.asSourceString(); UElement parent = LintUtils.skipParentheses(parentNode); return (text.equals(R2) || text.contains(".R2")) && parent instanceof UExpression && endsWithAny(parent.asSourceString(), SUPPORTED_TYPES); }
@Override public void visitSuperExpression(PsiSuperExpression node) { super.visitSuperExpression(node); PsiElement parent = skipParentheses(node.getParent()); if (parent instanceof PsiReferenceExpression) { PsiElement resolved = ((PsiReferenceExpression) parent).resolve(); if (mMethod.equals(resolved)) { mCallsSuper = true; } } } }
@Override public void visitReferenceExpression(PsiReferenceExpression expression) { PsiElement parent = skipParentheses(expression.getParent()); if (parent instanceof PsiReferenceExpression) { // We only want to look at the leaf expressions; e.g. if you have // "foo.bar.baz" we only care about the select foo.bar.baz, not foo.bar // However, if it's something like field.methodCall() we still want // to check the field if (!(skipParentheses(parent.getParent()) instanceof PsiCallExpression)) { return; } } PsiElement resolved = expression.resolve(); if (resolved instanceof PsiField) { checkField(expression, (PsiField)resolved); } else if (resolved instanceof PsiMethod && expression instanceof PsiMethodReferenceExpression) { checkMethodReference(expression, (PsiMethod) resolved); } }
private static boolean isEditorCommittedInChainedCalls(@NonNull JavaContext context, @NonNull PsiMethodCallExpression node) { PsiElement parent = skipParentheses(node.getParent()); while (parent != null) { if (parent instanceof PsiMethodCallExpression) { PsiMethodCallExpression methodInvocation = (PsiMethodCallExpression) parent; if (isEditorCommitMethodCall(context, methodInvocation) || isEditorApplyMethodCall(context, methodInvocation)) { return true; } } else if (!(parent instanceof PsiReferenceExpression)) { // reference expressions are method references return false; } parent = skipParentheses(parent.getParent()); } return false; }
@Nullable public static PsiVariable getVariableElement(@NonNull PsiElement rhs, boolean allowChainedCalls, boolean allowFields) { PsiElement parent = skipParentheses(rhs.getParent()); while (true) { if ((parent instanceof PsiReferenceExpression)) { PsiElement parentParent = skipParentheses(parent.getParent()); if ((parentParent instanceof PsiMethodCallExpression)) { parent = skipParentheses(parentParent.getParent()); } else { break;
private static boolean isCommittedInChainedCalls(@NonNull JavaContext context, @NonNull PsiMethodCallExpression node) { // Look for chained calls since the FragmentManager methods all return "this" // to allow constructor chaining, e.g. // getFragmentManager().beginTransaction().addToBackStack("test") // .disallowAddToBackStack().hide(mFragment2).setBreadCrumbShortTitle("test") // .show(mFragment2).setCustomAnimations(0, 0).commit(); PsiElement parent = skipParentheses(node.getParent()); while (parent != null) { if (parent instanceof PsiMethodCallExpression) { PsiMethodCallExpression methodInvocation = (PsiMethodCallExpression) parent; if (isTransactionCommitMethodCall(context, methodInvocation) || isShowFragmentMethodCall(context, methodInvocation)) { return true; } } else if (!(parent instanceof PsiReferenceExpression)) { // reference expressions are method references return false; } parent = skipParentheses(parent.getParent()); } return false; }
/** * Reports issue if this call is inside PLog.x(). * Calling this method assumes actual calling method is 'String#format'. * * @see #ISSUE_NESTED_FORMAT */ private static void checkNestedStringFormat(JavaContext context, PsiMethodCallExpression call) { PsiElement current = call; while (true) { current = LintUtils.skipParentheses(current.getParent()); if (current == null || current instanceof PsiCodeBlock) { // Reached AST root or code block node; String.format not inside PLog.X(..). return; } if (current instanceof PsiMethodCallExpression) { PsiMethodCallExpression expression = (PsiMethodCallExpression) current; if (Pattern.matches("org\\.mym\\.plog\\.PLog\\.(v|d|i|w|e)", expression.getMethodExpression().getQualifiedName())) { sHelper.reportIssue(context, ISSUE_NESTED_FORMAT, call); return; } } } }
private static void checkResult(@NonNull JavaContext context, @NonNull PsiElement node, @NonNull PsiMethod method, @NonNull PsiAnnotation annotation) { if (skipParentheses(node.getParent()) instanceof PsiExpressionStatement) { String methodName = JavaContext.getMethodName(node); String suggested = getAnnotationStringValue(annotation, ATTR_SUGGEST);
&& !(skipParentheses(node.getParent()) instanceof PsiThrowStatement) && mCheckAllocations) {
public static boolean isWithinVersionCheckConditional(@NonNull PsiElement element, int api) { PsiElement current = skipParentheses(element.getParent()); PsiElement prev = element; while (current != null) { current = skipParentheses(current.getParent());
PsiElement parent = skipParentheses(node.getParent()); while (parent instanceof PsiReferenceExpression) { parent = skipParentheses(parent.getParent());
PsiElement parent = LintUtils.skipParentheses(call.getParent()); if (parent instanceof PsiTypeCastExpression) { PsiTypeCastExpression cast = (PsiTypeCastExpression) parent;
@Override public void visitMethod(@NonNull JavaContext context, @Nullable JavaElementVisitor visitor, @NonNull PsiMethodCallExpression node, @NonNull PsiMethod method) { if (skipParentheses(node.getParent()) instanceof PsiExpressionStatement) { if (!context.getEvaluator().isMemberInSubClassOf(method, CLASS_CONTEXT, false)) { return;
PsiElement parent = skipParentheses(node.getParent()); if (parent instanceof PsiSwitchLabelStatement) { PsiExpression condition = ((PsiSwitchLabelStatement) parent)
@Nullable private static String getLhs(@NonNull PsiMethodCallExpression call) { PsiElement parent = skipParentheses(call.getParent()); if (parent instanceof PsiTypeCastExpression) { parent = parent.getParent(); } if (parent instanceof PsiLocalVariable) { return ((PsiLocalVariable)parent).getName(); } else if (parent instanceof PsiBinaryExpression) { PsiBinaryExpression be = (PsiBinaryExpression) parent; PsiExpression left = be.getLOperand(); if (left instanceof PsiReference) { return left.getText(); } else if (left instanceof PsiArrayAccessExpression) { PsiArrayAccessExpression aa = (PsiArrayAccessExpression) left; return aa.getArrayExpression().getText(); } } else if (parent instanceof PsiAssignmentExpression) { PsiExpression left = ((PsiAssignmentExpression) parent).getLExpression(); if (left instanceof PsiReference) { return left.getText(); } else if (left instanceof PsiArrayAccessExpression) { PsiArrayAccessExpression aa = (PsiArrayAccessExpression) left; return aa.getArrayExpression().getText(); } } return null; }