/*
 * Copyright 2003-2005 Dave Griffith
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.siyeh.ipp.initialization;

import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.ipp.base.Intention;
import com.siyeh.ipp.base.PsiElementPredicate;
import com.siyeh.ipp.psiutils.HighlightUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;

import java.util.Collections;

public class SplitDeclarationAndInitializationIntention extends Intention {

    @NotNull
    protected PsiElementPredicate getElementPredicate() {
        return new SplitDeclarationAndInitializationPredicate();
    }

    public void processIntention(@NotNull PsiElement element)
            throws IncorrectOperationException {
        final PsiField field = (PsiField)element.getParent();
        field.normalizeDeclaration();
        final PsiExpression initializer = field.getInitializer();
        if (initializer == null) {
            return;
        }
        final String initializerText = initializer.getText();
        final PsiClass containingClass = field.getContainingClass();
        if (containingClass == null) {
            return;
        }
        final boolean fieldIsStatic =
                field.hasModifierProperty(PsiModifier.STATIC);
        final PsiClassInitializer[] classInitializers =
                containingClass.getInitializers();
        PsiClassInitializer classInitializer = null;
        final int fieldOffset = field.getTextOffset();
        for (PsiClassInitializer existingClassInitializer : classInitializers) {
            final int initializerOffset =
                    existingClassInitializer.getTextOffset();
            if (initializerOffset <= fieldOffset) {
                continue;
            }
            final boolean initializerIsStatic =
                    existingClassInitializer.hasModifierProperty(
                            PsiModifier.STATIC);
            if (initializerIsStatic == fieldIsStatic) {
                classInitializer = existingClassInitializer;
                break;
            }
        }
        final PsiManager manager = field.getManager();
      final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
        if (classInitializer == null) {
            classInitializer = elementFactory.createClassInitializer();
            classInitializer = (PsiClassInitializer)
                    containingClass.addAfter(classInitializer, field);
        }
        final PsiCodeBlock body = classInitializer.getBody();
        @NonNls final String initializationStatementText =
                field.getName() + " = " + initializerText + ';';
        final PsiExpressionStatement statement =
                (PsiExpressionStatement)elementFactory.createStatementFromText(
                        initializationStatementText, body);
        final PsiElement addedElement = body.add(statement);
        if (fieldIsStatic) {
            final PsiModifierList modifierList =
                    classInitializer.getModifierList();
            if (modifierList != null) {
                modifierList.setModifierProperty(PsiModifier.STATIC, true);
            }
        }
        initializer.delete();
        final CodeStyleManager codeStyleManager = manager.getCodeStyleManager();
        codeStyleManager.reformat(field);
        codeStyleManager.reformat(classInitializer);
        HighlightUtil.highlightElements(Collections.singleton(addedElement));
    }
}
@@ -21,9 +21,9 @@
 -200
-5
+8
  Dav
@@ -32,16 +32,33 @@
 Griffith
+, Bas Leijdekkers
 %0A *%0A * L
@@ -776,24 +776,69 @@
 nException;%0A
+import com.intellij.openapi.project.Project;%0A
 import com.s
@@ -1671,33 +1671,453 @@
 Text
- = initializer.getText();
+;%0A        if (initializer instanceof PsiArrayInitializerExpression) %7B%0A            final PsiType type = initializer.getType();%0A            if (type == null) %7B%0A                initializerText = initializer.getText();%0A            %7D else %7B%0A                initializerText = %22new %22 + type.getCanonicalText() +%0A                        initializer.getText();%0A            %7D%0A        %7D else %7B%0A            initializerText = initializer.getText();%0A        %7D
 %0A   
@@ -3222,24 +3222,80 @@
 tManager();%0A
+        final Project project = manager.getProject();%0A  
       final 
@@ -3328,16 +3328,32 @@
 actory =
+%0A               
  JavaPsi
@@ -3371,36 +3371,23 @@
 nstance(
-manager.getP
+p
 roject
-()
 ).getEle

fix split declaration and initialization of field for array initializer expression