blob: 298ad55b510fce260a693d28a16a356b9943aa19 [file] [log] [blame]
/*
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInspection.sameParameterValue;
import com.intellij.analysis.AnalysisScope;
import com.intellij.codeInsight.daemon.GroupNames;
import com.intellij.codeInspection.*;
import com.intellij.codeInspection.reference.*;
import com.intellij.psi.PsiReference;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
/**
* @author max
*/
public class SameParameterValueInspectionBase extends GlobalJavaBatchInspectionTool {
@Override
@Nullable
public CommonProblemDescriptor[] checkElement(@NotNull RefEntity refEntity,
@NotNull AnalysisScope scope,
@NotNull InspectionManager manager,
@NotNull GlobalInspectionContext globalContext,
@NotNull ProblemDescriptionsProcessor processor) {
List<ProblemDescriptor> problems = null;
if (refEntity instanceof RefMethod) {
final RefMethod refMethod = (RefMethod)refEntity;
if (refMethod.hasSuperMethods()) return null;
if (refMethod.isEntry()) return null;
RefParameter[] parameters = refMethod.getParameters();
for (RefParameter refParameter : parameters) {
String value = refParameter.getActualValueIfSame();
if (value != null) {
if (problems == null) problems = new ArrayList<ProblemDescriptor>(1);
final String paramName = refParameter.getName();
problems.add(manager.createProblemDescriptor(refParameter.getElement(), InspectionsBundle.message(
"inspection.same.parameter.problem.descriptor", "<code>" + paramName + "</code>", "<code>" + value + "</code>"),
createFix(paramName, value),
ProblemHighlightType.GENERIC_ERROR_OR_WARNING, false));
}
}
}
return problems == null ? null : problems.toArray(new CommonProblemDescriptor[problems.size()]);
}
@Override
protected boolean queryExternalUsagesRequests(@NotNull final RefManager manager, @NotNull final GlobalJavaInspectionContext globalContext,
@NotNull final ProblemDescriptionsProcessor processor) {
manager.iterate(new RefJavaVisitor() {
@Override public void visitElement(@NotNull RefEntity refEntity) {
if (refEntity instanceof RefElement && processor.getDescriptions(refEntity) != null) {
refEntity.accept(new RefJavaVisitor() {
@Override public void visitMethod(@NotNull final RefMethod refMethod) {
globalContext.enqueueMethodUsagesProcessor(refMethod, new GlobalJavaInspectionContext.UsagesProcessor() {
@Override
public boolean process(PsiReference psiReference) {
processor.ignoreElement(refMethod);
return false;
}
});
}
});
}
}
});
return false;
}
@Override
@NotNull
public String getDisplayName() {
return InspectionsBundle.message("inspection.same.parameter.display.name");
}
@Override
@NotNull
public String getGroupDisplayName() {
return GroupNames.DECLARATION_REDUNDANCY;
}
@Override
@NotNull
public String getShortName() {
return "SameParameterValue";
}
@Override
@Nullable
public QuickFix getQuickFix(final String hint) {
if (hint == null) return null;
final int spaceIdx = hint.indexOf(' ');
if (spaceIdx == -1 || spaceIdx >= hint.length() - 1) return null; //invalid hint
final String paramName = hint.substring(0, spaceIdx);
final String value = hint.substring(spaceIdx + 1);
return createFix(paramName, value);
}
protected LocalQuickFix createFix(String paramName, String value) {
throw new UnsupportedOperationException();
}
@Override
@Nullable
public String getHint(@NotNull final QuickFix fix) {
return fix.toString();
}
}