如何在Kotlin中实例化@Interface(注释类)?

时间:2019-10-24 02:08:36

标签: java kotlin annotations

我有这个注释

@interface MyKey {
    String name();
    Class<?> implementingClass();
    int[] thresholds();
}

在Java中,我可以在Java中实例化它。

final class AutoAnnotation_AutoValueUtil_createMyKey implements MyKey {

  private final String name;

  private final Class<?> implementingClass;

  private final int[] thresholds;

  AutoAnnotation_AutoValueUtil_createMyKey(
      String name,
      Class<?> implementingClass,
      int[] thresholds) {
    if (name == null) {
      throw new NullPointerException("Null name");
    }
    this.name = name;
    if (implementingClass == null) {
      throw new NullPointerException("Null implementingClass");
    }
    this.implementingClass = implementingClass;
    if (thresholds == null) {
      throw new NullPointerException("Null thresholds");
    }
    this.thresholds = thresholds.clone()
  ;
  }

  @Override
  public Class<? extends MyKey> annotationType() {
    return MyKey.class;
  }

  @Override
  public String name() {
    return name;
  }

  @Override
  public Class<?> implementingClass() {
    return implementingClass;
  }

  @Override
  public int[] thresholds() {
    return thresholds.clone()
  ;
  }

  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder("@com.elyeproj.daggertwomultibindings.mapmultibindings.MyKey(");
    sb.append("name=");
    appendQuoted(sb, name);
    sb.append(", ");
    sb.append("implementingClass=");
    sb.append(implementingClass);
    sb.append(", ");
    sb.append("thresholds=");
    sb.append(Arrays.toString(thresholds));
    return sb.append(')').toString();
  }

  @Override
  public boolean equals(Object o) {
    if (o == this) {
      return true;
    }
    if (o instanceof MyKey) {
      MyKey that = (MyKey) o;
      return (name.equals(that.name()))
           && (implementingClass.equals(that.implementingClass()))
           && (Arrays.equals(thresholds,
        (that instanceof AutoAnnotation_AutoValueUtil_createMyKey)
            ? ((AutoAnnotation_AutoValueUtil_createMyKey) that).thresholds
            : that.thresholds()));
    }
    return false;
  }

  @Override
  public int hashCode() {
    return
        + (428460789 ^ (name.hashCode()))
            // 428460789 is 127 * "name".hashCode()
        + (-1447677017 ^ (implementingClass.hashCode()))
            // -1447677017 is 127 * "implementingClass".hashCode()
        + (1425672856 ^ (Arrays.hashCode(thresholds)))
            // 1425672856 is 127 * "thresholds".hashCode()
        ;
  }

  private static void appendQuoted(StringBuilder sb, String s) {
    sb.append('"');
    for (int i = 0; i < s.length(); i++) {
      appendEscaped(sb, s.charAt(i));
    }
    sb.append('"');
  }

  private static void appendEscaped(StringBuilder sb, char c) {
    switch (c) {
    case '\\':
    case '"':
    case '\'':
      sb.append('\\').append(c);
      break;
    case '\n':
      sb.append("\\n");
      break;
    case '\r':
      sb.append("\\r");
      break;
    case '\t':
      sb.append("\\t");
      break;
    default:
      if (c < 0x20) {
        sb.append('\\');
        appendWithZeroPadding(sb, Integer.toOctalString(c), 3);
      } else if (c < 0x7f || Character.isLetter(c)) {
        sb.append(c);
      } else {
        sb.append("\\u");
        appendWithZeroPadding(sb, Integer.toHexString(c), 4);
      }
      break;
    }
  }

  private static void appendWithZeroPadding(StringBuilder sb, String s, int width) {
    for (int i = width - s.length(); i > 0; i--) {
      sb.append('0');
    }
    sb.append(s);
  }
}

当我将其转换为Kotlin时,如下所示

internal class AutoAnnotation_AutoValueUtil_createMyKey(
    private val name: String?,
    private val implementingClass: Class<*>?,
    thresholds: IntArray?) : MyKey {

    private val thresholds: IntArray

    init {
        if (name == null) {
            throw NullPointerException("Null name")
        }
        if (implementingClass == null) {
            throw NullPointerException("Null implementingClass")
        }
        if (thresholds == null) {
            throw NullPointerException("Null thresholds")
        }
        this.thresholds = thresholds.clone()
    }

    override fun annotationType(): Class<out MyKey> {
        return MyKey::class.java
    }

    override fun name(): String {
        return name
    }

    override fun implementingClass(): Class<*> {
        return implementingClass
    }

    override fun thresholds(): IntArray {
        return thresholds.clone()
    }

    override fun toString(): String {
        val sb = StringBuilder("@com.elyeproj.daggertwomultibindings.mapmultibindings.MyKey(")
        sb.append("name=")
        appendQuoted(sb, name)
        sb.append(", ")
        sb.append("implementingClass=")
        sb.append(implementingClass)
        sb.append(", ")
        sb.append("thresholds=")
        sb.append(Arrays.toString(thresholds))
        return sb.append(')').toString()
    }

    override fun equals(o: Any?): Boolean {
        if (o === this) {
            return true
        }
        if (o is MyKey) {
            val that = o as MyKey?
            return (name == that!!.name()
                && implementingClass == that.implementingClass()
                && Arrays.equals(thresholds,
                if (that is AutoAnnotation_AutoValueUtil_createMyKey)
                    (that as AutoAnnotation_AutoValueUtil_createMyKey).thresholds
                else
                    that.thresholds()))
        }
        return false
    }

    override fun hashCode(): Int {
        return (+(428460789 xor name.hashCode())
            // 428460789 is 127 * "name".hashCode()
            + (-1447677017 xor implementingClass.hashCode())
            // -1447677017 is 127 * "implementingClass".hashCode()
            + (1425672856 xor Arrays.hashCode(thresholds)))// 1425672856 is 127 * "thresholds".hashCode()
    }

    private fun appendQuoted(sb: StringBuilder, s: String) {
        sb.append('"')
        for (i in 0 until s.length) {
            appendEscaped(sb, s[i])
        }
        sb.append('"')
    }

    private fun appendEscaped(sb: StringBuilder, c: Char) {
        when (c) {
            '\\', '"', '\'' -> sb.append('\\').append(c)
            '\n' -> sb.append("\\n")
            '\r' -> sb.append("\\r")
            '\t' -> sb.append("\\t")
            else -> if (c.toInt() < 0x20) {
                sb.append('\\')
                appendWithZeroPadding(sb, Integer.toOctalString(c.toInt()), 3)
            } else if (c.toInt() < 0x7f || Character.isLetter(c)) {
                sb.append(c)
            } else {
                sb.append("\\u")
                appendWithZeroPadding(sb, Integer.toHexString(c.toInt()), 4)
            }
        }
    }

    private fun appendWithZeroPadding(sb: StringBuilder, s: String, width: Int) {
        for (i in width - s.length downTo 1) {
            sb.append('0')
        }
        sb.append(s)
    }
}

但是它抱怨MyKey is final, and cannot be inherited from。如何在Kotlin中实例化MyKey

0 个答案:

没有答案