lundi 11 janvier 2016

Custom annotation Java / android

I'm trying to make some custom annotations to reduce boiler plate code in my android apps. I know it is doable since there are many libraries using the same technique, e.g. ButterKnife. So Imagine this simple Android Activity. I'd like to know how to make CustomLibrary.printGoodInts work the way I want (perhaps using reflection).

PS: if what I am asking is crazy and too much to be a simple answer, a good reference will do great for me too :)

public class MainActivity extends Activity {

    @GoodInt
    private int m1 = 10;

    @GoodInt
    private int m2 = 20;

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      CustomLibrary.printGoodInts(this); // <<<<<<<<<< This is where magic happens
   }
}


public class CustomLibrary {
    public static void printGoodInts(Object obj){
        // Find all member variables that are int and labeled as @GoodInt
        // Print them!!
    }
}





Aucun commentaire:

Enregistrer un commentaire