Skip to content

Commit

Permalink
Add a new command line parameter for setting the forbidden annotations.
Browse files Browse the repository at this point in the history
Forbidden annotations are no longer specific to a backend. Kotlin backend forbids different annotations depending the targeted platform.

PiperOrigin-RevId: 580351180
  • Loading branch information
jDramaix authored and copybara-github committed Nov 8, 2023
1 parent e3cf043 commit 0028c8f
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 4 deletions.
1 change: 1 addition & 0 deletions build_defs/internal_do_not_use/j2cl_common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ def _j2cl_transpile(
args.add("-generatekytheindexingmetadata")
args.add_all(kotlincopts, format_each = "-kotlincOptions=%s")
args.add_joined(kt_friend_jars, format_joined = "-kotlincOptions=-Xfriend-paths=%s", join_with = ",")
args.add("-forbiddenAnnotation", "GwtIncompatible")
args.add_all(srcs)

# TODO(b/217287994): Remove the ability to do transpiler override.
Expand Down
1 change: 1 addition & 0 deletions build_defs/internal_do_not_use/j2cl_java_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def _impl_j2cl_library(ctx):
exported_plugins = _javaplugin_providers_of(ctx.attr.exported_plugins),
output_jar = ctx.actions.declare_file(ctx.label.name + "_j2kt_web_jvm.jar"),
javac_opts = ctx.attr.javacopts,
strip_annotation = "GwtIncompatible",
)

# Pass the package-info.java files as srcs so Kotlin frontend can correctly resolved
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ final class BazelJ2clBuilder extends BazelWorker {
@Option(name = "-experimentalGenerateWasmExport", hidden = true)
List<String> wasmEntryPoints = new ArrayList<>();

@Option(name = "-forbiddenAnnotation", hidden = true)
List<String> forbiddenAnnotations = new ArrayList();

@Option(name = "-experimentalDefineForWasm", handler = MapOptionHandler.class, hidden = true)
Map<String, String> definesForWasm = new HashMap<>();

Expand Down Expand Up @@ -196,6 +199,7 @@ private J2clTranspilerOptions createOptions(Output output, Problems problems) {
.setWasmRemoveAssertStatement(wasmRemoveAssertStatement)
.setNullMarkedSupported(this.enableJSpecifySupport)
.setKotlincOptions(ImmutableList.copyOf(kotlincOptions))
.setForbiddenAnnotations(ImmutableList.copyOf(forbiddenAnnotations))
.build(problems);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ public final class J2clCommandLineRunner extends CommandLineTool {
@Option(name = "-generateWasmExport", hidden = true)
List<String> wasmEntryPoints = new ArrayList<>();

@Option(name = "-forbiddenAnnotation", hidden = true)
List<String> forbiddenAnnotations = new ArrayList<>();

@Option(name = "-defineForWasm", handler = MapOptionHandler.class, hidden = true)
Map<String, String> definesForWasm = new HashMap<>();

Expand Down Expand Up @@ -171,6 +174,7 @@ private J2clTranspilerOptions createOptions(Output output, Problems problems) {
.setBackend(this.backend)
.setWasmEntryPointStrings(ImmutableList.copyOf(wasmEntryPoints))
.setDefinesForWasm(ImmutableMap.copyOf(definesForWasm))
.setForbiddenAnnotations(ImmutableList.copyOf(forbiddenAnnotations))
.build(problems);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ public static Builder newBuilder() {
public abstract ImmutableList<EntryPointPattern> getWasmEntryPointPatterns();

@Override
public ImmutableList<String> getForbiddenAnnotations() {
return ImmutableList.of(
getBackend() == Backend.KOTLIN ? "J2ktIncompatible" : "GwtIncompatible");
}
public abstract ImmutableList<String> getForbiddenAnnotations();

/** A Builder for J2clTranspilerOptions. */
@AutoValue.Builder
Expand Down Expand Up @@ -99,6 +96,8 @@ public Builder setWasmEntryPointStrings(ImmutableList<String> wasmEntryPoints) {

public abstract Builder setKotlincOptions(ImmutableList<String> kotlincOptions);

public abstract Builder setForbiddenAnnotations(ImmutableList<String> forbiddenAnnotations);

abstract J2clTranspilerOptions autoBuild();

public J2clTranspilerOptions build(Problems problems) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ public void testOutputsToZipFile() throws IOException {

public void testForbiddenAnnotations() {
newTesterWithDefaults()
.addArgs("-forbiddenAnnotation", "GwtIncompatible")
.addCompilationUnit(
"annotation.GwtIncompatible",
"import java.lang.annotation.*;",
Expand All @@ -376,5 +377,21 @@ public void testForbiddenAnnotations() {
.assertErrorsWithoutSourcePosition(
"Unexpected @GwtIncompatible annotation found. Please run this library through the"
+ " incompatible annotated code stripper tool.");

newTesterWithDefaults()
.addArgs("-forbiddenAnnotation", "Foo")
.addCompilationUnit(
"annotation.GwtIncompatible",
"import java.lang.annotation.*;",
"@Retention(RetentionPolicy.CLASS)",
"@Target({ElementType.METHOD})",
"@interface GwtIncompatible {}")
.addCompilationUnit(
"annotation.ClassWithForbiddenAnnotation",
"import jsinterop.annotations.*;",
"public class ClassWithForbiddenAnnotation {",
" @GwtIncompatible public void nativeInstanceMethod() {}",
"}")
.assertTranspileSucceeds();
}
}

0 comments on commit 0028c8f

Please sign in to comment.