JAVACOPTS = [ ... ]

def java_library(javacopts = [], **kwargs):
    """Loaded by prelude_bazel to shadow the existing `java_library` and add additional javacopts when building local java code."""
    updated_opts = []
    updated_opts += javacopts
    if native.repository_name() == "@":
        # When building any java target in our repository (i.e. not ones that are loaded into the WORKSPACE with
        # http_archive and friends), apply our javac options.  We don't apply them to external code because that code
        # may not adhere to our rules.
        updated_opts += JAVACOPTS

        # Generate a test target for every source package, containing a no-op test that imports the package.  If a
        # package is not imported by any test, the package and its sources will be completely excluded from the coverage
        # report.  That's bad, as it artificially increases our coverage numbers by skipping many packages.  Thus these
        # PackageCoverageTests are generated and executed when running coverage, to ensure that every package is
        # referenced and evey source is included.  The test needs to assert *something* but doesn't need to actually
        # test the code, so it runs a cheap assert.
        if kwargs.get("srcs") and native.existing_rule("PackageCoverageTest") == None:
            native.genrule(
                name = "PackageCoverageTestSource",
                srcs = [kwargs["srcs"][0]],
                outs = ["PackageCoverageTest.java"],
                # Grab the package name from one of the package's sources.
                cmd = '''grep '^package ' $< | head -n1 > $@; echo "public class PackageCoverageTest {
                    @org.junit.Test
                    public void testFoo() {
                        org.junit.Assert.assertNotNull(1);
                    }
                }" >> $@''',
            )
            native.java_test(
                name = "PackageCoverageTest",
                srcs = [":PackageCoverageTest.java"],
                deps = [
                    kwargs["name"],
                    "//third_party/jvm/junit",
                ] + kwargs.get("deps", []),
                tags = ["coverage-only"],
            )
    native.java_library(javacopts = updated_opts, **kwargs)