diff --git a/tools/aconfig/aconfig/src/codegen/java.rs b/tools/aconfig/aconfig/src/codegen/java.rs index 9abc892908..3360ddd68b 100644 --- a/tools/aconfig/aconfig/src/codegen/java.rs +++ b/tools/aconfig/aconfig/src/codegen/java.rs @@ -428,10 +428,16 @@ mod tests { /** @hide */ public class FakeFeatureFlagsImpl extends CustomFeatureFlags { - private Map mFlagMap = new HashMap<>(); + private final Map mFlagMap = new HashMap<>(); + private final FeatureFlags mDefaults; public FakeFeatureFlagsImpl() { + this(null); + } + + public FakeFeatureFlagsImpl(FeatureFlags defaults) { super(null); + mDefaults = defaults; // Initialize the map with null values for (String flagName : getFlagNames()) { mFlagMap.put(flagName, null); @@ -441,10 +447,13 @@ mod tests { @Override protected boolean getValue(String flagName, Predicate getter) { Boolean value = this.mFlagMap.get(flagName); - if (value == null) { - throw new IllegalArgumentException(flagName + " is not set"); + if (value != null) { + return value; } - return value; + if (mDefaults != null) { + return getter.test(mDefaults); + } + throw new IllegalArgumentException(flagName + " is not set"); } public void setFlag(String flagName, boolean value) { diff --git a/tools/aconfig/aconfig/templates/FakeFeatureFlagsImpl.java.template b/tools/aconfig/aconfig/templates/FakeFeatureFlagsImpl.java.template index c20d3c5061..290d2c4b24 100644 --- a/tools/aconfig/aconfig/templates/FakeFeatureFlagsImpl.java.template +++ b/tools/aconfig/aconfig/templates/FakeFeatureFlagsImpl.java.template @@ -6,10 +6,16 @@ import java.util.function.Predicate; /** @hide */ public class FakeFeatureFlagsImpl extends CustomFeatureFlags \{ - private Map mFlagMap = new HashMap<>(); + private final Map mFlagMap = new HashMap<>(); + private final FeatureFlags mDefaults; public FakeFeatureFlagsImpl() \{ + this(null); + } + + public FakeFeatureFlagsImpl(FeatureFlags defaults) \{ super(null); + mDefaults = defaults; // Initialize the map with null values for (String flagName : getFlagNames()) \{ mFlagMap.put(flagName, null); @@ -19,10 +25,13 @@ public class FakeFeatureFlagsImpl extends CustomFeatureFlags \{ @Override protected boolean getValue(String flagName, Predicate getter) \{ Boolean value = this.mFlagMap.get(flagName); - if (value == null) \{ - throw new IllegalArgumentException(flagName + " is not set"); + if (value != null) \{ + return value; } - return value; + if (mDefaults != null) \{ + return getter.test(mDefaults); + } + throw new IllegalArgumentException(flagName + " is not set"); } public void setFlag(String flagName, boolean value) \{