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
.codeInsight
.generation
;
18 import com
.intellij
.codeInsight
.CodeInsightBundle
;
19 import com
.intellij
.codeInsight
.CodeInsightSettings
;
20 import com
.intellij
.codeInsight
.generation
.ui
.GenerateEqualsWizard
;
21 import com
.intellij
.openapi
.application
.ApplicationManager
;
22 import com
.intellij
.openapi
.diagnostic
.Logger
;
23 import com
.intellij
.openapi
.project
.Project
;
24 import com
.intellij
.openapi
.ui
.DialogWrapper
;
25 import com
.intellij
.openapi
.ui
.Messages
;
26 import com
.intellij
.openapi
.util
.Computable
;
27 import com
.intellij
.psi
.PsiAnonymousClass
;
28 import com
.intellij
.psi
.PsiClass
;
29 import com
.intellij
.psi
.PsiField
;
30 import com
.intellij
.psi
.PsiMethod
;
31 import com
.intellij
.psi
.search
.GlobalSearchScope
;
32 import com
.intellij
.util
.IncorrectOperationException
;
33 import org
.jetbrains
.annotations
.NotNull
;
35 import java
.util
.Collections
;
36 import java
.util
.List
;
41 public class GenerateEqualsHandler
extends GenerateMembersHandlerBase
{
42 private static final Logger LOG
= Logger
.getInstance("#com.intellij.codeInsight.generation.GenerateEqualsHandler");
43 private PsiField
[] myEqualsFields
= null;
44 private PsiField
[] myHashCodeFields
= null;
45 private PsiField
[] myNonNullFields
= null;
46 private static final PsiElementClassMember
[] DUMMY_RESULT
= new PsiElementClassMember
[1]; //cannot return empty array, but this result won't be used anyway
48 public GenerateEqualsHandler() {
52 protected ClassMember
[] chooseOriginalMembers(PsiClass aClass
, Project project
) {
53 myEqualsFields
= null;
54 myHashCodeFields
= null;
55 myNonNullFields
= PsiField
.EMPTY_ARRAY
;
58 GlobalSearchScope scope
= aClass
.getResolveScope();
59 final PsiMethod equalsMethod
= GenerateEqualsHelper
.findMethod(aClass
, GenerateEqualsHelper
.getEqualsSignature(project
, scope
));
60 final PsiMethod hashCodeMethod
= GenerateEqualsHelper
.findMethod(aClass
, GenerateEqualsHelper
.getHashCodeSignature());
62 boolean needEquals
= equalsMethod
== null;
63 boolean needHashCode
= hashCodeMethod
== null;
64 if (!needEquals
&& !needHashCode
) {
65 String text
= aClass
instanceof PsiAnonymousClass
66 ? CodeInsightBundle
.message("generate.equals.and.hashcode.already.defined.warning.anonymous")
67 : CodeInsightBundle
.message("generate.equals.and.hashcode.already.defined.warning", aClass
.getQualifiedName());
69 if (Messages
.showYesNoDialog(project
, text
,
70 CodeInsightBundle
.message("generate.equals.and.hashcode.already.defined.title"),
71 Messages
.getQuestionIcon()) == DialogWrapper
.OK_EXIT_CODE
) {
72 if (!ApplicationManager
.getApplication().runWriteAction(new Computable
<Boolean
>() {
73 public Boolean
compute() {
75 equalsMethod
.delete();
76 hashCodeMethod
.delete();
79 catch (IncorrectOperationException e
) {
87 needEquals
= needHashCode
= true;
94 GenerateEqualsWizard wizard
= new GenerateEqualsWizard(project
, aClass
, needEquals
, needHashCode
);
96 if (!wizard
.isOK()) return null;
97 myEqualsFields
= wizard
.getEqualsFields();
98 myHashCodeFields
= wizard
.getHashCodeFields();
99 myNonNullFields
= wizard
.getNonNullFields();
104 protected List
<?
extends GenerationInfo
> generateMemberPrototypes(PsiClass aClass
, ClassMember
[] originalMembers
) throws IncorrectOperationException
{
105 Project project
= aClass
.getProject();
106 final boolean useInstanceofToCheckParameterType
= CodeInsightSettings
.getInstance().USE_INSTANCEOF_ON_EQUALS_PARAMETER
;
108 GenerateEqualsHelper helper
= new GenerateEqualsHelper(project
, aClass
, myEqualsFields
, myHashCodeFields
, myNonNullFields
,
109 useInstanceofToCheckParameterType
);
110 return OverrideImplementUtil
.convert2GenerationInfos(helper
.generateMembers());
113 protected ClassMember
[] getAllOriginalMembers(PsiClass aClass
) {
117 protected GenerationInfo
[] generateMemberPrototypes(PsiClass aClass
, ClassMember originalMember
) {
121 protected void cleanup() {
123 myEqualsFields
= null;
124 myHashCodeFields
= null;
125 myNonNullFields
= null;