Skip to content
Branch: master
Find file History
Pull request Compare This branch is 22 commits ahead, 80 commits behind square:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Annotation processor


The main caveat for retrofit is that it was not until runtime that retrofit validates the interfaces and their methods' annotations or return types as well as parameters'.

These could be done at compile time with help of annotation.

So we come up with a annotation processor which will validate the interface with only an extra annotation @RetrofitService

The interface marked by @RetrofitService will be checked by annotation processor to avoid some issues like a method is marked by @GET and @POST at the same time or a method has a return type of void.


  • phase 1

    implement the annotation processor to check this processor should be consistent to the current retrofit aka compile time error at circumstances when retrofit would invoke methodError or parameterError

  • phase 2

    implement the code generator to generate classes

    package your.packagename.whatever;
    public interface YourInterface {
      @GET("/{param1}") @AnnotationsNotInRetrofit
      Observable<YourModel> sample(@Path("param1") @AnnotationsNotInRetrofit String param1);
      // and other methods 

    the generated classes should look like this:

    package retrofit2.generated;
    public class YourInterfaceService {
      public Observable<YourModel> sample(@AnnotationsNotInRetrofit String param1) {
        // generated code
      public static YourInterface build(Retrofit retrofit) {
        // generated code


    package your.packagename.whatever;
    public class Main {
      public static void main(String[] args) {
          // just like how you used to build retrofit instance
          Retrofit retrofit = new Retrofit.Builder()
          YourInterface serviceInstance =;
          Observable<YourModel> yourModelObservable = serviceInstance.sample("your_path");
          // do things with the method return value


all test cases in Retrofit should pass, in another word, the generated class should behave exactly like what it used to be in Retrofit


  1. How to register adapters/converters

    If it's done in runtime, the corresponding type check is then not available at compile time

You can’t perform that action at this time.