当前位置: 首页>>代码示例>>Java>>正文


Java SameParameterValueInspection类代码示例

本文整理汇总了Java中com.intellij.codeInspection.sameParameterValue.SameParameterValueInspection的典型用法代码示例。如果您正苦于以下问题:Java SameParameterValueInspection类的具体用法?Java SameParameterValueInspection怎么用?Java SameParameterValueInspection使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


SameParameterValueInspection类属于com.intellij.codeInspection.sameParameterValue包,在下文中一共展示了SameParameterValueInspection类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: performRefactoring

import com.intellij.codeInspection.sameParameterValue.SameParameterValueInspection; //导入依赖的package包/类
@Override
protected void performRefactoring(@NotNull UsageInfo[] usages) {
  final List<PsiClassType> thrownExceptions = ExceptionUtil.getThrownCheckedExceptions(new PsiElement[]{myInitializer});
  final Set<PsiVariable> varsUsedInInitializer = new HashSet<PsiVariable>();
  final Set<PsiJavaCodeReferenceElement> paramRefsToInline = new HashSet<PsiJavaCodeReferenceElement>();
  final Map<PsiElement, PsiElement> replacements = new HashMap<PsiElement, PsiElement>();
  for (UsageInfo usage : usages) {
    if (usage instanceof LocalReplacementUsageInfo) {
      final LocalReplacementUsageInfo replacementUsageInfo = (LocalReplacementUsageInfo)usage;
      final PsiElement element = replacementUsageInfo.getElement();
      final PsiElement replacement = replacementUsageInfo.getReplacement();
      if (element != null && replacement != null) {
        replacements.put(element, replacement);
      }
      varsUsedInInitializer.add(replacementUsageInfo.getVariable());
    }
    else {
      LOG.assertTrue(!myCreateLocal);
      paramRefsToInline.add((PsiJavaCodeReferenceElement)usage.getElement());
    }
  }
  myInitializer = (PsiExpression)RefactoringUtil.replaceElementsWithMap(myInitializer, replacements);

  if (myCreateLocal) {
    final PsiElementFactory factory = JavaPsiFacade.getInstance(myMethod.getProject()).getElementFactory();
    PsiDeclarationStatement localDeclaration =
      factory.createVariableDeclarationStatement(myParameter.getName(), myParameter.getType(), myInitializer);
    final PsiLocalVariable declaredVar = (PsiLocalVariable)localDeclaration.getDeclaredElements()[0];
    PsiUtil.setModifierProperty(declaredVar, PsiModifier.FINAL, myParameter.hasModifierProperty(PsiModifier.FINAL));
    final PsiExpression localVarInitializer =
      InlineUtil.inlineVariable(myParameter, myInitializer, (PsiReferenceExpression)factory.createExpressionFromText(myParameter.getName(), myMethod));
    final PsiExpression initializer = declaredVar.getInitializer();
    LOG.assertTrue(initializer != null);
    initializer.replace(localVarInitializer);
    final PsiCodeBlock body = myMethod.getBody();
    if (body != null) {
      PsiElement anchor = findAnchorForLocalVariableDeclaration(body);
      body.addAfter(localDeclaration, anchor);
    }
  } else {
    for (PsiJavaCodeReferenceElement paramRef : paramRefsToInline) {
      InlineUtil.inlineVariable(myParameter, myInitializer, paramRef);
    }
  }

  //delete var if it becomes unused
  for (PsiVariable variable : varsUsedInInitializer) {
    if (variable != null && variable.isValid()) {
      if (ReferencesSearch.search(variable).findFirst() == null) {
        variable.delete();
      }
    }
  }

  SameParameterValueInspection.InlineParameterValueFix.removeParameter(myMethod, myParameter);

  if (!thrownExceptions.isEmpty()) {
    for (PsiClassType exception : thrownExceptions) {
      PsiClass exceptionClass = exception.resolve();
      if (exceptionClass != null) {
        PsiUtil.addException(myMethod, exceptionClass);
      }
    }
  }
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:66,代码来源:InlineParameterExpressionProcessor.java

示例2: performRefactoring

import com.intellij.codeInspection.sameParameterValue.SameParameterValueInspection; //导入依赖的package包/类
@Override
protected void performRefactoring(UsageInfo[] usages) {
  final List<PsiClassType> thrownExceptions = ExceptionUtil.getThrownCheckedExceptions(new PsiElement[]{myInitializer});
  final Set<PsiVariable> varsUsedInInitializer = new HashSet<PsiVariable>();
  final Set<PsiJavaCodeReferenceElement> paramRefsToInline = new HashSet<PsiJavaCodeReferenceElement>();
  final Map<PsiElement, PsiElement> replacements = new HashMap<PsiElement, PsiElement>();
  for (UsageInfo usage : usages) {
    if (usage instanceof LocalReplacementUsageInfo) {
      final LocalReplacementUsageInfo replacementUsageInfo = (LocalReplacementUsageInfo)usage;
      final PsiElement element = replacementUsageInfo.getElement();
      final PsiElement replacement = replacementUsageInfo.getReplacement();
      if (element != null && replacement != null) {
        replacements.put(element, replacement);
      }
      varsUsedInInitializer.add(replacementUsageInfo.getVariable());
    }
    else {
      LOG.assertTrue(!myCreateLocal);
      paramRefsToInline.add((PsiJavaCodeReferenceElement)usage.getElement());
    }
  }
  myInitializer = (PsiExpression)RefactoringUtil.replaceElementsWithMap(myInitializer, replacements);

  if (myCreateLocal) {
    final PsiElementFactory factory = JavaPsiFacade.getInstance(myMethod.getProject()).getElementFactory();
    PsiDeclarationStatement localDeclaration =
      factory.createVariableDeclarationStatement(myParameter.getName(), myParameter.getType(), myInitializer);
    final PsiLocalVariable declaredVar = (PsiLocalVariable)localDeclaration.getDeclaredElements()[0];
    PsiUtil.setModifierProperty(declaredVar, PsiModifier.FINAL, myParameter.hasModifierProperty(PsiModifier.FINAL));
    final PsiExpression localVarInitializer =
      InlineUtil.inlineVariable(myParameter, myInitializer, (PsiReferenceExpression)factory.createExpressionFromText(myParameter.getName(), myMethod));
    final PsiExpression initializer = declaredVar.getInitializer();
    LOG.assertTrue(initializer != null);
    initializer.replace(localVarInitializer);
    final PsiCodeBlock body = myMethod.getBody();
    if (body != null) {
      PsiElement anchor = findAnchorForLocalVariableDeclaration(body);
      body.addAfter(localDeclaration, anchor);
    }
  } else {
    for (PsiJavaCodeReferenceElement paramRef : paramRefsToInline) {
      InlineUtil.inlineVariable(myParameter, myInitializer, paramRef);
    }
  }

  //delete var if it becomes unused
  for (PsiVariable variable : varsUsedInInitializer) {
    if (variable != null && variable.isValid()) {
      if (ReferencesSearch.search(variable).findFirst() == null) {
        variable.delete();
      }
    }
  }

  SameParameterValueInspection.InlineParameterValueFix.removeParameter(myMethod, myParameter);

  if (!thrownExceptions.isEmpty()) {
    for (PsiClassType exception : thrownExceptions) {
      PsiClass exceptionClass = exception.resolve();
      if (exceptionClass != null) {
        PsiUtil.addException(myMethod, exceptionClass);
      }
    }
  }
}
 
开发者ID:lshain-android-source,项目名称:tools-idea,代码行数:66,代码来源:InlineParameterExpressionProcessor.java


注:本文中的com.intellij.codeInspection.sameParameterValue.SameParameterValueInspection类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。