2 * Copyright 2003-2005 Dave Griffith
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
.siyeh
.ig
.fixes
;
18 import com
.intellij
.codeInspection
.ProblemDescriptor
;
19 import com
.intellij
.openapi
.project
.Project
;
20 import com
.intellij
.psi
.*;
21 import com
.intellij
.util
.IncorrectOperationException
;
22 import com
.siyeh
.InspectionGadgetsBundle
;
23 import com
.siyeh
.ig
.InspectionGadgetsFix
;
24 import org
.jetbrains
.annotations
.NonNls
;
25 import org
.jetbrains
.annotations
.NotNull
;
27 public class MakeInitializerExplicitFix
extends InspectionGadgetsFix
{
30 public String
getName(){
31 return InspectionGadgetsBundle
.message(
32 "make.initialization.explicit.quickfix");
35 public void doFix(Project project
, ProblemDescriptor descriptor
)
36 throws IncorrectOperationException
{
37 final PsiElement fieldName
= descriptor
.getPsiElement();
38 final PsiField field
= (PsiField
) fieldName
.getParent();
42 if (field
.getInitializer() != null) {
45 final PsiType type
= field
.getType();
46 final PsiManager psiManager
= PsiManager
.getInstance(project
);
47 final PsiElementFactory factory
= JavaPsiFacade
.getInstance(psiManager
.getProject()).getElementFactory();
48 final PsiExpression initializer
=
49 factory
.createExpressionFromText(getDefaultValue(type
), field
);
50 field
.setInitializer(initializer
);
53 @NonNls private static String
getDefaultValue(PsiType type
){
54 if(PsiType
.INT
.equals(type
)){
56 } else if(PsiType
.LONG
.equals(type
)){
58 } else if(PsiType
.DOUBLE
.equals(type
)){
60 } else if(PsiType
.FLOAT
.equals(type
)){
62 } else if(PsiType
.SHORT
.equals(type
)){
64 } else if(PsiType
.BYTE
.equals(type
)){
66 } else if(PsiType
.BOOLEAN
.equals(type
)){
67 return PsiKeyword
.FALSE
;
68 } else if(PsiType
.CHAR
.equals(type
)){
71 return PsiKeyword
.NULL
;