From 0c4ef0f88ce280b7e934a4da73316597fc0e58df Mon Sep 17 00:00:00 2001 From: Joe Onorato Date: Sat, 13 May 2023 11:30:11 -0700 Subject: [PATCH] Use the namepace as the java package name in aconfig and call the class "Flags" Test: aconfig.test Change-Id: I15032262aad2c80d1ec1bd8fa81664a846b307ec --- tools/aconfig/src/codegen_java.rs | 34 +++++++++------------------ tools/aconfig/templates/java.template | 4 ++-- 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/tools/aconfig/src/codegen_java.rs b/tools/aconfig/src/codegen_java.rs index 3a10f2ef0a..476a89ddcc 100644 --- a/tools/aconfig/src/codegen_java.rs +++ b/tools/aconfig/src/codegen_java.rs @@ -16,6 +16,7 @@ use anyhow::Result; use serde::Serialize; +use std::path::PathBuf; use tinytemplate::TinyTemplate; use crate::aconfig::{FlagState, Permission}; @@ -25,12 +26,14 @@ use crate::commands::OutputFile; pub fn generate_java_code(cache: &Cache) -> Result { let class_elements: Vec = cache.iter().map(create_class_element).collect(); let readwrite = class_elements.iter().any(|item| item.readwrite); - let namespace = uppercase_first_letter(cache.namespace()); - let context = Context { namespace: namespace.clone(), readwrite, class_elements }; + let namespace = cache.namespace(); + let context = Context { namespace: namespace.to_string(), readwrite, class_elements }; let mut template = TinyTemplate::new(); template.add_template("java_code_gen", include_str!("../templates/java.template"))?; let contents = template.render("java_code_gen", &context)?; - let path = ["com", "android", "internal", "aconfig", &(namespace + ".java")].iter().collect(); + let mut path: PathBuf = namespace.split('.').collect(); + // TODO: Allow customization of the java class name + path.push("Flags.java"); Ok(OutputFile { contents: contents.into(), path }) } @@ -64,21 +67,6 @@ fn create_class_element(item: &Item) -> ClassElement { } } -fn uppercase_first_letter(s: &str) -> String { - s.chars() - .enumerate() - .map( - |(index, ch)| { - if index == 0 { - ch.to_ascii_uppercase() - } else { - ch.to_ascii_lowercase() - } - }, - ) - .collect() -} - #[cfg(test)] mod tests { use super::*; @@ -87,7 +75,7 @@ mod tests { #[test] fn test_generate_java_code() { - let namespace = "TeSTFlaG"; + let namespace = "com.example"; let mut cache = Cache::new(namespace.to_string()).unwrap(); cache .add_flag_declaration( @@ -118,11 +106,11 @@ mod tests { }, ) .unwrap(); - let expect_content = r#"package com.android.internal.aconfig; + let expect_content = r#"package com.example; import android.provider.DeviceConfig; - public final class Testflag { + public final class Flags { public static boolean test() { return false; @@ -130,7 +118,7 @@ mod tests { public static boolean test2() { return DeviceConfig.getBoolean( - "Testflag", + "com.example", "test2__test2", false ); @@ -139,7 +127,7 @@ mod tests { } "#; let file = generate_java_code(&cache).unwrap(); - assert_eq!("com/android/internal/aconfig/Testflag.java", file.path.to_str().unwrap()); + assert_eq!("com/example/Flags.java", file.path.to_str().unwrap()); assert_eq!( expect_content.replace(' ', ""), String::from_utf8(file.contents).unwrap().replace(' ', "") diff --git a/tools/aconfig/templates/java.template b/tools/aconfig/templates/java.template index ebcd6075a2..89da18bc43 100644 --- a/tools/aconfig/templates/java.template +++ b/tools/aconfig/templates/java.template @@ -1,8 +1,8 @@ -package com.android.internal.aconfig; +package {namespace}; {{ if readwrite }} import android.provider.DeviceConfig; {{ endif }} -public final class {namespace} \{ +public final class Flags \{ {{ for item in class_elements}} public static boolean {item.method_name}() \{ {{ if item.readwrite- }}