public void updateReturnValueTemplate(PsiExpression expression) { if (myReturnValueTemplate == null) return; if (!getSuperMethods().isEmpty()) { for (final RefMethod refMethod : getSuperMethods()) { RefMethodImpl refSuper = (RefMethodImpl) refMethod; refSuper.updateReturnValueTemplate(expression); } } else { String newTemplate = null; final RefJavaUtil refUtil = RefJavaUtil.getInstance(); if (expression instanceof PsiLiteralExpression) { PsiLiteralExpression psiLiteralExpression = (PsiLiteralExpression) expression; newTemplate = psiLiteralExpression.getText(); } else if (expression instanceof PsiReferenceExpression) { PsiReferenceExpression referenceExpression = (PsiReferenceExpression) expression; PsiElement resolved = referenceExpression.resolve(); if (resolved instanceof PsiField) { PsiField psiField = (PsiField) resolved; if (psiField.hasModifierProperty(PsiModifier.STATIC) && psiField.hasModifierProperty(PsiModifier.FINAL) && refUtil.compareAccess(refUtil.getAccessModifier(psiField), getAccessModifier()) >= 0) { newTemplate = PsiFormatUtil.formatVariable( psiField, PsiFormatUtilBase.SHOW_NAME | PsiFormatUtilBase.SHOW_CONTAINING_CLASS | PsiFormatUtilBase.SHOW_FQ_NAME, PsiSubstitutor.EMPTY); } } } else if (refUtil.isCallToSuperMethod(expression, (PsiMethod) getElement())) return; //noinspection StringEquality if (myReturnValueTemplate == RETURN_VALUE_UNDEFINED) { myReturnValueTemplate = newTemplate; } else if (!Comparing.equal(myReturnValueTemplate, newTemplate)) { myReturnValueTemplate = null; } } }
@Override public void visitLiteralExpression(@NotNull PsiLiteralExpression value) { super.visitLiteralExpression(value); final String text = value.getText(); if (!PsiKeyword.NULL.equals(text)) { return; } PsiElement parent = value.getParent(); while (parent instanceof PsiParenthesizedExpression || parent instanceof PsiConditionalExpression || parent instanceof PsiTypeCastExpression) { parent = parent.getParent(); } if (parent == null || !(parent instanceof PsiReturnStatement)) { return; } final PsiMethod method = PsiTreeUtil.getParentOfType(value, PsiMethod.class); if (method == null) { return; } final PsiType returnType = method.getReturnType(); if (returnType == null) { return; } final boolean isArray = returnType.getArrayDimensions() > 0; if (AnnotationUtil.isAnnotated(method, AnnotationUtil.NULLABLE, false)) { return; } if (m_reportCollectionMethods && CollectionUtils.isCollectionClassOrInterface(returnType)) { registerError(value, value); } else if (m_reportArrayMethods && isArray) { registerError(value, value); } else if (m_reportObjectMethods && !isArray) { registerError(value, value); } }