Esempio n. 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);
  }
Esempio n. 2
0
 public static void checkFieldConflicts(
     @Nullable PsiClass aClass, String newName, final Map<PsiElement, String> conflicts) {
   PsiField existingField = aClass != null ? aClass.findFieldByName(newName, true) : null;
   if (existingField != null) {
     if (aClass.equals(existingField.getContainingClass())) {
       String className =
           aClass instanceof PsiAnonymousClass
               ? RefactoringBundle.message("current.class")
               : RefactoringUIUtil.getDescription(aClass, false);
       final String conflict =
           RefactoringBundle.message(
               "field.0.is.already.defined.in.the.1", existingField.getName(), className);
       conflicts.put(existingField, conflict);
     } else { // method somewhere in base class
       if (!existingField.hasModifierProperty(PsiModifier.PRIVATE)) {
         String fieldInfo =
             PsiFormatUtil.formatVariable(
                 existingField,
                 PsiFormatUtil.SHOW_NAME | PsiFormatUtil.SHOW_TYPE | PsiFormatUtil.TYPE_AFTER,
                 PsiSubstitutor.EMPTY);
         String className =
             RefactoringUIUtil.getDescription(existingField.getContainingClass(), false);
         final String descr =
             RefactoringBundle.message(
                 "field.0.will.hide.field.1.of.the.base.class", newName, fieldInfo, className);
         conflicts.put(existingField, descr);
       }
     }
   }
 }
 @NotNull
 @Override
 protected String getMemberPresentableText(PsiField field) {
   return PsiFormatUtil.formatVariable(
       field,
       PsiFormatUtilBase.SHOW_NAME
           | PsiFormatUtilBase.SHOW_CONTAINING_CLASS
           | PsiFormatUtilBase.SHOW_FQ_NAME,
       PsiSubstitutor.EMPTY);
 }
  @NotNull
  public String getText() {
    PsiLocalVariable variable = myOutOfScopeVariable;

    String varText =
        variable == null
            ? ""
            : PsiFormatUtil.formatVariable(
                variable, PsiFormatUtil.SHOW_NAME | PsiFormatUtil.SHOW_TYPE, PsiSubstitutor.EMPTY);
    return QuickFixBundle.message("bring.variable.to.scope.text", varText);
  }
  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;
      }
    }
  }
Esempio n. 6
0
  @Override
  @NotNull
  public String getNodeText(@NotNull PsiElement element, boolean useFullName) {
    if (element instanceof PsiDirectory) {
      return getPackageName((PsiDirectory) element, false);
    }

    if (element instanceof PsiPackage) {
      return getPackageName((PsiPackage) element);
    }

    if (element instanceof PsiFile) {
      return useFullName
          ? ((PsiFile) element).getVirtualFile().getPresentableUrl()
          : ((PsiFile) element).getName();
    }

    if (element instanceof PsiLabeledStatement) {
      return ((PsiLabeledStatement) element).getLabelIdentifier().getText();
    }

    if (ThrowSearchUtil.isSearchable(element)) {
      return ThrowSearchUtil.getSearchableTypeName(element);
    }

    if (element instanceof PsiClass) {
      String name = ((PsiClass) element).getQualifiedName();
      if (name == null || !useFullName) {
        name = ((PsiClass) element).getName();
      }
      if (name != null) return name;
    }

    if (element instanceof PsiMethod) {
      PsiMethod psiMethod = (PsiMethod) element;
      if (useFullName) {
        int options =
            PsiFormatUtilBase.TYPE_AFTER
                | PsiFormatUtilBase.SHOW_TYPE
                | PsiFormatUtilBase.SHOW_NAME
                | PsiFormatUtilBase.SHOW_PARAMETERS;
        String s =
            PsiFormatUtil.formatMethod(
                (PsiMethod) element,
                PsiSubstitutor.EMPTY,
                options,
                PsiFormatUtilBase.SHOW_TYPE | PsiFormatUtilBase.SHOW_NAME);
        return appendClassName(s, psiMethod.getContainingClass());
      } else {
        int options = PsiFormatUtilBase.SHOW_NAME | PsiFormatUtilBase.SHOW_PARAMETERS;
        return PsiFormatUtil.formatMethod(
            psiMethod, PsiSubstitutor.EMPTY, options, PsiFormatUtilBase.SHOW_TYPE);
      }
    }

    if (element instanceof PsiParameter
        && ((PsiParameter) element).getDeclarationScope() instanceof PsiMethod) {
      PsiMethod method = (PsiMethod) ((PsiParameter) element).getDeclarationScope();
      int varOptions =
          PsiFormatUtilBase.TYPE_AFTER | PsiFormatUtilBase.SHOW_TYPE | PsiFormatUtilBase.SHOW_NAME;
      int methodOptions = PsiFormatUtilBase.SHOW_NAME | PsiFormatUtilBase.SHOW_PARAMETERS;
      String s =
          LangBundle.message(
              "java.terms.variable.of.method",
              PsiFormatUtil.formatVariable((PsiVariable) element, varOptions, PsiSubstitutor.EMPTY),
              PsiFormatUtil.formatMethod(
                  method, PsiSubstitutor.EMPTY, methodOptions, PsiFormatUtilBase.SHOW_TYPE));
      return appendClassName(s, method.getContainingClass());
    }

    if (element instanceof PsiField) {
      PsiField psiField = (PsiField) element;
      int options =
          PsiFormatUtilBase.TYPE_AFTER | PsiFormatUtilBase.SHOW_TYPE | PsiFormatUtilBase.SHOW_NAME;
      String s = PsiFormatUtil.formatVariable(psiField, options, PsiSubstitutor.EMPTY);
      return appendClassName(s, psiField.getContainingClass());
    }

    if (element instanceof PsiVariable) {
      int options =
          PsiFormatUtilBase.TYPE_AFTER | PsiFormatUtilBase.SHOW_TYPE | PsiFormatUtilBase.SHOW_NAME;
      return PsiFormatUtil.formatVariable((PsiVariable) element, options, PsiSubstitutor.EMPTY);
    }

    return "";
  }
Esempio n. 7
0
  @Override
  @NotNull
  public String getDescriptiveName(@NotNull final PsiElement element) {
    if (ThrowSearchUtil.isSearchable(element)) {
      return ThrowSearchUtil.getSearchableTypeName(element);
    }
    if (element instanceof PsiDirectory) {
      return getPackageName((PsiDirectory) element, false);
    }
    if (element instanceof PsiPackage) {
      return getPackageName((PsiPackage) element);
    }
    if (element instanceof PsiFile) {
      return ((PsiFile) element).getVirtualFile().getPresentableUrl();
    }
    if (element instanceof PsiLabeledStatement) {
      return ((PsiLabeledStatement) element).getLabelIdentifier().getText();
    }
    if (element instanceof PsiClass) {
      if (element instanceof PsiAnonymousClass) {
        String name = ((PsiAnonymousClass) element).getBaseClassReference().getReferenceName();
        return "anonymous " + StringUtil.notNullize(name, "class");
      } else {
        PsiClass aClass = (PsiClass) element;
        String qName = aClass.getQualifiedName();
        return qName != null ? qName : aClass.getName() != null ? aClass.getName() : "<unknown>";
      }
    }
    if (element instanceof PsiMethod) {
      PsiMethod psiMethod = (PsiMethod) element;
      String formatted =
          PsiFormatUtil.formatMethod(
              psiMethod,
              PsiSubstitutor.EMPTY,
              PsiFormatUtilBase.SHOW_NAME | PsiFormatUtilBase.SHOW_PARAMETERS,
              PsiFormatUtilBase.SHOW_TYPE | PsiFormatUtilBase.SHOW_RAW_NON_TOP_TYPE);
      PsiClass psiClass = psiMethod.getContainingClass();
      if (psiClass != null) {
        return getContainingClassDescription(psiClass, formatted);
      }

      return formatted;
    }
    if (element instanceof PsiField) {
      PsiField psiField = (PsiField) element;
      String formatted =
          PsiFormatUtil.formatVariable(psiField, PsiFormatUtilBase.SHOW_NAME, PsiSubstitutor.EMPTY);
      PsiClass psiClass = psiField.getContainingClass();
      if (psiClass != null) {
        return getContainingClassDescription(psiClass, formatted);
      }

      return formatted;
    }
    if (element instanceof PsiVariable) {
      return PsiFormatUtil.formatVariable(
          (PsiVariable) element, PsiFormatUtilBase.SHOW_NAME, PsiSubstitutor.EMPTY);
    }
    if (element instanceof PsiLiteralExpression) {
      return element.getText();
    }
    if (element instanceof PsiJavaModule) {
      return ((PsiJavaModule) element).getModuleName();
    }

    return "";
  }