@Retention(value=RUNTIME)
@Target(value={METHOD,TYPE})
public @interface JsAccessible
Only public methods annotated with JsAccessible
or declared in the class annotated
with JsAccessible
can be accessed from JavaScript. Annotated protected,
private, or package-private methods (or methods declared in a class with such modifiers) remain
inaccessible from JavaScript.
Examples:
Annotated methods of a public top level class are accessible:
public final class TopClass { @JsAccessible public void accessibleMethod() {} }
Annotated methods of a public static nested class are accessible:
public final class TopClass { public static class NestedClass { @JsAccessible public void accessibleMethod() {} } }
Unannotated methods of an annotated class are accessible:
@JsAccessible public final class TopClass { public void accessibleMethod() {} }
Methods of a base annotated class are accessible from inheritors:
public final class TopClass { @JsAccessible public static class BaseNestedClass { public void accessibleMethodFromInheritor() {} } public static class NestedClass extends BaseNestedClass { public void inaccessibleMethod() {} } }
Inherited methods are not accessible if they or the class they are declared in are not annotated:
public final class TopClass { public static class BaseNestedClass { public void inaccessibleMethod() {} } @JsAccessible public static class NestedClass extends BaseNestedClass { 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 that can accept the passed parameters found, JavaScript throws an exception indicating that the requested method does not exist.