IDEADEV-31824 (Incorrect "manual array copy" warning)
[fedora-idea.git] / plugins / InspectionGadgets / src / com / siyeh / ig / fixes / MakeInitializerExplicitFix.java
blob93c0522b73e19997295c68d271150ea3066e6d3c
1 /*
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{
29 @NotNull
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();
39 if (field == null) {
40 return;
42 if (field.getInitializer() != null) {
43 return;
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)){
55 return "0";
56 } else if(PsiType.LONG.equals(type)){
57 return "0L";
58 } else if(PsiType.DOUBLE.equals(type)){
59 return "0.0";
60 } else if(PsiType.FLOAT.equals(type)){
61 return "0.0F";
62 } else if(PsiType.SHORT.equals(type)){
63 return "(short)0";
64 } else if(PsiType.BYTE.equals(type)){
65 return "(byte)0";
66 } else if(PsiType.BOOLEAN.equals(type)){
67 return PsiKeyword.FALSE;
68 } else if(PsiType.CHAR.equals(type)){
69 return "(char)0";
71 return PsiKeyword.NULL;