@Retention(value=RUNTIME)
@Target(value={METHOD,FIELD,TYPE})
public @interface JsAccessible
Only public non-static methods and fields annotated with JsAccessible
or declared in
the class annotated with JsAccessible
can be accessed from JavaScript. Annotated
protected, private, or package-private methods and fields (or methods and fields declared in a
class with such modifiers) remain inaccessible from JavaScript.
Examples:
Annotated methods and fields of a public top level class are accessible:
public final class TopClass { @JsAccessible public Object accessibleField; @JsAccessible public void accessibleMethod() {} }
Annotated methods and fields of a public static nested class are accessible:
public final class TopClass { public static class NestedClass { @JsAccessible public Object accessibleField; @JsAccessible public void accessibleMethod() {} } }
Unannotated methods and fields of an annotated class are accessible:
@JsAccessible public final class TopClass { public Object accessibleField; public void accessibleMethod() {} }
Methods and fields of a base annotated class are accessible from inheritors:
public final class TopClass { @JsAccessible public static class BaseNestedClass { public Object accessibleFieldFromInheritor; public void accessibleMethodFromInheritor() {} } public static class NestedClass extends BaseNestedClass { public Object inaccessibleField; public void inaccessibleMethod() {} } }
Inherited methods and fields are not accessible if they or the class they are declared in are not annotated:
public final class TopClass { public static class BaseNestedClass { public Object inaccessibleField; public void inaccessibleMethod() {} } @JsAccessible public static class NestedClass extends BaseNestedClass { public Object accessibleField; public void accessibleMethod() {} } }
Overridden accessible methods become inaccessible. To make them accessible, annotate them or
their declaring class with JsAccessible
.
public final class TopClass { public static class BaseNestedClass { @JsAccessible public void method() {} } public static class NestedClass extends BaseNestedClass { @Override public void method() {} // inaccessible } public static class AnotherNestedClass extends BaseNestedClass { @Override @JsAccessible public void method() {} // accessible } }
Overridden default accessible interface methods become inaccessible.
public static class TopClass { public interface NestedInterface { @JsAccessible default void method() { } } public static class AccessibleImplementor implements NestedInterface { // inherited annotated default method is accessible } public static class InaccessibleImplementor implements NestedInterface { @Override public void method() { } // inaccessible } }
If the signature of the Java method has a primitive numeric parameter, then the number transferred from JavaScript will be checked for the possibility of converting to the Java parameter type. If the conversion can be performed without data loss and no other suitable overloaded methods were found, the method will be invoked.
If more than one method that can accept the passed parameters does exist, JavaScript throws an exception to indicate that the requested method call is ambiguous and cannot be performed.
If no methods or fields that correspond to the requested name found, JavaScript throws an exception indicating that the requested member does not exist.
If both method and field with the same name requested by JavaScript do exist, JavaScript throws an exception to indicate that the requested member is ambiguous and cannot be accessed.