示例#1
0
  private String format(PsiVariable variable, PsiModifierList modifierList) {
    String name = null;
    PsiElement parent =
        variable == null ? modifierList == null ? null : modifierList.getParent() : variable;
    if (parent instanceof PsiClass) {
      name = ((PsiClass) parent).getName();
    } else {
      int options =
          PsiFormatUtilBase.SHOW_NAME
              | (myShowContainingClass ? PsiFormatUtilBase.SHOW_CONTAINING_CLASS : 0);
      if (parent instanceof PsiMethod) {
        name = PsiFormatUtil.formatMethod((PsiMethod) parent, PsiSubstitutor.EMPTY, options, 0);
      } else if (parent instanceof PsiVariable) {
        name = PsiFormatUtil.formatVariable((PsiVariable) parent, options, PsiSubstitutor.EMPTY);
      } else if (parent instanceof PsiClassInitializer) {
        PsiClass containingClass = ((PsiClassInitializer) parent).getContainingClass();
        String className =
            containingClass instanceof PsiAnonymousClass
                ? QuickFixBundle.message(
                    "anonymous.class.presentation",
                    ((PsiAnonymousClass) containingClass).getBaseClassType().getPresentableText())
                : containingClass != null ? containingClass.getName() : "unknown";
        name = QuickFixBundle.message("class.initializer.presentation", className);
      }
    }

    String modifierText = VisibilityUtil.toPresentableText(myModifier);

    return QuickFixBundle.message(
        myShouldHave ? "add.modifier.fix" : "remove.modifier.fix", name, modifierText);
  }
 public static boolean isInsideParameterList(PsiElement position) {
   PsiElement prev = PsiTreeUtil.prevVisibleLeaf(position);
   PsiModifierList modifierList = PsiTreeUtil.getParentOfType(prev, PsiModifierList.class);
   if (modifierList != null) {
     if (PsiTreeUtil.isAncestor(modifierList, position, false)) {
       return false;
     }
     PsiElement parent = modifierList.getParent();
     return parent instanceof PsiParameterList
         || parent instanceof PsiParameter && parent.getParent() instanceof PsiParameterList;
   }
   return INSIDE_PARAMETER_LIST.accepts(position);
 }
 private static Collection<PsiClass> getClassesByAnnotation(
     String annotationName, Project project, GlobalSearchScope scope) {
   Collection<PsiClass> classes = Sets.newHashSet();
   Collection<PsiAnnotation> annotations =
       JavaAnnotationIndex.getInstance().get(annotationName, project, scope);
   for (PsiAnnotation annotation : annotations) {
     PsiModifierList modifierList = (PsiModifierList) annotation.getParent();
     PsiElement owner = modifierList.getParent();
     if (owner instanceof PsiClass) {
       classes.add((PsiClass) owner);
     }
   }
   return classes;
 }
 public void doFix(Project project, ProblemDescriptor descriptor)
     throws IncorrectOperationException {
   final PsiElement element = descriptor.getPsiElement();
   final PsiModifierList modifierList;
   if (element instanceof PsiModifierList) {
     modifierList = (PsiModifierList) element;
   } else {
     modifierList = (PsiModifierList) element.getParent();
   }
   assert modifierList != null;
   if (modifierList.getParent() instanceof PsiClass) {
     modifierList.setModifierProperty(PsiModifier.STATIC, false);
   } else {
     modifierList.setModifierProperty(PsiModifier.PRIVATE, false);
   }
 }
示例#5
0
  @Override
  public void invoke(
      @NotNull Project project,
      @NotNull PsiFile file,
      @Nullable("is null when called from inspection") Editor editor,
      @NotNull PsiElement startElement,
      @NotNull PsiElement endElement) {
    final PsiModifierList myModifierList = (PsiModifierList) startElement;
    final PsiVariable variable = myVariable == null ? null : myVariable.getElement();
    if (!CodeInsightUtilBase.preparePsiElementForWrite(myModifierList)) return;
    final List<PsiModifierList> modifierLists = new ArrayList<PsiModifierList>();
    final PsiFile containingFile = myModifierList.getContainingFile();
    final PsiModifierList modifierList;
    if (variable != null && variable.isValid()) {
      ApplicationManager.getApplication()
          .runWriteAction(
              new Runnable() {
                public void run() {
                  try {
                    variable.normalizeDeclaration();
                  } catch (IncorrectOperationException e) {
                    LOG.error(e);
                  }
                }
              });
      modifierList = variable.getModifierList();
      assert modifierList != null;
    } else {
      modifierList = myModifierList;
    }
    PsiElement owner = modifierList.getParent();
    if (owner instanceof PsiMethod) {
      PsiModifierList copy = (PsiModifierList) myModifierList.copy();
      changeModifierList(copy);
      final int accessLevel = PsiUtil.getAccessLevel(copy);

      OverridingMethodsSearch.search((PsiMethod) owner, owner.getResolveScope(), true)
          .forEach(
              new PsiElementProcessorAdapter<PsiMethod>(
                  new PsiElementProcessor<PsiMethod>() {
                    public boolean execute(@NotNull PsiMethod inheritor) {
                      PsiModifierList list = inheritor.getModifierList();
                      if (inheritor.getManager().isInProject(inheritor)
                          && PsiUtil.getAccessLevel(list) < accessLevel) {
                        modifierLists.add(list);
                      }
                      return true;
                    }
                  }));
    }

    if (!CodeInsightUtilBase.prepareFileForWrite(containingFile)) return;

    if (!modifierLists.isEmpty()) {
      if (Messages.showYesNoDialog(
              project,
              QuickFixBundle.message("change.inheritors.visibility.warning.text"),
              QuickFixBundle.message("change.inheritors.visibility.warning.title"),
              Messages.getQuestionIcon())
          == DialogWrapper.OK_EXIT_CODE) {
        ApplicationManager.getApplication()
            .runWriteAction(
                new Runnable() {
                  public void run() {
                    if (!CodeInsightUtilBase.preparePsiElementsForWrite(modifierLists)) {
                      return;
                    }

                    for (final PsiModifierList modifierList : modifierLists) {
                      changeModifierList(modifierList);
                    }
                  }
                });
      }
    }

    ApplicationManager.getApplication()
        .runWriteAction(
            new Runnable() {
              public void run() {
                changeModifierList(modifierList);
                UndoUtil.markPsiFileForUndo(containingFile);
              }
            });
  }