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.
17 package com
.intellij
.codeInsight
.generation
;
19 import com
.intellij
.openapi
.project
.Project
;
20 import com
.intellij
.psi
.*;
21 import com
.intellij
.psi
.codeStyle
.JavaCodeStyleManager
;
22 import com
.intellij
.psi
.codeStyle
.VariableKind
;
23 import com
.intellij
.psi
.util
.PropertyMemberType
;
24 import com
.intellij
.util
.IncorrectOperationException
;
25 import org
.jetbrains
.annotations
.NotNull
;
27 import java
.util
.Arrays
;
28 import java
.util
.Collections
;
29 import java
.util
.List
;
32 * @author Gregory.Shrago
34 public class GenerateFieldOrPropertyHandler
extends GenerateMembersHandlerBase
{
35 private final String myAttributeName
;
36 private final PsiType myType
;
37 private final PropertyMemberType myMemberType
;
38 private final PsiAnnotation
[] myAnnotations
;
40 public GenerateFieldOrPropertyHandler(String attributeName
, PsiType type
, final PropertyMemberType memberType
, final PsiAnnotation
... annotations
) {
42 myAttributeName
= attributeName
;
44 myMemberType
= memberType
;
45 myAnnotations
= annotations
;
48 protected ClassMember
[] chooseOriginalMembers(PsiClass aClass
, Project project
) {
49 return ClassMember
.EMPTY_ARRAY
;
54 public List
<?
extends GenerationInfo
> generateMemberPrototypes(PsiClass aClass
, ClassMember
[] members
) throws IncorrectOperationException
{
55 final PsiElementFactory psiElementFactory
= JavaPsiFacade
.getInstance(aClass
.getProject()).getElementFactory();
57 final String name
= myMemberType
== PropertyMemberType
.FIELD? myAttributeName
: JavaCodeStyleManager
.getInstance(aClass
.getProject()).propertyNameToVariableName(myAttributeName
, VariableKind
.FIELD
);
58 final PsiField psiField
= psiElementFactory
.createField(name
, myType
);
59 final GenerationInfo
[] objects
= new GenerateGetterAndSetterHandler().generateMemberPrototypes(aClass
, new PsiFieldMember(psiField
));
60 final GenerationInfo getter
= objects
[0];
61 final GenerationInfo setter
= objects
[1];
62 if (myAnnotations
.length
> 0) {
63 final PsiMember targetMember
;
64 switch (myMemberType
) {
65 case FIELD
: targetMember
= psiField
;
67 case GETTER
: targetMember
= getter
.getPsiMember();
69 case SETTER
: targetMember
= setter
.getPsiMember();
71 default: targetMember
= null;
74 assert targetMember
!= null;
75 for (PsiAnnotation annotation
: myAnnotations
) {
76 targetMember
.getModifierList().addAfter(annotation
, null);
79 JavaCodeStyleManager
.getInstance(targetMember
.getProject()).shortenClassReferences(targetMember
.getModifierList());
81 return Arrays
.asList(new PsiGenerationInfo
<PsiField
>(psiField
), getter
, setter
);
83 catch (IncorrectOperationException e
) {
85 return Collections
.emptyList();
89 protected ClassMember
[] getAllOriginalMembers(PsiClass aClass
) {
90 throw new UnsupportedOperationException();
93 protected GenerationInfo
[] generateMemberPrototypes(PsiClass aClass
, ClassMember originalMember
) throws IncorrectOperationException
{
94 throw new UnsupportedOperationException();