@Nullable private static PsiMethod findNearestMethod(String name, @Nullable PsiClass cls) { if (cls == null) return null; for (PsiMethod method : cls.getMethods()) { if (method.getParameterList().getParametersCount() == 0 && method.getName().equals(name)) { return method.getModifierList().hasModifierProperty(PsiModifier.ABSTRACT) ? null : method; } } return findNearestMethod(name, cls.getSuperClass()); }
@Nullable private ProblemDescriptor createDescription( @NotNull PsiTypeCastExpression cast, @NotNull InspectionManager manager, boolean onTheFly) { PsiExpression operand = cast.getOperand(); PsiTypeElement castType = cast.getCastType(); if (operand == null || castType == null) return null; PsiElement parent = cast.getParent(); while (parent instanceof PsiParenthesizedExpression) { parent = parent.getParent(); } if (parent instanceof PsiReferenceExpression) { if (IGNORE_ANNOTATED_METHODS) { final PsiElement gParent = parent.getParent(); if (gParent instanceof PsiMethodCallExpression) { final PsiMethod psiMethod = ((PsiMethodCallExpression) gParent).resolveMethod(); if (psiMethod != null && NullableNotNullManager.isNotNull(psiMethod)) { final PsiClass superClass = PsiUtil.resolveClassInType(operand.getType()); final PsiClass containingClass = psiMethod.getContainingClass(); if (containingClass != null && superClass != null && containingClass.isInheritor(superClass, true)) { for (PsiMethod method : psiMethod.findSuperMethods(superClass)) { if (NullableNotNullManager.isNullable(method)) { return null; } } } } } } } else if (parent instanceof PsiExpressionList) { final PsiElement gParent = parent.getParent(); if (gParent instanceof PsiMethodCallExpression && IGNORE_SUSPICIOUS_METHOD_CALLS) { final String message = SuspiciousCollectionsMethodCallsInspection.getSuspiciousMethodCallMessage( (PsiMethodCallExpression) gParent, operand.getType(), true, new ArrayList<PsiMethod>(), new IntArrayList()); if (message != null) { return null; } } } String message = InspectionsBundle.message( "inspection.redundant.cast.problem.descriptor", "<code>" + operand.getText() + "</code>", "<code>#ref</code> #loc"); return manager.createProblemDescriptor( castType, message, myQuickFixAction, ProblemHighlightType.LIKE_UNUSED_SYMBOL, onTheFly); }
private boolean isAccessibleFrom(RefElement from, RefJavaElement to, String accessModifier) { if (accessModifier == PsiModifier.PUBLIC) return true; final RefJavaUtil refUtil = RefJavaUtil.getInstance(); if (accessModifier == PsiModifier.PACKAGE_LOCAL) { return RefJavaUtil.getPackage(from) == RefJavaUtil.getPackage(to); } RefClass fromTopLevel = refUtil.getTopLevelClass(from); RefClass toTopLevel = refUtil.getTopLevelClass(to); RefClass fromOwner = refUtil.getOwnerClass(from); RefClass toOwner = refUtil.getOwnerClass(to); if (accessModifier == PsiModifier.PROTECTED) { if (SUGGEST_PRIVATE_FOR_INNERS) { return refUtil.isInheritor(fromTopLevel, toOwner) || fromOwner != null && refUtil.isInheritor(fromOwner, toTopLevel) || toOwner != null && refUtil.getOwnerClass(toOwner) == from; } return refUtil.isInheritor(fromTopLevel, toOwner); } if (accessModifier == PsiModifier.PRIVATE) { if (SUGGEST_PRIVATE_FOR_INNERS) { if (isInExtendsList(to, fromTopLevel.getElement().getExtendsList())) return false; if (isInExtendsList(to, fromTopLevel.getElement().getImplementsList())) return false; if (isInAnnotations(to, fromTopLevel)) return false; return fromTopLevel == toOwner || fromOwner == toTopLevel || toOwner != null && refUtil.getOwnerClass(toOwner) == from; } if (fromOwner != null && fromOwner.isStatic() && !to.isStatic() && refUtil.isInheritor(fromOwner, toOwner)) return false; if (fromTopLevel == toOwner) { if (from instanceof RefClass && to instanceof RefClass) { final PsiClass fromClass = ((RefClass) from).getElement(); LOG.assertTrue(fromClass != null); if (isInExtendsList(to, fromClass.getExtendsList())) return false; if (isInExtendsList(to, fromClass.getImplementsList())) return false; } return true; } } return false; }
@Nullable private static String getDescriptionDirName(PsiClass aClass) { String descriptionDir = ""; PsiClass each = aClass; while (each != null) { String name = each.getName(); if (StringUtil.isEmptyOrSpaces(name)) { return null; } descriptionDir = name + descriptionDir; each = each.getContainingClass(); } return descriptionDir; }
@Override public ProblemDescriptor[] checkClass( @NotNull PsiClass aClass, @NotNull InspectionManager manager, boolean isOnTheFly) { final Project project = aClass.getProject(); final PsiIdentifier nameIdentifier = aClass.getNameIdentifier(); final Module module = ModuleUtil.findModuleForPsiElement(aClass); if (nameIdentifier == null || module == null || !PsiUtil.isInstanciatable(aClass)) return null; final PsiClass base = JavaPsiFacade.getInstance(project) .findClass(INTENTION, GlobalSearchScope.allScope(project)); if (base == null || !aClass.isInheritor(base, true)) return null; String descriptionDir = getDescriptionDirName(aClass); if (StringUtil.isEmptyOrSpaces(descriptionDir)) { return null; } for (PsiDirectory description : getIntentionDescriptionsDirs(module)) { PsiDirectory dir = description.findSubdirectory(descriptionDir); if (dir == null) continue; final PsiFile descr = dir.findFile("description.html"); if (descr != null) { if (!hasBeforeAndAfterTemplate(dir.getVirtualFile())) { PsiElement problem = aClass.getNameIdentifier(); ProblemDescriptor problemDescriptor = manager.createProblemDescriptor( problem == null ? nameIdentifier : problem, "Intention must have 'before.*.template' and 'after.*.template' beside 'description.html'", isOnTheFly, ProblemHighlightType.GENERIC_ERROR_OR_WARNING); return new ProblemDescriptor[] {problemDescriptor}; } return null; } } final PsiElement problem = aClass.getNameIdentifier(); final ProblemDescriptor problemDescriptor = manager.createProblemDescriptor( problem == null ? nameIdentifier : problem, "Intention does not have a description", isOnTheFly, new LocalQuickFix[] {new CreateHtmlDescriptionFix(descriptionDir, module, true)}, ProblemHighlightType.GENERIC_ERROR_OR_WARNING); return new ProblemDescriptor[] {problemDescriptor}; }
private void reportMissingOnClickProblem( OnClickConverter.MyReference reference, PsiClass activity, String methodName, boolean incorrectSignature) { String activityName = activity.getName(); if (activityName == null) { activityName = ""; } final String message = incorrectSignature ? AndroidBundle.message( "android.inspections.on.click.missing.incorrect.signature", methodName, activityName) : AndroidBundle.message( "android.inspections.on.click.missing.problem", methodName, activityName); final LocalQuickFix[] fixes = StringUtil.isJavaIdentifier(methodName) ? new LocalQuickFix[] {new MyQuickFix(methodName, reference.getConverter(), activity)} : LocalQuickFix.EMPTY_ARRAY; myResult.add( myInspectionManager.createProblemDescriptor( reference.getElement(), reference.getRangeInElement(), message, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, myOnTheFly, fixes)); }
@NotNull @Override public String getName() { return "Create '" + myMethodName + "(" + myConverter.getShortParameterName() + ")' in '" + myClass.getName() + "'"; }
/** * Returns true if the given associated activity class is either found in the given set of * classes, or (less likely) extends any of the classes in that set */ private static boolean containsOrExtends( @NotNull Set<PsiClass> resolvedClasses, @NotNull PsiClass relatedActivity) { if (resolvedClasses.contains(relatedActivity)) { return true; } for (PsiClass resolvedClass : resolvedClasses) { if (relatedActivity.isInheritor(resolvedClass, false)) { return true; } } return false; }
@NotNull private static Collection<PsiClass> findRelatedActivities( @NotNull XmlFile file, @NotNull AndroidFacet facet, @NotNull DomFileDescription<?> description) { if (description instanceof LayoutDomFileDescription) { final Computable<List<GotoRelatedItem>> computable = AndroidGotoRelatedProvider.getLazyItemsForXmlFile(file, facet); if (computable == null) { return Collections.emptyList(); } final List<GotoRelatedItem> items = computable.compute(); if (items.isEmpty()) { return Collections.emptyList(); } final PsiClass activityClass = findActivityClass(facet.getModule()); if (activityClass == null) { return Collections.emptyList(); } final List<PsiClass> result = new ArrayList<PsiClass>(); for (GotoRelatedItem item : items) { final PsiElement element = item.getElement(); if (element instanceof PsiClass) { final PsiClass aClass = (PsiClass) element; if (aClass.isInheritor(activityClass, true)) { result.add(aClass); } } } return result; } else { return findRelatedActivitiesForMenu(file, facet); } }
@Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { if (!FileModificationService.getInstance() .preparePsiElementForWrite(descriptor.getPsiElement())) return; final PsiModifierListOwner element = PsiTreeUtil.getParentOfType(descriptor.getPsiElement(), PsiModifierListOwner.class); if (element != null) { RefElement refElement = null; if (myManager != null) { refElement = myManager.getReference(element); } try { if (element instanceof PsiVariable) { ((PsiVariable) element).normalizeDeclaration(); } PsiModifierList list = element.getModifierList(); LOG.assertTrue(list != null); if (element instanceof PsiMethod) { PsiMethod psiMethod = (PsiMethod) element; PsiClass containingClass = psiMethod.getContainingClass(); if (containingClass != null && containingClass.getParent() instanceof PsiFile && myHint == PsiModifier.PRIVATE && list.hasModifierProperty(PsiModifier.FINAL)) { list.setModifierProperty(PsiModifier.FINAL, false); } } list.setModifierProperty(myHint, true); if (refElement instanceof RefJavaElement) { RefJavaUtil.getInstance().setAccessModifier((RefJavaElement) refElement, myHint); } } catch (IncorrectOperationException e) { LOG.error(e); } } }
private static boolean canCallMethodsInConstructors(PsiClass aClass, boolean virtual) { for (PsiMethod constructor : aClass.getConstructors()) { if (!constructor.getLanguage().isKindOf(JavaLanguage.INSTANCE)) return true; PsiCodeBlock body = constructor.getBody(); if (body == null) continue; for (PsiMethodCallExpression call : SyntaxTraverser.psiTraverser().withRoot(body).filter(PsiMethodCallExpression.class)) { PsiReferenceExpression methodExpression = call.getMethodExpression(); if (methodExpression instanceof PsiThisExpression || methodExpression instanceof PsiSuperExpression) continue; if (!virtual) return true; PsiMethod target = call.resolveMethod(); if (target != null && PsiUtil.canBeOverriden(target)) return true; } } return false; }