mardi 11 février 2020

I have added a predefined annotation to a method at runtime, I want the changes to be permanent?

So, lets say I have a class Person and i want to add Myntra annotation to getLastName() method at runtime (which I have accomplished using Javassist library )

But I want the changes to be permanent so that when I run it next time the annotation should be there !!

How can I do that?

package com.flipkart.aditya.annotations;


public class Person {
    private String firstName;
    private String lastName;
    private String adhaarID;
    private int employeeID;


    public Person(String firstName, String lastName, String adhaarID, int employeeID) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.adhaarID = adhaarID;
        this.employeeID = employeeID;
    }

    @Myntra
    @Jabong
    public String getFullName()
    {
        return firstName+" "+lastName;
    }


    @Xyz
    public String getFirstName()
    {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    @Myntra
    public String getAdhaarID() {
        return adhaarID;
    }

    @Jabong
    @Xyz
    public int getEmployeeID() {
        return employeeID;
    }
}

This is How I did it using javassist lib

package com.flipkart.aditya.annotations;


import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.bytecode.AnnotationsAttribute;
import javassist.bytecode.ClassFile;
import javassist.bytecode.ConstPool;
import javassist.bytecode.annotation.Annotation;

public class AddRunTimeAnnotation {
    public static void addNewAnnotationToMethod(String className,String methodName) throws Exception{

        ClassPool pool = ClassPool.getDefault();
        CtClass cc = pool.getCtClass(className);
        CtMethod ctMethod = cc.getDeclaredMethod(methodName);
        ClassFile ccFile = cc.getClassFile();
        ConstPool constpool = ccFile.getConstPool();
        AnnotationsAttribute attr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag);
        Annotation annot = new Annotation("com.flipkart.aditya.annotations.Myntra", constpool);
        attr.addAnnotation(annot);
        ctMethod.getMethodInfo().addAttribute(attr);
        Class<?> c =cc.toClass();



    }
}

This is my Myntra annotation

package com.flipkart.aditya.annotations;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface Myntra {
}

NOTE: Jabong and xyz are also the annotation similiar to Myntra but have nothing to do with my question.

And this is how my main() functions looks like--> I am here adding a new Myntra annotation to the getLastName() method and then printing all the methods which have Myntra annotation on it using java reflection.

public class NewApplicationToTestApproachOne {
    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
        try {
            AddRunTimeAnnotation.addNewAnnotationToMethod("com.flipkart.aditya.annotations.Person", "getLastName");
        } catch (Exception e) {

            e.printStackTrace();
        }
        Person person = new Person("Ravan", "lanka wale", "0000 0000 0000", 420);
        Class<?> reflectClass = Person.class;
        Method[] arrayOfMethods = reflectClass.getDeclaredMethods();
        for (Method method : arrayOfMethods) {
            if (method.isAnnotationPresent(Myntra.class)) {
                System.out.println(method.invoke(person, null));
            }
        }

    }

}

So its working fine at the runtime--> OUTPUT IS:

Ravan lanka wale
0000 0000 0000
lanka wale

But I want the annotation added to the getLastName() method to be permanent and for ever. I am not sure if its possible or not? If yes--> please tell me how to do it !!





Aucun commentaire:

Enregistrer un commentaire