コード例 #1
0
 @Override
 public void doFix(Project project, ProblemDescriptor descriptor)
     throws IncorrectOperationException {
   final PsiIdentifier name = (PsiIdentifier) descriptor.getPsiElement();
   final PsiReferenceExpression expression = (PsiReferenceExpression) name.getParent();
   if (expression == null) {
     return;
   }
   final PsiMethodCallExpression call = (PsiMethodCallExpression) expression.getParent();
   final String methodName = expression.getReferenceName();
   if (call == null) {
     return;
   }
   final PsiMethod method = call.resolveMethod();
   if (method == null) {
     return;
   }
   final PsiClass containingClass = method.getContainingClass();
   final PsiExpressionList argumentList = call.getArgumentList();
   if (containingClass == null) {
     return;
   }
   final String containingClassName = containingClass.getQualifiedName();
   final String argText = argumentList.getText();
   PsiReplacementUtil.replaceExpressionAndShorten(
       call, containingClassName + '.' + methodName + argText);
 }
 @Override
 public void visitBreakStatement(@NotNull PsiBreakStatement statement) {
   final PsiIdentifier labelIdentifier = statement.getLabelIdentifier();
   if (labelIdentifier == null) {
     return;
   }
   final String labelText = labelIdentifier.getText();
   if (labelText == null || labelText.length() == 0) {
     return;
   }
   final PsiStatement exitedStatement = statement.findExitedStatement();
   if (exitedStatement == null) {
     return;
   }
   final PsiStatement labelEnabledParent =
       PsiTreeUtil.getParentOfType(
           statement,
           PsiForStatement.class,
           PsiDoWhileStatement.class,
           PsiForeachStatement.class,
           PsiWhileStatement.class,
           PsiSwitchStatement.class);
   if (labelEnabledParent == null) {
     return;
   }
   if (exitedStatement.equals(labelEnabledParent)) {
     registerStatementError(statement);
   }
 }
コード例 #3
0
 private static boolean labeledStatementsAreEquivalent(
     @NotNull PsiLabeledStatement statement1, @NotNull PsiLabeledStatement statement2) {
   final PsiIdentifier identifier1 = statement1.getLabelIdentifier();
   final PsiIdentifier identifier2 = statement2.getLabelIdentifier();
   final String text1 = identifier1.getText();
   final String text2 = identifier2.getText();
   return text1.equals(text2);
 }
 public void doFix(Project project, ProblemDescriptor descriptor)
     throws IncorrectOperationException {
   final PsiElement breakKeywordElement = descriptor.getPsiElement();
   final PsiBreakStatement breakStatement = (PsiBreakStatement) breakKeywordElement.getParent();
   final PsiIdentifier identifier = breakStatement.getLabelIdentifier();
   if (identifier == null) {
     return;
   }
   identifier.delete();
 }
コード例 #5
0
 @Nullable
 public static PsiModifierListOwner getContainer(final PsiElement element) {
   PsiModifierListOwner listOwner =
       PsiTreeUtil.getParentOfType(element, PsiParameter.class, false);
   if (listOwner == null) {
     final PsiIdentifier psiIdentifier =
         PsiTreeUtil.getParentOfType(element, PsiIdentifier.class, false);
     if (psiIdentifier != null && psiIdentifier.getParent() instanceof PsiModifierListOwner) {
       listOwner = (PsiModifierListOwner) psiIdentifier.getParent();
     }
   }
   return listOwner;
 }
コード例 #6
0
 private static boolean continueStatementsAreEquivalent(
     @NotNull PsiContinueStatement statement1, @NotNull PsiContinueStatement statement2) {
   final PsiIdentifier identifier1 = statement1.getLabelIdentifier();
   final PsiIdentifier identifier2 = statement2.getLabelIdentifier();
   if (identifier1 == null) {
     return identifier2 == null;
   }
   if (identifier2 == null) {
     return false;
   }
   final String text1 = identifier1.getText();
   final String text2 = identifier2.getText();
   return text1.equals(text2);
 }
コード例 #7
0
 @Override
 protected void doFix(Project project, ProblemDescriptor descriptor)
     throws IncorrectOperationException {
   final PsiElement element = descriptor.getPsiElement();
   final PsiElement parent = element.getParent();
   if (!(parent instanceof PsiCatchSection)) {
     return;
   }
   final PsiCatchSection catchSection = (PsiCatchSection) parent;
   final PsiParameter parameter = catchSection.getParameter();
   if (parameter == null) {
     return;
   }
   final PsiIdentifier identifier = parameter.getNameIdentifier();
   if (identifier == null) {
     return;
   }
   final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
   final PsiIdentifier newIdentifier = factory.createIdentifier("ignored");
   identifier.replace(newIdentifier);
 }
  private static void processPrimaryMethod(
      JavaChangeInfo changeInfo, PsiMethod method, PsiMethod baseMethod, boolean isOriginal)
      throws IncorrectOperationException {
    PsiElementFactory factory = JavaPsiFacade.getInstance(method.getProject()).getElementFactory();

    if (changeInfo.isVisibilityChanged()) {
      PsiModifierList modifierList = method.getModifierList();
      final String highestVisibility =
          isOriginal
              ? changeInfo.getNewVisibility()
              : VisibilityUtil.getHighestVisibility(
                  changeInfo.getNewVisibility(),
                  VisibilityUtil.getVisibilityModifier(modifierList));
      VisibilityUtil.setVisibility(modifierList, highestVisibility);
    }

    if (changeInfo.isNameChanged()) {
      String newName =
          baseMethod == null
              ? changeInfo.getNewName()
              : RefactoringUtil.suggestNewOverriderName(
                  method.getName(), baseMethod.getName(), changeInfo.getNewName());

      if (newName != null && !newName.equals(method.getName())) {
        final PsiIdentifier nameId = method.getNameIdentifier();
        assert nameId != null;
        nameId.replace(
            JavaPsiFacade.getInstance(method.getProject())
                .getElementFactory()
                .createIdentifier(newName));
      }
    }

    final PsiSubstitutor substitutor =
        baseMethod == null
            ? PsiSubstitutor.EMPTY
            : ChangeSignatureProcessor.calculateSubstitutor(method, baseMethod);

    if (changeInfo.isReturnTypeChanged()) {
      PsiType newTypeElement =
          changeInfo
              .getNewReturnType()
              .getType(changeInfo.getMethod().getParameterList(), method.getManager());
      final PsiType returnType = substitutor.substitute(newTypeElement);
      // don't modify return type for non-Java overriders (EJB)
      if (method.getName().equals(changeInfo.getNewName())) {
        final PsiTypeElement typeElement = method.getReturnTypeElement();
        if (typeElement != null) {
          typeElement.replace(factory.createTypeElement(returnType));
        }
      }
    }

    PsiParameterList list = method.getParameterList();
    PsiParameter[] parameters = list.getParameters();

    final JavaParameterInfo[] parameterInfos = changeInfo.getNewParameters();
    final int delta =
        baseMethod != null
            ? baseMethod.getParameterList().getParametersCount()
                - method.getParameterList().getParametersCount()
            : 0;
    PsiParameter[] newParms = new PsiParameter[Math.max(parameterInfos.length - delta, 0)];
    final String[] oldParameterNames = changeInfo.getOldParameterNames();
    final String[] oldParameterTypes = changeInfo.getOldParameterTypes();
    for (int i = 0; i < newParms.length; i++) {
      JavaParameterInfo info = parameterInfos[i];
      int index = info.getOldIndex();
      if (index >= 0) {
        PsiParameter parameter = parameters[index];
        newParms[i] = parameter;

        String oldName = oldParameterNames[index];
        if (!oldName.equals(info.getName()) && oldName.equals(parameter.getName())) {
          PsiIdentifier newIdentifier = factory.createIdentifier(info.getName());
          parameter.getNameIdentifier().replace(newIdentifier);
        }

        String oldType = oldParameterTypes[index];
        if (!oldType.equals(info.getTypeText())) {
          parameter.normalizeDeclaration();
          PsiType newType =
              substitutor.substitute(
                  info.createType(changeInfo.getMethod().getParameterList(), method.getManager()));

          parameter.getTypeElement().replace(factory.createTypeElement(newType));
        }
      } else {
        newParms[i] = createNewParameter(changeInfo, info, substitutor);
      }
    }

    resolveParameterVsFieldsConflicts(newParms, method, list, changeInfo.toRemoveParm());
    fixJavadocsForChangedMethod(method, changeInfo, newParms.length);
    if (changeInfo.isExceptionSetOrOrderChanged()) {
      final PsiClassType[] newExceptions = getPrimaryChangedExceptionInfo(changeInfo);
      fixPrimaryThrowsLists(method, newExceptions);
    }
  }
コード例 #9
0
ファイル: Converter.java プロジェクト: rlnbpr/kotlin
 @NotNull
 public static Identifier identifierToIdentifier(@Nullable PsiIdentifier identifier) {
   if (identifier == null) return Identifier.EMPTY_IDENTIFIER;
   return new IdentifierImpl(identifier.getText());
 }