Revert^2 "Cache Java codegen'd flags in static member variables."
This reverts commit efda207c09de55f133b22d397a78add7673fd560.
Reason for revert: this unrevert contains the fix for b/311187402
Test: cargo test
Change-Id: I210aebd30edd864a7c141ede336c12aebf4f1fcd
diff --git a/tools/aconfig/templates/FeatureFlagsImpl.java.template b/tools/aconfig/templates/FeatureFlagsImpl.java.template
index ff089df..ec8822c 100644
--- a/tools/aconfig/templates/FeatureFlagsImpl.java.template
+++ b/tools/aconfig/templates/FeatureFlagsImpl.java.template
@@ -8,10 +8,41 @@
{{ endif }}
/** @hide */
public final class FeatureFlagsImpl implements FeatureFlags \{
-{{ if is_read_write- }}
-{{ for properties in properties_set }}
- private Properties {properties};
+{{- if is_read_write }}
+{{- for namespace_with_flags in namespace_flags }}
+ private static boolean {namespace_with_flags.namespace}_is_cached = false;
+{{- endfor- }}
+
+{{ for flag in flag_elements }}
+{{- if flag.is_read_write }}
+ private static boolean {flag.method_name} = {flag.default_value};
+{{- endif- }}
{{ endfor }}
+
+{{ for namespace_with_flags in namespace_flags }}
+ private void load_overrides_{namespace_with_flags.namespace}() \{
+ try \{
+ Properties properties = DeviceConfig.getProperties("{namespace_with_flags.namespace}");
+
+ {{- for flag in namespace_with_flags.flags }}
+ {{- if flag.is_read_write }}
+ {flag.method_name} =
+ properties.getBoolean("{flag.device_config_flag}", {flag.default_value});
+ {{- endif- }}
+ {{ endfor }}
+ } catch (NullPointerException e) \{
+ throw new RuntimeException(
+ "Cannot read value from namespace {namespace_with_flags.namespace} "
+ + "from DeviceConfig. It could be that the code using flag "
+ + "executed before SettingsProvider initialization. Please use "
+ + "fixed read-only flag by adding is_fixed_read_only: true in "
+ + "flag declaration.",
+ e
+ );
+ }
+ {namespace_with_flags.namespace}_is_cached = true;
+ }
+{{ endfor- }}
{{ endif- }}
{{ for flag in flag_elements }}
@@ -19,45 +50,15 @@
@UnsupportedAppUsage
public boolean {flag.method_name}() \{
{{ -if flag.is_read_write }}
- if ({flag.properties} == null) \{
- {flag.properties} =
- getProperties(
- "{flag.device_config_namespace}",
- "{flag.device_config_flag}"
- );
+ if (!{flag.device_config_namespace}_is_cached) \{
+ load_overrides_{flag.device_config_namespace}();
}
- return {flag.properties}
- .getBoolean(
- "{flag.device_config_flag}",
- {flag.default_value}
- );
+ return {flag.method_name};
{{ else }}
return {flag.default_value};
{{ endif- }}
}
{{ endfor }}
-
-{{ -if is_read_write }}
- private Properties getProperties(
- String namespace,
- String flagName) \{
- Properties properties = null;
- try \{
- properties = DeviceConfig.getProperties(namespace);
- } catch (NullPointerException e) \{
- throw new RuntimeException(
- "Cannot read value of flag " + flagName + " from DeviceConfig. "
- + "It could be that the code using flag executed "
- + "before SettingsProvider initialization. "
- + "Please use fixed read-only flag by adding "
- + "is_fixed_read_only: true in flag declaration.",
- e
- );
- }
-
- return properties;
- }
-{{ endif- }}
}
{{ else }}
{#- Generate only stub if in test mode #}
@@ -70,6 +71,6 @@
throw new UnsupportedOperationException(
"Method is not implemented.");
}
-{{ endfor }}
+{{ endfor- }}
}
{{ endif }}