reflection - Is there a way we can put the decorator /interceptor on all the objects that are being declared as field of a class? -


i have class teacher have 2 variables 1 collection of student class , student class object. intercept teacher class per understanding objects under teacher class should have interceptor attached it. instance 1 call getter method on student variable retrieved teacher class or list .it should call intercept method not called.this makes our axiom design false.so question : there way can automatically intercept objects declared within class , extend further down hierarchy within tree? below code :

//teacher class  package com.anz.interceptorproject; import java.util.arraylist; import java.util.list; /**  * created mehakanand on 4/24/16.  */public class teacher {      private string username;     private string cource;     private list<student> students=new arraylist<student>(  );      public student getcomplexobjectstudent() {     return complexobjectstudent; }      public void setcomplexobjectstudent( student complexobjectstudent ) {     this.complexobjectstudent = complexobjectstudent; }      private student complexobjectstudent=new student();     public list<student> getstudents() {         return students;     }      public void setstudents(list<student> students) {         this.students = students;     }      public string getusername() {         return username;     }      public void setusername(string username) {         this.username = username;     }      public string getcource() {         return cource;     }      public void setcource(string cource) {         this.cource = cource;     } }  //student class  package com.anz.interceptorproject;  /**  * created mehakanand on 4/24/16.  */public class student {      private string name;     private int age;      public string getname() {         return name;     }     public void setname(string name) {         this.name = name;     }     public int getage() {         return age;     }     public void setage(int age) {         this.age = age;     } }  //interceptor class package com.anz.interceptorproject.change; import java.lang.reflect.*; import net.sf.cglib.proxy.*; /**  * created mehakanand on 4/24/16.  */ public class classfacadecglib implements methodinterceptor{      private object target;      public object getinstance(object target) {         this.target = target;         enhancer enhancer = new enhancer();         enhancer.setsuperclass(this.target.getclass());         // callback method         enhancer.setcallback(this);         // create proxy object         return enhancer.create();     }       public object intercept(object obj, method method, object[] args,                             methodproxy proxy) throws throwable {         object res=null;          if(method.getname().startswith("set")){             system.out.println(method.getname()+" start");            res = method.invoke(target, args);              proxy.invokesuper(obj, args);             system.out.println(method.getname()+" end..");         }         if(method.getname().startswith("get")){             system.out.println(method.getname()+" start");              res = method.invoke(target, args);              proxy.invokesuper(obj, args);             system.out.println(method.getname()+" end");         }          return  res;     }  } 

//delegate class

package com.anz.interceptorproject;  import static org.junit.assert.*; import static org.junit.assert.assertequals;  import net.sf.cglib.proxy.methodinterceptor; import org.junit.test;  public class simpleunittest { // test being used test if when object being intercepted child object intercepted automatically or not     @test     public void testifchildrenobjectintercepted() {         string proxystudentname="";         classfacadecglib cglib=new classfacadecglib();          student studentmehak=new student();         studentmehak.setage( 30 );         studentmehak.setname( "mehak anand" );         student studentcomploexproxy=new student();         studentcomploexproxy.setage( 23 );         studentcomploexproxy.setname( "proxystudent complex" );         //let assume teacher object object return jcr after adapto() function called on resource         teacher teacher=new teacher();         teacher.setcomplexobjectstudent( studentcomploexproxy );         teacher.getstudents().add( studentmehak );         teacher.setcource("math");         teacher.setusername("mehak");         teacher.getusername();         teacher proxyteacher=(teacher)cglib.getinstance(teacher);        / proxyteacher.getclass().getdeclaredmethods();         (student proxystudentlist:proxyteacher.getstudents())         {             //the intercept method not called.             proxystudentname= proxystudentlist.getname();         }         student testcomplexstudent=teacher.getcomplexobjectstudent();          assertequals("math",proxyteacher.getcource());         //the intercept method not called         testcomplexstudent.getage();          system.out.println(  teacher.getusername());          asserttrue(true);     }  } 

my understanding objects under teacher class should have interceptor attached it. instance 1 call getter method on student variable retrieved teacher class or list .it should call intercept method

this not true. method interceptors attached object created enhancer.create(). object created class constructor - such student objects - not enhanced , have no interceptors attached. 1 possible solution use factory method instead of public constructor produce enhanced objects:

public class student {     protected student() {     }      public static student getinstance() {          enhancer e = new enhancer();          // set superclass, interceptors etc here....          return (student) e.create();     } } 

Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -