2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.intellij
.codeInspection
.sameReturnValue
;
18 import com
.intellij
.analysis
.AnalysisScope
;
19 import com
.intellij
.codeInsight
.daemon
.GroupNames
;
20 import com
.intellij
.codeInspection
.*;
21 import com
.intellij
.codeInspection
.reference
.*;
22 import com
.intellij
.psi
.PsiMethod
;
23 import org
.jetbrains
.annotations
.NotNull
;
24 import org
.jetbrains
.annotations
.Nullable
;
29 public class SameReturnValueInspection
extends GlobalJavaInspectionTool
{
31 public CommonProblemDescriptor
[] checkElement(RefEntity refEntity
, AnalysisScope scope
, InspectionManager manager
, GlobalInspectionContext globalContext
,
32 ProblemDescriptionsProcessor processor
) {
33 if (refEntity
instanceof RefMethod
) {
34 final RefMethod refMethod
= (RefMethod
)refEntity
;
36 if (refMethod
.isConstructor()) return null;
37 if (refMethod
.hasSuperMethods()) return null;
39 String returnValue
= refMethod
.getReturnValueIfSame();
40 if (returnValue
!= null) {
42 if (refMethod
.getDerivedMethods().isEmpty()) {
43 message
= InspectionsBundle
.message("inspection.same.return.value.problem.descriptor", "<code>" + returnValue
+ "</code>");
44 } else if (refMethod
.hasBody()) {
45 message
= InspectionsBundle
.message("inspection.same.return.value.problem.descriptor1", "<code>" + returnValue
+ "</code>");
47 message
= InspectionsBundle
.message("inspection.same.return.value.problem.descriptor2", "<code>" + returnValue
+ "</code>");
50 return new ProblemDescriptor
[] {manager
.createProblemDescriptor(refMethod
.getElement().getNavigationElement(), message
, (LocalQuickFix
[])null, ProblemHighlightType
.GENERIC_ERROR_OR_WARNING
,
59 protected boolean queryExternalUsagesRequests(final RefManager manager
, final GlobalJavaInspectionContext globalContext
,
60 final ProblemDescriptionsProcessor processor
) {
61 manager
.iterate(new RefJavaVisitor() {
62 @Override public void visitElement(RefEntity refEntity
) {
63 if (refEntity
instanceof RefElement
&& processor
.getDescriptions(refEntity
) != null) {
64 refEntity
.accept(new RefJavaVisitor() {
65 @Override public void visitMethod(final RefMethod refMethod
) {
66 globalContext
.enqueueDerivedMethodsProcessor(refMethod
, new GlobalJavaInspectionContext
.DerivedMethodsProcessor() {
67 public boolean process(PsiMethod derivedMethod
) {
68 processor
.ignoreElement(refMethod
);
82 public String
getDisplayName() {
83 return InspectionsBundle
.message("inspection.same.return.value.display.name");
87 public String
getGroupDisplayName() {
88 return GroupNames
.DECLARATION_REDUNDANCY
;
92 public String
getShortName() {
93 return "SameReturnValue";