java - Call a method when object state changes -
i providing annotation @validatename
can include in code. suppose 1 coded
class person { @validatename private string name; .... }
then can call namevalidator.validate(personobject)
or similar method validate field.
i want make sure name field in valid state i.e. want call validate()
method automatically whenever annotated variable changes (no matter changes whether inside or outside class).
willing write plugin hooks eclipse , gets invoked during compilation phase. please provide pointers can start looking solutions.
(i guess have implement sort of aop or should modify bytecode using bcel or something. not sure haven't tried both.)
with aspectj can this:
annotation
package de.scrum_master.aop.app; import java.lang.annotation.retention; import java.lang.annotation.retentionpolicy; @retention(retentionpolicy.runtime) public @interface validatename {}
driver class
package de.scrum_master.aop.app; public class application { private int id; @validatename private string firstname; @validatename private string lastname; private string placeofbirth; public application(int id, string firstname, string lastname, string placeofbirth) { this.id = id; this.firstname = firstname; this.lastname = lastname; this.placeofbirth = placeofbirth; } @override public string tostring() { return "application [id=" + id + ", firstname=" + firstname + ", lastname=" + lastname + ", placeofbirth=" + placeofbirth + "]"; } public static void main(string[] args) { system.out.println(new application(1, "galileo", "galilei", "pisa, italy")); system.out.println(new application(2, "isaac", "newton", "woolsthorpe-by-colsterworth, united kingdom")); system.out.println(new application(3, "albert", "einstein", "ulm, germany")); system.out.println(new application(4, "werner", "heisenberg", "würzburg, germany")); } }
validator aspect
package de.scrum_master.aop.aspect; import java.util.random; import de.scrum_master.aop.app.validatename; public aspect namevalidator { void validate(string name) { if (new random().nextboolean()) throw new runtimeexception("invalid name " + name); } void around(string name) : set(@validatename * *.*) && args(name) { //system.out.println(thisjoinpointstaticpart); system.out.print("validating name " + name); try { validate(name); system.out.println(" -> ok"); proceed(name); } catch (exception e) { name = name.touppercase(); system.out.println(" -> " + e.getmessage() + " -> replaced " + name); proceed(name); } } }
as can see, validator randomly fails in ca. 50% of cases based on pseudo-random value. when does, replaces "invalid" name capitalised version. output looks variation of this:
validating name galileo -> ok validating name galilei -> invalid name galilei -> replaced galilei application [id=1, firstname=galileo, lastname=galilei, placeofbirth=pisa, italy] validating name isaac -> invalid name isaac -> replaced isaac validating name newton -> invalid name newton -> replaced newton application [id=2, firstname=isaac, lastname=newton, placeofbirth=woolsthorpe-by-colsterworth, united kingdom] validating name albert -> ok validating name einstein -> invalid name einstein -> replaced einstein application [id=3, firstname=albert, lastname=einstein, placeofbirth=ulm, germany] validating name werner -> ok validating name heisenberg -> invalid name heisenberg -> replaced heisenberg application [id=4, firstname=werner, lastname=heisenberg, placeofbirth=würzburg, germany]
Comments
Post a Comment