Revert^2 "Cache Java codegen'd flags in static member variables."

This reverts commit efda207c09.

Reason for revert: this unrevert contains the fix for b/311187402

Test: cargo test
Change-Id: I210aebd30edd864a7c141ede336c12aebf4f1fcd
This commit is contained in:
Ted Bauer
2023-11-15 18:04:54 +00:00
parent 5003ff741a
commit c3073786b3
10 changed files with 305 additions and 90 deletions

View File

@@ -16,7 +16,7 @@
use anyhow::Result;
use serde::Serialize;
use std::collections::BTreeSet;
use std::collections::{BTreeMap, BTreeSet};
use std::path::PathBuf;
use tinytemplate::TinyTemplate;
@@ -34,12 +34,14 @@ where
{
let flag_elements: Vec<FlagElement> =
parsed_flags_iter.map(|pf| create_flag_element(package, pf)).collect();
let namespace_flags = gen_flags_by_namespace(&flag_elements);
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_flags,
is_test_mode,
is_read_write,
properties_set,
@@ -72,16 +74,44 @@ where
.collect::<Result<Vec<OutputFile>>>()
}
fn gen_flags_by_namespace(flags: &[FlagElement]) -> Vec<NamespaceFlags> {
let mut namespace_to_flag: BTreeMap<String, Vec<FlagElement>> = BTreeMap::new();
for flag in flags {
match namespace_to_flag.get_mut(&flag.device_config_namespace) {
Some(flag_list) => flag_list.push(flag.clone()),
None => {
namespace_to_flag.insert(flag.device_config_namespace.clone(), vec![flag.clone()]);
}
}
}
namespace_to_flag
.iter()
.map(|(namespace, flags)| NamespaceFlags {
namespace: namespace.to_string(),
flags: flags.clone(),
})
.collect()
}
#[derive(Serialize)]
struct Context {
pub flag_elements: Vec<FlagElement>,
pub namespace_flags: Vec<NamespaceFlags>,
pub is_test_mode: bool,
pub is_read_write: bool,
pub properties_set: BTreeSet<String>,
pub package_name: String,
}
#[derive(Serialize)]
#[derive(Serialize, Debug)]
struct NamespaceFlags {
pub namespace: String,
pub flags: Vec<FlagElement>,
}
#[derive(Serialize, Clone, Debug)]
struct FlagElement {
pub default_value: bool,
pub device_config_namespace: String,
@@ -148,6 +178,8 @@ mod tests {
boolean disabledRo();
@UnsupportedAppUsage
boolean disabledRw();
@UnsupportedAppUsage
boolean disabledRw2();
@com.android.aconfig.annotations.AssumeTrueForR8
@UnsupportedAppUsage
boolean enabledFixedRo();
@@ -170,6 +202,8 @@ mod tests {
/** @hide */
public static final String FLAG_DISABLED_RW = "com.android.aconfig.test.disabled_rw";
/** @hide */
public static final String FLAG_DISABLED_RW_2 = "com.android.aconfig.test.disabled_rw_2";
/** @hide */
public static final String FLAG_ENABLED_FIXED_RO = "com.android.aconfig.test.enabled_fixed_ro";
/** @hide */
public static final String FLAG_ENABLED_RO = "com.android.aconfig.test.enabled_ro";
@@ -185,6 +219,10 @@ mod tests {
public static boolean disabledRw() {
return FEATURE_FLAGS.disabledRw();
}
@UnsupportedAppUsage
public static boolean disabledRw2() {
return FEATURE_FLAGS.disabledRw2();
}
@com.android.aconfig.annotations.AssumeTrueForR8
@UnsupportedAppUsage
public static boolean enabledFixedRo() {
@@ -224,6 +262,11 @@ mod tests {
}
@Override
@UnsupportedAppUsage
public boolean disabledRw2() {
return getValue(Flags.FLAG_DISABLED_RW_2);
}
@Override
@UnsupportedAppUsage
public boolean enabledFixedRo() {
return getValue(Flags.FLAG_ENABLED_FIXED_RO);
}
@@ -259,6 +302,7 @@ mod tests {
Map.ofEntries(
Map.entry(Flags.FLAG_DISABLED_RO, false),
Map.entry(Flags.FLAG_DISABLED_RW, false),
Map.entry(Flags.FLAG_DISABLED_RW_2, false),
Map.entry(Flags.FLAG_ENABLED_FIXED_RO, false),
Map.entry(Flags.FLAG_ENABLED_RO, false),
Map.entry(Flags.FLAG_ENABLED_RW, false)
@@ -289,7 +333,52 @@ mod tests {
import android.provider.DeviceConfig.Properties;
/** @hide */
public final class FeatureFlagsImpl implements FeatureFlags {
private Properties mPropertiesAconfigTest;
private static boolean aconfig_test_is_cached = false;
private static boolean other_namespace_is_cached = false;
private static boolean disabledRw = false;
private static boolean disabledRw2 = 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 void load_overrides_other_namespace() {
try {
Properties properties = DeviceConfig.getProperties("other_namespace");
disabledRw2 =
properties.getBoolean("com.android.aconfig.test.disabled_rw_2", false);
} catch (NullPointerException e) {
throw new RuntimeException(
"Cannot read value from namespace other_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
);
}
other_namespace_is_cached = true;
}
@Override
@UnsupportedAppUsage
public boolean disabledRo() {
@@ -298,18 +387,18 @@ mod tests {
@Override
@UnsupportedAppUsage
public boolean disabledRw() {
if (mPropertiesAconfigTest == null) {
mPropertiesAconfigTest =
getProperties(
"aconfig_test",
"com.android.aconfig.test.disabled_rw"
);
if (!aconfig_test_is_cached) {
load_overrides_aconfig_test();
}
return mPropertiesAconfigTest
.getBoolean(
"com.android.aconfig.test.disabled_rw",
false
);
return disabledRw;
}
@Override
@UnsupportedAppUsage
public boolean disabledRw2() {
if (!other_namespace_is_cached) {
load_overrides_other_namespace();
}
return disabledRw2;
}
@Override
@UnsupportedAppUsage
@@ -324,36 +413,10 @@ mod tests {
@Override
@UnsupportedAppUsage
public boolean enabledRw() {
if (mPropertiesAconfigTest == null) {
mPropertiesAconfigTest =
getProperties(
"aconfig_test",
"com.android.aconfig.test.enabled_rw"
);
if (!aconfig_test_is_cached) {
load_overrides_aconfig_test();
}
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 properties;
return enabledRw;
}
}
"#;
@@ -426,6 +489,12 @@ mod tests {
}
@Override
@UnsupportedAppUsage
public boolean disabledRw2() {
throw new UnsupportedOperationException(
"Method is not implemented.");
}
@Override
@UnsupportedAppUsage
public boolean enabledFixedRo() {
throw new UnsupportedOperationException(
"Method is not implemented.");