// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.abicheck.collector;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.objectweb.asm.Opcodes;

public class Util {

  public static final List<AccessFlag> classFlags = Arrays.asList(
      AccessFlag.make(Opcodes.ACC_PUBLIC, "public"),
      AccessFlag.make(Opcodes.ACC_PRIVATE, "private"),
      AccessFlag.make(Opcodes.ACC_PROTECTED, "protected"),
      AccessFlag.make(Opcodes.ACC_FINAL, "final"),
      AccessFlag.ignored(Opcodes.ACC_SUPER), // Ignored, always set by modern Java
      AccessFlag.make(Opcodes.ACC_INTERFACE, "interface"),
      AccessFlag.make(Opcodes.ACC_ABSTRACT, "abstract"),
      AccessFlag.make(Opcodes.ACC_SYNTHETIC, "synthetic"), // FIXME: Do we want this?
      AccessFlag.make(Opcodes.ACC_ANNOTATION, "annotation"),
      AccessFlag.make(Opcodes.ACC_ENUM, "enum"),
// FIXME: Module support
//      AccessFlag.make(Opcodes.ACC_MODULE, "module")
      AccessFlag.ignored(Opcodes.ACC_DEPRECATED)
  );

  public static final List<AccessFlag> methodFlags = Arrays.asList(
      AccessFlag.make(Opcodes.ACC_PUBLIC, "public"),
      AccessFlag.make(Opcodes.ACC_PRIVATE, "private"),
      AccessFlag.make(Opcodes.ACC_PROTECTED, "protected"),
      AccessFlag.make(Opcodes.ACC_STATIC, "static"),
      AccessFlag.make(Opcodes.ACC_FINAL, "final"),
      AccessFlag.make(Opcodes.ACC_SYNCHRONIZED, "synchronized"),
      AccessFlag.make(Opcodes.ACC_BRIDGE, "bridge"),
      AccessFlag.make(Opcodes.ACC_VARARGS, "varargs"), // FIXME: Do we want this?
      AccessFlag.make(Opcodes.ACC_NATIVE, "native"),
      AccessFlag.make(Opcodes.ACC_ABSTRACT, "abstract"),
      AccessFlag.make(Opcodes.ACC_STRICT, "strict"), // FIXME: Do we want this?
      AccessFlag.make(Opcodes.ACC_SYNTHETIC, "synthetic"), // FIXME: Do we want this?
      AccessFlag.ignored(Opcodes.ACC_DEPRECATED)
  );

  public static final List<AccessFlag> fieldFlags = Arrays.asList(
      AccessFlag.make(Opcodes.ACC_PUBLIC, "public"),
      AccessFlag.make(Opcodes.ACC_PRIVATE, "private"),
      AccessFlag.make(Opcodes.ACC_PROTECTED, "protected"),
      AccessFlag.make(Opcodes.ACC_STATIC, "static"),
      AccessFlag.make(Opcodes.ACC_FINAL, "final"),
      AccessFlag.make(Opcodes.ACC_VOLATILE, "volatile"),
      AccessFlag.make(Opcodes.ACC_TRANSIENT, "transient"),
      AccessFlag.make(Opcodes.ACC_SYNTHETIC, "synthetic"), // FIXME: Do we want this?
      AccessFlag.make(Opcodes.ACC_ENUM, "enum"),
      AccessFlag.ignored(Opcodes.ACC_DEPRECATED)
  );

  public static List<String> convertAccess(int access, List<AccessFlag> flags) {
    List<String> result = new ArrayList<>();
    for (AccessFlag flag : flags) {
      if ((access & flag.bit) != 0 && flag.attribute != null) {
        result.add(flag.attribute);
      }
      access &= ~flag.bit;
    }
    if (access != 0) {
      throw new IllegalArgumentException(String.format("Unexpected access bits: 0x%x", access));
    }
    return result;
  }

  public static class AccessFlag {

    public final int bit;
    public final String attribute;

    private AccessFlag(int bit, String attribute) {
      this.bit = bit;
      this.attribute = attribute;
    }

    private static AccessFlag make(int bit, String attribute) {
      return new AccessFlag(bit, attribute);
    }

    private static AccessFlag ignored(int bit) {
      return new AccessFlag(bit, null);
    }
  }
}

@@ -148,28 +148,35 @@
 ort 
-java.util.ArrayList;
+org.objectweb.asm.Opcodes;%0A
 %0Aimp
@@ -194,17 +194,20 @@
 il.Array
-s
+List
 ;%0Aimport
@@ -226,42 +226,8 @@
 ist;
-%0Aimport org.objectweb.asm.Opcodes;
 %0A%0Apu
@@ -293,37 +293,31 @@
 lassFlags = 
-Arrays.as
 List
+.of
 (%0A      Acce
@@ -919,16 +919,69 @@
 enum%22),%0A
+      AccessFlag.make(Opcodes.ACC_RECORD, %22record%22),%0A
 // FIXME
@@ -1155,37 +1155,31 @@
 thodFlags = 
-Arrays.as
 List
+.of
 (%0A      Acce
@@ -2026,21 +2026,15 @@
 s = 
-Arrays.as
 List
+.of
 (%0A  

Support records in abi-check-plugin