You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Before creating a plugin, ask the AndroidAnnotations developers first. We may want to include your new annotation(s) in AA core.
Creating an AndroidAnnotations plugin
Since AndroidAnnotations 4.0.0, you can easily add new annotations by creating plugins. Please note that plugins can only allow to add new annotations, not to modify behaviour of existing ones, or add new parameters to them.
Structure
A typical plugin will consist of two modules, just like AndroidAnnotations core:
an API jar, which contains the annotations,
the processor jar, which will process the annotations and generate the code:
depend on the API jar
depend on org.androidannotations:androidannotations
You can create these with the build system of your choice, but you may want to look at one of AA's core plugins as an example.
You must choose a name for your plugin, which should be unique, so it can be found in the classpath without problems.
In these modules, you have to include some meta-data so the main AndroidAnnotations processor can find and invoke your plugin:
in the API jar, add a resource named {pluginname}-api.properties (like this),
in the processor jar, add a resource named {pluginname}.properties (like this).
Substitute pluginname with the real name of your plugin. Both of these files should have this content:
version=YOUR_VERSION
Substitute YOUR_VERSION with the real version of your plugin, which should be for example the Maven version of the artefact.
Create the service descriptor file in your processor module, named META-INF/services/org.androidannotations.plugin.AndroidAnnotationsPlugin, like this. It should contain one line, the fully qualified class name of your plugin class:
com.example.YourPlugin
Creating your annotations
In the source folder of the api module, you can create your annotations. A source file for an annotation should look like this one:
/** * Overrides toString() in the generated class. */@Retention(RetentionPolicy.CLASS) // required@Target(ElementType.TYPE) // this can vary per annotationpublic @interface ToString {
}
Creating your processor
First, you have to create the entry point class of your plugin, which should inherit from AndroidAnnotationsPlugin:
packagecom.example;
importjava.util.ArrayList;
importjava.util.List;
importorg.androidannotations.AndroidAnnotationsEnvironment;
importorg.androidannotations.handler.AnnotationHandler;
importorg.androidannotations.plugin.AndroidAnnotationsPlugin;
// the same class declared in the service descriptor filepublicclassYourPluginextendsAndroidAnnotationsPlugin {
@OverridepublicStringgetName() {
return"pluginname"; // the same name used in your .properties files
}
@OverridepublicList<AnnotationHandler<?>> getHandlers(AndroidAnnotationsEnvironmentandroidAnnotationEnv) {
List<AnnotationHandler<?>> handlers = newArrayList<>();
handlers.add(newToStringHandler(androidAnnotationEnv));
// all the handlers which process your annotationsreturnhandlers;
}
}
Then you can create the actual handler class which will validate and process your annotation:
packagecom.example;
importjavax.lang.model.element.Element;
importorg.androidannotations.AndroidAnnotationsEnvironment;
importorg.androidannotations.ElementValidation;
importorg.androidannotations.handler.BaseAnnotationHandler;
importorg.androidannotations.holder.EComponentHolder;
importcom.example.api.ToString;
importcom.helger.jcodemodel.JExpr;
importcom.helger.jcodemodel.JMethod;
importcom.helger.jcodemodel.JMod;
publicclassToStringHandlerextendsBaseAnnotationHandler<EComponentHolder> {
publicToStringHandler(AndroidAnnotationsEnvironmentenvironment) {
super(ToString.class, environment); // this handles your @ToString annotation
}
@Overrideprotectedvoidvalidate(Elementelement, ElementValidationvalidation) {
validatorHelper.enclosingElementHasEnhancedComponentAnnotation(element, validation);
// the annotation only can be used in an enhanced class
}
@Overridepublicvoidprocess(Elementelement, EComponentHolderholder) throwsException {
JMethodtoString = holder.getGeneratedClass().method(JMod.PUBLIC, getClasses().STRING, "toString");
toString.body()._return(JExpr.lit("Hello, AndroidAnnotations!"));
toString.annotate(Override.class);
// creates a method in the generated class:// @Override// public String toString() {// return "Hello, AndroidAnnotations!";// }
}
}
And that is it! With this minimal setup, you can process this class:
@EActivity@ToString// your annotationpublicclassMyActivityextendsActivity {
}