summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Ted Bauer <tedbauer@google.com> 2023-11-15 16:52:02 +0000
committer Gerrit Code Review <noreply-gerritcodereview@google.com> 2023-11-15 16:52:02 +0000
commitefda207c09de55f133b22d397a78add7673fd560 (patch)
tree741bb99a0558b82aeccad910c3ace38ef88e1464
parentafe55106e57111f57014f952a469a22b19a94603 (diff)
Revert "Cache Java codegen'd flags in static member variables."
This reverts commit afe55106e57111f57014f952a469a22b19a94603. Reason for revert: causes a bug b/311187402 Change-Id: Ic3f56fbb66a6412cd702cebd9e4247032f950324
-rw-r--r--tools/aconfig/Android.bp1
-rw-r--r--tools/aconfig/Cargo.toml1
-rw-r--r--tools/aconfig/src/codegen_java.rs80
-rw-r--r--tools/aconfig/templates/FeatureFlagsImpl.java.template75
4 files changed, 78 insertions, 79 deletions
diff --git a/tools/aconfig/Android.bp b/tools/aconfig/Android.bp
index e2fadb05fb..1ad9053851 100644
--- a/tools/aconfig/Android.bp
+++ b/tools/aconfig/Android.bp
@@ -58,7 +58,6 @@ rust_defaults {
"libaconfig_protos",
"libanyhow",
"libclap",
- "libitertools",
"libprotobuf",
"libserde",
"libserde_json",
diff --git a/tools/aconfig/Cargo.toml b/tools/aconfig/Cargo.toml
index 2edf4b89cc..941b30d909 100644
--- a/tools/aconfig/Cargo.toml
+++ b/tools/aconfig/Cargo.toml
@@ -11,7 +11,6 @@ cargo = []
[dependencies]
anyhow = "1.0.69"
clap = { version = "4.1.8", features = ["derive"] }
-itertools = "0.10.5"
paste = "1.0.11"
protobuf = "3.2.0"
serde = { version = "1.0.152", features = ["derive"] }
diff --git a/tools/aconfig/src/codegen_java.rs b/tools/aconfig/src/codegen_java.rs
index 954eccc8f6..05ee0d759f 100644
--- a/tools/aconfig/src/codegen_java.rs
+++ b/tools/aconfig/src/codegen_java.rs
@@ -15,7 +15,6 @@
*/
use anyhow::Result;
-use itertools::Itertools;
use serde::Serialize;
use std::collections::BTreeSet;
use std::path::PathBuf;
@@ -35,18 +34,12 @@ where
{
let flag_elements: Vec<FlagElement> =
parsed_flags_iter.map(|pf| create_flag_element(package, pf)).collect();
- let namespace_set: BTreeSet<String> = flag_elements
- .iter()
- .unique_by(|f| &f.device_config_namespace)
- .map(|f| f.device_config_namespace.clone())
- .collect();
let properties_set: BTreeSet<String> =
flag_elements.iter().map(|fe| format_property_name(&fe.device_config_namespace)).collect();
let is_read_write = flag_elements.iter().any(|elem| elem.is_read_write);
let is_test_mode = codegen_mode == CodegenMode::Test;
let context = Context {
flag_elements,
- namespace_set,
is_test_mode,
is_read_write,
properties_set,
@@ -82,7 +75,6 @@ where
#[derive(Serialize)]
struct Context {
pub flag_elements: Vec<FlagElement>,
- pub namespace_set: BTreeSet<String>,
pub is_test_mode: bool,
pub is_read_write: bool,
pub properties_set: BTreeSet<String>,
@@ -297,31 +289,7 @@ mod tests {
import android.provider.DeviceConfig.Properties;
/** @hide */
public final class FeatureFlagsImpl implements FeatureFlags {
- private static boolean aconfig_test_is_cached = false;
- private static boolean disabledRw = false;
- private static boolean enabledRw = true;
-
-
- private void load_overrides_aconfig_test() {
- try {
- Properties properties = DeviceConfig.getProperties("aconfig_test");
- disabledRw =
- properties.getBoolean("com.android.aconfig.test.disabled_rw", false);
- enabledRw =
- properties.getBoolean("com.android.aconfig.test.enabled_rw", true);
- } catch (NullPointerException e) {
- throw new RuntimeException(
- "Cannot read value from namespace aconfig_test "
- + "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
- );
- }
- aconfig_test_is_cached = true;
- }
-
+ private Properties mPropertiesAconfigTest;
@Override
@UnsupportedAppUsage
public boolean disabledRo() {
@@ -330,10 +298,18 @@ mod tests {
@Override
@UnsupportedAppUsage
public boolean disabledRw() {
- if (!aconfig_test_is_cached) {
- load_overrides_aconfig_test();
+ if (mPropertiesAconfigTest == null) {
+ mPropertiesAconfigTest =
+ getProperties(
+ "aconfig_test",
+ "com.android.aconfig.test.disabled_rw"
+ );
}
- return disabledRw;
+ return mPropertiesAconfigTest
+ .getBoolean(
+ "com.android.aconfig.test.disabled_rw",
+ false
+ );
}
@Override
@UnsupportedAppUsage
@@ -348,10 +324,36 @@ mod tests {
@Override
@UnsupportedAppUsage
public boolean enabledRw() {
- if (!aconfig_test_is_cached) {
- load_overrides_aconfig_test();
+ if (mPropertiesAconfigTest == null) {
+ mPropertiesAconfigTest =
+ getProperties(
+ "aconfig_test",
+ "com.android.aconfig.test.enabled_rw"
+ );
+ }
+ return mPropertiesAconfigTest
+ .getBoolean(
+ "com.android.aconfig.test.enabled_rw",
+ true
+ );
+ }
+ 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 enabledRw;
+ return properties;
}
}
"#;
diff --git a/tools/aconfig/templates/FeatureFlagsImpl.java.template b/tools/aconfig/templates/FeatureFlagsImpl.java.template
index 87d567c05b..ff089dfdc8 100644
--- a/tools/aconfig/templates/FeatureFlagsImpl.java.template
+++ b/tools/aconfig/templates/FeatureFlagsImpl.java.template
@@ -8,41 +8,10 @@ import android.provider.DeviceConfig.Properties;
{{ endif }}
/** @hide */
public final class FeatureFlagsImpl implements FeatureFlags \{
-{{- if is_read_write }}
-{{- for namespace in namespace_set }}
- private static boolean {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- }}
+{{ if is_read_write- }}
+{{ for properties in properties_set }}
+ private Properties {properties};
{{ endfor }}
-
-{{ for namespace in namespace_set }}
- private void load_overrides_{namespace}() \{
- try \{
- Properties properties = DeviceConfig.getProperties("{namespace}");
-
- {{- for flag in flag_elements }}
- {{- 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} "
- + "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}_is_cached = true;
- }
-{{ endfor- }}
{{ endif- }}
{{ for flag in flag_elements }}
@@ -50,15 +19,45 @@ public final class FeatureFlagsImpl implements FeatureFlags \{
@UnsupportedAppUsage
public boolean {flag.method_name}() \{
{{ -if flag.is_read_write }}
- if (!{flag.device_config_namespace}_is_cached) \{
- load_overrides_{flag.device_config_namespace}();
+ if ({flag.properties} == null) \{
+ {flag.properties} =
+ getProperties(
+ "{flag.device_config_namespace}",
+ "{flag.device_config_flag}"
+ );
}
- return {flag.method_name};
+ return {flag.properties}
+ .getBoolean(
+ "{flag.device_config_flag}",
+ {flag.default_value}
+ );
{{ 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 #}
@@ -71,6 +70,6 @@ public final class FeatureFlagsImpl implements FeatureFlags \{
throw new UnsupportedOperationException(
"Method is not implemented.");
}
-{{ endfor- }}
+{{ endfor }}
}
{{ endif }}