private static void checkMethodCall(RefElement refWhat, final PsiElement element) { if (!(refWhat instanceof RefMethod)) return; final RefMethod refMethod = (RefMethod) refWhat; final PsiElement psiElement = refMethod.getElement(); if (!(psiElement instanceof PsiMethod)) return; final PsiMethod psiMethod = (PsiMethod) psiElement; if (!PsiType.BOOLEAN.equals(psiMethod.getReturnType())) return; element.accept( new JavaRecursiveElementWalkingVisitor() { @Override public void visitMethodCallExpression(PsiMethodCallExpression call) { super.visitMethodCallExpression(call); final PsiReferenceExpression methodExpression = call.getMethodExpression(); if (methodExpression.isReferenceTo(psiMethod)) { if (isInvertedMethodCall(methodExpression)) return; refMethod.putUserData(ALWAYS_INVERTED, Boolean.FALSE); } } @Override public void visitMethodReferenceExpression(PsiMethodReferenceExpression expression) { super.visitMethodReferenceExpression(expression); if (expression.isReferenceTo(psiElement)) { refMethod.putUserData(ALWAYS_INVERTED, Boolean.FALSE); } } }); }
@Override public void onInitialize(RefElement refElement) { ((RefElementImpl) refElement).setFlag(true, CAN_BE_FINAL_MASK); if (refElement instanceof RefClass) { final RefClass refClass = (RefClass) refElement; final PsiClass psiClass = refClass.getElement(); if (refClass.isEntry()) { ((RefClassImpl) refClass).setFlag(false, CAN_BE_FINAL_MASK); return; } if (refClass.isAbstract() || refClass.isAnonymous() || refClass.isInterface()) { ((RefClassImpl) refClass).setFlag(false, CAN_BE_FINAL_MASK); return; } if (!refClass.isSelfInheritor(psiClass)) { for (PsiClass psiSuperClass : psiClass.getSupers()) { if (myManager.belongsToScope(psiSuperClass)) { RefClass refSuperClass = (RefClass) myManager.getReference(psiSuperClass); if (refSuperClass != null) { ((RefClassImpl) refSuperClass).setFlag(false, CAN_BE_FINAL_MASK); } } } } } else if (refElement instanceof RefMethod) { final RefMethod refMethod = (RefMethod) refElement; final PsiElement element = refMethod.getElement(); if (element instanceof PsiMethod) { PsiMethod psiMethod = (PsiMethod) element; if (refMethod.isConstructor() || refMethod.isAbstract() || refMethod.isStatic() || PsiModifier.PRIVATE.equals(refMethod.getAccessModifier()) || refMethod.getOwnerClass().isAnonymous() || refMethod.getOwnerClass().isInterface()) { ((RefMethodImpl) refMethod).setFlag(false, CAN_BE_FINAL_MASK); } if (PsiModifier.PRIVATE.equals(refMethod.getAccessModifier()) && refMethod.getOwner() != null && !(refMethod.getOwnerClass().getOwner() instanceof RefElement)) { ((RefMethodImpl) refMethod).setFlag(false, CAN_BE_FINAL_MASK); } for (PsiMethod psiSuperMethod : psiMethod.findSuperMethods()) { if (myManager.belongsToScope(psiSuperMethod)) { RefMethod refSuperMethod = (RefMethod) myManager.getReference(psiSuperMethod); if (refSuperMethod != null) { ((RefMethodImpl) refSuperMethod).setFlag(false, CAN_BE_FINAL_MASK); } } } } } }
@Override public CommonProblemDescriptor[] checkElement( @NotNull RefEntity refEntity, @NotNull AnalysisScope scope, @NotNull final InspectionManager manager, @NotNull final GlobalInspectionContext globalContext) { if (refEntity instanceof RefMethod) { RefMethod refMethod = (RefMethod) refEntity; if (!refMethod.isReferenced()) return null; if (hasNonInvertedCalls(refMethod)) return null; if (!refMethod.getSuperMethods().isEmpty()) return null; final PsiMethod psiMethod = (PsiMethod) refMethod.getElement(); final PsiIdentifier psiIdentifier = psiMethod.getNameIdentifier(); if (psiIdentifier != null) { final Collection<RefElement> inReferences = refMethod.getInReferences(); if (inReferences.size() == 1) { final RefElement refElement = inReferences.iterator().next(); final PsiElement usagesContainer = refElement.getElement(); if (usagesContainer == null) return null; if (ReferencesSearch.search(psiMethod, new LocalSearchScope(usagesContainer)) .forEach( new Processor<PsiReference>() { private final Set<PsiReference> myFoundRefs = new HashSet<>(); @Override public boolean process(PsiReference reference) { myFoundRefs.add(reference); return myFoundRefs.size() < 2; } })) return null; } return new ProblemDescriptor[] { manager.createProblemDescriptor( psiIdentifier, InspectionsBundle.message("boolean.method.is.always.inverted.problem.descriptor"), (LocalQuickFix) getQuickFix(null), ProblemHighlightType.GENERIC_ERROR_OR_WARNING, false) }; } } return null; }
public void appendReferencePresentation( RefEntity refElement, final StringBuffer buf, final boolean isPackageIncluded) { if (refElement instanceof RefImplicitConstructor) { buf.append(InspectionsBundle.message("inspection.export.results.implicit.constructor")); refElement = ((RefImplicitConstructor) refElement).getOwnerClass(); } buf.append(HTMLComposerImpl.CODE_OPENING); if (refElement instanceof RefField) { RefField field = (RefField) refElement; PsiField psiField = field.getElement(); buf.append(psiField.getType().getPresentableText()); buf.append(HTMLComposerImpl.NBSP); } else if (refElement instanceof RefMethod) { RefMethod method = (RefMethod) refElement; PsiMethod psiMethod = (PsiMethod) method.getElement(); PsiType returnType = psiMethod.getReturnType(); if (returnType != null) { buf.append(returnType.getPresentableText()); buf.append(HTMLComposerImpl.NBSP); } } buf.append(HTMLComposerImpl.A_HREF_OPENING); if (myComposer.myExporter == null) { buf.append(((RefElementImpl) refElement).getURL()); } else { buf.append(myComposer.myExporter.getURL(refElement)); } buf.append("\">"); if (refElement instanceof RefClass && ((RefClass) refElement).isAnonymous()) { buf.append(InspectionsBundle.message("inspection.reference.anonymous")); } else if (refElement instanceof RefJavaElement && ((RefJavaElement) refElement).isSyntheticJSP()) { buf.append(XmlStringUtil.escapeString(refElement.getName())); } else if (refElement instanceof RefMethod) { PsiMethod psiMethod = (PsiMethod) ((RefMethod) refElement).getElement(); buf.append(psiMethod.getName()); } else { buf.append(refElement.getName()); } buf.append(HTMLComposerImpl.A_CLOSING); if (refElement instanceof RefMethod) { PsiMethod psiMethod = (PsiMethod) ((RefMethod) refElement).getElement(); appendMethodParameters(buf, psiMethod, false); } buf.append(HTMLComposerImpl.CODE_CLOSING); if (refElement instanceof RefClass && ((RefClass) refElement).isAnonymous()) { buf.append(" "); buf.append(InspectionsBundle.message("inspection.export.results.anonymous.ref.in.owner")); buf.append(" "); myComposer.appendElementReference( buf, ((RefElement) refElement.getOwner()), isPackageIncluded); } else if (isPackageIncluded) { buf.append(" ").append(HTMLComposerImpl.CODE_OPENING).append("("); myComposer.appendQualifiedName(buf, refElement.getOwner()); // buf.append(RefUtil.getPackageName(refElement)); buf.append(")").append(HTMLComposerImpl.CODE_CLOSING); } }