Java 类com.intellij.psi.util.RedundantCastUtil 实例源码

项目:intellij-ce-playground    文件:RedundantCastInspection.java   
@Override
@Nullable
public ProblemDescriptor[] getDescriptions(@NotNull PsiElement where, @NotNull InspectionManager manager, boolean isOnTheFly) {
  List<PsiTypeCastExpression> redundantCasts = RedundantCastUtil.getRedundantCastsInside(where);
  if (redundantCasts.isEmpty()) return null;
  List<ProblemDescriptor> descriptions = new ArrayList<ProblemDescriptor>(redundantCasts.size());
  for (PsiTypeCastExpression redundantCast : redundantCasts) {
    ProblemDescriptor descriptor = createDescription(redundantCast, manager, isOnTheFly);
    if (descriptor != null) {
      descriptions.add(descriptor);
    }
  }
  if (descriptions.isEmpty()) return null;
  return descriptions.toArray(new ProblemDescriptor[descriptions.size()]);
}
项目:intellij-ce-playground    文件:RedundantCastInspection.java   
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
  if (!FileModificationService.getInstance().preparePsiElementForWrite(descriptor.getPsiElement())) return;
  PsiElement castTypeElement = descriptor.getPsiElement();
  PsiTypeCastExpression cast = castTypeElement == null ? null : (PsiTypeCastExpression)castTypeElement.getParent();
  if (cast != null) {
    RedundantCastUtil.removeCast(cast);
  }
}
项目:intellij-ce-playground    文件:StreamApiMigrationInspection.java   
private static void simplifyRedundantCast(PsiElement result) {
  final PsiMethodReferenceExpression methodReferenceExpression = PsiTreeUtil.findChildOfType(result, PsiMethodReferenceExpression.class);
  if (methodReferenceExpression != null) {
    final PsiElement parent = methodReferenceExpression.getParent();
    if (parent instanceof PsiTypeCastExpression) {
      if (RedundantCastUtil.isCastRedundant((PsiTypeCastExpression)parent)) {
        final PsiExpression operand = ((PsiTypeCastExpression)parent).getOperand();
        LOG.assertTrue(operand != null);
        parent.replace(operand);
      }
    }
  }
}
项目:intellij-ce-playground    文件:AnonymousCanBeMethodReferenceInspection.java   
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
  final PsiElement element = descriptor.getPsiElement();
  if (element instanceof PsiNewExpression) {
    if (!FileModificationService.getInstance().preparePsiElementForWrite(element)) return;
    final PsiAnonymousClass anonymousClass = ((PsiNewExpression)element).getAnonymousClass();
    if (anonymousClass == null) return;
    final PsiMethod[] methods = anonymousClass.getMethods();
    if (methods.length != 1) return;

    final PsiParameter[] parameters = methods[0].getParameterList().getParameters();
    final PsiCallExpression callExpression = LambdaCanBeMethodReferenceInspection
      .canBeMethodReferenceProblem(methods[0].getBody(), parameters, anonymousClass.getBaseClassType());
    if (callExpression == null) return;
    final String methodRefText =
      LambdaCanBeMethodReferenceInspection.createMethodReferenceText(callExpression, anonymousClass.getBaseClassType(), parameters);

    if (methodRefText != null) {
      final String canonicalText = anonymousClass.getBaseClassType().getCanonicalText();
      final PsiExpression psiExpression = JavaPsiFacade.getElementFactory(project).createExpressionFromText("(" + canonicalText + ")" + methodRefText, anonymousClass);

      PsiElement castExpr = anonymousClass.getParent().replace(psiExpression);
      if (RedundantCastUtil.isCastRedundant((PsiTypeCastExpression)castExpr)) {
        final PsiExpression operand = ((PsiTypeCastExpression)castExpr).getOperand();
        LOG.assertTrue(operand != null);
        castExpr = castExpr.replace(operand);
      }
      JavaCodeStyleManager.getInstance(project).shortenClassReferences(castExpr);
    }
  }
}
项目:tools-idea    文件:RedundantCastInspection.java   
@Override
@Nullable
public ProblemDescriptor[] getDescriptions(PsiElement where, InspectionManager manager, boolean isOnTheFly) {
  List<PsiTypeCastExpression> redundantCasts = RedundantCastUtil.getRedundantCastsInside(where);
  if (redundantCasts.isEmpty()) return null;
  List<ProblemDescriptor> descriptions = new ArrayList<ProblemDescriptor>(redundantCasts.size());
  for (PsiTypeCastExpression redundantCast : redundantCasts) {
    ProblemDescriptor descriptor = createDescription(redundantCast, manager, isOnTheFly);
    if (descriptor != null) {
      descriptions.add(descriptor);
    }
  }
  if (descriptions.isEmpty()) return null;
  return descriptions.toArray(new ProblemDescriptor[descriptions.size()]);
}
项目:tools-idea    文件:RedundantCastInspection.java   
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
  if (!FileModificationService.getInstance().preparePsiElementForWrite(descriptor.getPsiElement())) return;
  PsiElement castTypeElement = descriptor.getPsiElement();
  PsiTypeCastExpression cast = castTypeElement == null ? null : (PsiTypeCastExpression)castTypeElement.getParent();
  if (cast != null) {
    RedundantCastUtil.removeCast(cast);
  }
}
项目:tools-idea    文件:AnonymousCanBeMethodReferenceInspection.java   
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
  final PsiElement element = descriptor.getPsiElement();
  final PsiAnonymousClass anonymousClass = PsiTreeUtil.getParentOfType(element, PsiAnonymousClass.class);
  if (anonymousClass == null) return;
  final PsiMethod[] methods = anonymousClass.getMethods();
  if (methods.length != 1) return;

  final PsiParameter[] parameters = methods[0].getParameterList().getParameters();
  final PsiCallExpression callExpression = LambdaCanBeMethodReferenceInspection
    .canBeMethodReferenceProblem(methods[0].getBody(), parameters, anonymousClass.getBaseClassType());
  if (callExpression == null) return;
  final String methodRefText =
    LambdaCanBeMethodReferenceInspection.createMethodReferenceText(callExpression, anonymousClass.getBaseClassType());

  if (methodRefText != null) {
    final String canonicalText = anonymousClass.getBaseClassType().getCanonicalText();
    final PsiExpression psiExpression = JavaPsiFacade.getElementFactory(project).createExpressionFromText("(" + canonicalText + ")" + methodRefText, anonymousClass);

    PsiElement castExpr = anonymousClass.getParent().replace(psiExpression);
    if (RedundantCastUtil.isCastRedundant((PsiTypeCastExpression)castExpr)) {
      final PsiExpression operand = ((PsiTypeCastExpression)castExpr).getOperand();
      LOG.assertTrue(operand != null);
      castExpr = castExpr.replace(operand);
    }
    JavaCodeStyleManager.getInstance(project).shortenClassReferences(castExpr);
  }
}
项目:consulo-java    文件:RedundantCastInspection.java   
@Override
@Nullable
public ProblemDescriptor[] getDescriptions(PsiElement where, InspectionManager manager, boolean isOnTheFly) {
  List<PsiTypeCastExpression> redundantCasts = RedundantCastUtil.getRedundantCastsInside(where);
  if (redundantCasts.isEmpty()) return null;
  List<ProblemDescriptor> descriptions = new ArrayList<ProblemDescriptor>(redundantCasts.size());
  for (PsiTypeCastExpression redundantCast : redundantCasts) {
    ProblemDescriptor descriptor = createDescription(redundantCast, manager, isOnTheFly);
    if (descriptor != null) {
      descriptions.add(descriptor);
    }
  }
  if (descriptions.isEmpty()) return null;
  return descriptions.toArray(new ProblemDescriptor[descriptions.size()]);
}
项目:consulo-java    文件:RedundantCastInspection.java   
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
  if (!FileModificationService.getInstance().preparePsiElementForWrite(descriptor.getPsiElement())) return;
  PsiElement castTypeElement = descriptor.getPsiElement();
  PsiTypeCastExpression cast = castTypeElement == null ? null : (PsiTypeCastExpression)castTypeElement.getParent();
  if (cast != null) {
    RedundantCastUtil.removeCast(cast);
  }
}
项目:consulo-java    文件:AnonymousCanBeLambdaInspection.java   
/**
 * Try convert given method of given anonymous class into lambda and replace given element.
 *
 * @param anonymousClass      physical anonymous class containing method
 * @param method              physical method to convert with non-empty body
 * @param replacer            an operator which actually inserts a lambda into the file (possibly removing anonymous class)
 *                            and returns an inserted physical lambda
 * @param forceIgnoreTypeCast if false, type cast might be added if necessary
 * @return newly-generated lambda expression (possibly with typecast)
 */
@NotNull
static PsiExpression generateLambdaByMethod(PsiAnonymousClass anonymousClass, PsiMethod method, UnaryOperator<PsiLambdaExpression> replacer, boolean forceIgnoreTypeCast)
{
    ChangeContextUtil.encodeContextInfo(anonymousClass, true);
    final String canonicalText = anonymousClass.getBaseClassType().getCanonicalText();

    final PsiCodeBlock body = method.getBody();
    LOG.assertTrue(body != null);

    final Collection<PsiComment> comments = collectCommentsOutsideMethodBody(anonymousClass, body);
    final Project project = anonymousClass.getProject();
    final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project);

    final String withoutTypesDeclared = ReplaceWithLambdaFix.composeLambdaText(method);

    PsiLambdaExpression lambdaExpression = (PsiLambdaExpression) elementFactory.createExpressionFromText(withoutTypesDeclared, anonymousClass);

    PsiElement lambdaBody = lambdaExpression.getBody();
    LOG.assertTrue(lambdaBody != null);
    lambdaBody.replace(body);
    lambdaExpression = replacer.apply(lambdaExpression);

    final Set<PsiVariable> variables = new HashSet<>();
    final Set<String> usedLocalNames = new HashSet<>();

    collectLocalVariablesDefinedInsideLambda(lambdaExpression, variables, usedLocalNames);

    ReplaceWithLambdaFix.giveUniqueNames(project, elementFactory, lambdaExpression, usedLocalNames, variables.toArray(new PsiVariable[variables.size()]));

    final PsiExpression singleExpr = RedundantLambdaCodeBlockInspection.isCodeBlockRedundant(lambdaExpression.getBody());
    if(singleExpr != null)
    {
        lambdaExpression.getBody().replace(singleExpr);
    }
    ChangeContextUtil.decodeContextInfo(lambdaExpression, null, null);
    restoreComments(comments, lambdaExpression);

    final JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(project);
    if(forceIgnoreTypeCast)
    {
        return (PsiExpression) javaCodeStyleManager.shortenClassReferences(lambdaExpression);
    }

    PsiTypeCastExpression typeCast = (PsiTypeCastExpression) elementFactory.createExpressionFromText("(" + canonicalText + ")" + withoutTypesDeclared, lambdaExpression);
    final PsiExpression typeCastOperand = typeCast.getOperand();
    LOG.assertTrue(typeCastOperand instanceof PsiLambdaExpression);
    final PsiElement fromText = ((PsiLambdaExpression) typeCastOperand).getBody();
    LOG.assertTrue(fromText != null);
    lambdaBody = lambdaExpression.getBody();
    LOG.assertTrue(lambdaBody != null);
    fromText.replace(lambdaBody);
    ((PsiLambdaExpression) typeCastOperand).getParameterList().replace(lambdaExpression.getParameterList());
    typeCast = (PsiTypeCastExpression) lambdaExpression.replace(typeCast);
    if(RedundantCastUtil.isCastRedundant(typeCast))
    {
        final PsiExpression operand = typeCast.getOperand();
        LOG.assertTrue(operand != null);
        return (PsiExpression) typeCast.replace(operand);
    }
    return (PsiExpression) javaCodeStyleManager.shortenClassReferences(typeCast);
}