2016-05-25 43 views
0

我有一个教师有两个变量,一个是Student类的集合,另一个是Student类Object。根据我的理解,我拦截了Teacher类,教师类下的所有对象都应该附加拦截器。 因此,举例来说,我们对从Teacher类或列表中检索到的Student变量调用一个getter方法。它应该调用不被调用的拦截方法。这使得我们的设计公理成为false。所以我的问题是:是否存在我们可以自动拦截在类中声明的所有对象,这可以扩展到树中更下层的层次结构吗? 下面是代码:有没有一种方法可以将所有对象声明为类的字段的装饰器/拦截器?

//Teacher class 

package com.anz.interceptorproject; 
import java.util.ArrayList; 
import java.util.List; 
/** 
* Created by 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 by 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 by 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类

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 { 
// this test is being used to test if when object is being intercepted will all its child object be 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 us assume the Teacher object is an object return from JCR after the adapTo() function is called on a 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(); 
     for (Student proxyStudentList:proxyTeacher.getStudents()) 
     { 
      //the intercept method is not called. 
      proxyStudentName= proxyStudentList.getName(); 
     } 
     Student testComplexStudent=teacher.getComplexObjectStudent(); 
     assertEquals("Math",proxyTeacher.getCource()); 
     //the intercept method is not called 
     testComplexStudent.getAge(); 

     System.out.println( teacher.getUserName()); 

     assertTrue(true); 
    } 

} 

回答

0

我的理解所有的教师类下的对象应该重视它的拦截器。因此,例如我们称之为Student变量的getter方法,该方法是从Teacher类或列表中检索的Student变量。它应调用拦截方法

这是不正确的。方法拦截器仅附加到使用enhancer.create()创建的对象。任何使用类构造函数创建的对象(如Student对象)都不会被增强,因此没有附加拦截器。一个可能的解决方案是使用的,而不是公共构造工厂方法来产生增强的对象:

public class Student { 
    protected Student() { 
    } 

    public static Student getInstance() { 
     Enhancer e = new Enhancer(); 
     // set superclass, interceptors etc here.... 
     return (Student) e.create(); 
    } 
} 
+0

亚历你好,谢谢你。但是如果我们想要做的反射的分配/增强对象在运行时,而不是在构造函数级别。因此,例如,如果我们发现feild与常规主时间不同,比如集合或任何其他类对象,则它应该附加反射。 –

相关问题