如何在导出的 API 中声明内部方法?

How to declare internal methods in exported APIs?

此问题与

有关(但不重复)

如何定义只能从内部访问的枚举方法 类?

具体来说:

影响:

我能想到的阻止用户调用 public 方法的唯一方法是让它引用非导出类型。例如:

public enum Color
{
  RED
  {
    public void operation(NotExported ignore)
    {
      // ...
    }
  },
  GREEN,
  {
    public void operation(NotExported ignore)
    {
      // ...
    }
  },
  BLUE;
  {
    public void operation(NotExported ignore)
    {
      // ...
    }
  };

  /**
   * Carries out an internal operation.
   *
   * @param ignore prevent users from invoking this method
   */
  public abstract void operation(NotExported ignore);
}

不幸的是,当我这样做时,编译器抱怨导出的 API 引用了非导出类型。有更好的方法吗?

Alan Bateman pointed out a 被 JDK 使用。这允许内部 类 跨包共享受包保护的方法,而无需诉诸反射。