vendredi 23 septembre 2016

How can I remove a method from the class definition on the fly?

Let me start by saying that this is purely an exercise to satisfy my curiosity. This is not meant to be used in any sort of production environment of any sort.

What I would like to know is if it is in any way possible to redefine a class on the fly by removing/adding methods to its definition.

For example, given this class:

public class PersonBuilder {

    private String name;
    private int age;

    public PersonBuilder name(String name) {
        this.name = name;
        System.out.println("Name defined");
        return this;
    }

    public PersonBuilder age(int age) {
        this.age = age;
        System.out.println("Age defined");
        return this;
    }

}

What I want is to completely remove name from the class definition once it is invoked. Equivalent behavior is expected if I invoke age. Basically whichever method is invoked gets removed.

I understand this would require some level of bytecode manipulation, I'm fairly certain it cannot be achieved via reflection.

Anyway, I am looking for some guidance on how I could develop such behavior. Any tips will be very helpful.





Aucun commentaire:

Enregistrer un commentaire