| /* | |
| * Copyright 2017 Google Inc. | |
| * | |
| * Licensed under the Apache License, Version 2.0 (the "License"); | |
| * you may not use this file except in compliance with the License. | |
| * You may obtain a copy of the License at | |
| * | |
| * https://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software | |
| * distributed under the License is distributed on an "AS IS" BASIS, | |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| * See the License for the specific language governing permissions and | |
| * limitations under the License. | |
| */ | |
| package jsmethod; | |
| import jsinterop.annotations.JsConstructor; | |
| import jsinterop.annotations.JsMethod; | |
| import jsinterop.annotations.JsType; | |
| public class JsMethodExample { | |
| abstract static class Base<T> { | |
| @JsMethod | |
| void m(T t) {} | |
| void o(String s) {} | |
| } | |
| interface I { | |
| @JsMethod(name = "mString") | |
| void m(String s); | |
| } | |
| interface InterfaceWithStaticMethod { | |
| @JsMethod | |
| public static void s() {} | |
| } | |
| // Regression test for b/124227197 | |
| static class Sub extends Base<String> implements I { | |
| public void m(String s) {} | |
| // Package-private JsMethod overriding a non-JsMethod. | |
| @JsMethod | |
| void o(String s) {} | |
| } | |
| @JsType | |
| static class SubJsType extends Base<String> { | |
| public void m(String s) {} | |
| // not a JsMethod | |
| static void n() {} | |
| } | |
| abstract static class AbstractChildTypeWithNoNewJsMembers extends SubJsType { | |
| public void p() {} | |
| } | |
| abstract static class AbstractGrandChildTypeWithNoNewJsMembers | |
| extends AbstractChildTypeWithNoNewJsMembers { | |
| public void q() {} | |
| } | |
| static class ConcreteGreatGrandChildTypeWithNewJsMembers | |
| extends AbstractGrandChildTypeWithNoNewJsMembers { | |
| @JsMethod | |
| public void r(String s) {} | |
| } | |
| @JsType | |
| static class SubGenericsJsType<T> extends Base<T> { | |
| public void m(T s) {} | |
| } | |
| @JsType | |
| static class SubJsTypeWithRenamedJsMethod extends Base<String> { | |
| @JsMethod(name = "renamedM") | |
| public void m(String s) {} | |
| } | |
| interface InterfaceWithMethod { | |
| void m(); | |
| void n(); | |
| } | |
| interface InterfaceExposingJsMethods extends InterfaceWithMethod { | |
| @JsMethod | |
| default void m() {} | |
| @JsMethod | |
| void n(); | |
| } | |
| static class SuperClassWithFinalMethod { | |
| public final void n() {} | |
| } | |
| // This class inherits implementations from supertypes and needs to exposes both the Java | |
| // contracts and js contracts. | |
| static class ExposesOverrideableJsMethod extends SuperClassWithFinalMethod | |
| implements InterfaceExposingJsMethods {} | |
| abstract static class AbstractTypeExposingJsMethod { | |
| @JsMethod | |
| public abstract void abstractJsMethod(String s); | |
| } | |
| static class AbstractTypeImpl extends AbstractTypeExposingJsMethod { | |
| @Override | |
| public void abstractJsMethod(String s) {} | |
| } | |
| // Repro for b/545779164 | |
| interface ParameterizedInterface<T> { | |
| @JsMethod | |
| default T m() { | |
| return null; | |
| } | |
| } | |
| static class ParameterizedInterfaceImpl implements ParameterizedInterface<String> {} | |
| // Repro for b/587632968 | |
| static class AParameterizedInterfaceImpl implements ParameterizedInterface<SubJsType> {} | |
| static class BParameterizedInterfaceImpl | |
| implements ParameterizedInterface<SubJsTypeWithRenamedJsMethod> {} | |
| @JsType(isNative = true, namespace = "jsmethod", name = "Foo") | |
| static class NativeFoo { | |
| public native long takesAndReturnsPrimitiveLong(long l); | |
| } | |
| @JsType(namespace = "jsmethod") | |
| @SuppressWarnings("unusable-by-js") | |
| static class Foo { | |
| public long takesAndReturnsPrimitiveLong(long l) { | |
| return l; | |
| } | |
| } | |
| static class ExposesMethodWithDifferentTypes<T> { | |
| @JsMethod | |
| public void m( | |
| String s, | |
| Double d, | |
| Long l, | |
| long pl, | |
| @SuppressWarnings("unusable-by-js") Comparable c, | |
| Object o, | |
| T t, | |
| NativeFoo nf, | |
| ExposesMethodWithDifferentTypes<T> e) {} | |
| @JsMethod | |
| @SuppressWarnings("unusable-by-js") | |
| public long roundtripLong(long l) { | |
| return l; | |
| } | |
| @JsMethod | |
| public long callNative(NativeFoo nf, long l) { | |
| return nf.takesAndReturnsPrimitiveLong(l); | |
| } | |
| } | |
| interface InterfaceWithJsMethod { | |
| @JsMethod | |
| void m(); | |
| } | |
| abstract static class AbstractClassExposingJsMethodM implements InterfaceWithJsMethod {} | |
| abstract static class AbstractSubclassWithNoMembers extends AbstractClassExposingJsMethodM {} | |
| static class ConcreteSubclassWithNoNewJsMembers extends AbstractSubclassWithNoMembers { | |
| public void m() {} | |
| } | |
| static class ConcreteSubclassWithJsConstructor extends ConcreteSubclassWithNoNewJsMembers { | |
| @JsConstructor | |
| public ConcreteSubclassWithJsConstructor() {} | |
| } | |
| static class JsKeywords { | |
| @JsMethod | |
| public void test(int in, int let) {} | |
| } | |
| } |