It's a pain to set data in Bundle ... Especially the definition of the key string is super annoying ... To be honest, Serialize and Parcelable are annoying ...
Specifically, it looks like this ↓ ↓ ↓ work ... It's annoying ...
Work to pack values in Bundle
final Bundle args = new Bundle();
args.putInt("key_id", value.getId());
args.putString("key_tag", value.getTag());
args.putBoolean("key_enabled", value.isEnabled());
Fragment of values from Bundle
final Bundle args = getArguments();
final int id = args.getInt("key_id");
final String tag = args.getString("key_tag");
final boolean isEnabled = args.getBoolean("key_enabled");
I want to automatically create a class that can retrieve a value from a Bundle that changes it to a Bundle just by adding Annotation like AutoValue. Specifically, I would like to use ʻAnnotation Processsing Tool` to automatically generate a class that meets the following requirements.
--Automatic class is generated when you build by annotating the value you want to save in the data class --If you pass the data class to the automatically generated class, it will be converted to Bundle. --If you pass the converted Bundle to the automatically generated class again, you can get the value of the original model class.
Sample (model class)
@BundleGenerator //Specify class
public class Sample {
private final int mId;
public Sample(int id) {
mId = id;
}
@BundleSet //Specify a value
public int getId() { return mId; }
}
The following classes are automatically generated in the build.
SampleBundleGenerator (automatically generated class)
//Data class name+Automatically generated by BundleGenerator
public class SampleBundleGenerator {
//Method to convert model to Bundle
@NonNull
public static Bundle bundle(@NonNull Sample target) {
return bundle(target, new Bundle());
}
@NonNull
public static Bundle bundle(@NonNull Sample target, @NonNull Bundle bundle) {
bundle.putInt("xxx.xxx.Sample_getId", target.getId());
return bundle;
}
//Method to retrieve value from Bundle
@NonNull
public static Wrapper restore(@NonNull Bundle bundle) {
return new Wrapper(bundle);
}
public static class Wrapper {
final Bundle mBundle;
BundleWrapper(@NonNull Bundle bundle) {
mBundle = bundle;
}
public int getId() {
return mBundle.getInt("xxx.xxx.Sample_getId");
}
}
}
Set data in Bundle
Bundle bundle = SampleBundleGenerator.bundle(value);
Fetch values from Bundle
SampleBundleGenerator.Wrapper sample = SampleBundleGenerator.restore(getArguments());
Click here for the result of implementing it for the time being → github: BundleGenerator
Details are posted in other articles, so only the general flow is introduced.
(1) Create a new project in Android Studio (like making an app normally)
(2) Added "java library" in a new module (it is easy to understand if the module name is processor)
(3) Create a class that inherits AbstractProcessor on the library side and a class of annotations that you want to use (in the Processor class, find the element annotated at compile time and automatically generate the class you want to realize)
@SupportedAnnotationTypes({
"abj.bundlegenerator.processor.BundleGenerator",
"abj.bundlegenerator.processor.BundleSet"})
@SupportedSourceVersion(SourceVersion.RELEASE_7)
public class BundleGeneratorProcessor extends AbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
//Generate a class here
//Extract the Element corresponding to the desired annotation from the argument roundEnv and generate a class using that information.
//Creating a class is very easy with a library called JavaPoet
}
}
(4) Define the entry point of Processor so that javac can be hooked. Create a file called javax.annotation.processing.Processor below and describe the class name of the created annotation processor (abj.bundlegenerator.processor.BundleGeneratorProcessor in the above)
(5) Define the library on the module side you want to use
build.gradle
dependencies {
implementation project(':processor')
annotationProcessor project(':processor')
}
https://qiita.com/LyricalMaestro0/items/9a4e3ec3ea7bda9ee523 https://qiita.com/opengl-8080/items/beda51fe4f23750c33e9 https://qiita.com/shiraji/items/ed674c5883ed0520791b
Recommended Posts