0

Docs: fix documentation for java_cpp_strings

No change to logic, only docs.

This fixes several mistakes in the java_cpp_strings docs:
 * Do not need GENERATED_JAVA_PACKAGE
 * {NATIVE_STRINGS}, not {NATIVE_SWITCHES}
 * Do not need "This file is autogenerated..." boilerplate in the
   template
 * Consistent package name between template and generated file
 * Consistent class name between template and generated file
 * Change {YEAR} to $YEAR, as this must not be left templated, but
   should be filled in by the author

This also adds some more opinionated changes:
 * Mark the class as "public final" not "public abstract" and mark the
   constructor private, as this is a more robust way to mark the class
   as non-instantiable
 * Include the boilerplate "This following string constants..." [sic]
   message in the generated file, as that's what the script inserts.
 * Use more realistic file names (foo_switches.cc and FooSwitches.java).
 * Add javadoc for the template file, as a suggestion for others to
   document the generated class.

Bug: 1011991
Test: tools/md_browser/md_browser.py
Test: Upload to gerrit > open file > click "gitiles"
Change-Id: Ia1aa96cf50842bf226f35440fd2cafe8a186f8b5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1863866
Reviewed-by: Ian Vollick <vollick@chromium.org>
Commit-Queue: Nate Fischer <ntfschr@chromium.org>
Cr-Commit-Position: refs/heads/master@{#706456}
This commit is contained in:
Nate Fischer
2019-10-16 15:31:58 +00:00
committed by Commit Bot
parent ecc2c84279
commit 9320324617

@ -11,76 +11,82 @@ specified in a comment within the switch file itself.
## Usage
1. Add directives to your C++ switch file
```cpp
// GENERATED_JAVA_PACKAGE: org.chromium.chrome
// ...snip...
// Documentation for the following switch.
const char kSomeSwitch[] = "some-switch";
// ...snip...
```
2. Create a template file
1. Create a template file (ex. `FooSwitches.java.tmpl`)
```java
// Copyright {YEAR} The Chromium Authors. All rights reserved.
// Copyright $YEAR The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This file is autogenerated by
// {SCRIPT_NAME}
// From
// {SOURCE_PATH}, and
// {TEMPLATE_PATH}
package my.java.package
package org.chromium.foo;
// Be sure to escape any curly braces in your template by doubling as
// follows.
public abstract class MySwitches {{
/**
* Contains command line switches that are specific to the foo project.
*/
public final class FooSwitches {{
{NATIVE_SWITCHES}
{NATIVE_STRINGS}
// Prevents instantiation.
private FooSwitches() {{}}
}}
```
3. Add a new build target
2. Add a new build target
```gn
import("//build/config/android/rules.gni")
java_cpp_strings("foo_generated_switch") {
java_cpp_strings("java_switches") {
sources = [
"//base/android/native_foo_switches.cc",
"//base/android/foo_switches.cc",
]
template = "//base/android/java_templates/MySwitches.java.tmpl"
template = "//base/android/java_templates/FooSwitches.java.tmpl"
}
```
5. Add the new target to the desired android_library targets srcjar_deps:
3. Add the new target to the desired `android_library` targets `srcjar_deps`:
```gn
android_library("base_java") {
srcjar_deps = [
":foo_generated_switches",
":java_switches",
]
}
```
5. The generated file `org/chromium/chrome/NativeFooSwitches.java` would contain:
4. The generated file `out/Default/gen/.../org/chromium/foo/FooSwitches.java`
would contain:
```java
package org.chromium.chrome;
// Copyright $YEAR The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.foo;
/**
* Contains command line switches that are specific to the foo project.
*/
public final class FooSwitches {
public final class NativeFooSwitches {
// ...snip...
// This following string constants were inserted by
// java_cpp_strings.py
// From
// ../../base/android/foo_switches.cc
// Into
// ../../base/android/java_templates/FooSwitches.java.tmpl
// Documentation for the C++ switch is copied here.
public static final String SOME_SWITCH = "some-switch";
// ...snip...
// Prevents instantiation.
private FooSwitches() {}
}
```