From 58edc171a5d1e0584047214770fd54562be555a3 Mon Sep 17 00:00:00 2001 From: Googler Date: Wed, 19 Oct 2022 13:03:42 -0700 Subject: [PATCH] Do not require option converters to be public. Change how we reflectively instantiate the option converters so that Bazel no longer crashes when we try to use a non-public class. Technically, this is overriding the visibility of the class and the constructor, however this is merely a side effect of the fact that annotations do not allow us to use an actual object. It is not obvious why these classes have to be public and a mistake here results with Bazel failing to start. PiperOrigin-RevId: 482281892 Change-Id: I064301ce88f83fd55a305385309cfb357e5e054c --- .../java/com/google/devtools/common/options/Option.java | 6 ++++-- .../google/devtools/common/options/OptionDefinition.java | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/google/devtools/common/options/Option.java b/src/main/java/com/google/devtools/common/options/Option.java index 00a602eba8f1b9..6e20a9701607f4 100644 --- a/src/main/java/com/google/devtools/common/options/Option.java +++ b/src/main/java/com/google/devtools/common/options/Option.java @@ -123,9 +123,11 @@ * an object or a simple type. The default is to use the builtin converters ({@link * Converters#DEFAULT_CONVERTERS}). Custom converters must implement the {@link Converter} * interface. + * + *

This class will be instantiated reflectively using a nullary constructor. Provided class + * does not have to be visible in the {@linkplain com.google.devtools.common.options options} + * package, e.g. private classes are allowed. */ - @SuppressWarnings({"unchecked", "rawtypes"}) - // Can't figure out how to coerce Converter.class into Class> Class converter() default Converter.class; /** diff --git a/src/main/java/com/google/devtools/common/options/OptionDefinition.java b/src/main/java/com/google/devtools/common/options/OptionDefinition.java index 9b863b683c6c14..5cf932fa41bbde 100644 --- a/src/main/java/com/google/devtools/common/options/OptionDefinition.java +++ b/src/main/java/com/google/devtools/common/options/OptionDefinition.java @@ -238,7 +238,8 @@ public Converter getConverter() { } else { try { // Instantiate the given Converter class. - Constructor constructor = converterClass.getConstructor(); + Constructor constructor = converterClass.getDeclaredConstructor(); + constructor.setAccessible(true); converter = (Converter) constructor.newInstance(); } catch (SecurityException | IllegalArgumentException | ReflectiveOperationException e) { // This indicates an error in the Converter, and should be discovered the first time it is