├── .bazelrc ├── .bazelversion ├── .github └── workflows │ └── ci.yaml ├── .gitignore ├── BUILD ├── CONTRIBUTING.md ├── LICENSE ├── MODULE.bazel ├── README.md ├── build_test.sh ├── internal_do_not_use ├── BUILD └── format_srcjar.sh ├── java └── jsinterop │ └── generator │ ├── closure │ ├── BUILD │ ├── ClosureJsInteropGenerator.java │ ├── JarFileCreator.java │ ├── JavaFile.java │ ├── Options.java │ ├── Runner.java │ ├── helper │ │ ├── AbstractNoOpVisitor.java │ │ ├── BUILD │ │ ├── ClosureTypeRegistry.java │ │ └── GenerationContext.java │ └── visitor │ │ ├── AbstractClosureVisitor.java │ │ ├── AnonymousTypeCollector.java │ │ ├── BUILD │ │ ├── InheritanceVisitor.java │ │ ├── MemberCollector.java │ │ ├── ThisTemplateTypeVisitor.java │ │ ├── TypeCollector.java │ │ └── TypeParameterCollector.java │ ├── helper │ ├── AbortError.java │ ├── AbstractTypeRegistry.java │ ├── BUILD │ ├── GeneratorUtils.java │ ├── ModelHelper.java │ └── Problems.java │ ├── model │ ├── AbstractTypeReference.java │ ├── AccessModifier.java │ ├── Annotation.java │ ├── AnnotationType.java │ ├── ArrayTypeReference.java │ ├── BUILD │ ├── CastExpression.java │ ├── ClassDecomposition.java │ ├── DelegableTypeReference.java │ ├── Entity.java │ ├── EntityKind.java │ ├── Expression.java │ ├── ExpressionStatement.java │ ├── Field.java │ ├── HasName.java │ ├── HasTypeParameters.java │ ├── InstanceOfExpression.java │ ├── JavaTypeReference.java │ ├── LiteralExpression.java │ ├── Method.java │ ├── MethodInvocation.java │ ├── ModelVisitor.java │ ├── Parameter.java │ ├── ParametrizedTypeReference.java │ ├── PredefinedTypes.java │ ├── Program.java │ ├── ReturnStatement.java │ ├── Statement.java │ ├── Type.java │ ├── TypeQualifier.java │ ├── TypeReference.java │ ├── TypeVariableReference.java │ ├── UnionTypeReference.java │ └── WildcardTypeReference.java │ ├── visitor │ ├── AbstractJsOverlayMethodCreator.java │ ├── BUILD │ ├── ClosureOptionalParameterCleaner.java │ ├── ConstantRewriter.java │ ├── ConstructorVisitor.java │ ├── DependencyFileWriter.java │ ├── DictionaryTypeVisitor.java │ ├── DuplicatedTypesUnifier.java │ ├── EmptyNamespaceFilter.java │ ├── FieldsConverter.java │ ├── FixReferencesToDuplicatedTypes.java │ ├── FixTypeParametersOfReferencesToSyntheticTypes.java │ ├── FixTypeParametersOfSyntheticTypes.java │ ├── FunctionalInterfaceAnnotator.java │ ├── IntegerEntitiesConverter.java │ ├── JavaArrayParameterJsOverlayCreator.java │ ├── JsConstructorFnParameterJsOverlayCreator.java │ ├── MembersClassCleaner.java │ ├── NamespaceAttributeRewriter.java │ ├── ObjectParameterJsOverlayCreator.java │ ├── OptionalParameterHandler.java │ ├── ParametrizedObjectReferenceRewriter.java │ ├── ResolveVarargsAmbiguity.java │ ├── TypeReferenceUtils.java │ ├── UnionTypeHelperTypeCreator.java │ ├── UnionTypeMethodParameterHandler.java │ ├── ValidJavaIdentifierVisitor.java │ ├── VisitorHelper.java │ └── WildcardTypeCreator.java │ └── writer │ ├── AnnotationWriter.java │ ├── BUILD │ ├── CodeWriter.java │ ├── FieldWriter.java │ ├── MethodWriter.java │ └── TypeWriter.java ├── javatests └── jsinterop │ └── generator │ ├── BUILD │ ├── closure │ ├── BUILD │ └── TestUtil.java │ ├── externs │ ├── bigint │ │ ├── BUILD │ │ ├── Foo.java.txt │ │ └── bigint.js │ ├── dependency │ │ ├── BUILD │ │ ├── MyLibGlobal.java.txt │ │ ├── MyLibGlobal__Constants.java.txt │ │ ├── MyLibThirdPartyClass.java.txt │ │ ├── SimpleClass.java.txt │ │ ├── SimpleInterface.java.txt │ │ ├── dependency.js │ │ ├── parentthirdparty │ │ │ ├── BUILD │ │ │ └── parentthirdparty.js │ │ ├── thirdparty │ │ │ ├── BUILD │ │ │ ├── name_mapping.txt │ │ │ └── thirdparty.js │ │ └── thirdparty2 │ │ │ ├── BUILD │ │ │ └── thirdparty2.js │ ├── dictionarytype │ │ ├── BUILD │ │ ├── DictionaryType.java.txt │ │ ├── SimpleDictionaryType.java.txt │ │ └── dictionarytype.js │ ├── entitiesrenaming │ │ ├── BUILD │ │ ├── Foo.java.txt │ │ ├── Global.java.txt │ │ ├── SimpleInterface.java.txt │ │ ├── entitiesrenaming.js │ │ └── renaming.txt │ ├── enums │ │ ├── BUILD │ │ ├── Bar.java.txt │ │ ├── Baz.java.txt │ │ ├── Foo.java.txt │ │ └── enums.js │ ├── functionalinterface │ │ ├── BUILD │ │ ├── InterfaceWithOneMethod.java.txt │ │ ├── InterfaceWithTwoMethods.java.txt │ │ ├── StructuralInterface.java.txt │ │ └── functionalinterface.js │ ├── functiontype │ │ ├── AliasedFunctionType.java.txt │ │ ├── BUILD │ │ ├── Global.java.txt │ │ ├── SimpleClass.java.txt │ │ ├── SimpleInterface.java.txt │ │ ├── SimpleModule.java.txt │ │ └── functiontype.js │ ├── generics │ │ ├── AnonymousTypes.java.txt │ │ ├── BUILD │ │ ├── Bar.java.txt │ │ ├── BarChild.java.txt │ │ ├── ExtendInterfaceWithGeneric.java.txt │ │ ├── InterfaceWithGeneric.java.txt │ │ ├── RecordWithGeneric.java.txt │ │ ├── SimpleClass.java.txt │ │ ├── SimpleClassChild.java.txt │ │ ├── SimpleInterface.java.txt │ │ └── generics.js │ ├── globalscope │ │ ├── BUILD │ │ ├── Global.java.txt │ │ ├── Global__Constants.java.txt │ │ └── globalscope.js │ ├── inheritance │ │ ├── BUILD │ │ ├── GreatParentClass.java.txt │ │ ├── GreatParentInterface.java.txt │ │ ├── InterfaceWithStructuralType.java.txt │ │ ├── InterfaceWithStructuralTypeImpl.java.txt │ │ ├── Parent1Interface.java.txt │ │ ├── Parent2Interface.java.txt │ │ ├── ParentClass.java.txt │ │ ├── SimpleClass.java.txt │ │ └── inheritance.js │ ├── integerentities │ │ ├── BUILD │ │ ├── Foo.java.txt │ │ ├── Foo__Constants.java.txt │ │ ├── Global.java.txt │ │ ├── integerentities.js │ │ ├── integerentities.txt │ │ └── renaming.txt │ ├── iobjectiarraylike │ │ ├── BUILD │ │ ├── Bar.java.txt │ │ ├── Baz.java.txt │ │ ├── Foo.java.txt │ │ ├── Varargs.java.txt │ │ └── iobjectiarraylike.js │ ├── modules │ │ ├── BUILD │ │ ├── Interface.java.txt │ │ ├── InterfaceFromNestedNamespace.java.txt │ │ ├── Namespace.java.txt │ │ ├── NamespacedClass.java.txt │ │ ├── NamespacedEnum.java.txt │ │ ├── NamespacedFunctionType.java.txt │ │ ├── NamespacedInterface.java.txt │ │ ├── NamespacedRecord.java.txt │ │ ├── NamespacedTypeDefOfRecord.java.txt │ │ ├── Nestednamespace.java.txt │ │ ├── Othernamespace.java.txt │ │ └── modules.js │ ├── natives │ │ ├── BUILD │ │ ├── JsArray.java │ │ ├── JsIterable.java │ │ ├── JsObject.java │ │ ├── native.js │ │ └── natives.types │ ├── nestedclasses │ │ ├── BUILD │ │ ├── Bar.java.txt │ │ ├── Foo.java.txt │ │ └── nestedclasses.js │ ├── nullabletypes │ │ ├── BUILD │ │ ├── ClassWithNullableRefs.java.txt │ │ ├── NullableFunctionType.java.txt │ │ ├── NullableTypeDefOfRecord.java.txt │ │ ├── SimpleClass.java.txt │ │ ├── SimpleEnum.java.txt │ │ ├── SimpleInterface.java.txt │ │ ├── SimpleRecord.java.txt │ │ └── nullabletypes.js │ ├── optionalparameters │ │ ├── BUILD │ │ ├── SimpleClass.java.txt │ │ ├── SimpleInterface.java.txt │ │ └── optionalparameters.js │ ├── simpleclass │ │ ├── BUILD │ │ ├── DeprecatedInterface.java.txt │ │ ├── PrivateClass.java.txt │ │ ├── SimpleClass.java.txt │ │ ├── SimpleClass__Constants.java.txt │ │ ├── SimpleInterface.java.txt │ │ ├── SimpleInterface__Constants.java.txt │ │ ├── SimpleStructuralInterface.java.txt │ │ └── simpleclass.js │ ├── structuraltypes │ │ ├── BUILD │ │ ├── ClassInModule.java.txt │ │ ├── FooBar.java.txt │ │ ├── FooBar2.java.txt │ │ ├── Global.java.txt │ │ ├── InnerStructuralType.java.txt │ │ ├── SimpleClass.java.txt │ │ ├── SimpleModule.java.txt │ │ └── structuraltypes.js │ ├── uniontypes │ │ ├── BUILD │ │ ├── Child.java.txt │ │ ├── Foo.java.txt │ │ ├── ParentInterface.java.txt │ │ └── uniontypes.js │ └── wildcardtypes │ │ ├── BUILD │ │ ├── Bar.java.txt │ │ ├── Foo.java.txt │ │ ├── wildcardtypes.js │ │ └── wildcardtypes.txt │ ├── gen_jsinterop_golden.py │ ├── jsinterop_generator_test.bzl │ ├── jsinterop_generator_test.sh │ └── unit │ └── undefinedtypes │ ├── BUILD │ └── UndefinedTypesTest.java ├── jsinterop_generator.bzl ├── jsinterop_generator_import.bzl ├── maven ├── pom-closure-generator.xml └── release_jsinterop_generator.sh └── third_party └── BUILD /.bazelrc: -------------------------------------------------------------------------------- 1 | # Recommended bazel settings for working with J2CL. 2 | # You can copy this into root of your workspace. 3 | 4 | build --watchfs 5 | 6 | build --spawn_strategy=local 7 | 8 | build --strategy=J2cl=worker 9 | build --strategy=J2clStrip=worker 10 | build --strategy=J2clRta=worker 11 | 12 | build --strategy=J2wasm=worker 13 | build --strategy=J2wasmStrip=worker 14 | build --strategy=J2wasmApp=local 15 | 16 | build --strategy=Closure=worker 17 | build --strategy=Javac=worker 18 | build --strategy=JavaIjar=local 19 | build --strategy=JavaDeployJar=local 20 | build --strategy=JavaSourceJar=local 21 | build --strategy=Turbine=local 22 | 23 | # --experimental_inprocess_symlink_creation is used to workaround the missing 24 | # support for paths with spaces https://github.com/bazelbuild/bazel/issues/4327. 25 | # Remove the two flags after this issue is fixed in bazel new release. 26 | build --enable_platform_specific_config 27 | build:macos --experimental_inprocess_symlink_creation 28 | 29 | test --test_output=errors 30 | 31 | # Enable Java 21. Note this doesn't control whether J2CL accepts Java 21 inputs, 32 | # that is controlled in build_defs/internal_do_not_use/j2cl_common.bzl. 33 | build --java_language_version=21 34 | build --java_runtime_version=21 35 | 36 | # Enable Java 21 for J2CL compiler itself 37 | build --tool_java_language_version=21 38 | build --tool_java_runtime_version=21 -------------------------------------------------------------------------------- /.bazelversion: -------------------------------------------------------------------------------- 1 | 8.0.1 -------------------------------------------------------------------------------- /.github/workflows/ci.yaml: -------------------------------------------------------------------------------- 1 | # Copyright 2020 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | # Format reference: https://docs.github.com/en/actions/reference 16 | 17 | name: CI 18 | 19 | # Declare default permissions as read only. 20 | permissions: read-all 21 | 22 | # https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#on 23 | on: 24 | push: 25 | branches: [ master ] 26 | pull_request: 27 | branches: [ master ] 28 | schedule: 29 | # Daily at 12pm UTC 30 | - cron: '0 12 * * *' 31 | 32 | # https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobs 33 | jobs: 34 | build: 35 | 36 | strategy: 37 | fail-fast: false 38 | matrix: 39 | test-target: ['default', 'samples'] 40 | os: [macos-latest, ubuntu-latest] 41 | 42 | runs-on: ${{ matrix.os }} 43 | 44 | steps: 45 | - name: Setup Java 46 | uses: actions/setup-java@c5195efecf7bdfc987ee8bae7a71cb8b11521c00 # v4.7.1 47 | with: 48 | java-version: '21' 49 | distribution: 'zulu' 50 | java-package: jdk 51 | 52 | - name: Checkout current commit 53 | uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 54 | 55 | - name: Cache Bazel repositories 56 | uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3 57 | with: 58 | path: ~/bazel-repository-cache 59 | key: bazel-repositories-${{hashFiles('**/MODULE.bazel')}} 60 | restore-keys: | 61 | bazel-repositories- 62 | 63 | - name: Cache Bazel results 64 | uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3 65 | with: 66 | path: ~/bazel-action-cache 67 | key: bazel-actions-${{runner.os}}-${{github.sha}} 68 | restore-keys: | 69 | bazel-actions-${{runner.os}}- 70 | 71 | - name: Configure bazel 72 | run: | 73 | echo "build --repository_cache=~/bazel-repository-cache" >> ~/.bazelrc 74 | echo "build --disk_cache=~/bazel-action-cache" >> ~/.bazelrc 75 | echo "build -c opt" >> ~/.bazelrc 76 | echo "build --verbose_failures" >> ~/.bazelrc 77 | bazel info 78 | - name: Run tests 79 | if: matrix.test-target == 'default' 80 | run: ./build_test.sh CI 81 | - name: Run samples tests 82 | if: matrix.test-target == 'samples' 83 | run: | 84 | if [[ -f "./build_test_samples.sh" ]]; then 85 | ./build_test_samples.sh CI 86 | fi -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # NOTE: we don't include OS- or IDE/editor-specific files (e.g. Windows' Thumbs.db, 2 | # OSX's .DS_Store, Vim's *.swp, Emacs' *~, Visual Studio Code's .vscode, etc.) on-purpose. 3 | # Those patterns should go in a global gitignore or repository-specific excludes. 4 | # See https://help.github.com/articles/ignoring-files/ for how to configure your environment. 5 | # See https://github.com/github/gitignore/tree/master/Global for a list of global ignore rules. 6 | /bazel-* 7 | -------------------------------------------------------------------------------- /BUILD: -------------------------------------------------------------------------------- 1 | # Description: 2 | # jsinterop (https://goo.gl/agme3T) is a mechanism providing interoperability 3 | # with JavaScript to GWT or J2CL applications. 4 | # 5 | # 6 | 7 | load("@rules_license//rules:license.bzl", "license") 8 | 9 | package( 10 | default_applicable_licenses = ["//:license"], 11 | default_visibility = ["//visibility:public"], 12 | licenses = ["notice"], 13 | ) 14 | 15 | license( 16 | name = "license", 17 | package_name = "jsinterop_generator", 18 | ) 19 | 20 | exports_files(["LICENSE"]) 21 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # How to become a contributor and submit your own code 2 | 3 | ## Contributor License Agreements 4 | 5 | Before to propose a patch please fill out either the individual or corporate Contributor License Agreement (CLA). 6 | 7 | * If you are an individual writing original source code and you're sure you own the intellectual property, then you'll need to sign an [individual CLA](http://code.google.com/legal/individual-cla-v1.0.html). 8 | * If you work for a company that wants to allow you to contribute your work, then you'll need to sign a [corporate CLA](http://code.google.com/legal/corporate-cla-v1.0.html). 9 | 10 | Follow either of the two links above to access the appropriate CLA and instructions for how to sign and return it. Once we receive it, we'll be able to accept your pull requests. 11 | 12 | ## Contributing A Patch 13 | 14 | 1. Submit an issue describing your proposed change to the repo in question. 15 | 1. The repo owner will respond to your issue. 16 | 1. If your proposed change is accepted, and you haven't already done so, sign a Contributor License Agreement (see details above). 17 | 1. Fork the desired repo, develop and test your code changes. 18 | 19 | bazel test //javatests/... 20 | 21 | 1. Submit a pull request. 22 | 23 | -------------------------------------------------------------------------------- /MODULE.bazel: -------------------------------------------------------------------------------- 1 | module( 2 | name = "com_google_jsinterop_generator", 3 | bazel_compatibility = [">=7.4.1"], 4 | ) 5 | 6 | bazel_dep(name = "com_google_j2cl") 7 | 8 | archive_override( 9 | module_name = "com_google_j2cl", 10 | strip_prefix = "j2cl-master", 11 | urls = ["https://github.com/google/j2cl/archive/master.zip"], 12 | ) 13 | 14 | bazel_dep(name = "com_google_jsinterop_base") 15 | 16 | archive_override( 17 | module_name = "com_google_jsinterop_base", 18 | strip_prefix = "jsinterop-base-master", 19 | urls = ["https://github.com/google/jsinterop-base/archive/master.zip"], 20 | ) 21 | 22 | bazel_dep( 23 | name = "rules_license", 24 | version = "1.0.0", 25 | ) 26 | 27 | bazel_dep( 28 | name = "rules_shell", 29 | version = "0.4.1", 30 | ) 31 | 32 | bazel_dep( 33 | name = "bazel_skylib", 34 | version = "1.7.1", 35 | ) 36 | 37 | bazel_dep( 38 | name = "rules_java", 39 | version = "8.6.1", 40 | ) 41 | 42 | bazel_dep( 43 | name = "rules_jvm_external", 44 | version = "6.6", 45 | ) 46 | 47 | bazel_dep( 48 | name = "google_bazel_common", 49 | version = "0.0.1", 50 | ) 51 | 52 | bazel_dep( 53 | name = "io_bazel_rules_closure", 54 | ) 55 | 56 | # rules_closure is not available in BCR. 57 | git_override( 58 | module_name = "io_bazel_rules_closure", 59 | commit = "790a1bd79cde595a5d296963a78d344681ff245c", 60 | remote = "https://github.com/bazelbuild/rules_closure", 61 | ) 62 | 63 | # rules_webtesting is not available in BCR. 64 | git_override( 65 | module_name = "rules_webtesting", 66 | commit = "7a1c88f61e35ee5ce0892ae24e2aa2a3106cbfed", 67 | remote = "https://github.com/bazelbuild/rules_webtesting", 68 | ) 69 | 70 | # rules_scala is not available in BCR. 71 | # The root module has to declare the same override as rules_webtesting. 72 | git_override( 73 | module_name = "rules_scala", 74 | commit = "219e63983e8e483e66ebf70372969ba227382001", 75 | remote = "https://github.com/mbland/rules_scala", 76 | ) 77 | 78 | maven = use_extension("@rules_jvm_external//:extensions.bzl", "maven") 79 | 80 | maven.install( 81 | artifacts = [ 82 | "args4j:args4j:2.33", 83 | ], 84 | ) 85 | 86 | maven.artifact( 87 | artifact = "closure-compiler", 88 | group = "com.google.javascript", 89 | version = "v20240317", 90 | ) 91 | 92 | use_repo(maven, "maven") 93 | 94 | # Works around https://github.com/bazelbuild/rules_python/issues/1169 95 | bazel_dep( 96 | name = "rules_python", 97 | version = "0.23.1", 98 | ) 99 | 100 | python = use_extension("@rules_python//python/extensions:python.bzl", "python") 101 | python.toolchain( 102 | configure_coverage_tool = False, 103 | ignore_root_user_error = True, 104 | python_version = "3.11", 105 | ) 106 | 107 | -------------------------------------------------------------------------------- /build_test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Copyright 2019 Google Inc. All Rights Reserved 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS-IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | bazel build ... 17 | bazel test ... 18 | -------------------------------------------------------------------------------- /internal_do_not_use/BUILD: -------------------------------------------------------------------------------- 1 | # Description: 2 | # Build rules in this directory are used by our tooling and build rules 3 | 4 | load("@rules_shell//shell:sh_binary.bzl", "sh_binary") 5 | 6 | package( 7 | default_applicable_licenses = ["//:license"], 8 | default_visibility = ["//visibility:public"], 9 | licenses = ["notice"], 10 | ) 11 | 12 | ########################################################## 13 | # # 14 | # __ __ _____ _ _ _____ _ _ _____ # 15 | # \ \ / /\ | __ \| \ | |_ _| \ | |/ ____| # 16 | # \ \ /\ / / \ | |__) | \| | | | | \| | | __ # 17 | # \ \/ \/ / /\ \ | _ /| . ` | | | | . ` | | |_ | # 18 | # \ /\ / ____ \| | \ \| |\ |_| |_| |\ | |__| | # 19 | # \/ \/_/ \_\_| \_\_| \_|_____|_| \_|\_____| # 20 | # # 21 | # # 22 | ########################################################## 23 | # Never depend on any of the targets in this BUILD file # 24 | # manually. They are used within tools/build rules and # 25 | # and should actually be private, but Bazel does not # 26 | # support this yet, b/34359566. # 27 | ########################################################## 28 | 29 | # An implicit dependency of all "jsinterop_generator" skylark rules. 30 | sh_binary( 31 | name = "format_srcjar", 32 | srcs = ["format_srcjar.sh"], 33 | ) 34 | -------------------------------------------------------------------------------- /internal_do_not_use/format_srcjar.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # Script taking as input the jar file containing the generated java classes and 3 | # formatting each java file with the google java formatter 4 | set -e 5 | 6 | generated_jar=$(pwd)/$1 7 | output=$(pwd)/$2 8 | formatter=$(pwd)/$3 9 | zip_tool=$4 10 | 11 | # if a relative path is passed, use the absolute path 12 | if [ "${zip_tool}" != "jar" ]; then 13 | zip_tool=$(pwd)/${zip_tool} 14 | fi 15 | 16 | GENERATED_FILES_DIR=$(mktemp -d) 17 | 18 | function format_java_code () { 19 | java_files=$(find ${1} -name '*.java') 20 | chmod -R 664 ${java_files} 21 | ${formatter} -i ${java_files} 22 | } 23 | 24 | 25 | cd $GENERATED_FILES_DIR 26 | ${zip_tool} x "${generated_jar}" 27 | format_java_code . 28 | ${zip_tool} c "${output}" $(find * -print) 29 | cd - 30 | 31 | rm -rf ${GENERATED_FILES_DIR} 32 | -------------------------------------------------------------------------------- /java/jsinterop/generator/closure/BUILD: -------------------------------------------------------------------------------- 1 | # Description: 2 | # Generates JsTypes classes from closure extern files 3 | 4 | load("@rules_java//java:defs.bzl", "java_binary", "java_library") 5 | 6 | package( 7 | default_applicable_licenses = ["//:license"], 8 | default_visibility = [ 9 | "//:__subpackages__", 10 | ], 11 | licenses = ["notice"], 12 | ) 13 | 14 | java_library( 15 | name = "closure", 16 | srcs = glob(["*.java"]), 17 | deps = [ 18 | "//java/jsinterop/generator/closure/helper", 19 | "//java/jsinterop/generator/closure/visitor", 20 | "//java/jsinterop/generator/helper", 21 | "//java/jsinterop/generator/model", 22 | "//java/jsinterop/generator/visitor", 23 | "//java/jsinterop/generator/writer", 24 | "//third_party:args4j", 25 | "//third_party:auto_value", 26 | "//third_party:guava", 27 | "//third_party:jscomp", 28 | "//third_party:jsr305_annotations", 29 | ], 30 | ) 31 | 32 | java_binary( 33 | name = "ClosureJsinteropGenerator", 34 | main_class = "jsinterop.generator.closure.Runner", 35 | visibility = ["//visibility:public"], 36 | runtime_deps = [":closure"], 37 | ) 38 | -------------------------------------------------------------------------------- /java/jsinterop/generator/closure/JarFileCreator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package jsinterop.generator.closure; 18 | 19 | import com.google.common.io.ByteSource; 20 | import com.google.common.io.CharSource; 21 | import java.io.BufferedOutputStream; 22 | import java.io.FileOutputStream; 23 | import java.io.IOException; 24 | import java.nio.charset.StandardCharsets; 25 | import java.util.GregorianCalendar; 26 | import java.util.List; 27 | import java.util.jar.Attributes; 28 | import java.util.jar.JarEntry; 29 | import java.util.jar.JarFile; 30 | import java.util.jar.JarOutputStream; 31 | import java.util.jar.Manifest; 32 | 33 | /** Create the source jar file */ 34 | public class JarFileCreator { 35 | /** Use the same fixed deterministic timestamp as JarSanitizer. */ 36 | private static final long FIXED_TIMESTAMP = 37 | new GregorianCalendar(2010, 0, 1, 0, 0, 0).getTimeInMillis(); 38 | 39 | public static void generateJarFile(String jarPath, List javaFiles) throws IOException { 40 | try (JarOutputStream jarFile = new JarOutputStream(new FileOutputStream(jarPath))) { 41 | writeManifest(jarFile); 42 | 43 | // write java file 44 | for (JavaFile file : javaFiles) { 45 | addFile(jarFile, file); 46 | } 47 | 48 | // close the last entry 49 | jarFile.closeEntry(); 50 | } 51 | } 52 | 53 | private static void writeManifest(JarOutputStream jarFile) throws IOException { 54 | Manifest manifest = new Manifest(); 55 | manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); 56 | initNextEntry(JarFile.MANIFEST_NAME, jarFile); 57 | manifest.write(new BufferedOutputStream(jarFile)); 58 | } 59 | 60 | private static void addFile(JarOutputStream jarFile, JavaFile javaFile) throws IOException { 61 | initNextEntry(javaFile.getFilePath(), jarFile); 62 | ByteSource byteSource = 63 | CharSource.wrap(javaFile.getFileContent()).asByteSource(StandardCharsets.UTF_8); 64 | byteSource.copyTo(jarFile); 65 | } 66 | 67 | private static void initNextEntry(String filePath, JarOutputStream jarFile) throws IOException { 68 | // ensure to close the previous entry if it exists 69 | jarFile.closeEntry(); 70 | 71 | JarEntry entry = new JarEntry(filePath); 72 | // ensure the output is deterministic by always setting the same timestamp for all files. 73 | entry.setTime(FIXED_TIMESTAMP); 74 | jarFile.putNextEntry(entry); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /java/jsinterop/generator/closure/JavaFile.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | package jsinterop.generator.closure; 17 | 18 | import com.google.auto.value.AutoValue; 19 | 20 | @AutoValue 21 | abstract class JavaFile { 22 | @AutoValue.Builder 23 | abstract static class Builder { 24 | abstract Builder filePath(String filePath); 25 | 26 | abstract Builder fileContent(String fileContent); 27 | 28 | abstract JavaFile build(); 29 | } 30 | 31 | abstract String getFileContent(); 32 | 33 | abstract String getFilePath(); 34 | 35 | static Builder builder() { 36 | return new AutoValue_JavaFile.Builder(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /java/jsinterop/generator/closure/helper/BUILD: -------------------------------------------------------------------------------- 1 | # Description: 2 | # Helper classes 3 | 4 | load("@rules_java//java:defs.bzl", "java_library") 5 | 6 | package( 7 | default_applicable_licenses = ["//:license"], 8 | default_visibility = [ 9 | "//:__subpackages__", 10 | ], 11 | licenses = ["notice"], 12 | ) 13 | 14 | java_library( 15 | name = "helper", 16 | srcs = glob(["*.java"]), 17 | deps = [ 18 | "//java/jsinterop/generator/helper", 19 | "//java/jsinterop/generator/model", 20 | "//third_party:auto_value", 21 | "//third_party:guava", 22 | "//third_party:jscomp", 23 | ], 24 | ) 25 | -------------------------------------------------------------------------------- /java/jsinterop/generator/closure/helper/GenerationContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | * 16 | */ 17 | 18 | package jsinterop.generator.closure.helper; 19 | 20 | import com.google.auto.value.AutoValue; 21 | import com.google.common.collect.ImmutableMap; 22 | import com.google.common.collect.ImmutableSet; 23 | import com.google.javascript.jscomp.Compiler; 24 | import com.google.javascript.rhino.StaticSourceFile; 25 | import java.util.Collection; 26 | import java.util.Map; 27 | import jsinterop.generator.helper.Problems; 28 | import jsinterop.generator.model.Program; 29 | 30 | /** Keep contextual information on the current generation. */ 31 | @AutoValue 32 | public abstract class GenerationContext { 33 | 34 | /** Builder for {@link GenerationContext} */ 35 | @AutoValue.Builder 36 | public abstract static class Builder { 37 | public abstract Builder compiler(Compiler compiler); 38 | 39 | public abstract Builder javaProgram(Program javaProgram); 40 | 41 | public abstract Builder typeRegistry(ClosureTypeRegistry typeRegistry); 42 | 43 | public abstract Builder sourceFiles(Collection externFiles); 44 | 45 | public abstract Builder externDependencyFiles( 46 | Collection externFiles); 47 | 48 | public abstract Builder nameMapping(Map nameMapping); 49 | 50 | public abstract Builder problems(Problems problems); 51 | 52 | public abstract GenerationContext build(); 53 | } 54 | 55 | public abstract Compiler getCompiler(); 56 | 57 | public abstract ImmutableSet getSourceFiles(); 58 | 59 | public abstract ImmutableSet getExternDependencyFiles(); 60 | 61 | public abstract Program getJavaProgram(); 62 | 63 | public abstract ClosureTypeRegistry getTypeRegistry(); 64 | 65 | public abstract ImmutableMap getNameMapping(); 66 | 67 | public abstract Problems getProblems(); 68 | 69 | public static Builder builder() { 70 | return new AutoValue_GenerationContext.Builder(); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /java/jsinterop/generator/closure/visitor/BUILD: -------------------------------------------------------------------------------- 1 | # Description: 2 | # Closure visitor. 3 | 4 | load("@rules_java//java:defs.bzl", "java_library") 5 | 6 | package( 7 | default_applicable_licenses = ["//:license"], 8 | default_visibility = [ 9 | "//:__subpackages__", 10 | ], 11 | licenses = ["notice"], 12 | ) 13 | 14 | java_library( 15 | name = "visitor", 16 | srcs = glob(["*.java"]), 17 | deps = [ 18 | "//java/jsinterop/generator/closure/helper", 19 | "//java/jsinterop/generator/helper", 20 | "//java/jsinterop/generator/model", 21 | "//third_party:guava", 22 | "//third_party:jscomp", 23 | ], 24 | ) 25 | -------------------------------------------------------------------------------- /java/jsinterop/generator/closure/visitor/InheritanceVisitor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | package jsinterop.generator.closure.visitor; 17 | 18 | import static java.util.stream.StreamSupport.stream; 19 | import static jsinterop.generator.helper.AbstractTypeRegistry.ReferenceContext.IN_HERITAGE_CLAUSE; 20 | 21 | import com.google.javascript.rhino.jstype.FunctionType; 22 | import com.google.javascript.rhino.jstype.ObjectType; 23 | import java.util.List; 24 | import java.util.Optional; 25 | import java.util.stream.Collectors; 26 | import jsinterop.generator.closure.helper.GenerationContext; 27 | import jsinterop.generator.model.Type; 28 | import jsinterop.generator.model.TypeReference; 29 | 30 | /** Visit and convert inheritance clause. */ 31 | public class InheritanceVisitor extends AbstractClosureVisitor { 32 | 33 | public InheritanceVisitor(GenerationContext context) { 34 | super(context); 35 | } 36 | 37 | @Override 38 | protected boolean visitClassOrInterface(FunctionType type) { 39 | Type javaType = getCurrentJavaType(); 40 | 41 | if (type.isInterface() && type.getExtendedInterfacesCount() > 0) { 42 | javaType.getExtendedTypes().addAll(createTypeReferences(type.getExtendedInterfaces())); 43 | } else if (type.isConstructor()) { 44 | getSuperType(type).ifPresent(t -> javaType.getExtendedTypes().add(createTypeReference(t))); 45 | javaType 46 | .getImplementedTypes() 47 | .addAll(createTypeReferences(type.getOwnImplementedInterfaces())); 48 | } 49 | 50 | return false; 51 | } 52 | 53 | private List createTypeReferences(Iterable types) { 54 | return stream(types.spliterator(), false) 55 | .map(this::createTypeReference) 56 | .collect(Collectors.toList()); 57 | } 58 | 59 | private TypeReference createTypeReference(ObjectType type) { 60 | return getJavaTypeRegistry().createTypeReference(type, IN_HERITAGE_CLAUSE); 61 | } 62 | 63 | private Optional getSuperType(FunctionType type) { 64 | ObjectType proto = type.getPrototype(); 65 | if (proto == null) { 66 | return Optional.empty(); 67 | } 68 | 69 | ObjectType implicitProto = proto.getImplicitPrototype(); 70 | if (implicitProto == null) { 71 | return Optional.empty(); 72 | } 73 | 74 | return "Object".equals(implicitProto.getDisplayName()) 75 | ? Optional.empty() 76 | : Optional.of(implicitProto); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /java/jsinterop/generator/closure/visitor/ThisTemplateTypeVisitor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package jsinterop.generator.closure.visitor; 17 | 18 | import com.google.javascript.rhino.jstype.FunctionType; 19 | import com.google.javascript.rhino.jstype.JSType; 20 | import com.google.javascript.rhino.jstype.TemplateType; 21 | import java.util.ArrayDeque; 22 | import java.util.Deque; 23 | import java.util.Optional; 24 | import jsinterop.generator.closure.helper.GenerationContext; 25 | 26 | /** 27 | * Visit methods scanning for @this annotations that declare the receiver type to be just a template 28 | * variable defined in the method. 29 | * 30 | *

In the Closure type system, @this annotation can be used on a method to specify (or customize) 31 | * the type of the instance the method is called on. Because the template variable can be any type, 32 | * this pattern is useful for allowing seamless fluent style method even in subclasses. 33 | * 34 | *

This visitor is used to record these type variables so that they can be handled in a 35 | * meaningful way by the generator. 36 | */ 37 | public class ThisTemplateTypeVisitor extends AbstractClosureVisitor { 38 | private final Deque currentJsTypeStack = new ArrayDeque<>(); 39 | 40 | public ThisTemplateTypeVisitor(GenerationContext context) { 41 | super(context); 42 | } 43 | 44 | @Override 45 | protected boolean visitClassOrInterface(FunctionType type) { 46 | currentJsTypeStack.push(type.getInstanceType()); 47 | return true; 48 | } 49 | 50 | @Override 51 | protected void endVisitClassOrInterface(FunctionType type) { 52 | currentJsTypeStack.pop(); 53 | } 54 | 55 | @Override 56 | protected boolean visitMethod(FunctionType method, boolean isStatic) { 57 | Optional thisTemplateType = getThisTemplateType(method); 58 | thisTemplateType.ifPresent( 59 | templateType -> 60 | getJavaTypeRegistry() 61 | .registerThisTemplateType(templateType, currentJsTypeStack.peek())); 62 | return true; 63 | } 64 | 65 | private Optional getThisTemplateType(FunctionType type) { 66 | return type.getTypeOfThis().isTemplateType() 67 | ? Optional.of(type.getTypeOfThis().toMaybeTemplateType()) 68 | : Optional.empty(); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /java/jsinterop/generator/helper/AbortError.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package jsinterop.generator.helper; 17 | 18 | /** 19 | * AbortError is thrown to signal that a System.exit should be performed at a higher level. 20 | * 21 | *

Note: It should never be caught except on the top level. 22 | */ 23 | public class AbortError extends Error {} 24 | -------------------------------------------------------------------------------- /java/jsinterop/generator/helper/BUILD: -------------------------------------------------------------------------------- 1 | # Description: 2 | # Generator helper 3 | 4 | load("@rules_java//java:defs.bzl", "java_library") 5 | 6 | package( 7 | default_applicable_licenses = ["//:license"], 8 | licenses = ["notice"], 9 | ) 10 | 11 | java_library( 12 | name = "helper", 13 | srcs = glob(["*.java"]), 14 | visibility = [ 15 | # Do not use. Temporary visible to workaround https://github.com/bazelbuild/bazel/issues/25214. 16 | "//visibility:public", 17 | ], 18 | deps = [ 19 | "//java/jsinterop/generator/model", 20 | "//third_party:guava", 21 | ], 22 | ) 23 | -------------------------------------------------------------------------------- /java/jsinterop/generator/model/AbstractTypeReference.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | * 16 | */ 17 | 18 | package jsinterop.generator.model; 19 | 20 | import com.google.j2cl.common.visitor.Processor; 21 | import com.google.j2cl.common.visitor.Visitable; 22 | import java.util.Objects; 23 | 24 | /** Abstract implementation of TypeReference */ 25 | @Visitable 26 | public abstract class AbstractTypeReference extends TypeReference { 27 | public AbstractTypeReference(boolean isNullable) { 28 | super(isNullable); 29 | } 30 | 31 | @Override 32 | public final boolean equals(Object o) { 33 | if (this == o) { 34 | return true; 35 | } 36 | if (!(o instanceof AbstractTypeReference)) { 37 | return false; 38 | } 39 | 40 | return Objects.equals(getJavaTypeFqn(), ((AbstractTypeReference) o).getJavaTypeFqn()); 41 | } 42 | 43 | @Override 44 | public int hashCode() { 45 | return Objects.hashCode(getJavaTypeFqn()); 46 | } 47 | 48 | @Override 49 | public TypeReference acceptInternal(Processor processor) { 50 | return Visitor_AbstractTypeReference.visit(processor, this); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /java/jsinterop/generator/model/AccessModifier.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | * 16 | */ 17 | 18 | package jsinterop.generator.model; 19 | 20 | /** Models Java access modifier. */ 21 | public enum AccessModifier { 22 | DEFAULT(""), 23 | PUBLIC("public"), 24 | PROTECTED("protected"), 25 | PRIVATE("private"); 26 | 27 | private final String litteral; 28 | 29 | AccessModifier(String litteral) { 30 | this.litteral = litteral; 31 | } 32 | 33 | public String getLitteral() { 34 | return litteral; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /java/jsinterop/generator/model/Annotation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | * 16 | */ 17 | package jsinterop.generator.model; 18 | 19 | import com.google.auto.value.AutoValue; 20 | import javax.annotation.Nullable; 21 | 22 | /** Model Java Annotation and more especially JsInterop annotations. */ 23 | @AutoValue 24 | public abstract class Annotation { 25 | /** Builder for {@link Annotation} */ 26 | @AutoValue.Builder 27 | public abstract static class Builder { 28 | public abstract Builder type(AnnotationType type); 29 | 30 | public abstract Builder nameAttribute(@Nullable String nameAttribute); 31 | 32 | public abstract Builder isNativeAttribute(boolean isNativeAttribute); 33 | 34 | public abstract Annotation build(); 35 | } 36 | 37 | public static Builder builder() { 38 | return new AutoValue_Annotation.Builder().isNativeAttribute(false); 39 | } 40 | 41 | @Nullable 42 | public abstract String getNameAttribute(); 43 | 44 | public abstract boolean getIsNativeAttribute(); 45 | 46 | public abstract AnnotationType getType(); 47 | 48 | public Annotation withNameAttribute(String nameAttribute) { 49 | return toBuilder().nameAttribute(nameAttribute).build(); 50 | } 51 | 52 | abstract Builder toBuilder(); 53 | } 54 | -------------------------------------------------------------------------------- /java/jsinterop/generator/model/AnnotationType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | package jsinterop.generator.model; 17 | 18 | 19 | /** A list of annotations we use in our JsInterop code generation. */ 20 | public enum AnnotationType { 21 | JS_ENUM(PredefinedTypes.JS_ENUM, true), 22 | JS_TYPE(PredefinedTypes.JS_TYPE, true), 23 | JS_PROPERTY(PredefinedTypes.JS_PROPERTY, false), 24 | JS_METHOD(PredefinedTypes.JS_METHOD, false), 25 | JS_PACKAGE(PredefinedTypes.JS_PACKAGE, false), 26 | JS_FUNCTION(PredefinedTypes.JS_FUNCTION, false), 27 | JS_OVERLAY(PredefinedTypes.JS_OVERLAY, false), 28 | DEPRECATED(PredefinedTypes.DEPRECATED, false), 29 | NULLABLE(PredefinedTypes.NULLABLE, false), 30 | FUNCTIONAL_INTERFACE(PredefinedTypes.FUNCTIONAL_INTERFACE, false); 31 | 32 | private final TypeReference type; 33 | private final boolean isJsInteropTypeAnnotation; 34 | 35 | AnnotationType(PredefinedTypes type, boolean isJsInteropTypeAnnotation) { 36 | this.type = type.getReference(false); 37 | this.isJsInteropTypeAnnotation = isJsInteropTypeAnnotation; 38 | } 39 | 40 | public TypeReference getType() { 41 | return type; 42 | } 43 | 44 | /** 45 | * Returns {@code true} if the annotation is a JsInterop annotation targeting a type, {@code 46 | * false} otherwise. 47 | */ 48 | public boolean isJsInteropTypeAnnotation() { 49 | return isJsInteropTypeAnnotation; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /java/jsinterop/generator/model/BUILD: -------------------------------------------------------------------------------- 1 | # Description: 2 | # JsInterop version of the typescript compiler api 3 | 4 | load("@rules_java//java:defs.bzl", "java_library") 5 | 6 | package( 7 | default_applicable_licenses = ["//:license"], 8 | licenses = ["notice"], 9 | ) 10 | 11 | java_library( 12 | name = "model", 13 | srcs = glob(["*.java"]), 14 | visibility = [ 15 | # Do not use. Temporary visible to workaround https://github.com/bazelbuild/bazel/issues/25214. 16 | "//visibility:public", 17 | ], 18 | deps = [ 19 | "//third_party:auto_value", 20 | "//third_party:error_prone_annotations", 21 | "//third_party:guava", 22 | "//third_party:jsr305_annotations", 23 | "@com_google_j2cl//transpiler/java/com/google/j2cl/common/visitor", 24 | ], 25 | ) 26 | -------------------------------------------------------------------------------- /java/jsinterop/generator/model/CastExpression.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | * 16 | */ 17 | package jsinterop.generator.model; 18 | 19 | import com.google.j2cl.common.visitor.Processor; 20 | import com.google.j2cl.common.visitor.Visitable; 21 | 22 | /** Represents a cast operation. */ 23 | @Visitable 24 | public class CastExpression extends Expression { 25 | @Visitable TypeReference type; 26 | @Visitable Expression expression; 27 | 28 | public CastExpression(TypeReference type, Expression expression) { 29 | this.type = type; 30 | this.expression = expression; 31 | } 32 | 33 | public Expression getExpression() { 34 | return expression; 35 | } 36 | 37 | public TypeReference getType() { 38 | return type; 39 | } 40 | 41 | @Override 42 | Expression acceptInternal(Processor processor) { 43 | return Visitor_CastExpression.visit(processor, this); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /java/jsinterop/generator/model/ClassDecomposition.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | * 16 | */ 17 | 18 | package jsinterop.generator.model; 19 | 20 | /** Models class decomposition pattern in typescript. */ 21 | public class ClassDecomposition { 22 | private final Type instanceType; 23 | private final Type staticType; 24 | 25 | public ClassDecomposition(Type instanceType, Type staticType) { 26 | this.instanceType = instanceType; 27 | this.staticType = staticType; 28 | } 29 | 30 | public Type getInstanceType() { 31 | return instanceType; 32 | } 33 | 34 | public Type getStaticType() { 35 | return staticType; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /java/jsinterop/generator/model/DelegableTypeReference.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | * 16 | */ 17 | 18 | package jsinterop.generator.model; 19 | 20 | /** Base contract for TypeReference wrapping another TypeReference */ 21 | public interface DelegableTypeReference { 22 | TypeReference getDelegate(); 23 | } 24 | -------------------------------------------------------------------------------- /java/jsinterop/generator/model/EntityKind.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | package jsinterop.generator.model; 17 | 18 | /** Represent the kind of Java entity. */ 19 | public enum EntityKind { 20 | INTERFACE, 21 | CLASS, 22 | NAMESPACE, 23 | ENUM, 24 | FIELD, 25 | METHOD, 26 | CONSTRUCTOR 27 | } 28 | -------------------------------------------------------------------------------- /java/jsinterop/generator/model/Expression.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | * 16 | */ 17 | package jsinterop.generator.model; 18 | 19 | import com.google.j2cl.common.visitor.Processor; 20 | import com.google.j2cl.common.visitor.Visitable; 21 | 22 | /** Base contract for objects that represent Java expressions. */ 23 | @Visitable 24 | public abstract class Expression { 25 | abstract Expression acceptInternal(Processor processor); 26 | } 27 | -------------------------------------------------------------------------------- /java/jsinterop/generator/model/ExpressionStatement.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | * 16 | */ 17 | package jsinterop.generator.model; 18 | 19 | import com.google.j2cl.common.visitor.Processor; 20 | import com.google.j2cl.common.visitor.Visitable; 21 | 22 | /** Used to convert an expression to a statement */ 23 | @Visitable 24 | public class ExpressionStatement extends Statement { 25 | @Visitable Expression expression; 26 | 27 | public ExpressionStatement(Expression expression) { 28 | this.expression = expression; 29 | } 30 | 31 | public Expression getExpression() { 32 | return expression; 33 | } 34 | 35 | void setExpression(Expression expression) { 36 | this.expression = expression; 37 | } 38 | 39 | @Override 40 | Statement acceptInternal(Processor processor) { 41 | return Visitor_ExpressionStatement.visit(processor, this); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /java/jsinterop/generator/model/HasName.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | package jsinterop.generator.model; 17 | 18 | /** A model entity that has name associated with it. */ 19 | public interface HasName { 20 | String getName(); 21 | } 22 | -------------------------------------------------------------------------------- /java/jsinterop/generator/model/HasTypeParameters.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | package jsinterop.generator.model; 17 | 18 | import java.util.Collection; 19 | 20 | /** Interface implemented by Entity in our Java model that can have type parameter. */ 21 | public interface HasTypeParameters { 22 | Collection getTypeParameters(); 23 | 24 | void addTypeParameter(TypeReference typeParameterReference); 25 | } 26 | -------------------------------------------------------------------------------- /java/jsinterop/generator/model/InstanceOfExpression.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | * 16 | */ 17 | package jsinterop.generator.model; 18 | 19 | import com.google.j2cl.common.visitor.Processor; 20 | import com.google.j2cl.common.visitor.Visitable; 21 | 22 | /** Represents an instanceof expression. */ 23 | @Visitable 24 | public class InstanceOfExpression extends Expression { 25 | @Visitable Expression leftOperand; 26 | @Visitable TypeReference rightOperand; 27 | 28 | public InstanceOfExpression(Expression leftOperand, TypeReference rightOperand) { 29 | this.leftOperand = leftOperand; 30 | this.rightOperand = rightOperand; 31 | } 32 | 33 | public Expression getLeftOperand() { 34 | return leftOperand; 35 | } 36 | 37 | public TypeReference getRightOperand() { 38 | return rightOperand; 39 | } 40 | 41 | @Override 42 | Expression acceptInternal(Processor processor) { 43 | return Visitor_InstanceOfExpression.visit(processor, this); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /java/jsinterop/generator/model/JavaTypeReference.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | * 16 | */ 17 | 18 | package jsinterop.generator.model; 19 | 20 | import com.google.j2cl.common.visitor.Processor; 21 | import com.google.j2cl.common.visitor.Visitable; 22 | 23 | /** Model a reference to a declared type. */ 24 | @Visitable 25 | public class JavaTypeReference extends AbstractTypeReference { 26 | private Type typeDeclaration; 27 | public String comment; 28 | 29 | public JavaTypeReference(Type typeDeclaration) { 30 | this(typeDeclaration, false); 31 | } 32 | 33 | public JavaTypeReference(Type typeDeclaration, boolean isNullable) { 34 | super(isNullable); 35 | this.typeDeclaration = typeDeclaration; 36 | } 37 | 38 | @Override 39 | public String getTypeName() { 40 | return typeDeclaration.getName(); 41 | } 42 | 43 | @Override 44 | public String getImport() { 45 | return typeDeclaration.getJavaFqn(); 46 | } 47 | 48 | @Override 49 | public String getComment() { 50 | return comment; 51 | } 52 | 53 | @Override 54 | public String getJsDocAnnotationString() { 55 | return typeDeclaration.getNativeFqn(); 56 | } 57 | 58 | @Override 59 | public String getJavaTypeFqn() { 60 | return typeDeclaration.getJavaFqn(); 61 | } 62 | 63 | @Override 64 | public String getJavaRelativeQualifiedTypeName() { 65 | return typeDeclaration.getJavaRelativeQualifiedTypeName(); 66 | } 67 | 68 | @Override 69 | public String getJniSignature() { 70 | return "L" + typeDeclaration.getJavaFqn().replace('.', '/') + ";"; 71 | } 72 | 73 | @Override 74 | public TypeReference toNonNullableTypeReference() { 75 | return new JavaTypeReference(this.typeDeclaration, false); 76 | } 77 | 78 | @Override 79 | public TypeReference toNullableTypeReference() { 80 | return new JavaTypeReference(this.typeDeclaration, true); 81 | } 82 | 83 | @Override 84 | public Type getTypeDeclaration() { 85 | return typeDeclaration; 86 | } 87 | 88 | public void setTypeDeclaration(Type typeDeclaration) { 89 | this.typeDeclaration = typeDeclaration; 90 | } 91 | 92 | public void setComment(String comment) { 93 | this.comment = comment; 94 | } 95 | 96 | @Override 97 | public boolean isInstanceofAllowed() { 98 | return !typeDeclaration.isNativeInterface(); 99 | } 100 | 101 | @Override 102 | public TypeReference acceptInternal(Processor processor) { 103 | return Visitor_JavaTypeReference.visit(processor, this); 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /java/jsinterop/generator/model/LiteralExpression.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | * 16 | */ 17 | package jsinterop.generator.model; 18 | 19 | import com.google.errorprone.annotations.Immutable; 20 | import com.google.j2cl.common.visitor.Processor; 21 | import com.google.j2cl.common.visitor.Visitable; 22 | 23 | /** Represents a literal. */ 24 | @Immutable 25 | @Visitable 26 | public class LiteralExpression extends Expression { 27 | public static final LiteralExpression ZERO = new LiteralExpression("0"); 28 | public static final LiteralExpression FALSE = new LiteralExpression("false"); 29 | public static final LiteralExpression NULL = new LiteralExpression("null"); 30 | public static final LiteralExpression THIS = new LiteralExpression("this"); 31 | 32 | private final String literal; 33 | 34 | public LiteralExpression(String value) { 35 | this.literal = value; 36 | } 37 | 38 | public String getLiteral() { 39 | return literal; 40 | } 41 | 42 | @Override 43 | Expression acceptInternal(Processor processor) { 44 | return Visitor_LiteralExpression.visit(processor, this); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /java/jsinterop/generator/model/ModelVisitor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | * 16 | */ 17 | 18 | package jsinterop.generator.model; 19 | 20 | /** Contracts for visitors that aims to visit our Java model. */ 21 | public interface ModelVisitor { 22 | void applyTo(Program program); 23 | } 24 | -------------------------------------------------------------------------------- /java/jsinterop/generator/model/Program.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | * 16 | */ 17 | 18 | package jsinterop.generator.model; 19 | 20 | import com.google.common.collect.ImmutableList; 21 | import com.google.j2cl.common.visitor.Context; 22 | import com.google.j2cl.common.visitor.Processor; 23 | import com.google.j2cl.common.visitor.Visitable; 24 | import java.util.ArrayList; 25 | import java.util.List; 26 | import java.util.Map; 27 | 28 | /** Keep the list of generated types. */ 29 | @Visitable 30 | @Context 31 | public class Program { 32 | 33 | private final Map thirdPartyTypesMapping; 34 | @Visitable List types = new ArrayList<>(); 35 | 36 | public Program(Map thirdPartyTypesMapping) { 37 | this.thirdPartyTypesMapping = thirdPartyTypesMapping; 38 | } 39 | 40 | public void addType(Type type) { 41 | types.add(type); 42 | } 43 | 44 | public List getAllTypes() { 45 | return ImmutableList.copyOf(types); 46 | } 47 | 48 | public boolean isThirdPartyType(String tsFqn) { 49 | return thirdPartyTypesMapping.containsKey(tsFqn); 50 | } 51 | 52 | public String getThirdPartyTypeJavaFqn(String tsFqn) { 53 | return thirdPartyTypesMapping.get(tsFqn); 54 | } 55 | 56 | public void accept(Processor processor) { 57 | acceptInternal(processor); 58 | } 59 | 60 | Program acceptInternal(Processor processor) { 61 | return Visitor_Program.visit(processor, this); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /java/jsinterop/generator/model/ReturnStatement.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | * 16 | */ 17 | package jsinterop.generator.model; 18 | 19 | import com.google.j2cl.common.visitor.Processor; 20 | import com.google.j2cl.common.visitor.Visitable; 21 | 22 | /** Models a return statement. */ 23 | @Visitable 24 | public class ReturnStatement extends Statement { 25 | @Visitable Expression expression; 26 | 27 | public ReturnStatement(Expression expression) { 28 | this.expression = expression; 29 | } 30 | 31 | public Expression getExpression() { 32 | return expression; 33 | } 34 | 35 | @Override 36 | Statement acceptInternal(Processor processor) { 37 | return Visitor_ReturnStatement.visit(processor, this); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /java/jsinterop/generator/model/Statement.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | * 16 | */ 17 | package jsinterop.generator.model; 18 | 19 | import com.google.j2cl.common.visitor.Processor; 20 | import com.google.j2cl.common.visitor.Visitable; 21 | 22 | /** Base contract for objects that represent Java statements. */ 23 | @Visitable 24 | public abstract class Statement { 25 | private String leadingComment; 26 | 27 | Statement() {} 28 | 29 | public String getLeadingComment() { 30 | return leadingComment; 31 | } 32 | 33 | public void setLeadingComment(String leadingComment) { 34 | this.leadingComment = leadingComment; 35 | } 36 | 37 | Statement acceptInternal(Processor processor) { 38 | return Visitor_Statement.visit(processor, this); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /java/jsinterop/generator/model/TypeQualifier.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | * 16 | */ 17 | package jsinterop.generator.model; 18 | 19 | import com.google.j2cl.common.visitor.Processor; 20 | import com.google.j2cl.common.visitor.Visitable; 21 | 22 | /** Represents an expression to a type qualifier. */ 23 | @Visitable 24 | public class TypeQualifier extends Expression { 25 | @Visitable TypeReference type; 26 | 27 | public TypeQualifier(TypeReference type) { 28 | this.type = type; 29 | } 30 | 31 | public TypeReference getType() { 32 | return type; 33 | } 34 | 35 | @Override 36 | Expression acceptInternal(Processor processor) { 37 | return Visitor_TypeQualifier.visit(processor, this); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /java/jsinterop/generator/model/TypeVariableReference.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | * 16 | */ 17 | package jsinterop.generator.model; 18 | 19 | import static jsinterop.generator.model.PredefinedTypes.OBJECT; 20 | 21 | import com.google.j2cl.common.visitor.Processor; 22 | import com.google.j2cl.common.visitor.Visitable; 23 | 24 | /** Models a reference to a type variable. */ 25 | @Visitable 26 | public class TypeVariableReference extends AbstractTypeReference { 27 | @Visitable TypeReference upperBound; 28 | private final String name; 29 | 30 | public TypeVariableReference(String name, TypeReference upperBound) { 31 | this(name, upperBound, false); 32 | } 33 | 34 | public TypeVariableReference(String name, TypeReference upperBound, boolean isNullable) { 35 | super(isNullable); 36 | this.name = name; 37 | this.upperBound = upperBound != null ? upperBound : OBJECT.getReference(true); 38 | } 39 | 40 | @Override 41 | public String getTypeName() { 42 | return name; 43 | } 44 | 45 | @Override 46 | public TypeReference toNonNullableTypeReference() { 47 | return new TypeVariableReference(this.name, this.upperBound, false); 48 | } 49 | 50 | @Override 51 | public TypeReference toNullableTypeReference() { 52 | return new TypeVariableReference(this.name, this.upperBound, true); 53 | } 54 | 55 | @Override 56 | public String getImport() { 57 | return null; 58 | } 59 | 60 | @Override 61 | public String getComment() { 62 | return null; 63 | } 64 | 65 | @Override 66 | public String getJsDocAnnotationString() { 67 | return name; 68 | } 69 | 70 | @Override 71 | public String getJavaTypeFqn() { 72 | return name; 73 | } 74 | 75 | @Override 76 | public String getJavaRelativeQualifiedTypeName() { 77 | return getTypeName(); 78 | } 79 | 80 | @Override 81 | public String getJniSignature() { 82 | return upperBound.getJniSignature(); 83 | } 84 | 85 | public TypeReference getUpperBound() { 86 | return upperBound; 87 | } 88 | 89 | public void setUpperBound(TypeReference upperBound) { 90 | this.upperBound = upperBound; 91 | } 92 | 93 | @Override 94 | public String toString() { 95 | return "TypeVariableReference " + name; 96 | } 97 | 98 | @Override 99 | public boolean isInstanceofAllowed() { 100 | return false; 101 | } 102 | 103 | @Override 104 | public TypeReference acceptInternal(Processor processor) { 105 | return Visitor_TypeVariableReference.visit(processor, this); 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /java/jsinterop/generator/visitor/BUILD: -------------------------------------------------------------------------------- 1 | # Description: 2 | # JsInterop version of the typescript compiler api 3 | 4 | load("@rules_java//java:defs.bzl", "java_library") 5 | 6 | package( 7 | default_applicable_licenses = ["//:license"], 8 | default_visibility = [ 9 | "//:__subpackages__", 10 | ], 11 | licenses = ["notice"], 12 | ) 13 | 14 | java_library( 15 | name = "visitor", 16 | srcs = glob(["*.java"]), 17 | deps = [ 18 | "//java/jsinterop/generator/helper", 19 | "//java/jsinterop/generator/model", 20 | "//third_party:guava", 21 | ], 22 | ) 23 | -------------------------------------------------------------------------------- /java/jsinterop/generator/visitor/ClosureOptionalParameterCleaner.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | * 16 | */ 17 | 18 | package jsinterop.generator.visitor; 19 | 20 | import static jsinterop.generator.helper.GeneratorUtils.toSafeTypeName; 21 | 22 | import java.util.HashSet; 23 | import java.util.ListIterator; 24 | import java.util.Set; 25 | import jsinterop.generator.model.AbstractVisitor; 26 | import jsinterop.generator.model.Method; 27 | import jsinterop.generator.model.ModelVisitor; 28 | import jsinterop.generator.model.Parameter; 29 | import jsinterop.generator.model.Program; 30 | 31 | /** 32 | * Closure requires that optional parameters are prefixed by opt_. In Java, we create a 33 | * method overloading for each optional parameters. This visitor removes the unneeded prefix 34 | * opt_. 35 | */ 36 | public class ClosureOptionalParameterCleaner implements ModelVisitor { 37 | 38 | @Override 39 | public void applyTo(Program program) { 40 | program.accept( 41 | new AbstractVisitor() { 42 | @Override 43 | public void exitMethod(Method node) { 44 | Set parameterNames = new HashSet<>(); 45 | 46 | for (ListIterator it = node.getParameters().listIterator(); it.hasNext(); ) { 47 | Parameter parameter = it.next(); 48 | 49 | if (parameter.getName().startsWith("opt_")) { 50 | it.set( 51 | parameter.toBuilder() 52 | .setName(toSafeTypeName(parameter.getName().substring(4), parameterNames)) 53 | .build()); 54 | } 55 | parameterNames.add(parameter.getName()); 56 | } 57 | } 58 | }); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /java/jsinterop/generator/visitor/DependencyFileWriter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | * 16 | */ 17 | 18 | package jsinterop.generator.visitor; 19 | 20 | import static jsinterop.generator.helper.ModelHelper.isGlobalType; 21 | 22 | import jsinterop.generator.model.AbstractVisitor; 23 | import jsinterop.generator.model.AccessModifier; 24 | import jsinterop.generator.model.Program; 25 | import jsinterop.generator.model.Type; 26 | 27 | /** 28 | * Traverses the Program and write the dependency file. This file makes the link between typescript 29 | * type and java type and is used when a generated library is used as dependency of another 30 | * generated library. The generator uses this file to know that a java type has already been 31 | * generated for a typescript type. 32 | */ 33 | public class DependencyFileWriter { 34 | public static String render(Program program) { 35 | StringBuilder fileContentBuilder = new StringBuilder(); 36 | 37 | program.accept( 38 | new AbstractVisitor() { 39 | @Override 40 | public boolean enterType(Type type) { 41 | if (!isGlobalType(type) 42 | && !type.isExtern() 43 | && !type.isExtensionType() 44 | && type.getAccessModifier() == AccessModifier.PUBLIC) { 45 | fileContentBuilder 46 | .append(type.getNativeFqn()) 47 | .append("=") 48 | .append(type.getJavaFqn()) 49 | .append("\n"); 50 | } 51 | 52 | // Inner types are generated by the compiler and won't be referenced by 53 | // another third party libraries 54 | return false; 55 | } 56 | }); 57 | 58 | return fileContentBuilder.toString(); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /java/jsinterop/generator/visitor/EmptyNamespaceFilter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | * 16 | */ 17 | package jsinterop.generator.visitor; 18 | 19 | import jsinterop.generator.model.AbstractRewriter; 20 | import jsinterop.generator.model.ModelVisitor; 21 | import jsinterop.generator.model.Program; 22 | import jsinterop.generator.model.Type; 23 | 24 | /** 25 | * Deletes Java class from our model that have been generated from a namespace that don't contain 26 | * any functions nor variables. 27 | */ 28 | public class EmptyNamespaceFilter implements ModelVisitor { 29 | @Override 30 | public void applyTo(Program program) { 31 | program.accept( 32 | new AbstractRewriter() { 33 | @Override 34 | public Type rewriteType(Type node) { 35 | return isEmptyNamespace(node) ? null : node; 36 | } 37 | }); 38 | } 39 | 40 | private static boolean isEmptyNamespace(Type type) { 41 | return type.isNamespace() 42 | && type.getFields().isEmpty() 43 | && type.getMethods().isEmpty() 44 | && type.getInnerTypes().isEmpty(); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /java/jsinterop/generator/visitor/FixReferencesToDuplicatedTypes.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | * 16 | */ 17 | package jsinterop.generator.visitor; 18 | 19 | import java.util.Map; 20 | import jsinterop.generator.model.AbstractVisitor; 21 | import jsinterop.generator.model.JavaTypeReference; 22 | import jsinterop.generator.model.ModelVisitor; 23 | import jsinterop.generator.model.Program; 24 | import jsinterop.generator.model.Type; 25 | import jsinterop.generator.model.TypeReference; 26 | 27 | /** 28 | * {@link DuplicatedTypesUnifier} detects synthetic types that are duplicated across type hierarchy. 29 | * 30 | *

This visitor will visit all type references and reuse the type defined on parent interfaces if 31 | * it found a duplicated type. It will remove the duplicated types from the java program. 32 | */ 33 | public class FixReferencesToDuplicatedTypes implements ModelVisitor { 34 | 35 | private final Map typesToReplace; 36 | 37 | public FixReferencesToDuplicatedTypes(Map typesToReplace) { 38 | this.typesToReplace = typesToReplace; 39 | } 40 | 41 | @Override 42 | public void applyTo(Program program) { 43 | program.accept( 44 | new AbstractVisitor() { 45 | @Override 46 | public boolean enterTypeReference(TypeReference typeReference) { 47 | if (typeReference instanceof JavaTypeReference) { 48 | JavaTypeReference javaTypeReference = (JavaTypeReference) typeReference; 49 | Type syntheticType = javaTypeReference.getTypeDeclaration(); 50 | 51 | if (typesToReplace.containsKey(syntheticType)) { 52 | javaTypeReference.setTypeDeclaration(typesToReplace.get(syntheticType)); 53 | } 54 | } 55 | return true; 56 | } 57 | 58 | @Override 59 | public void exitProgram(Program node) { 60 | // Remove the synthetic types at the end of this visitor. Otherwise we delete the 61 | // relationship with their enclosing type and we cannot clean correctly all type 62 | // references to the synthetic types. 63 | typesToReplace.keySet().forEach(Type::removeFromParent); 64 | } 65 | }); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /java/jsinterop/generator/visitor/FunctionalInterfaceAnnotator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package jsinterop.generator.visitor; 17 | 18 | import static jsinterop.generator.model.AnnotationType.FUNCTIONAL_INTERFACE; 19 | 20 | import jsinterop.generator.model.AbstractVisitor; 21 | import jsinterop.generator.model.Annotation; 22 | import jsinterop.generator.model.ModelVisitor; 23 | import jsinterop.generator.model.Program; 24 | import jsinterop.generator.model.Type; 25 | 26 | /** Annotates natives interfaces with one method with FunctionalInterface annotation. */ 27 | public final class FunctionalInterfaceAnnotator implements ModelVisitor { 28 | 29 | @Override 30 | public void applyTo(Program program) { 31 | program.accept( 32 | new AbstractVisitor() { 33 | @Override 34 | public void exitType(Type type) { 35 | if (type.isInterface() 36 | && !type.isStructural() 37 | && type.getExtendedTypes().isEmpty() 38 | && type.getMethods().size() == 1) { 39 | type.addAnnotation(Annotation.builder().type(FUNCTIONAL_INTERFACE).build()); 40 | } 41 | } 42 | }); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /java/jsinterop/generator/visitor/NamespaceAttributeRewriter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | * 16 | */ 17 | package jsinterop.generator.visitor; 18 | 19 | import static jsinterop.generator.helper.GeneratorUtils.maybeRemoveClutzNamespace; 20 | import static jsinterop.generator.model.AnnotationType.JS_TYPE; 21 | 22 | import jsinterop.generator.model.AbstractVisitor; 23 | import jsinterop.generator.model.Annotation; 24 | import jsinterop.generator.model.Entity; 25 | import jsinterop.generator.model.ModelVisitor; 26 | import jsinterop.generator.model.Program; 27 | import jsinterop.generator.model.Type; 28 | 29 | /** This visitor is in charge to remove the clutz namespace if present. */ 30 | public class NamespaceAttributeRewriter implements ModelVisitor { 31 | 32 | @Override 33 | public void applyTo(Program program) { 34 | program.accept( 35 | new AbstractVisitor() { 36 | @Override 37 | public boolean enterType(Type type) { 38 | if (type.isExtern()) { 39 | // because we won't emit any code for extern type, we don't need to fix the namespace. 40 | return false; 41 | } 42 | 43 | if (isNativeJsType(type)) { 44 | maybeRewriteAnnotationAttributes(type); 45 | } 46 | 47 | // The namespace for native JsTypes is fixed at the type level and we don't set 48 | // namespace on theirs members. We don't need to visit them. 49 | // Namespaces for non native JsTypes don't need to be fixed. 50 | // We don't generate any non-JsType classes. 51 | return false; 52 | } 53 | }); 54 | } 55 | 56 | private static boolean isNativeJsType(Type currentType) { 57 | return currentType.hasAnnotation(JS_TYPE) 58 | && currentType.getAnnotation(JS_TYPE).getIsNativeAttribute(); 59 | } 60 | 61 | private static void maybeRewriteAnnotationAttributes(Entity owner) { 62 | Annotation originalAnnotation = owner.getAnnotation(JS_TYPE); 63 | 64 | String name = originalAnnotation.getNameAttribute(); 65 | 66 | if (name == null) { 67 | return; 68 | } 69 | 70 | String cleanName = maybeRemoveClutzNamespace(name); 71 | 72 | if (!cleanName.equals(name)) { 73 | owner.removeAnnotation(JS_TYPE); 74 | owner.addAnnotation(originalAnnotation.withNameAttribute(cleanName)); 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /java/jsinterop/generator/visitor/ObjectParameterJsOverlayCreator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | * 16 | */ 17 | 18 | package jsinterop.generator.visitor; 19 | 20 | import static jsinterop.generator.model.PredefinedTypes.OBJECT; 21 | 22 | import jsinterop.generator.helper.ModelHelper; 23 | import jsinterop.generator.model.Method; 24 | import jsinterop.generator.model.Parameter; 25 | 26 | /** 27 | * Creates a JsOverlay method that will accept java.lang.Object as parameter and delegate to 28 | * existing native method that accepts JavaScript object so the API becomes Java friendly. 29 | * 30 | *

We cannot convert native js Object reference to java.lang.Object because java.lang.Object 31 | * reference is transpiled by J2CL to any type '*'. That breaks closure type checking. 32 | */ 33 | public class ObjectParameterJsOverlayCreator extends AbstractJsOverlayMethodCreator { 34 | 35 | @Override 36 | protected boolean processMethod(Method method) { 37 | Method jsOverlayMethod = 38 | ModelHelper.createDelegatingOverlayMethod( 39 | method, 40 | ObjectParameterJsOverlayCreator::toJavaLangObject, 41 | ModelHelper::callUncheckedCast); 42 | 43 | if (jsOverlayMethod != null) { 44 | method.getEnclosingType().addMethod(jsOverlayMethod); 45 | } 46 | 47 | return false; 48 | } 49 | 50 | private static Parameter toJavaLangObject(int unusedIndex, Parameter parameter) { 51 | if ("Object".equals(parameter.getType().getJsDocAnnotationString())) { 52 | return parameter.toBuilder() 53 | .setType(OBJECT.getReference(parameter.getType().isNullable())) 54 | .build(); 55 | } 56 | return parameter; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /java/jsinterop/generator/visitor/TypeReferenceUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2025 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | * 16 | */ 17 | 18 | package jsinterop.generator.visitor; 19 | 20 | import static jsinterop.generator.model.PredefinedTypes.JS_ARRAY_LIKE; 21 | import static jsinterop.generator.model.PredefinedTypes.JS_CONSTRUCTOR_FN; 22 | 23 | import jsinterop.generator.model.ParametrizedTypeReference; 24 | import jsinterop.generator.model.TypeReference; 25 | 26 | /** Util class for {@link TypeReference}. */ 27 | final class TypeReferenceUtils { 28 | 29 | private TypeReferenceUtils() {} 30 | 31 | static boolean isJsArrayLikeOrJsArrayTypeReference(TypeReference typeReference) { 32 | if (!(typeReference instanceof ParametrizedTypeReference)) { 33 | return false; 34 | } 35 | return isJsArrayLikeTypeReference(typeReference) || isJsArrayTypeReference(typeReference); 36 | } 37 | 38 | static boolean isJsArrayLikeTypeReference(TypeReference typeReference) { 39 | return typeReference.isReferenceTo(JS_ARRAY_LIKE); 40 | } 41 | 42 | static boolean isJsArrayTypeReference(TypeReference typeReference) { 43 | return typeReference.getTypeDeclaration() != null 44 | && typeReference.getTypeDeclaration().getNativeFqn().equals("Array"); 45 | } 46 | 47 | static boolean isJsObjectTypeReference(TypeReference typeReference) { 48 | return typeReference.getJsDocAnnotationString().equals("Object"); 49 | } 50 | 51 | static boolean isJsConstructorFnTypeReference(TypeReference typeReference) { 52 | return typeReference.isReferenceTo(JS_CONSTRUCTOR_FN); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /java/jsinterop/generator/writer/AnnotationWriter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | package jsinterop.generator.writer; 17 | 18 | import static jsinterop.generator.model.AnnotationType.JS_PACKAGE; 19 | 20 | import com.google.common.base.Joiner; 21 | import jsinterop.generator.model.Annotation; 22 | import jsinterop.generator.model.AnnotationType; 23 | import jsinterop.generator.model.TypeReference; 24 | 25 | /** AnnotationWriter generates java code for {@link Annotation} instances. */ 26 | public class AnnotationWriter { 27 | 28 | public static void emit(Annotation annotation, CodeWriter codeWriter) { 29 | AnnotationType annotationType = annotation.getType(); 30 | 31 | codeWriter.emit("@").emitTypeReference(annotationType.getType()); 32 | 33 | String annotationParameters = 34 | Joiner.on(",") 35 | .skipNulls() 36 | .join( 37 | annotation.getIsNativeAttribute() ? "isNative = true" : null, 38 | annotation.getNameAttribute() != null 39 | ? "name = \"" + annotation.getNameAttribute() + "\"" 40 | : null, 41 | annotationType.isJsInteropTypeAnnotation() 42 | ? "namespace = " + getGlobalNamespace(codeWriter) 43 | : null); 44 | 45 | if (!annotationParameters.isEmpty()) { 46 | codeWriter.emit("(").emit(annotationParameters).emit(")"); 47 | } 48 | 49 | codeWriter.emitNewLine(); 50 | } 51 | 52 | private static String getGlobalNamespace(CodeWriter codeWriter) { 53 | TypeReference jsPackage = JS_PACKAGE.getType(); 54 | boolean imported = codeWriter.addImport(jsPackage); 55 | 56 | return (imported ? jsPackage.getTypeName() : jsPackage.getImport()) + ".GLOBAL"; 57 | } 58 | 59 | private AnnotationWriter() {} 60 | } 61 | -------------------------------------------------------------------------------- /java/jsinterop/generator/writer/BUILD: -------------------------------------------------------------------------------- 1 | # Description: 2 | # JsInterop version of the typescript compiler api 3 | 4 | load("@rules_java//java:defs.bzl", "java_library") 5 | 6 | package( 7 | default_applicable_licenses = ["//:license"], 8 | default_visibility = [ 9 | "//:__subpackages__", 10 | ], 11 | licenses = ["notice"], 12 | ) 13 | 14 | java_library( 15 | name = "writer", 16 | srcs = glob(["*.java"]), 17 | deps = [ 18 | "//java/jsinterop/generator/helper", 19 | "//java/jsinterop/generator/model", 20 | "//third_party:error_prone_annotations", 21 | "//third_party:guava", 22 | ], 23 | ) 24 | -------------------------------------------------------------------------------- /java/jsinterop/generator/writer/FieldWriter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | package jsinterop.generator.writer; 17 | 18 | import static com.google.common.base.Strings.isNullOrEmpty; 19 | 20 | import jsinterop.generator.model.AccessModifier; 21 | import jsinterop.generator.model.Field; 22 | import jsinterop.generator.model.Type; 23 | 24 | /** FieldWriter generates java code for {@link Field} instances. */ 25 | public class FieldWriter { 26 | 27 | public static void emit(Field field, CodeWriter writer, Type parentType) { 28 | writer.emitAnnotations(field.getAnnotations()); 29 | 30 | AccessModifier accessModifier = field.getAccessModifier(); 31 | 32 | if (!parentType.isInterface() || accessModifier == AccessModifier.PROTECTED) { 33 | writer.emit(accessModifier).emit(" "); 34 | } 35 | 36 | if (!parentType.isInterface() && field.isStatic()) { 37 | writer.emit("static "); 38 | } 39 | 40 | if (!parentType.isInterface() && field.isFinal()) { 41 | writer.emit("final "); 42 | } 43 | 44 | writer.emitTypeReference(field.getType()).emit(" ").emit(field.getName()); 45 | 46 | if (!isNullOrEmpty(field.getInitialValue())) { 47 | writer.emit("=").emit(field.getInitialValue()); 48 | } 49 | 50 | writer.emit(";").emitNewLine(); 51 | } 52 | 53 | private FieldWriter() {} 54 | } 55 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/BUILD: -------------------------------------------------------------------------------- 1 | # Description: 2 | # JsInterop generator tests. 3 | 4 | package( 5 | default_applicable_licenses = ["//:license"], 6 | default_visibility = ["//:__subpackages__"], 7 | licenses = ["notice"], 8 | ) 9 | 10 | exports_files([ 11 | "jsinterop_generator_test.sh", 12 | ]) 13 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/closure/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_java//java:defs.bzl", "java_library") 2 | 3 | # Helper classes for test. 4 | package( 5 | default_applicable_licenses = ["//:license"], 6 | default_visibility = ["//javatests:__subpackages__"], 7 | licenses = ["notice"], 8 | ) 9 | 10 | java_library( 11 | name = "helpers", 12 | srcs = ["TestUtil.java"], 13 | deps = [ 14 | "//java/jsinterop/generator/closure", 15 | "//third_party:guava", 16 | "//third_party:jscomp", 17 | ], 18 | ) 19 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/closure/TestUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package jsinterop.generator.closure; 17 | 18 | import com.google.common.collect.ImmutableList; 19 | import com.google.javascript.jscomp.SourceFile; 20 | 21 | /** Helper class for creating closure JsInterop Generator options. */ 22 | public final class TestUtil { 23 | private TestUtil() {} 24 | 25 | private static Options createOptions(SourceFile sourceFile) { 26 | return Options.builder() 27 | .sources(ImmutableList.of(sourceFile)) 28 | .outputJarFile("unused") 29 | .outputDependencyFile("unused") 30 | .globalScopeClassName("unused") 31 | .extensionTypePrefix("unused") 32 | .debugEnabled(false) 33 | .strict(true) 34 | .dependencyMappingFiles(ImmutableList.of()) 35 | .dependencies(ImmutableList.of()) 36 | .nameMappingFiles(ImmutableList.of()) 37 | .integerEntitiesFiles(ImmutableList.of()) 38 | .wildcardTypesFiles(ImmutableList.of()) 39 | .customPreprocessingPasses(ImmutableList.of()) 40 | .build(); 41 | } 42 | 43 | public static void runClosureJsInteropGenerator(SourceFile sourceFile) { 44 | new ClosureJsInteropGenerator(createOptions(sourceFile)).convert(); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/bigint/BUILD: -------------------------------------------------------------------------------- 1 | # Description: 2 | # Tests for bigint. 3 | # 4 | 5 | load( 6 | "//javatests/jsinterop/generator:jsinterop_generator_test.bzl", 7 | "jsinterop_generator_test", 8 | ) 9 | 10 | package( 11 | default_applicable_licenses = ["//:license"], 12 | licenses = ["notice"], 13 | ) 14 | 15 | jsinterop_generator_test( 16 | name = "bigint", 17 | srcs = ["bigint.js"], 18 | expected_output = glob(["*.java.txt"]), 19 | ) 20 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/bigint/bigint.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @fileoverview Simulate third_party extern file 3 | * @externs 4 | */ 5 | 6 | /** 7 | * @constructor 8 | */ 9 | function Foo() {} 10 | 11 | /** 12 | * @return {bigint} 13 | */ 14 | Foo.prototype.asBigint = function() {}; 15 | /** 16 | * @param {bigint|IArrayLike} bigintOrArrayLikeOfBigint 17 | * @param {function(number): bigint=} mapFn 18 | * @return {undefined} 19 | */ 20 | Foo.prototype.consumeBigint = function(bigintOrArrayLikeOfBigint, mapFn) {}; 21 | 22 | /** 23 | * @type {bigint} 24 | */ 25 | Foo.prototype.bigintField; 26 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/dependency/BUILD: -------------------------------------------------------------------------------- 1 | # Description: 2 | # Tests dependency mechanism 3 | # 4 | 5 | load( 6 | "//javatests/jsinterop/generator:jsinterop_generator_test.bzl", 7 | "jsinterop_generator_test", 8 | ) 9 | 10 | package( 11 | default_applicable_licenses = ["//:license"], 12 | licenses = ["notice"], 13 | ) 14 | 15 | jsinterop_generator_test( 16 | name = "dependency", 17 | srcs = ["dependency.js"], 18 | expected_output = glob(["*.java.txt"]), 19 | extension_type_prefix = "MyLib", 20 | deps = [ 21 | "//javatests/jsinterop/generator/externs/dependency/parentthirdparty", 22 | "//javatests/jsinterop/generator/externs/dependency/thirdparty", 23 | "//javatests/jsinterop/generator/externs/dependency/thirdparty2", 24 | ], 25 | ) 26 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/dependency/MyLibGlobal.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.dependency; 2 | 3 | import jsinterop.annotations.JsOverlay; 4 | import jsinterop.annotations.JsPackage; 5 | import jsinterop.annotations.JsType; 6 | 7 | @JsType(isNative = true, name = "goog.global", namespace = JsPackage.GLOBAL) 8 | public class MyLibGlobal { 9 | @JsOverlay public static final double globalConstant = MyLibGlobal__Constants.globalConstant; 10 | 11 | public static native String foo(); 12 | } 13 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/dependency/MyLibGlobal__Constants.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.dependency; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsType; 5 | 6 | @JsType(isNative = true, name = "goog.global", namespace = JsPackage.GLOBAL) 7 | class MyLibGlobal__Constants { 8 | static double globalConstant; 9 | } 10 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/dependency/SimpleClass.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.dependency; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsType; 5 | import jsinterop.generator.externs.dependency.parentthirdparty.ParentThirdPartyInterface; 6 | import jsinterop.generator.externs.dependency.thirdparty.ThirdPartyClass; 7 | import jsinterop.generator.externs.dependency.thirdparty.ThirdPartyInterface; 8 | import org.jspecify.annotations.Nullable; 9 | 10 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 11 | public class SimpleClass extends ThirdPartyClass implements ThirdPartyInterface { 12 | public @Nullable ThirdPartyClass field; 13 | 14 | public SimpleClass() { 15 | // This super call is here only for the code to compile; it is never executed. 16 | super((ThirdPartyClass.ConstructorFooUnionType) null); 17 | } 18 | 19 | public native @Nullable ThirdPartyClass method(@Nullable ThirdPartyInterface foo); 20 | 21 | public native void parentThirdpartyMethod( 22 | ParentThirdPartyInterface.ParentThirdpartyMethodFooType foo); 23 | } 24 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/dependency/SimpleInterface.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.dependency; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsType; 5 | import jsinterop.generator.externs.dependency.thirdparty.ThirdPartyInterface; 6 | 7 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 8 | public interface SimpleInterface extends ThirdPartyInterface {} 9 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/dependency/dependency.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @fileoverview Simulate third_party extern file 3 | * @externs 4 | */ 5 | 6 | /** 7 | * @constructor 8 | * @extends {ThirdPartyClass} 9 | * @implements {ThirdPartyInterface} 10 | */ 11 | function SimpleClass() {} 12 | 13 | /** 14 | * @type {ThirdPartyClass} 15 | */ 16 | SimpleClass.prototype.field; 17 | 18 | 19 | /** 20 | * @param {ThirdPartyInterface} foo 21 | * @return {ThirdPartyClass} 22 | */ 23 | SimpleClass.prototype.method = function(foo) {}; 24 | 25 | /** 26 | * @param {{foo:string}} foo 27 | * @return {undefined} 28 | */ 29 | SimpleClass.prototype.parentThirdpartyMethod = function(foo) {}; 30 | 31 | /** 32 | * @interface 33 | * @extends {ThirdPartyInterface} 34 | */ 35 | function SimpleInterface() {} 36 | 37 | /** 38 | * @type {ThirdParty2Class} 39 | */ 40 | ThirdPartyClass.prototype.extraField; 41 | 42 | /** 43 | * @type {?namespace1.InterfaceWithConflictingName} 44 | */ 45 | ThirdPartyClass.prototype.extraField2; 46 | 47 | /** 48 | * @type {?namespace2.InterfaceWithConflictingName} 49 | */ 50 | ThirdPartyClass.prototype.extraField3; 51 | 52 | // TODO(b/35681242): reenable the test with local parameter when the bug is 53 | // fixed 54 | //@param {function(T):U} baz 55 | //@return {U} 56 | //@template U 57 | /** 58 | * @param {T} foo 59 | * @param {(T |{bar:T})} bar 60 | * @return {undefined} 61 | */ 62 | ThirdPartyClass.prototype.extraMethod = function(foo, bar) {}; 63 | 64 | // Test global scope extension. 65 | /** 66 | * @return {string} 67 | */ 68 | function foo() {} 69 | 70 | /** @const {number} */ 71 | var globalConstant; 72 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/dependency/parentthirdparty/BUILD: -------------------------------------------------------------------------------- 1 | # Description: 2 | # Tests dependency mechanism 3 | # 4 | 5 | load( 6 | "//:jsinterop_generator.bzl", 7 | "jsinterop_generator", 8 | ) 9 | 10 | package( 11 | default_applicable_licenses = ["//:license"], 12 | default_visibility = ["//:__subpackages__"], 13 | licenses = ["notice"], 14 | ) 15 | 16 | jsinterop_generator( 17 | name = "parentthirdparty", 18 | srcs = ["parentthirdparty.js"], 19 | generate_j2cl_build_test = False, 20 | ) 21 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/dependency/parentthirdparty/parentthirdparty.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @fileoverview Test function type across different scopes. 3 | * @externs 4 | */ 5 | 6 | 7 | /** 8 | * Interface provided by the third party lib 9 | * @interface 10 | */ 11 | function ParentThirdPartyInterface() {} 12 | 13 | /** 14 | * @param {{foo:string}} foo 15 | * @return {undefined} 16 | */ 17 | ParentThirdPartyInterface.prototype.parentThirdpartyMethod = function(foo) {}; 18 | 19 | 20 | /** 21 | * Class provided by the third party lib 22 | * class 23 | * @constructor 24 | * @template T 25 | */ 26 | function ParentThirdPartyClass() {} -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/dependency/thirdparty/BUILD: -------------------------------------------------------------------------------- 1 | # Description: 2 | # Tests dependency mechanism 3 | # 4 | 5 | load( 6 | "//:jsinterop_generator.bzl", 7 | "jsinterop_generator", 8 | ) 9 | 10 | package( 11 | default_applicable_licenses = ["//:license"], 12 | default_visibility = ["//:__subpackages__"], 13 | licenses = ["notice"], 14 | ) 15 | 16 | jsinterop_generator( 17 | name = "thirdparty", 18 | srcs = ["thirdparty.js"], 19 | generate_j2cl_build_test = False, 20 | name_mapping_files = ["name_mapping.txt"], 21 | deps = [ 22 | "//javatests/jsinterop/generator/externs/dependency/parentthirdparty", 23 | ], 24 | ) 25 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/dependency/thirdparty/name_mapping.txt: -------------------------------------------------------------------------------- 1 | jsinterop.generator.externs.dependency.thirdparty.ThirdpartyGlobal.ThirdpartyCallbackFn.onInvoke.p0=foo 2 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/dependency/thirdparty/thirdparty.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @fileoverview Test function type across different scopes. 3 | * @externs 4 | */ 5 | 6 | 7 | /** 8 | * Interface provided by the third party lib 9 | * @interface 10 | * @extends ParentThirdPartyInterface 11 | */ 12 | function ThirdPartyInterface() {} 13 | 14 | 15 | /** 16 | * Class provided by the third party lib 17 | * class 18 | * @constructor 19 | * @param {(string|number)} foo 20 | * @template T 21 | * @extends ParentThirdPartyClass 22 | */ 23 | function ThirdPartyClass(foo) {} 24 | 25 | /** 26 | * Test global scope extension 27 | * @param {function(number)} callback 28 | * @return {string} 29 | */ 30 | function thirdparty(callback) {} 31 | 32 | /** 33 | * @const 34 | */ 35 | var namespace1 = {}; 36 | 37 | /** 38 | * @interface 39 | */ 40 | namespace1.InterfaceWithConflictingName = function() {}; 41 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/dependency/thirdparty2/BUILD: -------------------------------------------------------------------------------- 1 | # Description: 2 | # Tests dependency mechanism 3 | # 4 | 5 | load( 6 | "//:jsinterop_generator.bzl", 7 | "jsinterop_generator", 8 | ) 9 | 10 | package( 11 | default_applicable_licenses = ["//:license"], 12 | default_visibility = ["//:__subpackages__"], 13 | licenses = ["notice"], 14 | ) 15 | 16 | jsinterop_generator( 17 | name = "thirdparty2", 18 | srcs = ["thirdparty2.js"], 19 | extension_type_prefix = "Foo", 20 | generate_j2cl_build_test = False, 21 | deps = [ 22 | "//javatests/jsinterop/generator/externs/dependency/thirdparty", 23 | ], 24 | ) 25 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/dependency/thirdparty2/thirdparty2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @fileoverview Test function type across different scopes. 3 | * @externs 4 | */ 5 | 6 | 7 | /** 8 | * Interface provided by the third party lib 9 | * @interface 10 | */ 11 | function ThirdParty2Interface() {} 12 | 13 | 14 | /** 15 | * Class provided by the third party lib 16 | * class 17 | * @constructor 18 | * @template T 19 | */ 20 | function ThirdParty2Class() {} 21 | 22 | /** 23 | * Test api extension of a provided type. 24 | * @return {string} 25 | */ 26 | ThirdPartyClass.prototype.extraMethod2 = function() {}; 27 | 28 | /** 29 | * Test global scope extension 30 | * @return {string} 31 | */ 32 | function thirdparty2() {} 33 | 34 | /** 35 | * @const 36 | */ 37 | var namespace2 = {}; 38 | 39 | /** 40 | * @interface 41 | */ 42 | namespace2.InterfaceWithConflictingName = function() {}; 43 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/dictionarytype/BUILD: -------------------------------------------------------------------------------- 1 | # Description: 2 | # Tests dictionary types conversion 3 | # 4 | 5 | load( 6 | "//javatests/jsinterop/generator:jsinterop_generator_test.bzl", 7 | "jsinterop_generator_test", 8 | ) 9 | 10 | package( 11 | default_applicable_licenses = ["//:license"], 12 | licenses = ["notice"], 13 | ) 14 | 15 | jsinterop_generator_test( 16 | name = "dictionarytype", 17 | srcs = ["dictionarytype.js"], 18 | expected_output = glob(["*.java.txt"]), 19 | ) 20 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/dictionarytype/DictionaryType.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.dictionarytype; 2 | 3 | import jsinterop.annotations.JsOverlay; 4 | import jsinterop.annotations.JsPackage; 5 | import jsinterop.annotations.JsProperty; 6 | import jsinterop.annotations.JsType; 7 | import jsinterop.base.Js; 8 | import jsinterop.base.JsPropertyMap; 9 | 10 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 11 | public interface DictionaryType { 12 | @JsOverlay 13 | static DictionaryType create() { 14 | return Js.uncheckedCast(JsPropertyMap.of()); 15 | } 16 | 17 | @JsProperty 18 | double getBar(); 19 | 20 | @JsProperty 21 | String getFoo(); 22 | 23 | @JsProperty 24 | void setBar(double bar); 25 | 26 | @JsProperty 27 | void setFoo(String foo); 28 | } 29 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/dictionarytype/SimpleDictionaryType.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.dictionarytype; 2 | 3 | import jsinterop.annotations.JsFunction; 4 | import jsinterop.annotations.JsOverlay; 5 | import jsinterop.annotations.JsPackage; 6 | import jsinterop.annotations.JsProperty; 7 | import jsinterop.annotations.JsType; 8 | import jsinterop.base.Js; 9 | import jsinterop.base.JsPropertyMap; 10 | 11 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 12 | public interface SimpleDictionaryType { 13 | @JsType(isNative = true, name = "?", namespace = JsPackage.GLOBAL) 14 | public interface BarFieldType { 15 | @JsOverlay 16 | static SimpleDictionaryType.BarFieldType create() { 17 | return Js.uncheckedCast(JsPropertyMap.of()); 18 | } 19 | 20 | @JsProperty 21 | double getBar(); 22 | 23 | @JsProperty 24 | String getFoo(); 25 | 26 | @JsProperty 27 | void setBar(double bar); 28 | 29 | @JsProperty 30 | void setFoo(String foo); 31 | } 32 | 33 | @JsFunction 34 | public interface BazFn { 35 | @JsType(isNative = true, name = "?", namespace = JsPackage.GLOBAL) 36 | public interface P0Type { 37 | @JsOverlay 38 | static SimpleDictionaryType.BazFn.P0Type create() { 39 | return Js.uncheckedCast(JsPropertyMap.of()); 40 | } 41 | 42 | @JsProperty 43 | double getBar(); 44 | 45 | @JsProperty 46 | String getFoo(); 47 | 48 | @JsProperty 49 | void setBar(double bar); 50 | 51 | @JsProperty 52 | void setFoo(String foo); 53 | } 54 | 55 | void onInvoke(SimpleDictionaryType.BazFn.P0Type p0); 56 | } 57 | 58 | @JsOverlay 59 | static SimpleDictionaryType create() { 60 | return Js.uncheckedCast(JsPropertyMap.of()); 61 | } 62 | 63 | @JsProperty 64 | SimpleDictionaryType.BarFieldType getBar(); 65 | 66 | @JsProperty 67 | SimpleDictionaryType.BazFn getBaz(); 68 | 69 | @JsProperty 70 | double getFoo(); 71 | 72 | @JsProperty 73 | void setBar(SimpleDictionaryType.BarFieldType bar); 74 | 75 | @JsProperty 76 | void setBaz(SimpleDictionaryType.BazFn baz); 77 | 78 | @JsProperty 79 | void setFoo(double foo); 80 | } 81 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/dictionarytype/dictionarytype.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @fileoverview Test conversion of dictionary types. 3 | * @externs 4 | */ 5 | 6 | /** 7 | * @record 8 | */ 9 | function SimpleDictionaryType() {} 10 | 11 | /** 12 | * @type {number} 13 | */ 14 | SimpleDictionaryType.prototype.foo; 15 | 16 | /** 17 | * @type {{foo: string, bar: number}} 18 | */ 19 | SimpleDictionaryType.prototype.bar; 20 | 21 | /** 22 | * @type {function({foo: string, bar: number}):void} 23 | */ 24 | SimpleDictionaryType.prototype.baz; 25 | 26 | /** 27 | * @typedef {{foo: string, bar: number}} 28 | */ 29 | var DictionaryType; 30 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/entitiesrenaming/BUILD: -------------------------------------------------------------------------------- 1 | # Description: 2 | # Tests conversion of function types. 3 | # 4 | 5 | load( 6 | "//javatests/jsinterop/generator:jsinterop_generator_test.bzl", 7 | "jsinterop_generator_test", 8 | ) 9 | 10 | package( 11 | default_applicable_licenses = ["//:license"], 12 | licenses = ["notice"], 13 | ) 14 | 15 | jsinterop_generator_test( 16 | name = "entitiesrenaming", 17 | srcs = ["entitiesrenaming.js"], 18 | expected_output = glob(["*.java.txt"]), 19 | global_scope_class_name = "Global", 20 | name_mapping_files = ["renaming.txt"], 21 | ) 22 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/entitiesrenaming/Foo.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.entitiesrenaming; 2 | 3 | import jsinterop.annotations.JsFunction; 4 | import jsinterop.annotations.JsOverlay; 5 | import jsinterop.annotations.JsPackage; 6 | import jsinterop.annotations.JsProperty; 7 | import jsinterop.annotations.JsType; 8 | import jsinterop.base.Js; 9 | import jsinterop.base.JsPropertyMap; 10 | 11 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 12 | public class Foo { 13 | @JsType(isNative = true, name = "?", namespace = JsPackage.GLOBAL) 14 | public interface BarBarType { 15 | @JsOverlay 16 | static Foo.BarBarType create() { 17 | return Js.uncheckedCast(JsPropertyMap.of()); 18 | } 19 | 20 | @JsProperty 21 | String getBar(); 22 | 23 | @JsProperty 24 | void setBar(String bar); 25 | } 26 | 27 | @JsFunction 28 | public interface FooFooCallbackFn { 29 | void onInvoke(String bar); 30 | } 31 | 32 | public native void bar(Foo.BarBarType bar); 33 | 34 | public native void foo(Foo.FooFooCallbackFn fooCallback); 35 | } 36 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/entitiesrenaming/Global.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.entitiesrenaming; 2 | 3 | import jsinterop.annotations.JsOverlay; 4 | import jsinterop.annotations.JsPackage; 5 | import jsinterop.annotations.JsType; 6 | import jsinterop.base.Js; 7 | import org.jspecify.annotations.Nullable; 8 | 9 | @JsType(isNative = true, name = "goog.global", namespace = JsPackage.GLOBAL) 10 | public class Global { 11 | @JsType(isNative = true, name = "?", namespace = JsPackage.GLOBAL) 12 | public interface FooFooUnionType { 13 | @JsOverlay 14 | static Global.FooFooUnionType of(Object o) { 15 | return Js.cast(o); 16 | } 17 | 18 | @JsOverlay 19 | default double asDouble() { 20 | return Js.asDouble(this); 21 | } 22 | 23 | @JsOverlay 24 | default String asString() { 25 | return Js.asString(this); 26 | } 27 | 28 | @JsOverlay 29 | default boolean isDouble() { 30 | return (Object) this instanceof Double; 31 | } 32 | 33 | @JsOverlay 34 | default boolean isString() { 35 | return (Object) this instanceof String; 36 | } 37 | } 38 | 39 | public static native @Nullable Object foo(Global.FooFooUnionType foo); 40 | 41 | @JsOverlay 42 | public static final @Nullable Object foo(String foo) { 43 | return foo(Js.uncheckedCast(foo)); 44 | } 45 | 46 | @JsOverlay 47 | public static final @Nullable Object foo(double foo) { 48 | return foo(Js.uncheckedCast(foo)); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/entitiesrenaming/SimpleInterface.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.entitiesrenaming; 2 | 3 | import jsinterop.annotations.JsFunction; 4 | import jsinterop.annotations.JsPackage; 5 | import jsinterop.annotations.JsType; 6 | 7 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 8 | @FunctionalInterface 9 | public interface SimpleInterface { 10 | @JsFunction 11 | public interface MethodFooCallbackFn { 12 | @JsFunction 13 | public interface ValueCallbackFn { 14 | void onInvoke(String baz); 15 | } 16 | 17 | boolean onInvoke(SimpleInterface.MethodFooCallbackFn.ValueCallbackFn valueCallback); 18 | } 19 | 20 | void method(SimpleInterface.MethodFooCallbackFn fooCallback); 21 | } 22 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/entitiesrenaming/entitiesrenaming.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @fileoverview Test renaming of some entities. 3 | * @externs 4 | */ 5 | 6 | /** 7 | * @constructor 8 | */ 9 | function Foo() {} 10 | 11 | /** 12 | * @param {function(string):undefined} parameterToRename 13 | * @return {undefined} 14 | */ 15 | Foo.prototype.foo = function(parameterToRename) {}; 16 | 17 | /** 18 | * @param {{bar: string}} parameterToRename 19 | * @return {undefined} 20 | */ 21 | Foo.prototype.bar = function(parameterToRename) {}; 22 | 23 | /** 24 | * @interface 25 | */ 26 | function SimpleInterface() {} 27 | 28 | /** 29 | * @param {function((function(string):undefined)):boolean} fooCallback 30 | * @return {undefined} 31 | */ 32 | SimpleInterface.prototype.method = function(fooCallback) {}; 33 | 34 | 35 | /** 36 | * @param {(string|number)} bar 37 | */ 38 | function foo(bar) {} 39 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/entitiesrenaming/renaming.txt: -------------------------------------------------------------------------------- 1 | jsinterop.generator.externs.entitiesrenaming.Foo.foo.parameterToRename=fooCallback 2 | jsinterop.generator.externs.entitiesrenaming.Foo.bar.parameterToRename=bar 3 | jsinterop.generator.externs.entitiesrenaming.Foo.FooFooCallbackFn.onInvoke.p0=bar 4 | jsinterop.generator.externs.entitiesrenaming.SimpleInterface.MethodFooCallbackFn.onInvoke.p0=valueCallback 5 | jsinterop.generator.externs.entitiesrenaming.SimpleInterface.MethodFooCallbackFn.ValueCallbackFn.onInvoke.p0=baz 6 | jsinterop.generator.externs.entitiesrenaming.Global.foo.bar=foo 7 | 8 | 9 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/enums/BUILD: -------------------------------------------------------------------------------- 1 | # Description: 2 | # Tests conversion of closure enums. 3 | # 4 | 5 | load( 6 | "//javatests/jsinterop/generator:jsinterop_generator_test.bzl", 7 | "jsinterop_generator_test", 8 | ) 9 | 10 | package( 11 | default_applicable_licenses = ["//:license"], 12 | licenses = ["notice"], 13 | ) 14 | 15 | jsinterop_generator_test( 16 | name = "enums", 17 | srcs = ["enums.js"], 18 | expected_output = glob(["*.java.txt"]), 19 | ) 20 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/enums/Bar.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.enums; 2 | 3 | import jsinterop.annotations.JsEnum; 4 | import jsinterop.annotations.JsPackage; 5 | 6 | @JsEnum(isNative = true, namespace = JsPackage.GLOBAL) 7 | public enum Bar { 8 | BAR1, 9 | BAR2; 10 | } 11 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/enums/Baz.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.enums; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsType; 5 | 6 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 7 | @FunctionalInterface 8 | public interface Baz { 9 | Bar toBar(Foo foo); 10 | } 11 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/enums/Foo.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.enums; 2 | 3 | import jsinterop.annotations.JsEnum; 4 | import jsinterop.annotations.JsPackage; 5 | 6 | @JsEnum(isNative = true, namespace = JsPackage.GLOBAL) 7 | public enum Foo { 8 | FOO1, 9 | FOO2; 10 | } 11 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/enums/enums.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @fileoverview Test conversion of closure enums 3 | * @externs 4 | */ 5 | 6 | /** 7 | * @enum {string} 8 | */ 9 | var Foo = {FOO1: 'foo1', FOO2: 'foo2'}; 10 | 11 | /** 12 | * @enum {number} 13 | */ 14 | var Bar = {BAR1: 'bar1', BAR2: 'bar2'}; 15 | 16 | /** 17 | * @interface 18 | */ 19 | function Baz() {} 20 | 21 | /** 22 | * @param {Foo} foo 23 | * @return {Bar} 24 | */ 25 | Baz.prototype.toBar = function(foo) {}; 26 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/functionalinterface/BUILD: -------------------------------------------------------------------------------- 1 | # Description: 2 | # Tests FunctionalInterface annotation logic. 3 | # 4 | load( 5 | "//javatests/jsinterop/generator:jsinterop_generator_test.bzl", 6 | "jsinterop_generator_test", 7 | ) 8 | 9 | package( 10 | default_applicable_licenses = ["//:license"], 11 | licenses = ["notice"], 12 | ) 13 | 14 | jsinterop_generator_test( 15 | name = "functionalinterface", 16 | srcs = ["functionalinterface.js"], 17 | expected_output = glob(["*.java.txt"]), 18 | ) 19 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/functionalinterface/InterfaceWithOneMethod.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.functionalinterface; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsType; 5 | 6 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 7 | @FunctionalInterface 8 | public interface InterfaceWithOneMethod { 9 | void foo(); 10 | } 11 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/functionalinterface/InterfaceWithTwoMethods.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.functionalinterface; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsType; 5 | 6 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 7 | public interface InterfaceWithTwoMethods { 8 | void bar(); 9 | 10 | void foo(); 11 | } 12 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/functionalinterface/StructuralInterface.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.functionalinterface; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsType; 5 | 6 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 7 | public interface StructuralInterface { 8 | void foo(); 9 | } 10 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/functionalinterface/functionalinterface.js: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | /** 16 | * @fileoverview Test FunctionalInterface annotation logic. 17 | * @externs 18 | */ 19 | 20 | /** 21 | * The resulting java interface should be annotated with @FunctionalInterface 22 | * @interface 23 | */ 24 | function InterfaceWithOneMethod() {} 25 | 26 | /** @return {undefined} */ 27 | InterfaceWithOneMethod.prototype.foo = function() {}; 28 | 29 | /** 30 | * The resulting java interface should not be annotated with 31 | * FunctionalInterface annotation. 32 | * @interface 33 | */ 34 | function InterfaceWithTwoMethods() {} 35 | 36 | /** @return {undefined} */ 37 | InterfaceWithTwoMethods.prototype.foo = function() {}; 38 | 39 | /** @return {undefined} */ 40 | InterfaceWithTwoMethods.prototype.bar = function() {}; 41 | 42 | 43 | /** 44 | * The resulting java interface should not be annotated with 45 | * FunctionalInterface annotation. 46 | * @record 47 | */ 48 | function StructuralInterface() {} 49 | 50 | /** @return {undefined} */ 51 | StructuralInterface.prototype.foo = function() {}; 52 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/functiontype/AliasedFunctionType.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.functiontype; 2 | 3 | import jsinterop.annotations.JsFunction; 4 | 5 | @JsFunction 6 | public interface AliasedFunctionType { 7 | boolean onInvoke(String p0); 8 | } 9 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/functiontype/BUILD: -------------------------------------------------------------------------------- 1 | # Description: 2 | # Tests conversion of function types. 3 | # 4 | 5 | load( 6 | "//javatests/jsinterop/generator:jsinterop_generator_test.bzl", 7 | "jsinterop_generator_test", 8 | ) 9 | 10 | package( 11 | default_applicable_licenses = ["//:license"], 12 | licenses = ["notice"], 13 | ) 14 | 15 | jsinterop_generator_test( 16 | name = "functiontype", 17 | srcs = ["functiontype.js"], 18 | expected_output = glob(["*.java.txt"]), 19 | global_scope_class_name = "Global", 20 | deps = [ 21 | "//javatests/jsinterop/generator/externs/natives", 22 | ], 23 | ) 24 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/functiontype/SimpleClass.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.functiontype; 2 | 3 | import jsinterop.annotations.JsFunction; 4 | import jsinterop.annotations.JsPackage; 5 | import jsinterop.annotations.JsType; 6 | 7 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 8 | public class SimpleClass { 9 | @JsFunction 10 | public interface FooFn { 11 | boolean onInvoke(String p0); 12 | } 13 | 14 | @JsFunction 15 | public interface Method1Fn { 16 | boolean onInvoke(String p0); 17 | } 18 | 19 | @JsFunction 20 | public interface MethodFooCallbackFn { 21 | boolean onInvoke(String p0); 22 | } 23 | 24 | public AliasedFunctionType bar; 25 | public SimpleClass.FooFn foo; 26 | 27 | public native void method(SimpleClass.MethodFooCallbackFn fooCallback); 28 | 29 | public native SimpleClass.Method1Fn method1(String foo); 30 | } 31 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/functiontype/SimpleInterface.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.functiontype; 2 | 3 | import jsinterop.annotations.JsFunction; 4 | import jsinterop.annotations.JsOverlay; 5 | import jsinterop.annotations.JsPackage; 6 | import jsinterop.annotations.JsProperty; 7 | import jsinterop.annotations.JsType; 8 | import jsinterop.base.Js; 9 | 10 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 11 | public interface SimpleInterface { 12 | @JsFunction 13 | public interface FooFn { 14 | boolean onInvoke(String p0); 15 | } 16 | 17 | @JsFunction 18 | public interface Method1Fn { 19 | boolean onInvoke(String p0); 20 | } 21 | 22 | @JsFunction 23 | public interface MethodFooCallbackFn { 24 | boolean onInvoke(String p0); 25 | } 26 | 27 | @JsFunction 28 | public interface WithUnionTypeFooCallbackFn { 29 | @JsType(isNative = true, name = "?", namespace = JsPackage.GLOBAL) 30 | public interface P0UnionType { 31 | @JsOverlay 32 | static SimpleInterface.WithUnionTypeFooCallbackFn.P0UnionType of(Object o) { 33 | return Js.cast(o); 34 | } 35 | 36 | @JsOverlay 37 | default double asDouble() { 38 | return Js.asDouble(this); 39 | } 40 | 41 | @JsOverlay 42 | default String asString() { 43 | return Js.asString(this); 44 | } 45 | 46 | @JsOverlay 47 | default boolean isDouble() { 48 | return (Object) this instanceof Double; 49 | } 50 | 51 | @JsOverlay 52 | default boolean isString() { 53 | return (Object) this instanceof String; 54 | } 55 | } 56 | 57 | boolean onInvoke(SimpleInterface.WithUnionTypeFooCallbackFn.P0UnionType p0); 58 | 59 | @JsOverlay 60 | default boolean onInvoke(String p0) { 61 | return onInvoke(Js.uncheckedCast(p0)); 62 | } 63 | 64 | @JsOverlay 65 | default boolean onInvoke(double p0) { 66 | return onInvoke(Js.uncheckedCast(p0)); 67 | } 68 | } 69 | 70 | @JsProperty 71 | AliasedFunctionType getBar(); 72 | 73 | @JsProperty 74 | SimpleInterface.FooFn getFoo(); 75 | 76 | void method(SimpleInterface.MethodFooCallbackFn fooCallback); 77 | 78 | SimpleInterface.Method1Fn method1(String foo); 79 | 80 | @JsProperty 81 | void setBar(AliasedFunctionType bar); 82 | 83 | @JsProperty 84 | void setFoo(SimpleInterface.FooFn foo); 85 | 86 | void withUnionType(SimpleInterface.WithUnionTypeFooCallbackFn fooCallback); 87 | } 88 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/functiontype/SimpleModule.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.functiontype; 2 | 3 | import jsinterop.annotations.JsFunction; 4 | import jsinterop.annotations.JsPackage; 5 | import jsinterop.annotations.JsType; 6 | 7 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 8 | public class SimpleModule { 9 | @JsFunction 10 | public interface FooFn { 11 | boolean onInvoke(String p0); 12 | } 13 | 14 | @JsFunction 15 | public interface Method1Fn { 16 | boolean onInvoke(String p0); 17 | } 18 | 19 | @JsFunction 20 | public interface MethodFooCallbackFn { 21 | boolean onInvoke(String p0); 22 | } 23 | 24 | public static AliasedFunctionType bar; 25 | public static SimpleModule.FooFn foo; 26 | 27 | public static native void method(SimpleModule.MethodFooCallbackFn fooCallback); 28 | 29 | public static native SimpleModule.Method1Fn method1(String foo); 30 | } 31 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/generics/BUILD: -------------------------------------------------------------------------------- 1 | # Description: 2 | # Tests conversion of generics 3 | 4 | load( 5 | "//javatests/jsinterop/generator:jsinterop_generator_test.bzl", 6 | "jsinterop_generator_test", 7 | ) 8 | 9 | package( 10 | default_applicable_licenses = ["//:license"], 11 | licenses = ["notice"], 12 | ) 13 | 14 | jsinterop_generator_test( 15 | name = "generics", 16 | srcs = ["generics.js"], 17 | expected_output = glob([ 18 | "*.java.txt", 19 | ]), 20 | deps = ["//javatests/jsinterop/generator/externs/natives"], 21 | ) 22 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/generics/BarChild.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.generics; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsType; 5 | import org.jspecify.annotations.Nullable; 6 | 7 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 8 | public interface BarChild< 9 | U extends @Nullable Object, T extends @Nullable Object, V extends @Nullable Object> 10 | extends Bar { 11 | T methodWithThis(U u, V v); 12 | } 13 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/generics/ExtendInterfaceWithGeneric.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.generics; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsType; 5 | import jsinterop.generator.externs.natives.JsArray; 6 | import org.jspecify.annotations.Nullable; 7 | 8 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 9 | public interface ExtendInterfaceWithGeneric extends InterfaceWithGeneric { 10 | void bar(@Nullable InterfaceWithGeneric<@Nullable JsArray> param); 11 | } 12 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/generics/InterfaceWithGeneric.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.generics; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsProperty; 5 | import jsinterop.annotations.JsType; 6 | import org.jspecify.annotations.Nullable; 7 | 8 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 9 | public interface InterfaceWithGeneric { 10 | @JsProperty 11 | T getFoo(); 12 | 13 | @JsProperty 14 | @Nullable T getNullabeFoo(); 15 | 16 | void method(T foo); 17 | 18 | T method2(); 19 | 20 | void methodWithNullableParam(@Nullable T foo); 21 | 22 | @Nullable T methodWithNullableReturn(); 23 | 24 | void methodWithOptionalNullableParam(); 25 | 26 | void methodWithOptionalNullableParam(@Nullable T foo); 27 | 28 | void methodWithOptionalParam(); 29 | 30 | void methodWithOptionalParam(T foo); 31 | 32 | @JsProperty 33 | void setFoo(T foo); 34 | 35 | @JsProperty 36 | void setNullabeFoo(@Nullable T nullabeFoo); 37 | } 38 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/generics/RecordWithGeneric.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.generics; 2 | 3 | import jsinterop.annotations.JsOverlay; 4 | import jsinterop.annotations.JsPackage; 5 | import jsinterop.annotations.JsProperty; 6 | import jsinterop.annotations.JsType; 7 | import jsinterop.base.Js; 8 | import jsinterop.base.JsPropertyMap; 9 | import org.jspecify.annotations.Nullable; 10 | 11 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 12 | public interface RecordWithGeneric { 13 | @JsOverlay 14 | static RecordWithGeneric create() { 15 | return Js.uncheckedCast(JsPropertyMap.of()); 16 | } 17 | 18 | @JsProperty 19 | T getFoo(); 20 | 21 | @JsProperty 22 | void setFoo(T foo); 23 | } 24 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/generics/SimpleClass.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.generics; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsType; 5 | import org.jspecify.annotations.Nullable; 6 | 7 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 8 | public class SimpleClass { 9 | public static native V foo(@Nullable SimpleClass obj); 10 | 11 | public SimpleClass(T value) {} 12 | 13 | public native SimpleClass chainableMethodWithThis(SimpleClass param); 14 | 15 | public native V foo(); 16 | } 17 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/generics/SimpleClassChild.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.generics; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsType; 5 | 6 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 7 | public class SimpleClassChild extends SimpleClass { 8 | public SimpleClassChild() { 9 | // This super call is here only for the code to compile; it is never executed. 10 | super((String) null); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/generics/SimpleInterface.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.generics; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsType; 5 | 6 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 7 | public interface SimpleInterface {} 8 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/globalscope/BUILD: -------------------------------------------------------------------------------- 1 | # Description: 2 | # Tests conversion property/functions defined on the global scope 3 | # 4 | 5 | load( 6 | "//javatests/jsinterop/generator:jsinterop_generator_test.bzl", 7 | "jsinterop_generator_test", 8 | ) 9 | 10 | package( 11 | default_applicable_licenses = ["//:license"], 12 | licenses = ["notice"], 13 | ) 14 | 15 | jsinterop_generator_test( 16 | name = "globalscope", 17 | srcs = ["globalscope.js"], 18 | expected_output = glob(["*.java.txt"]), 19 | global_scope_class_name = "Global", 20 | ) 21 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/globalscope/Global.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.globalscope; 2 | 3 | import jsinterop.annotations.JsOverlay; 4 | import jsinterop.annotations.JsPackage; 5 | import jsinterop.annotations.JsType; 6 | 7 | @JsType(isNative = true, name = "goog.global", namespace = JsPackage.GLOBAL) 8 | public class Global { 9 | @JsOverlay public static final double constantFoo = Global__Constants.constantFoo; 10 | public static String foo; 11 | 12 | public static native double bar(double bar, String foo, boolean baz); 13 | 14 | public static native double bar(double bar, String foo); 15 | 16 | public static native double bar(double bar); 17 | } 18 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/globalscope/Global__Constants.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.globalscope; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsType; 5 | 6 | @JsType(isNative = true, name = "goog.global", namespace = JsPackage.GLOBAL) 7 | class Global__Constants { 8 | static double constantFoo; 9 | } 10 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/globalscope/globalscope.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @fileoverview Test conversion of variable and functions defined on the global 3 | * scope. 4 | * @externs 5 | */ 6 | 7 | /** 8 | * @param {number} bar 9 | * @param {string=} opt_foo 10 | * @param {boolean=} opt_baz 11 | * @return {number} 12 | */ 13 | function bar(bar, opt_foo, opt_baz) {} 14 | 15 | /** 16 | * @type {string} 17 | */ 18 | var foo; 19 | 20 | /** @const {number} */ 21 | var constantFoo; 22 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/inheritance/BUILD: -------------------------------------------------------------------------------- 1 | # Description: 2 | # Tests conversion of type inehritance 3 | # 4 | 5 | load( 6 | "//javatests/jsinterop/generator:jsinterop_generator_test.bzl", 7 | "jsinterop_generator_test", 8 | ) 9 | 10 | package( 11 | default_applicable_licenses = ["//:license"], 12 | licenses = ["notice"], 13 | ) 14 | 15 | jsinterop_generator_test( 16 | name = "inheritance", 17 | srcs = ["inheritance.js"], 18 | expected_output = glob(["*.java.txt"]), 19 | ) 20 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/inheritance/GreatParentClass.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.inheritance; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsType; 5 | 6 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 7 | public class GreatParentClass { 8 | public double greatParentClassProperty; 9 | 10 | public GreatParentClass(String s, boolean b, double n) {} 11 | 12 | public native double greatParentClassMethod(); 13 | } 14 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/inheritance/GreatParentInterface.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.inheritance; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsProperty; 5 | import jsinterop.annotations.JsType; 6 | 7 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 8 | public interface GreatParentInterface { 9 | @JsProperty 10 | String getGreatParentInterfaceProperty(); 11 | 12 | String greatParentInterfaceMethod(boolean foo); 13 | 14 | @JsProperty 15 | void setGreatParentInterfaceProperty(String greatParentInterfaceProperty); 16 | } 17 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/inheritance/Parent1Interface.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.inheritance; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsProperty; 5 | import jsinterop.annotations.JsType; 6 | 7 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 8 | public interface Parent1Interface extends GreatParentInterface { 9 | @JsProperty 10 | boolean isParent1InterfaceProperty(); 11 | 12 | boolean parent1InterfaceMethod(double foo); 13 | 14 | @JsProperty 15 | void setParent1InterfaceProperty(boolean parent1InterfaceProperty); 16 | } 17 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/inheritance/Parent2Interface.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.inheritance; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsProperty; 5 | import jsinterop.annotations.JsType; 6 | 7 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 8 | public interface Parent2Interface { 9 | @JsProperty 10 | double getParent2InterfaceProperty(); 11 | 12 | double parent2InterfaceMethod(); 13 | 14 | @JsProperty 15 | void setParent2InterfaceProperty(double parent2InterfaceProperty); 16 | } 17 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/inheritance/ParentClass.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.inheritance; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsType; 5 | 6 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 7 | public class ParentClass extends GreatParentClass { 8 | public double parentClassProperty; 9 | 10 | public ParentClass() { 11 | // This super call is here only for the code to compile; it is never executed. 12 | super((String) null, false, 0); 13 | } 14 | 15 | public native double parentClassMethod(); 16 | } 17 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/inheritance/SimpleClass.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.inheritance; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsProperty; 5 | import jsinterop.annotations.JsType; 6 | 7 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 8 | public class SimpleClass extends ParentClass implements Parent1Interface, Parent2Interface { 9 | public String classProperty; 10 | public String greatParentInterfaceProperty; 11 | public boolean parent1InterfaceProperty; 12 | public double parent2InterfaceProperty; 13 | 14 | public native boolean classMethod(String foo); 15 | 16 | @JsProperty 17 | public native String getGreatParentInterfaceProperty(); 18 | 19 | @JsProperty 20 | public native double getParent2InterfaceProperty(); 21 | 22 | public native String greatParentInterfaceMethod(boolean foo); 23 | 24 | @JsProperty 25 | public native boolean isParent1InterfaceProperty(); 26 | 27 | public native boolean parent1InterfaceMethod(double foo); 28 | 29 | public native double parent2InterfaceMethod(); 30 | 31 | @JsProperty 32 | public native void setGreatParentInterfaceProperty(String greatParentInterfaceProperty); 33 | 34 | @JsProperty 35 | public native void setParent1InterfaceProperty(boolean parent1InterfaceProperty); 36 | 37 | @JsProperty 38 | public native void setParent2InterfaceProperty(double parent2InterfaceProperty); 39 | } 40 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/integerentities/BUILD: -------------------------------------------------------------------------------- 1 | # Description: 2 | # Tests conversion integer entities. 3 | # 4 | 5 | load( 6 | "//javatests/jsinterop/generator:jsinterop_generator_test.bzl", 7 | "jsinterop_generator_test", 8 | ) 9 | 10 | package( 11 | default_applicable_licenses = ["//:license"], 12 | licenses = ["notice"], 13 | ) 14 | 15 | jsinterop_generator_test( 16 | name = "integerentities", 17 | srcs = ["integerentities.js"], 18 | expected_output = glob(["*.java.txt"]), 19 | global_scope_class_name = "Global", 20 | integer_entities_files = ["integerentities.txt"], 21 | name_mapping_files = ["renaming.txt"], 22 | ) 23 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/integerentities/Foo.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.integerentities; 2 | 3 | import jsinterop.annotations.JsFunction; 4 | import jsinterop.annotations.JsOverlay; 5 | import jsinterop.annotations.JsPackage; 6 | import jsinterop.annotations.JsType; 7 | import jsinterop.base.Js; 8 | import org.jspecify.annotations.Nullable; 9 | 10 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 11 | public class Foo { 12 | @JsFunction 13 | public interface FooCallbackFn { 14 | void onInvoke(int foo); 15 | } 16 | 17 | @JsType(isNative = true, name = "?", namespace = JsPackage.GLOBAL) 18 | public interface UnionUnionParamUnionType { 19 | @JsOverlay 20 | static Foo.UnionUnionParamUnionType of(Object o) { 21 | return Js.cast(o); 22 | } 23 | 24 | @JsOverlay 25 | default int asInt() { 26 | return Js.asInt(this); 27 | } 28 | 29 | @JsOverlay 30 | default String asString() { 31 | return Js.asString(this); 32 | } 33 | 34 | @JsOverlay 35 | default boolean isInt() { 36 | return (Object) this instanceof Double; 37 | } 38 | 39 | @JsOverlay 40 | default boolean isString() { 41 | return (Object) this instanceof String; 42 | } 43 | } 44 | 45 | @JsOverlay public static final int INT_CONSTANT = Foo__Constants.INT_CONSTANT; 46 | public static int baz; 47 | public int bar; 48 | 49 | public Foo(int foo) {} 50 | 51 | public native int foo(int integerParam, double doubleParam, Foo.FooCallbackFn callback); 52 | 53 | public native void methodWithOptionalParameter(int param1, double optional); 54 | 55 | public native void methodWithOptionalParameter(int param1); 56 | 57 | @JsOverlay 58 | public final @Nullable Object union(String unionParam) { 59 | return union(Js.uncheckedCast(unionParam)); 60 | } 61 | 62 | public native @Nullable Object union(Foo.UnionUnionParamUnionType unionParam); 63 | 64 | @JsOverlay 65 | public final @Nullable Object union(int unionParam) { 66 | return union(Js.uncheckedCast(unionParam)); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/integerentities/Foo__Constants.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.integerentities; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsType; 5 | 6 | @JsType(isNative = true, name = "Foo", namespace = JsPackage.GLOBAL) 7 | class Foo__Constants { 8 | static int INT_CONSTANT; 9 | } 10 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/integerentities/Global.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.integerentities; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsType; 5 | 6 | @JsType(isNative = true, name = "goog.global", namespace = JsPackage.GLOBAL) 7 | public class Global { 8 | public static int baz; 9 | 10 | public static native int foo(int bar); 11 | } 12 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/integerentities/integerentities.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @fileoverview Test conversion of integer entities. 3 | * @externs 4 | */ 5 | 6 | /** 7 | * @param {number} foo 8 | * @constructor 9 | */ 10 | function Foo(foo) {} 11 | 12 | /** 13 | * @param {number} integerParam 14 | * @param {number} doubleParam 15 | * @param {function(number):undefined} callback 16 | * @return {number} 17 | */ 18 | Foo.prototype.foo = function(integerParam, doubleParam, callback) {}; 19 | 20 | 21 | /** 22 | * @param {number} param1 23 | * @param {number=} optional 24 | * @return {void} 25 | */ 26 | Foo.prototype.methodWithOptionalParameter = function(param1, optional) {}; 27 | 28 | /** 29 | * @type {number} 30 | */ 31 | Foo.prototype.bar; 32 | 33 | /** 34 | * @type {number} 35 | */ 36 | Foo.baz; 37 | 38 | /** 39 | * @const {number} 40 | */ 41 | Foo.INT_CONSTANT; 42 | 43 | /** 44 | * @param {number|string} unionParam 45 | */ 46 | Foo.prototype.union = function(unionParam) {}; 47 | 48 | /** 49 | * @param {number} bar 50 | * @return {number} 51 | */ 52 | function foo(bar) {} 53 | 54 | /** 55 | * @type {number} 56 | */ 57 | var baz; 58 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/integerentities/integerentities.txt: -------------------------------------------------------------------------------- 1 | jsinterop.generator.externs.integerentities.Foo.constructor.foo 2 | jsinterop.generator.externs.integerentities.Foo.foo 3 | jsinterop.generator.externs.integerentities.Foo.union.unionParam 4 | jsinterop.generator.externs.integerentities.Foo.foo.integerParam 5 | jsinterop.generator.externs.integerentities.Foo.bar 6 | jsinterop.generator.externs.integerentities.Foo.baz 7 | jsinterop.generator.externs.integerentities.Foo.INT_CONSTANT 8 | jsinterop.generator.externs.integerentities.Foo.methodWithOptionalParameter.param1 9 | jsinterop.generator.externs.integerentities.Foo.FooCallbackFn.onInvoke.foo 10 | jsinterop.generator.externs.integerentities.Global.foo 11 | jsinterop.generator.externs.integerentities.Global.foo.bar 12 | jsinterop.generator.externs.integerentities.Global.baz 13 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/integerentities/renaming.txt: -------------------------------------------------------------------------------- 1 | jsinterop.generator.externs.integerentities.Foo.FooCallbackFn.onInvoke.p0=foo 2 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/iobjectiarraylike/BUILD: -------------------------------------------------------------------------------- 1 | # Description: 2 | # Tests conversion index signatures 3 | # 4 | 5 | load( 6 | "//javatests/jsinterop/generator:jsinterop_generator_test.bzl", 7 | "jsinterop_generator_test", 8 | ) 9 | 10 | package( 11 | default_applicable_licenses = ["//:license"], 12 | licenses = ["notice"], 13 | ) 14 | 15 | jsinterop_generator_test( 16 | name = "iobjectiarraylike", 17 | srcs = ["iobjectiarraylike.js"], 18 | expected_output = glob(["*.java.txt"]), 19 | deps = ["//javatests/jsinterop/generator/externs/natives"], 20 | ) 21 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/iobjectiarraylike/Baz.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.iobjectiarraylike; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsType; 5 | import jsinterop.base.JsArrayLike; 6 | import jsinterop.base.JsPropertyMap; 7 | import org.jspecify.annotations.Nullable; 8 | 9 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 10 | public interface Baz extends JsArrayLike, JsPropertyMap {} 11 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/iobjectiarraylike/Foo.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.iobjectiarraylike; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsType; 5 | import jsinterop.base.JsPropertyMap; 6 | 7 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 8 | public class Foo implements JsPropertyMap {} 9 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/iobjectiarraylike/Varargs.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.iobjectiarraylike; 2 | 3 | import jsinterop.annotations.JsOverlay; 4 | import jsinterop.annotations.JsPackage; 5 | import jsinterop.annotations.JsType; 6 | import jsinterop.base.Js; 7 | import jsinterop.base.JsArrayLike; 8 | import jsinterop.generator.externs.natives.JsObject; 9 | import org.jspecify.annotations.Nullable; 10 | 11 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 12 | public class Varargs { 13 | public native void methodWithIArrayLikeVarargs( 14 | @Nullable JsObject object, @Nullable JsArrayLike... var_args); 15 | 16 | @JsOverlay 17 | public final void methodWithIArrayLikeVarargs( 18 | @Nullable JsObject object, String @Nullable []... var_args) { 19 | methodWithIArrayLikeVarargs( 20 | object, Js.<@Nullable JsArrayLike[]>uncheckedCast(var_args)); 21 | } 22 | 23 | public native void methodWithIArrayLikeVarargs(@Nullable JsObject object); 24 | 25 | @JsOverlay 26 | public final void methodWithIArrayLikeVarargs( 27 | @Nullable Object object, @Nullable JsArrayLike... var_args) { 28 | methodWithIArrayLikeVarargs(Js.<@Nullable JsObject>uncheckedCast(object), var_args); 29 | } 30 | 31 | @JsOverlay 32 | public final void methodWithIArrayLikeVarargs( 33 | @Nullable Object object, String @Nullable []... var_args) { 34 | methodWithIArrayLikeVarargs(Js.<@Nullable JsObject>uncheckedCast(object), var_args); 35 | } 36 | 37 | @JsOverlay 38 | public final void methodWithIArrayLikeVarargs(@Nullable Object object) { 39 | methodWithIArrayLikeVarargs(Js.<@Nullable JsObject>uncheckedCast(object)); 40 | } 41 | 42 | public native void methodWithJsObjectVarargs( 43 | @Nullable JsObject obj, @Nullable JsObject... var_args); 44 | 45 | public native void methodWithJsObjectVarargs(@Nullable JsObject obj); 46 | 47 | @JsOverlay 48 | public final void methodWithJsObjectVarargs(@Nullable Object obj, @Nullable Object... var_args) { 49 | methodWithJsObjectVarargs( 50 | Js.<@Nullable JsObject>uncheckedCast(obj), 51 | Js.<@Nullable JsObject[]>uncheckedCast(var_args)); 52 | } 53 | 54 | @JsOverlay 55 | public final void methodWithJsObjectVarargs(@Nullable Object obj) { 56 | methodWithJsObjectVarargs(Js.<@Nullable JsObject>uncheckedCast(obj)); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/iobjectiarraylike/iobjectiarraylike.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @fileoverview Test conversion of IObject and IArrayLike. IObject references 3 | * should be converted to jsinterop.base.JsPropertyMap and IArrayLike to 4 | * jsinterop.base.JsArrayLike. 5 | *

6 | * Internally, JsCompiler represent the Object type as a parametrized type with 7 | * two optional type parameters: IObject#KEY1, IObject#VALUE. 8 | * The tests using Object references are needed for checking that conversion of 9 | * the two optional type parameters are correct in all cases. 10 | * 11 | * @externs 12 | */ 13 | /** 14 | * @constructor 15 | * @implements {IObject} 16 | */ 17 | function Foo() {} 18 | 19 | /** 20 | * @constructor 21 | * @implements {IArrayLike} 22 | */ 23 | function Bar() {} 24 | 25 | /** 26 | * @return {IArrayLike} 27 | */ 28 | Bar.prototype.asIArrayLike = function() {}; 29 | 30 | /** 31 | * @return {IObject} 32 | */ 33 | Bar.prototype.asIObject = function() {}; 34 | 35 | /** 36 | * @param {IObject} object 37 | * @param {IArrayLike} arrayLike 38 | * @param {IArrayLike>} doubleArrayLike 39 | * @return {undefined} 40 | */ 41 | Bar.prototype.consumeIObjectAndIArrayLike = function( 42 | object, arrayLike, doubleArrayLike) {}; 43 | 44 | 45 | /** 46 | * @param {Object} object 47 | * @param {IArrayLike} arrayLike 48 | * @param {?function(new:Bar, string)} ctor 49 | * @return {undefined} 50 | */ 51 | Bar.prototype.consumeObjectIArrayLikeAndCtorFn = function( 52 | object, arrayLike, ctor) {}; 53 | 54 | /** 55 | * @type {IObject} 56 | */ 57 | Bar.prototype.iObjectField; 58 | 59 | /** 60 | * @type {IArrayLike} 61 | */ 62 | Bar.prototype.iArrayLikeField; 63 | 64 | /** 65 | * @type {Object} 66 | */ 67 | Bar.prototype.templatizedObject; 68 | 69 | /** 70 | * @type {Object} 71 | */ 72 | Bar.prototype.templatizedObjectWithTwoParameters; 73 | 74 | /** 75 | * @type {Object} 76 | */ 77 | Bar.prototype.templatizedObjectWithStringOrNumberKeys; 78 | 79 | /** 80 | * @type {Object} 81 | */ 82 | Bar.prototype.templatizedObjectWithStringOrSymbolKeys; 83 | 84 | /** 85 | * @interface 86 | * @extends {IObject} 87 | * @extends {IArrayLike} 88 | * @template T 89 | */ 90 | function Baz() {} 91 | 92 | /** @constructor */ 93 | function Varargs() {} 94 | 95 | /** 96 | * @param {Object} obj 97 | * @param {...Object} var_args 98 | * @return {void} 99 | */ 100 | Varargs.prototype.methodWithJsObjectVarargs = function(obj, var_args) {}; 101 | 102 | /** 103 | * @param {Object} object 104 | * @param {...IArrayLike} var_args 105 | * @return {void} 106 | */ 107 | Varargs.prototype.methodWithIArrayLikeVarargs = function(object, var_args) {}; 108 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/modules/BUILD: -------------------------------------------------------------------------------- 1 | # Description: 2 | # Tests conversion of modules 3 | # 4 | 5 | load( 6 | "//javatests/jsinterop/generator:jsinterop_generator_test.bzl", 7 | "jsinterop_generator_test", 8 | ) 9 | 10 | package( 11 | default_applicable_licenses = ["//:license"], 12 | licenses = ["notice"], 13 | ) 14 | 15 | jsinterop_generator_test( 16 | name = "modules", 17 | srcs = ["modules.js"], 18 | expected_output = glob([ 19 | "*.java.txt", 20 | ]), 21 | ) 22 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/modules/Interface.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.modules.namespacewithtypeonly; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsType; 5 | 6 | @JsType(isNative = true, name = "namespacewithtypeonly.Interface", namespace = JsPackage.GLOBAL) 7 | public interface Interface {} 8 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/modules/InterfaceFromNestedNamespace.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.modules.namespace.nestednamespace; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsType; 5 | 6 | @JsType( 7 | isNative = true, 8 | name = "namespace.nestednamespace.InterfaceFromNestedNamespace", 9 | namespace = JsPackage.GLOBAL) 10 | public interface InterfaceFromNestedNamespace {} 11 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/modules/Namespace.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.modules; 2 | 3 | import jsinterop.annotations.JsOverlay; 4 | import jsinterop.annotations.JsPackage; 5 | import jsinterop.annotations.JsType; 6 | import jsinterop.base.Js; 7 | import jsinterop.generator.externs.modules.namespace.NamespacedFunctionType; 8 | import jsinterop.generator.externs.modules.namespace.NamespacedTypeDefOfRecord; 9 | import jsinterop.generator.externs.modules.namespace.nestednamespace.InterfaceFromNestedNamespace; 10 | import org.jspecify.annotations.Nullable; 11 | 12 | @JsType(isNative = true, name = "namespace", namespace = JsPackage.GLOBAL) 13 | public class Namespace { 14 | @JsType(isNative = true, name = "?", namespace = JsPackage.GLOBAL) 15 | public interface NamespacedUnionTypeRefUnionType { 16 | @JsOverlay 17 | static Namespace.NamespacedUnionTypeRefUnionType of(Object o) { 18 | return Js.cast(o); 19 | } 20 | 21 | @JsOverlay 22 | default double asDouble() { 23 | return Js.asDouble(this); 24 | } 25 | 26 | @JsOverlay 27 | default String asString() { 28 | return Js.asString(this); 29 | } 30 | 31 | @JsOverlay 32 | default boolean isDouble() { 33 | return (Object) this instanceof Double; 34 | } 35 | 36 | @JsOverlay 37 | default boolean isString() { 38 | return (Object) this instanceof String; 39 | } 40 | } 41 | 42 | public static NamespacedFunctionType namespacedFunctionTypeRef; 43 | public static NamespacedTypeDefOfRecord namespacedTypeDefOfRecordRef; 44 | public static Namespace.NamespacedUnionTypeRefUnionType namespacedUnionTypeRef; 45 | public static @Nullable InterfaceFromNestedNamespace staticProperty; 46 | } 47 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/modules/NamespacedClass.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.modules.namespace; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsType; 5 | 6 | @JsType(isNative = true, name = "namespace.NamespacedClass", namespace = JsPackage.GLOBAL) 7 | public class NamespacedClass {} 8 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/modules/NamespacedEnum.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.modules.namespace; 2 | 3 | import jsinterop.annotations.JsEnum; 4 | import jsinterop.annotations.JsPackage; 5 | 6 | @JsEnum(isNative = true, name = "namespace.NamespacedEnum", namespace = JsPackage.GLOBAL) 7 | public enum NamespacedEnum { 8 | A, 9 | B; 10 | } 11 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/modules/NamespacedFunctionType.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.modules.namespace; 2 | 3 | import jsinterop.annotations.JsFunction; 4 | 5 | @JsFunction 6 | public interface NamespacedFunctionType { 7 | boolean onInvoke(String p0); 8 | } 9 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/modules/NamespacedInterface.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.modules.namespace; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsType; 5 | 6 | @JsType(isNative = true, name = "namespace.NamespacedInterface", namespace = JsPackage.GLOBAL) 7 | public interface NamespacedInterface {} 8 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/modules/NamespacedRecord.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.modules.namespace; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsType; 5 | 6 | @JsType(isNative = true, name = "namespace.NamespacedRecord", namespace = JsPackage.GLOBAL) 7 | public interface NamespacedRecord {} 8 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/modules/NamespacedTypeDefOfRecord.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.modules.namespace; 2 | 3 | import jsinterop.annotations.JsOverlay; 4 | import jsinterop.annotations.JsPackage; 5 | import jsinterop.annotations.JsProperty; 6 | import jsinterop.annotations.JsType; 7 | import jsinterop.base.Js; 8 | import jsinterop.base.JsPropertyMap; 9 | 10 | @JsType(isNative = true, name = "namespace.NamespacedTypeDefOfRecord", namespace = JsPackage.GLOBAL) 11 | public interface NamespacedTypeDefOfRecord { 12 | @JsOverlay 13 | static NamespacedTypeDefOfRecord create() { 14 | return Js.uncheckedCast(JsPropertyMap.of()); 15 | } 16 | 17 | @JsProperty 18 | String getBar(); 19 | 20 | @JsProperty 21 | double getFoo(); 22 | 23 | @JsProperty 24 | void setBar(String bar); 25 | 26 | @JsProperty 27 | void setFoo(double foo); 28 | } 29 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/modules/Nestednamespace.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.modules.namespace; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsType; 5 | import org.jspecify.annotations.Nullable; 6 | 7 | @JsType(isNative = true, name = "namespace.nestednamespace", namespace = JsPackage.GLOBAL) 8 | public class Nestednamespace { 9 | public static @Nullable NamespacedInterface staticProperty; 10 | 11 | public static native void staticFunction(); 12 | } 13 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/modules/Othernamespace.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.modules; 2 | 3 | import jsinterop.annotations.JsOverlay; 4 | import jsinterop.annotations.JsPackage; 5 | import jsinterop.annotations.JsType; 6 | import jsinterop.base.Js; 7 | import jsinterop.generator.externs.modules.namespace.NamespacedFunctionType; 8 | import jsinterop.generator.externs.modules.namespace.NamespacedTypeDefOfRecord; 9 | import jsinterop.generator.externs.modules.namespace.nestednamespace.InterfaceFromNestedNamespace; 10 | import org.jspecify.annotations.Nullable; 11 | 12 | @JsType(isNative = true, name = "othernamespace", namespace = JsPackage.GLOBAL) 13 | public class Othernamespace { 14 | @JsType(isNative = true, name = "?", namespace = JsPackage.GLOBAL) 15 | public interface NamespacedUnionTypeRefUnionType { 16 | @JsOverlay 17 | static Othernamespace.NamespacedUnionTypeRefUnionType of(Object o) { 18 | return Js.cast(o); 19 | } 20 | 21 | @JsOverlay 22 | default double asDouble() { 23 | return Js.asDouble(this); 24 | } 25 | 26 | @JsOverlay 27 | default String asString() { 28 | return Js.asString(this); 29 | } 30 | 31 | @JsOverlay 32 | default boolean isDouble() { 33 | return (Object) this instanceof Double; 34 | } 35 | 36 | @JsOverlay 37 | default boolean isString() { 38 | return (Object) this instanceof String; 39 | } 40 | } 41 | 42 | public static NamespacedFunctionType namespacedFunctionTypeRef; 43 | public static NamespacedTypeDefOfRecord namespacedTypeDefOfRecordRef; 44 | public static Othernamespace.NamespacedUnionTypeRefUnionType namespacedUnionTypeRef; 45 | public static @Nullable InterfaceFromNestedNamespace property; 46 | } 47 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/modules/modules.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @fileoverview Test conversion of modules and namespaces. 3 | * @externs 4 | */ 5 | 6 | /** 7 | * @const 8 | */ 9 | var namespace = {}; 10 | 11 | /** 12 | * @const 13 | */ 14 | namespace.nestednamespace = {}; 15 | 16 | /** 17 | * Property referring the type of the enclosing namespace. 18 | * 19 | * @type {namespace.NamespacedInterface} 20 | */ 21 | namespace.nestednamespace.staticProperty; 22 | 23 | /** 24 | * @return {undefined} 25 | */ 26 | namespace.nestednamespace.staticFunction = function() {}; 27 | 28 | /** 29 | * @interface 30 | */ 31 | namespace.nestednamespace.InterfaceFromNestedNamespace = function() {}; 32 | 33 | /** 34 | * Property referring the type of the nested namespace. 35 | * 36 | * @type {namespace.nestednamespace.InterfaceFromNestedNamespace} 37 | */ 38 | namespace.staticProperty; 39 | 40 | /** 41 | * @interface 42 | */ 43 | namespace.NamespacedInterface = function() {}; 44 | 45 | 46 | /** 47 | * @typedef {{ 48 | * foo: number, 49 | * bar: string 50 | * }} 51 | */ 52 | namespace.NamespacedTypeDefOfRecord; 53 | 54 | /** 55 | * @type {namespace.NamespacedTypeDefOfRecord} 56 | */ 57 | namespace.namespacedTypeDefOfRecordRef; 58 | 59 | /** 60 | * @record 61 | */ 62 | namespace.NamespacedRecord = function() {}; 63 | 64 | /** 65 | * @constructor 66 | */ 67 | namespace.NamespacedClass = function() {}; 68 | 69 | /** 70 | * @typedef {function(string):boolean} 71 | */ 72 | namespace.NamespacedFunctionType; 73 | 74 | /** @type {namespace.NamespacedFunctionType} */ 75 | namespace.namespacedFunctionTypeRef; 76 | 77 | /** 78 | * @typedef {string|number} 79 | */ 80 | namespace.NamespacedUnionType; 81 | 82 | /** @type {namespace.NamespacedUnionType} */ 83 | namespace.namespacedUnionTypeRef; 84 | 85 | /** 86 | * @enum {string} 87 | */ 88 | namespace.NamespacedEnum = {A: 'A', B: 'B'}; 89 | 90 | /** 91 | * @const 92 | */ 93 | var othernamespace = {}; 94 | 95 | /** 96 | * Property referring the type of another namespace. 97 | * 98 | * @type {namespace.nestednamespace.InterfaceFromNestedNamespace} 99 | */ 100 | othernamespace.property; 101 | 102 | /** 103 | * Property referring an namespaced typedef of record defined in another 104 | * namespace. 105 | * 106 | * @type {namespace.NamespacedTypeDefOfRecord} 107 | */ 108 | othernamespace.namespacedTypeDefOfRecordRef; 109 | 110 | /** 111 | * Property referring an namespaced union type defined in another namespace. 112 | * 113 | * @type {namespace.NamespacedUnionType} 114 | */ 115 | othernamespace.namespacedUnionTypeRef; 116 | 117 | /** 118 | * Property referring namespaced function type defined in another namespace. 119 | * 120 | * @type {namespace.NamespacedFunctionType} 121 | */ 122 | othernamespace.namespacedFunctionTypeRef; 123 | 124 | /** 125 | * This namespace defines types only. No Java class will be generated. 126 | * 127 | * @const 128 | */ 129 | var namespacewithtypeonly = {}; 130 | 131 | /** 132 | * @interface 133 | */ 134 | namespacewithtypeonly.Interface = function() {}; 135 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/natives/BUILD: -------------------------------------------------------------------------------- 1 | # Description: 2 | # Contains helper lib used by tests 3 | # 4 | 5 | load( 6 | "//:jsinterop_generator_import.bzl", 7 | "jsinterop_generator_import", 8 | ) 9 | 10 | package( 11 | default_applicable_licenses = ["//:license"], 12 | default_visibility = [ 13 | "//javatests/jsinterop/generator/externs:__subpackages__", 14 | ], 15 | licenses = ["notice"], 16 | ) 17 | 18 | # In our test, we use a reference to native js types like Object or Array. 19 | # In order to avoid generating java types for those types, we put its definitions in a separate lib 20 | # and use it as a dependency or our test. 21 | # We import handwritten java types so we don;t use the jsinterop generator for building dependencies 22 | # of our tests. 23 | jsinterop_generator_import( 24 | name = "natives", 25 | srcs = [ 26 | "JsArray.java", 27 | "JsIterable.java", 28 | "JsObject.java", 29 | ], 30 | externs_srcs = ["native.js"], 31 | types_mapping_files = ["natives.types"], 32 | ) 33 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/natives/JsArray.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package jsinterop.generator.externs.natives; 17 | 18 | import jsinterop.annotations.JsPackage; 19 | import jsinterop.annotations.JsType; 20 | 21 | @JsType(isNative = true, namespace = JsPackage.GLOBAL, name = "Array") 22 | public class JsArray implements JsIterable {} 23 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/natives/JsIterable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package jsinterop.generator.externs.natives; 17 | 18 | import jsinterop.annotations.JsPackage; 19 | import jsinterop.annotations.JsType; 20 | 21 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 22 | public interface JsIterable {} 23 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/natives/JsObject.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package jsinterop.generator.externs.natives; 17 | 18 | import jsinterop.annotations.JsPackage; 19 | import jsinterop.annotations.JsType; 20 | 21 | @JsType(isNative = true, namespace = JsPackage.GLOBAL, name = "Object") 22 | public class JsObject implements JsIterable {} 23 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/natives/native.js: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | /** 16 | * @fileoverview Extern files used as dependency of our test in order to provide 17 | * java class for native types without polluting our test. 18 | * @externs 19 | */ 20 | 21 | /** 22 | * @constructor 23 | * @param {*=} args 24 | * @suppress {duplicate} 25 | */ 26 | function Object(args) {} 27 | 28 | /** 29 | * @interface 30 | * @template T 31 | * 32 | * @suppress {duplicate} 33 | */ 34 | function Iterable(args) {} 35 | 36 | /** 37 | * @constructor 38 | * @template T 39 | * @implements {Iterable} 40 | * @param {...*} args 41 | * @suppress {duplicate} 42 | */ 43 | function Array(args) {} 44 | 45 | /** 46 | * @interface 47 | * @template KEY1, VALUE1 48 | * @suppress {duplicate} 49 | */ 50 | function IObject() {} 51 | 52 | /** 53 | * @record 54 | * @extends {IObject} 55 | * @template VALUE2 56 | * @suppress {duplicate} 57 | */ 58 | function IArrayLike() {} -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/natives/natives.types: -------------------------------------------------------------------------------- 1 | Object=jsinterop.generator.externs.natives.JsObject 2 | Array=jsinterop.generator.externs.natives.JsArray 3 | Iterable=jsinterop.generator.externs.natives.JsIterable -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/nestedclasses/BUILD: -------------------------------------------------------------------------------- 1 | # Description: 2 | # Tests conversion of nested class 3 | # 4 | 5 | load( 6 | "//javatests/jsinterop/generator:jsinterop_generator_test.bzl", 7 | "jsinterop_generator_test", 8 | ) 9 | 10 | package( 11 | default_applicable_licenses = ["//:license"], 12 | licenses = ["notice"], 13 | ) 14 | 15 | jsinterop_generator_test( 16 | name = "nestedclasses", 17 | srcs = ["nestedclasses.js"], 18 | expected_output = glob(["*.java.txt"]), 19 | ) 20 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/nestedclasses/Bar.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.nestedclasses.foo; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsType; 5 | 6 | @JsType(isNative = true, name = "Foo.Bar", namespace = JsPackage.GLOBAL) 7 | public class Bar { 8 | public static String staticStringField; 9 | 10 | public static native void staticMethod(String param); 11 | 12 | public String stringField; 13 | 14 | public native void method(String param); 15 | } 16 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/nestedclasses/Foo.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.nestedclasses; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsType; 5 | 6 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 7 | public class Foo { 8 | public static native String staticMethod(); 9 | } 10 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/nestedclasses/nestedclasses.js: -------------------------------------------------------------------------------- 1 | // Copyright 2019 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | /** 16 | * @fileoverview Test nested classes conversion. 17 | * @externs 18 | */ 19 | 20 | 21 | /** @constructor */ 22 | function Foo() {} 23 | 24 | /** @return {string} */ 25 | Foo.staticMethod = function() {}; 26 | 27 | /** @constructor */ 28 | Foo.Bar = function() {} 29 | 30 | /** @type {string} */ 31 | Foo.Bar.staticStringField; 32 | 33 | /** 34 | * @param {string} param 35 | * @return {undefined} 36 | */ 37 | Foo.Bar.staticMethod = function(param) {} 38 | 39 | /** @type {string} */ 40 | Foo.Bar.prototype.stringField; 41 | 42 | /** 43 | * @param {string} param 44 | * @return {undefined} 45 | */ 46 | Foo.Bar.prototype.method = function(param) {} 47 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/nullabletypes/BUILD: -------------------------------------------------------------------------------- 1 | load( 2 | "//javatests/jsinterop/generator:jsinterop_generator_test.bzl", 3 | "jsinterop_generator_test", 4 | ) 5 | 6 | package( 7 | default_applicable_licenses = ["//:license"], 8 | licenses = ["notice"], 9 | ) 10 | 11 | jsinterop_generator_test( 12 | name = "nullabletypes", 13 | srcs = ["nullabletypes.js"], 14 | expected_output = glob([ 15 | "*.java.txt", 16 | ]), 17 | deps = ["//javatests/jsinterop/generator/externs/natives"], 18 | ) 19 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/nullabletypes/NullableFunctionType.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.nullabletypes; 2 | 3 | import jsinterop.annotations.JsFunction; 4 | 5 | @JsFunction 6 | public interface NullableFunctionType { 7 | boolean onInvoke(String p0); 8 | } 9 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/nullabletypes/NullableTypeDefOfRecord.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.nullabletypes; 2 | 3 | import jsinterop.annotations.JsOverlay; 4 | import jsinterop.annotations.JsPackage; 5 | import jsinterop.annotations.JsProperty; 6 | import jsinterop.annotations.JsType; 7 | import jsinterop.base.Js; 8 | import jsinterop.base.JsPropertyMap; 9 | 10 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 11 | public interface NullableTypeDefOfRecord { 12 | @JsOverlay 13 | static NullableTypeDefOfRecord create() { 14 | return Js.uncheckedCast(JsPropertyMap.of()); 15 | } 16 | 17 | @JsProperty 18 | String getBar(); 19 | 20 | @JsProperty 21 | double getFoo(); 22 | 23 | @JsProperty 24 | void setBar(String bar); 25 | 26 | @JsProperty 27 | void setFoo(double foo); 28 | } 29 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/nullabletypes/SimpleClass.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.nullabletypes; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsType; 5 | 6 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 7 | public class SimpleClass {} 8 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/nullabletypes/SimpleEnum.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.nullabletypes; 2 | 3 | import jsinterop.annotations.JsEnum; 4 | import jsinterop.annotations.JsPackage; 5 | 6 | @JsEnum(isNative = true, namespace = JsPackage.GLOBAL) 7 | public enum SimpleEnum { 8 | A, 9 | B; 10 | } 11 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/nullabletypes/SimpleInterface.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.nullabletypes; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsType; 5 | 6 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 7 | public interface SimpleInterface {} 8 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/nullabletypes/SimpleRecord.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.nullabletypes; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsType; 5 | 6 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 7 | public interface SimpleRecord {} 8 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/optionalparameters/BUILD: -------------------------------------------------------------------------------- 1 | # Description: 2 | # Tests conversion of optional parameters 3 | # 4 | 5 | load( 6 | "//javatests/jsinterop/generator:jsinterop_generator_test.bzl", 7 | "jsinterop_generator_test", 8 | ) 9 | 10 | package( 11 | default_applicable_licenses = ["//:license"], 12 | licenses = ["notice"], 13 | ) 14 | 15 | jsinterop_generator_test( 16 | name = "optionalparameters", 17 | srcs = ["optionalparameters.js"], 18 | expected_output = glob(["*.java.txt"]), 19 | ) 20 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/optionalparameters/SimpleClass.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.optionalparameters; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsType; 5 | import org.jspecify.annotations.Nullable; 6 | 7 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 8 | public class SimpleClass { 9 | public SimpleClass() {} 10 | 11 | public SimpleClass(String foo) {} 12 | 13 | public native void foo(String foo, String bar, String baz); 14 | 15 | public native void foo(String foo, String bar); 16 | 17 | public native void foo(String foo); 18 | 19 | public native void optionalParameterWithVarArgs(); 20 | 21 | public native void optionalParameterWithVarArgs(String foo, @Nullable Object... bar); 22 | } 23 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/optionalparameters/SimpleInterface.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.optionalparameters; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsType; 5 | 6 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 7 | public interface SimpleInterface { 8 | void foo(); 9 | 10 | void foo(String foo, String bar, String baz); 11 | 12 | void foo(String foo, String bar); 13 | 14 | void foo(String foo); 15 | } 16 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/optionalparameters/optionalparameters.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @fileoverview Test conversion of methods and constructors with optional 3 | * parameters. 4 | * @externs 5 | */ 6 | 7 | /** 8 | * @param {string=} opt_foo 9 | * @constructor 10 | */ 11 | function SimpleClass(opt_foo) {} 12 | 13 | /** 14 | * @param {string} foo 15 | * @param {string=} opt_bar 16 | * @param {string=} opt_baz 17 | * @return {undefined} 18 | */ 19 | SimpleClass.prototype.foo = function(foo, opt_bar, opt_baz) {}; 20 | 21 | /** 22 | * @param {string=} opt_foo 23 | * @param {...*} bar 24 | * @return {undefined} 25 | */ 26 | SimpleClass.prototype.optionalParameterWithVarArgs = function(opt_foo, bar) {}; 27 | 28 | /** 29 | * @interface 30 | */ 31 | function SimpleInterface() {} 32 | 33 | /** 34 | * @param {string=} opt_foo 35 | * @param {string=} opt_bar 36 | * @param {string=} opt_baz 37 | * @return {undefined} 38 | */ 39 | SimpleInterface.prototype.foo = function(opt_foo, opt_bar, opt_baz) {}; 40 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/simpleclass/BUILD: -------------------------------------------------------------------------------- 1 | # Description: 2 | # Tests conversion of a simple type 3 | # 4 | 5 | load( 6 | "//javatests/jsinterop/generator:jsinterop_generator_test.bzl", 7 | "jsinterop_generator_test", 8 | ) 9 | 10 | package( 11 | default_applicable_licenses = ["//:license"], 12 | licenses = ["notice"], 13 | ) 14 | 15 | jsinterop_generator_test( 16 | name = "simpleclass", 17 | srcs = ["simpleclass.js"], 18 | expected_output = glob(["*.java.txt"]), 19 | deps = ["//javatests/jsinterop/generator/externs/natives"], 20 | ) 21 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/simpleclass/DeprecatedInterface.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.simpleclass; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsType; 5 | 6 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 7 | @Deprecated 8 | public interface DeprecatedInterface { 9 | boolean deprecatedMethod(String bar, String foo, boolean baz); 10 | 11 | boolean deprecatedMethod(String bar, String foo); 12 | } 13 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/simpleclass/PrivateClass.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.simpleclass; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsType; 5 | 6 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 7 | public class PrivateClass { 8 | private PrivateClass() {} 9 | } 10 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/simpleclass/SimpleClass__Constants.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.simpleclass; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsType; 5 | 6 | @JsType(isNative = true, name = "SimpleClass", namespace = JsPackage.GLOBAL) 7 | class SimpleClass__Constants { 8 | @Deprecated static String deprecatedConstant; 9 | static String staticReadonlyProperty; 10 | } 11 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/simpleclass/SimpleInterface.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.simpleclass; 2 | 3 | import jsinterop.annotations.JsOverlay; 4 | import jsinterop.annotations.JsPackage; 5 | import jsinterop.annotations.JsProperty; 6 | import jsinterop.annotations.JsType; 7 | 8 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 9 | public interface SimpleInterface { 10 | @Deprecated @JsOverlay 11 | boolean deprecatedStaticProperty = SimpleInterface__Constants.deprecatedStaticProperty; 12 | 13 | @JsOverlay String staticProperty = SimpleInterface__Constants.staticProperty; 14 | 15 | @Deprecated 16 | boolean deprecatedMethod(String bar, String foo, boolean baz); 17 | 18 | @Deprecated 19 | boolean deprecatedMethod(String bar, String foo); 20 | 21 | boolean fooMethod(String foo, String bar, boolean baz); 22 | 23 | boolean fooMethod(String foo, String bar); 24 | 25 | @Deprecated 26 | @JsProperty 27 | String getDeprecatedProperty(); 28 | 29 | @JsProperty 30 | String getFooProperty(); 31 | 32 | @JsProperty 33 | boolean isReadonlyProperty(); 34 | 35 | void methodWithNonAmbiguousVarargs(String... var_args); 36 | 37 | @Deprecated 38 | @JsProperty 39 | void setDeprecatedProperty(String deprecatedProperty); 40 | 41 | @JsProperty 42 | void setFooProperty(String fooProperty); 43 | } 44 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/simpleclass/SimpleInterface__Constants.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.simpleclass; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsType; 5 | 6 | @JsType(isNative = true, name = "SimpleInterface", namespace = JsPackage.GLOBAL) 7 | class SimpleInterface__Constants { 8 | @Deprecated static boolean deprecatedStaticProperty; 9 | static String staticProperty; 10 | } 11 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/simpleclass/SimpleStructuralInterface.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.simpleclass; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsType; 5 | 6 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 7 | public interface SimpleStructuralInterface { 8 | boolean fooMethod(String foo, String bar, boolean baz); 9 | 10 | boolean fooMethod(String foo, String bar); 11 | } 12 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/structuraltypes/BUILD: -------------------------------------------------------------------------------- 1 | # Description: 2 | # Tests conversion of type literal. 3 | # 4 | 5 | load( 6 | "//javatests/jsinterop/generator:jsinterop_generator_test.bzl", 7 | "jsinterop_generator_test", 8 | ) 9 | 10 | package( 11 | default_applicable_licenses = ["//:license"], 12 | licenses = ["notice"], 13 | ) 14 | 15 | jsinterop_generator_test( 16 | name = "structuraltypes", 17 | srcs = ["structuraltypes.js"], 18 | expected_output = glob(["*.java.txt"]), 19 | global_scope_class_name = "Global", 20 | deps = ["//javatests/jsinterop/generator/externs/natives"], 21 | ) 22 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/structuraltypes/ClassInModule.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.structuraltypes.simplemodule; 2 | 3 | import jsinterop.annotations.JsOverlay; 4 | import jsinterop.annotations.JsPackage; 5 | import jsinterop.annotations.JsProperty; 6 | import jsinterop.annotations.JsType; 7 | import jsinterop.base.Js; 8 | import jsinterop.base.JsPropertyMap; 9 | 10 | @JsType(isNative = true, name = "SimpleModule.ClassInModule", namespace = JsPackage.GLOBAL) 11 | public class ClassInModule { 12 | @JsType(isNative = true, name = "?", namespace = JsPackage.GLOBAL) 13 | public interface FooBazType { 14 | @JsOverlay 15 | static ClassInModule.FooBazType create() { 16 | return Js.uncheckedCast(JsPropertyMap.of()); 17 | } 18 | 19 | @JsProperty 20 | double getBaz(); 21 | 22 | @JsProperty 23 | void setBaz(double baz); 24 | } 25 | 26 | public native void foo(ClassInModule.FooBazType baz); 27 | } 28 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/structuraltypes/FooBar.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.structuraltypes; 2 | 3 | import jsinterop.annotations.JsOverlay; 4 | import jsinterop.annotations.JsPackage; 5 | import jsinterop.annotations.JsProperty; 6 | import jsinterop.annotations.JsType; 7 | import jsinterop.base.Js; 8 | import jsinterop.base.JsPropertyMap; 9 | 10 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 11 | public interface FooBar { 12 | @JsOverlay 13 | static FooBar create() { 14 | return Js.uncheckedCast(JsPropertyMap.of()); 15 | } 16 | 17 | @JsProperty 18 | String getBar(); 19 | 20 | @JsProperty 21 | double getFoo(); 22 | 23 | @JsProperty 24 | void setBar(String bar); 25 | 26 | @JsProperty 27 | void setFoo(double foo); 28 | } 29 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/structuraltypes/FooBar2.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.structuraltypes; 2 | 3 | import jsinterop.annotations.JsOverlay; 4 | import jsinterop.annotations.JsPackage; 5 | import jsinterop.annotations.JsProperty; 6 | import jsinterop.annotations.JsType; 7 | import jsinterop.base.Js; 8 | import jsinterop.base.JsPropertyMap; 9 | 10 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 11 | public interface FooBar2 { 12 | @JsOverlay 13 | static FooBar2 create() { 14 | return Js.uncheckedCast(JsPropertyMap.of()); 15 | } 16 | 17 | @JsProperty 18 | String getBar(); 19 | 20 | @JsProperty 21 | double getFoo(); 22 | 23 | @JsProperty 24 | void setBar(String bar); 25 | 26 | @JsProperty 27 | void setFoo(double foo); 28 | } 29 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/structuraltypes/InnerStructuralType.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.structuraltypes; 2 | 3 | import jsinterop.annotations.JsOverlay; 4 | import jsinterop.annotations.JsPackage; 5 | import jsinterop.annotations.JsProperty; 6 | import jsinterop.annotations.JsType; 7 | import jsinterop.base.Js; 8 | import jsinterop.base.JsPropertyMap; 9 | 10 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 11 | public interface InnerStructuralType { 12 | @JsType(isNative = true, name = "?", namespace = JsPackage.GLOBAL) 13 | public interface BarFieldType { 14 | @JsOverlay 15 | static InnerStructuralType.BarFieldType create() { 16 | return Js.uncheckedCast(JsPropertyMap.of()); 17 | } 18 | 19 | @JsProperty 20 | String getBaz(); 21 | 22 | @JsProperty 23 | void setBaz(String baz); 24 | } 25 | 26 | @JsOverlay 27 | static InnerStructuralType create() { 28 | return Js.uncheckedCast(JsPropertyMap.of()); 29 | } 30 | 31 | @JsProperty 32 | InnerStructuralType.BarFieldType getBar(); 33 | 34 | @JsProperty 35 | double getFoo(); 36 | 37 | @JsProperty 38 | void setBar(InnerStructuralType.BarFieldType bar); 39 | 40 | @JsProperty 41 | void setFoo(double foo); 42 | } 43 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/structuraltypes/SimpleModule.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.structuraltypes; 2 | 3 | import jsinterop.annotations.JsOverlay; 4 | import jsinterop.annotations.JsPackage; 5 | import jsinterop.annotations.JsProperty; 6 | import jsinterop.annotations.JsType; 7 | import jsinterop.base.Js; 8 | import jsinterop.base.JsPropertyMap; 9 | 10 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 11 | public class SimpleModule { 12 | @JsType(isNative = true, name = "?", namespace = JsPackage.GLOBAL) 13 | public interface FooBarType { 14 | @JsOverlay 15 | static SimpleModule.FooBarType create() { 16 | return Js.uncheckedCast(JsPropertyMap.of()); 17 | } 18 | 19 | @JsProperty 20 | String getBar(); 21 | 22 | @JsProperty 23 | void setBar(String bar); 24 | } 25 | 26 | public static native void foo(SimpleModule.FooBarType bar); 27 | } 28 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/uniontypes/BUILD: -------------------------------------------------------------------------------- 1 | # Description: 2 | # Tests conversion of union types 3 | # 4 | 5 | load( 6 | "//javatests/jsinterop/generator:jsinterop_generator_test.bzl", 7 | "jsinterop_generator_test", 8 | ) 9 | 10 | package( 11 | default_applicable_licenses = ["//:license"], 12 | licenses = ["notice"], 13 | ) 14 | 15 | jsinterop_generator_test( 16 | name = "uniontypes", 17 | srcs = ["uniontypes.js"], 18 | expected_output = glob(["*.java.txt"]), 19 | deps = ["//javatests/jsinterop/generator/externs/natives"], 20 | ) 21 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/uniontypes/Foo.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.uniontypes; 2 | 3 | import jsinterop.annotations.JsOverlay; 4 | import jsinterop.annotations.JsPackage; 5 | import jsinterop.annotations.JsProperty; 6 | import jsinterop.annotations.JsType; 7 | import jsinterop.base.Js; 8 | import org.jspecify.annotations.Nullable; 9 | 10 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 11 | public interface Foo { 12 | @JsType(isNative = true, name = "?", namespace = JsPackage.GLOBAL) 13 | public interface GetFooParentInterfaceTypeParameterUnionType< 14 | T extends @Nullable Object, V extends @Nullable Object> { 15 | @JsOverlay 16 | static 17 | Foo.GetFooParentInterfaceTypeParameterUnionType of(Object o) { 18 | return Js.cast(o); 19 | } 20 | 21 | @JsOverlay 22 | default T asT() { 23 | return Js.cast(this); 24 | } 25 | 26 | @JsOverlay 27 | default V asV() { 28 | return Js.cast(this); 29 | } 30 | } 31 | 32 | @JsProperty 33 | @Nullable ParentInterface> getFoo(); 34 | 35 | @JsProperty 36 | void setFoo(@Nullable ParentInterface> foo); 37 | } 38 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/uniontypes/ParentInterface.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.uniontypes; 2 | 3 | import jsinterop.annotations.JsOverlay; 4 | import jsinterop.annotations.JsPackage; 5 | import jsinterop.annotations.JsType; 6 | import jsinterop.base.Js; 7 | import org.jspecify.annotations.Nullable; 8 | 9 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 10 | public interface ParentInterface { 11 | @JsType(isNative = true, name = "?", namespace = JsPackage.GLOBAL) 12 | public interface ParentMethod2FooUnionType { 13 | @JsOverlay 14 | static ParentInterface.ParentMethod2FooUnionType of(Object o) { 15 | return Js.cast(o); 16 | } 17 | 18 | @JsOverlay 19 | default double asDouble() { 20 | return Js.asDouble(this); 21 | } 22 | 23 | @JsOverlay 24 | default String asString() { 25 | return Js.asString(this); 26 | } 27 | 28 | @JsOverlay 29 | default boolean isDouble() { 30 | return (Object) this instanceof Double; 31 | } 32 | 33 | @JsOverlay 34 | default boolean isString() { 35 | return (Object) this instanceof String; 36 | } 37 | } 38 | 39 | @JsType(isNative = true, name = "?", namespace = JsPackage.GLOBAL) 40 | public interface ParentMethodFooUnionType { 41 | @JsOverlay 42 | static ParentInterface.ParentMethodFooUnionType of(Object o) { 43 | return Js.cast(o); 44 | } 45 | 46 | @JsOverlay 47 | default double asDouble() { 48 | return Js.asDouble(this); 49 | } 50 | 51 | @JsOverlay 52 | default String asString() { 53 | return Js.asString(this); 54 | } 55 | 56 | @JsOverlay 57 | default boolean isDouble() { 58 | return (Object) this instanceof Double; 59 | } 60 | 61 | @JsOverlay 62 | default boolean isString() { 63 | return (Object) this instanceof String; 64 | } 65 | } 66 | 67 | @Nullable Object parentMethod(ParentInterface.ParentMethodFooUnionType foo); 68 | 69 | @JsOverlay 70 | default @Nullable Object parentMethod(String foo) { 71 | return parentMethod(Js.uncheckedCast(foo)); 72 | } 73 | 74 | @JsOverlay 75 | default @Nullable Object parentMethod(double foo) { 76 | return parentMethod(Js.uncheckedCast(foo)); 77 | } 78 | 79 | @Nullable Object parentMethod2(ParentInterface.ParentMethod2FooUnionType foo); 80 | 81 | @JsOverlay 82 | default @Nullable Object parentMethod2(String foo) { 83 | return parentMethod2(Js.uncheckedCast(foo)); 84 | } 85 | 86 | @JsOverlay 87 | default @Nullable Object parentMethod2(double foo) { 88 | return parentMethod2(Js.uncheckedCast(foo)); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/uniontypes/uniontypes.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @fileoverview Test conversion of union type 3 | * @externs 4 | */ 5 | 6 | /** 7 | * @interface 8 | * @template T 9 | */ 10 | function ParentInterface() {} 11 | 12 | /** 13 | * @param {(string| number)} foo 14 | */ 15 | ParentInterface.prototype.parentMethod = function(foo) {}; 16 | 17 | /** 18 | * @param {(string| number)} foo 19 | */ 20 | ParentInterface.prototype.parentMethod2 = function(foo) {}; 21 | 22 | /** 23 | * @constructor 24 | * @param {(string|number)} foo 25 | * @implements {ParentInterface<(string|number)>} 26 | */ 27 | function Child(foo) {} 28 | 29 | // Test that overridden methods with UnionTypes use helper types from the parent 30 | 31 | /** 32 | * @param {(string| number)} foo 33 | * @override 34 | */ 35 | Child.prototype.parentMethod = function(foo) {}; 36 | 37 | // Augment parent method with optional parameter. 38 | /** 39 | * @param {(string| number)} foo 40 | * @param {(string|boolean)=} bar 41 | */ 42 | Child.prototype.parentMethod2 = function(foo, bar) {}; 43 | 44 | /** 45 | * @return {(string|number|Child)} 46 | */ 47 | Child.prototype.method = function() {}; 48 | 49 | /** 50 | * @param {(string|number|Child)} foo 51 | * @param {(string|number|boolean)} bar 52 | * @param {boolean} baz 53 | */ 54 | Child.prototype.method1 = function(foo, bar, baz) {}; 55 | 56 | /** 57 | * @param {(string|number|Child)} foo 58 | * @return {(string|number|boolean)} 59 | */ 60 | Child.prototype.method2 = function(foo) {}; 61 | 62 | /** 63 | * @param {(string|Array | Foo)} foo 64 | * @return {undefined} 65 | */ 66 | Child.prototype.method3 = function(foo) {}; 67 | 68 | 69 | // Test that we don't create conflicting methods overloads for union type where 70 | // raw generics are involved. 71 | /** 72 | * @param {(T|V)} foo 73 | * @param {function((T|V)):boolean} barCallback 74 | * @return {V} 75 | * @template T,V 76 | */ 77 | Child.prototype.method4 = function(foo, barCallback) {}; 78 | 79 | /** 80 | * @param {string|number} numberOrString 81 | * @param {...(string|number|Child)} varargs 82 | * @return {undefined} 83 | */ 84 | Child.prototype.methodWithVarargsOfUnionType = function(numberOrString, varargs) {}; 85 | 86 | /** 87 | * @interface 88 | * @template T,V 89 | */ 90 | function Foo() {} 91 | 92 | /** 93 | * @type {ParentInterface<(T|V)>} 94 | */ 95 | Foo.prototype.foo; 96 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/wildcardtypes/BUILD: -------------------------------------------------------------------------------- 1 | # Description: 2 | # Tests conversion with forced wildcard types. 3 | # 4 | 5 | load( 6 | "//javatests/jsinterop/generator:jsinterop_generator_test.bzl", 7 | "jsinterop_generator_test", 8 | ) 9 | 10 | package( 11 | default_applicable_licenses = ["//:license"], 12 | licenses = ["notice"], 13 | ) 14 | 15 | jsinterop_generator_test( 16 | name = "wildcardtypes", 17 | srcs = ["wildcardtypes.js"], 18 | expected_output = glob(["*.java.txt"]), 19 | wildcard_types_files = ["wildcardtypes.txt"], 20 | ) 21 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/wildcardtypes/Bar.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.wildcardtypes; 2 | 3 | import jsinterop.annotations.JsFunction; 4 | import jsinterop.annotations.JsPackage; 5 | import jsinterop.annotations.JsType; 6 | import org.jspecify.annotations.Nullable; 7 | 8 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 9 | @FunctionalInterface 10 | public interface Bar< 11 | U extends @Nullable Object, T extends @Nullable Object, V extends @Nullable Object> { 12 | @JsFunction 13 | public interface BarCallbackFn< 14 | U extends @Nullable Object, T extends @Nullable Object, V extends @Nullable Object> { 15 | void onInvoke(U p0, T p1, V p2); 16 | } 17 | 18 | void bar(Bar.BarCallbackFn callback); 19 | } 20 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/wildcardtypes/Foo.java.txt: -------------------------------------------------------------------------------- 1 | package jsinterop.generator.externs.wildcardtypes; 2 | 3 | import jsinterop.annotations.JsPackage; 4 | import jsinterop.annotations.JsType; 5 | import org.jspecify.annotations.Nullable; 6 | 7 | @JsType(isNative = true, namespace = JsPackage.GLOBAL) 8 | public class Foo { 9 | public native void bar(@Nullable Bar bar); 10 | 11 | public native void foo(@Nullable Bar foo); 12 | } 13 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/wildcardtypes/wildcardtypes.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | * 16 | */ 17 | 18 | /** 19 | * @fileoverview Test creation of wildcard types. 20 | * @externs 21 | */ 22 | 23 | /** 24 | * @interface 25 | * @template U,T,V 26 | */ 27 | function Bar() {} 28 | 29 | // Override automatic wildcard type creation. 30 | /** 31 | * @param {function(U,T,V):undefined} callback 32 | * @return {undefined} 33 | */ 34 | Bar.prototype.bar = function(callback) {}; 35 | 36 | 37 | /** 38 | * @constructor 39 | * @template T 40 | */ 41 | function Foo() {} 42 | 43 | /** 44 | * @param {Bar} foo 45 | * @return {undefined} 46 | */ 47 | Foo.prototype.foo = function(foo) {}; 48 | 49 | /** 50 | * @param {Bar} bar 51 | * @return {undefined} 52 | */ 53 | Foo.prototype.bar = function(bar) {}; 54 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/externs/wildcardtypes/wildcardtypes.txt: -------------------------------------------------------------------------------- 1 | jsinterop.generator.externs.wildcardtypes.Foo.foo.foo#0=SUPER 2 | jsinterop.generator.externs.wildcardtypes.Foo.foo.foo#2=EXTENDS 3 | jsinterop.generator.externs.wildcardtypes.Foo.bar.bar#0=SUPER 4 | jsinterop.generator.externs.wildcardtypes.Bar.bar.callback#1=NONE 5 | jsinterop.generator.externs.wildcardtypes.Bar.bar.callback#2=EXTENDS 6 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/jsinterop_generator_test.bzl: -------------------------------------------------------------------------------- 1 | """jsinterop_generator_test build macro. 2 | 3 | Defines an integration test for the JsInteropGenerator 4 | 5 | 6 | Example usage: 7 | 8 | jsinterop_generator_test( 9 | name = "SimpleTest", 10 | srcs = ["test.d.ts"], 11 | expected_output = "test_output.txt", 12 | ) 13 | 14 | """ 15 | 16 | load("//:jsinterop_generator.bzl", "jsinterop_generator") 17 | 18 | def jsinterop_generator_test( 19 | name, 20 | srcs, 21 | expected_output, 22 | extension_type_prefix = None, 23 | global_scope_class_name = None, 24 | name_mapping_files = [], 25 | deps = [], 26 | conversion_mode = "closure", 27 | j2cl_test_externs_list = None, 28 | integer_entities_files = [], 29 | wildcard_types_files = [], 30 | generate_j2cl_build_test = None): 31 | if conversion_mode != "closure": 32 | # TODO(b/121201145): Migrate the typescript generator app to J2CL 33 | return 34 | 35 | jsinterop_generator_name = "%s__jsinterop_generator" % name 36 | generator_output = ":%s__internal_src_generated.srcjar" % jsinterop_generator_name 37 | jsinterop_generator( 38 | name = jsinterop_generator_name, 39 | srcs = srcs, 40 | extension_type_prefix = extension_type_prefix, 41 | global_scope_class_name = global_scope_class_name, 42 | name_mapping_files = name_mapping_files, 43 | deps = deps, 44 | conversion_mode = conversion_mode, 45 | externs_deps = j2cl_test_externs_list, 46 | integer_entities_files = integer_entities_files, 47 | wildcard_types_files = wildcard_types_files, 48 | generate_j2cl_build_test = generate_j2cl_build_test, 49 | ) 50 | 51 | zip_tool = "@bazel_tools//tools/zip:zipper" 52 | java_format_tool = "//third_party:google_java_format" 53 | 54 | native.sh_test( 55 | name = name, 56 | size = "small", 57 | srcs = [ 58 | "//javatests/jsinterop/generator:jsinterop_generator_test.sh", 59 | ], 60 | data = [ 61 | zip_tool, 62 | java_format_tool, 63 | generator_output, 64 | ] + expected_output, 65 | args = [ 66 | "$(location %s)" % generator_output, 67 | native.package_name(), 68 | "$(location %s)" % zip_tool, 69 | "$(location %s)" % java_format_tool, 70 | ], 71 | ) 72 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/jsinterop_generator_test.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # Script taking as input the jar file containing the generated java classes and the directory 3 | # containing the golden files and tests that for each generated java file, it exists one golden file 4 | # with the same name and the contents are the same. 5 | set -e 6 | 7 | readonly WORKSPACE=$(pwd) 8 | 9 | readonly GENERATED_JAR=${WORKSPACE}/$1 10 | readonly ORIGINAL_GOLDEN_FILES_DIR=$2 11 | readonly ZIP_TOOL=${WORKSPACE}/$3 12 | readonly GOOGLE_JAVA_FORMAT=$4 13 | 14 | readonly GENERATED_FILES_DIR=$(mktemp -d) 15 | readonly GOLDEN_FILES_DIR=$(mktemp -d) 16 | 17 | cleanup() { 18 | rm -rf ${GENERATED_FILES_DIR} 19 | rm -rf ${GOLDEN_FILES_DIR} 20 | } 21 | 22 | trap cleanup EXIT 23 | 24 | format_java_code() { 25 | for f in $(list_java_files ${1}); do 26 | # ensure we can write the file 27 | chmod 664 $f 28 | ${GOOGLE_JAVA_FORMAT} -i $f 29 | done 30 | } 31 | 32 | # allows us to comment some part of java code when a feature is not implemented. 33 | strip_java_comments() { 34 | # BSD version of sed requires a backup suffix when -i option is used. 35 | find "$1" -type f -name '*.java' -print -exec sed -i.bak '/\/\/.*$/d' '{}' \; 36 | } 37 | 38 | list_java_files() { 39 | echo -e "$(find "${1}" -name '*.java')" 40 | } 41 | 42 | setup_test() { 43 | # Extract java files from the jar 44 | cd $GENERATED_FILES_DIR 45 | "${ZIP_TOOL}" x "${GENERATED_JAR}" 46 | cd - 47 | strip_java_comments ${GENERATED_FILES_DIR} 48 | 49 | # copy golden files, remove .txt suffix and format them 50 | for f in $(find "${ORIGINAL_GOLDEN_FILES_DIR}" -name '*.java.txt'); do 51 | cp $f "${GOLDEN_FILES_DIR}"/$(basename ${f%.txt}) 52 | done 53 | # TODO(dramaix): remove this when all feature are implemented. 54 | strip_java_comments ${GOLDEN_FILES_DIR} 55 | format_java_code ${GOLDEN_FILES_DIR} 56 | } 57 | 58 | log() { 59 | echo -e >&2 $1 60 | } 61 | 62 | expect_file_content_eq() { 63 | local expected="$1" 64 | local actual="$2" 65 | 66 | if ! diff -u "${expected}" "${actual}"; then 67 | log "content of expected file \"${expected}\" and actual file \"${actual}\" differ" 68 | return 1 69 | fi 70 | return 0 71 | } 72 | 73 | run_test() { 74 | local nbr_generated_files=$(find "${GENERATED_FILES_DIR}" -name '*.java' | wc -l) 75 | local nbr_golden_files=$(find "${GOLDEN_FILES_DIR}" -name '*.java' | wc -l) 76 | 77 | if [ ${nbr_generated_files} -ne ${nbr_golden_files} ]; then 78 | log "The number of generated files [${nbr_generated_files}] doesn\'t match the number of golden files [${nbr_golden_files}]. 79 | >> Golden files: 80 | $(list_java_files ${GOLDEN_FILES_DIR}) 81 | >> Generated files: 82 | $(list_java_files ${GENERATED_FILES_DIR})" 83 | exit 1 84 | fi 85 | 86 | for file in $(list_java_files ${GENERATED_FILES_DIR}); do 87 | local golden_file="${GOLDEN_FILES_DIR}/$(basename ${file})" 88 | 89 | if [ -e ${golden_file} ]; then 90 | expect_file_content_eq ${file} ${golden_file} 91 | else 92 | log "Golden file $(basename ${file}) is missing" 93 | exit 1 94 | fi 95 | done 96 | } 97 | 98 | setup_test 99 | run_test 100 | 101 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/unit/undefinedtypes/BUILD: -------------------------------------------------------------------------------- 1 | # Description: 2 | # Test behavior of the jsinterop generator with type that are not defined 3 | # 4 | 5 | load("@rules_java//java:defs.bzl", "java_test") 6 | 7 | package( 8 | default_applicable_licenses = ["//:license"], 9 | licenses = ["notice"], 10 | ) 11 | 12 | java_test( 13 | name = "UndefinedTypesTest", 14 | srcs = ["UndefinedTypesTest.java"], 15 | deps = [ 16 | "//java/jsinterop/generator/helper", 17 | "//javatests/jsinterop/generator/closure:helpers", 18 | "//third_party:guava", 19 | "//third_party:jscomp", 20 | "//third_party:junit", 21 | ], 22 | ) 23 | -------------------------------------------------------------------------------- /javatests/jsinterop/generator/unit/undefinedtypes/UndefinedTypesTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | package jsinterop.generator.unit.undefinedtypes; 17 | 18 | import static jsinterop.generator.closure.TestUtil.runClosureJsInteropGenerator; 19 | import static org.junit.Assert.fail; 20 | 21 | import com.google.javascript.jscomp.SourceFile; 22 | import jsinterop.generator.helper.AbortError; 23 | import org.junit.Test; 24 | import org.junit.runner.RunWith; 25 | import org.junit.runners.JUnit4; 26 | 27 | @RunWith(JUnit4.class) 28 | public class UndefinedTypesTest { 29 | 30 | @Test 31 | public void undefinedTypes_throwException() { 32 | SourceFile source = 33 | SourceFile.fromCode( 34 | "undefinedtypes.js", 35 | "/** @externs */\n/** @return {UndefinedType} */ foo = function() {};"); 36 | 37 | try { 38 | runClosureJsInteropGenerator(source); 39 | fail("test should have throw an AbortException"); 40 | } catch (AbortError ignore) { 41 | // test succeed 42 | } catch (Throwable throwable) { 43 | throw new AssertionError("Unexpected error.", throwable); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /jsinterop_generator_import.bzl: -------------------------------------------------------------------------------- 1 | """jsinterop_generator_import macro. 2 | 3 | Takes non standard input and repackages it with names that will allow the 4 | jsinterop_generator_import() target to be directly depended upon from jsinterop_generator() 5 | targets. 6 | """ 7 | 8 | load("@rules_java//java:defs.bzl", "java_library") 9 | load("@com_google_j2cl//build_defs:rules.bzl", "j2cl_library") 10 | load(":jsinterop_generator.bzl", "JS_INTEROP_RULE_NAME_PATTERN", "JsInteropGeneratorInfo") 11 | 12 | _is_bazel = not hasattr(native, "genmpm") # this_is_bazel 13 | 14 | def _jsinterop_generator_import_impl(ctx): 15 | # expose files and properties used when the target is used as dependency 16 | return [ 17 | JsInteropGeneratorInfo( 18 | transitive_sources = depset(ctx.files.externs_srcs), 19 | transitive_types_mappings = depset(ctx.files.types_mapping_files), 20 | transitive_names_mappings = depset(), 21 | gwt_module_names = [ctx.attr.gwt_module_name], 22 | ), 23 | ] 24 | 25 | _jsinterop_generator_import = rule( 26 | attrs = { 27 | "externs_srcs": attr.label_list(allow_files = True), 28 | "types_mapping_files": attr.label_list(allow_files = True), 29 | "gwt_module_name": attr.string(), 30 | }, 31 | implementation = _jsinterop_generator_import_impl, 32 | ) 33 | 34 | def jsinterop_generator_import( 35 | name, 36 | srcs, 37 | externs_srcs = [], 38 | types_mapping_files = [], 39 | gwt_module_name = None, 40 | gwt_xml = None, 41 | enable_jspecify_support = False, 42 | visibility = None): 43 | _jsinterop_generator_import( 44 | name = JS_INTEROP_RULE_NAME_PATTERN % name, 45 | externs_srcs = externs_srcs, 46 | types_mapping_files = types_mapping_files, 47 | gwt_module_name = gwt_module_name, 48 | ) 49 | 50 | java_library_args = { 51 | "name": name, 52 | "srcs": srcs, 53 | "deps": [ 54 | Label("@com_google_j2cl//:jsinterop-annotations"), 55 | Label("@com_google_jsinterop_base//:jsinterop-base"), 56 | Label("//third_party:jspecify_annotations"), 57 | ], 58 | "visibility": visibility, 59 | } 60 | 61 | if gwt_xml: 62 | # bazel doesn't support constraint and gwtxml attributes 63 | if _is_bazel: 64 | java_library_args["resources"] = [gwt_xml] 65 | else: 66 | java_library_args["gwtxml"] = gwt_xml 67 | java_library_args["constraints"] = ["gwt", "public"] 68 | java_library(**java_library_args) 69 | 70 | j2cl_library( 71 | name = "%s-j2cl" % name, 72 | srcs = srcs, 73 | visibility = visibility, 74 | deps = [ 75 | Label("@com_google_j2cl//:jsinterop-annotations-j2cl"), 76 | Label("@com_google_jsinterop_base//:jsinterop-base-j2cl"), 77 | Label("//third_party:jspecify_annotations-j2cl"), 78 | ], 79 | experimental_enable_jspecify_support_do_not_enable_without_jspecify_static_checking_or_you_might_cause_an_outage = enable_jspecify_support, 80 | ) 81 | -------------------------------------------------------------------------------- /maven/pom-closure-generator.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 4.0.0 5 | 6 | __GROUP_ID__ 7 | __ARTIFACT_ID__ 8 | __VERSION__ 9 | jar 10 | 11 | JsInterop Closure Generator 12 | JsInterop generator for closure extern files 13 | https://www.gwtproject.org 14 | 15 | 16 | 17 | The Apache Software License, Version 2.0 18 | http://www.apache.org/licenses/LICENSE-2.0.txt 19 | repo 20 | 21 | 22 | 23 | 24 | scm:git:https://github.com/google/jsinterop-generator.git 25 | scm:git:git@github.com:google/jsinterop-generator.git 26 | https://github.com/google/jsinterop-generator 27 | 28 | 29 | 30 | https://github.com/google/jsinterop-generator/issues 31 | GitHub Issues 32 | 33 | 34 | 35 | 36 | J2CL Team 37 | Google 38 | http://www.google.com 39 | 40 | 41 | 42 | 43 | 44 | com.google.guava 45 | guava 46 | 21.0 47 | 48 | 49 | args4j 50 | args4j 51 | 2.33 52 | 53 | 54 | com.google.auto.value 55 | auto-value 56 | 1.4 57 | 58 | 59 | com.google.code.findbugs 60 | jsr305 61 | 3.0.1 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /maven/release_jsinterop_generator.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -i 2 | # Copyright 2019 Google Inc. All Rights Reserved 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS-IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | # The script creates a tag to mark an individual point in the repository history 17 | # of jsinterop generator (closure only) and includes a version number for jsinterop generator release. 18 | set -e 19 | 20 | usage() { 21 | echo "" 22 | echo "$(basename $0): Tag script for JsInterop Generator." 23 | echo "" 24 | echo "$(basename $0) --version " 25 | echo " --help" 26 | echo " Print this help output and exit." 27 | echo " --version " 28 | echo " Maven version of the library to use for deploying to sonatype." 29 | echo "" 30 | } 31 | 32 | lib_version="" 33 | 34 | while [[ "$1" != "" ]]; do 35 | case $1 in 36 | --version ) if [[ -z "$2" ]] || [[ "$2" == "--"* ]]; then 37 | echo "Error: Incorrect version value." 38 | usage 39 | exit 1 40 | fi 41 | shift 42 | lib_version=$1 43 | ;; 44 | --help ) usage 45 | exit 1 46 | ;; 47 | * ) echo "Error: unexpected option $1" 48 | usage 49 | exit 1 50 | ;; 51 | esac 52 | shift 53 | done 54 | 55 | if [[ -z "$lib_version" ]]; then 56 | echo "Error: --version flag is missing" 57 | usage 58 | exit 1 59 | fi 60 | 61 | if [ ! -f "MODULE.bazel" ]; then 62 | echo "Error: should be run from the root of the Bazel repository" 63 | exit 1 64 | fi 65 | 66 | git tag -a ${lib_version} -m "${lib_version} release" 67 | git push origin ${lib_version} 68 | 69 | -------------------------------------------------------------------------------- /third_party/BUILD: -------------------------------------------------------------------------------- 1 | load("@com_google_j2cl//build_defs:rules.bzl", "j2cl_import") 2 | 3 | # Description: 4 | # Thirdparty dependencies indirection for bazel. 5 | package(default_visibility = ["//:__subpackages__"]) 6 | 7 | licenses(["notice"]) 8 | 9 | # Direct dependencies for compiling java src code of the closure jsinterop generator. 10 | # These targets don't need to be public. 11 | 12 | java_library( 13 | name = "auto_value", 14 | exports = ["@google_bazel_common//third_party/java/auto:value"], 15 | ) 16 | 17 | java_library( 18 | name = "jscomp", 19 | exports = ["@maven//:com_google_javascript_closure_compiler"], 20 | ) 21 | 22 | java_library( 23 | name = "args4j", 24 | exports = ["@maven//:args4j_args4j"], 25 | ) 26 | 27 | java_library( 28 | name = "jsr305_annotations", 29 | exports = ["@google_bazel_common//third_party/java/jsr305_annotations"], 30 | ) 31 | 32 | java_library( 33 | name = "guava", 34 | exports = ["@google_bazel_common//third_party/java/guava"], 35 | ) 36 | 37 | # Dependencies used in the jsinterop_generator skylark rules. 38 | # These dependencies need to be publicly visible in order to allow any other repo to use the 39 | # jsinterop generator rule. 40 | 41 | java_binary( 42 | name = "google_java_format", 43 | main_class = "com.google.googlejavaformat.java.Main", 44 | visibility = ["//visibility:public"], 45 | runtime_deps = ["@google_bazel_common//third_party/java/google_java_format"], 46 | ) 47 | 48 | java_library( 49 | name = "junit", 50 | testonly = 1, 51 | exports = ["@google_bazel_common//third_party/java/junit"], 52 | ) 53 | 54 | alias( 55 | name = "error_prone_annotations", 56 | actual = "@google_bazel_common//third_party/java/error_prone:annotations", 57 | ) 58 | 59 | alias( 60 | name = "jspecify_annotations", 61 | actual = "@google_bazel_common//third_party/java/jspecify_annotations", 62 | visibility = [ 63 | # Do not use. Temporary visible to workaround https://github.com/bazelbuild/bazel/issues/25214. 64 | "//visibility:public", 65 | ], 66 | ) 67 | 68 | j2cl_import( 69 | name = "jspecify_annotations-j2cl", 70 | jar = "@google_bazel_common//third_party/java/jspecify_annotations", 71 | visibility = [ 72 | # Do not use. Temporary visible to workaround https://github.com/bazelbuild/bazel/issues/25214. 73 | "//visibility:public", 74 | ], 75 | ) 76 | --------------------------------------------------------------------------------