本文整理汇总了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);
}
}
}
}
示例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);
}
}
}
}