Monday 27 April 2020

Java: Remove a field from class

Follow below steps to remove a field from class.

Step 1: Get ClassPool instance.
ClassPool classPool = ClassPool.getDefault();

Step 2: Get CtClass instance.
CtClass pointClass = classPool.get("com.sample.app.model.Point");

Step 3: Get CtField instance that refer to the field that we want to delete.
CtField toBeDeleted = pointClass.getField("x");

Step 4: Remove the field.
pointClass.removeField(toBeDeleted);

Step 5: Publish modified byte code
pointClass.toClass();

Previous                                                    Next                                                    Home

No comments:

Post a Comment