本文整理匯總了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);
}
}
}
}