lundi 18 janvier 2016

Can Byte Buddy create fields and method annotations at runtime?

I would like to implement this 3rd party annotation to map my Class fields/properties to my database table columns on their server. I can easily implement the annotation at compiling time(as below code) but I can't find a way to do it at runtime (loading the library at runtime-using reflection).

Below is their annotation code and how I would implement at compiling time. My question is how can I implement this mapping annotation when loading library at run time? Can Byte Buddy handle this for Android?.

Thank you

//3rd party annotation code
package weborb.service;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
public @interface MapToProperty {
    String property();
}

/////////////////////////////////////////////////////

//Here is the implementation using non-reflection
//QUESTIONS: How to implement this at run time, can Byte Buddy handle this for Android?

import weborb.service;
    Class Person
    {
        @MapToProperty(property="Person_Name")
        String name;

        @MapToProperty(property="Person_Age")
        int age;

        @MaptoProperty(property="Person_Name")
        public String getName()
        {
            return this.name;
        }

        @MaptoProperty(property="Person_Name")
        public void setName(String name)
        {
            this.name = name;
        }

        @MaptoProperty(property="Person_Age")
        public int getAge()
        {
             return this.age;
        }

        @MaptoProperty(property="Person_Age")
        public void setAge(int age)
        {
            this.age = age;
        }
    }





Aucun commentaire:

Enregistrer un commentaire