/*
 * Copyright 2003-2018 Dave Griffith, Bas Leijdekkers
 *
 * 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.ig.serialization;

import com.intellij.openapi.util.WriteExternalException;
import com.intellij.psi.*;
import com.intellij.util.ui.CheckBox;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.psiutils.SerializationUtils;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;

public class SerializableHasSerializationMethodsInspection extends SerializableInspectionBase {

  public boolean ignoreClassWithoutFields = false;

  @Override
  @NotNull
  public String getDisplayName() {
    return InspectionGadgetsBundle.message(
      "serializable.has.serialization.methods.display.name");
  }

  @Override
  @NotNull
  public String buildErrorString(Object... infos) {
    final boolean hasReadObject = ((Boolean)infos[0]).booleanValue();
    final boolean hasWriteObject = ((Boolean)infos[1]).booleanValue();
    if (!hasReadObject && !hasWriteObject) {
      return InspectionGadgetsBundle.message(
        "serializable.has.serialization.methods.problem.descriptor");
    }
    else if (hasReadObject) {
      return InspectionGadgetsBundle.message(
        "serializable.has.serialization.methods.problem.descriptor1");
    }
    else {
      return InspectionGadgetsBundle.message(
        "serializable.has.serialization.methods.problem.descriptor2");
    }
  }

  @NotNull
  @Override
  protected JComponent[] createAdditionalOptions() {
    return new JComponent[] {new CheckBox(InspectionGadgetsBundle.message("serializable.has.serialization.methods.ignore.option"),
                                          this, "ignoreClassWithoutFields")};
  }

  @Override
  public void writeSettings(@NotNull Element node) throws WriteExternalException {
    defaultWriteSettings(node, "ignoreClassWithoutFields");
    writeBooleanOption(node, "ignoreClassWithoutFields", false);
  }

  @Override
  public BaseInspectionVisitor buildVisitor() {
    return new SerializableHasSerializationMethodsVisitor();
  }

  private class SerializableHasSerializationMethodsVisitor
    extends BaseInspectionVisitor {

    @Override
    public void visitClass(@NotNull PsiClass aClass) {
      // no call to super, so it doesn't drill down
      if (aClass.isInterface() || aClass.isAnnotationType() ||
          aClass.isEnum()) {
        return;
      }
      if (aClass instanceof PsiTypeParameter ||
          aClass instanceof PsiEnumConstantInitializer) {
        return;
      }
      if (ignoreAnonymousInnerClasses &&
          aClass instanceof PsiAnonymousClass) {
        return;
      }
      if (!SerializationUtils.isSerializable(aClass)) {
        return;
      }
      final boolean hasReadObject =
        SerializationUtils.hasReadObject(aClass);
      final boolean hasWriteObject =
        SerializationUtils.hasWriteObject(aClass);
      if (hasWriteObject && hasReadObject) {
        return;
      }
      if (isIgnoredSubclass(aClass)) {
        return;
      }
      if (ignoreClassWithoutFields) {
        final PsiField[] fields = aClass.getFields();
        boolean hasField = false;
        for (PsiField field : fields) {
          if (field.hasModifierProperty(PsiModifier.STATIC)) {
            continue;
          }
          hasField = true;
          break;
        }
        if (!hasField) {
          return;
        }
      }
      registerClassError(aClass, Boolean.valueOf(hasReadObject),
                         Boolean.valueOf(hasWriteObject));
    }
  }
}
@@ -21,9 +21,9 @@
 -201
-8
+9
  Dav
@@ -2370,16 +2370,100 @@
 ption %7B%0A
+    if (!superClassList.isEmpty()) superClassString = formatString(superClassList);%0A
     defa
@@ -2768,20 +2768,16 @@
 sVisitor
-%0A   
  extends
@@ -2985,26 +2985,16 @@
 ype() %7C%7C
-%0A         
  aClass.
@@ -3076,26 +3076,16 @@
 meter %7C%7C
-%0A         
  aClass 
@@ -3189,26 +3189,16 @@
 asses &&
-%0A         
  aClass 
@@ -3364,32 +3364,24 @@
 ReadObject =
-%0A       
  Serializati
@@ -3447,24 +3447,16 @@
 Object =
-%0A       
  Seriali
@@ -4069,33 +4069,8 @@
 ct),
-%0A                        
  Boo

store settings correctly (IDEA-224498)