Subversion Repositories bacoAlunos

Rev

Rev 1353 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1351 jmachado 1
package jomm.experiments;
2
 
3
import javassist.util.proxy.MethodFilter;
4
import javassist.util.proxy.MethodHandler;
5
import javassist.util.proxy.ProxyFactory;
1353 jmachado 6
import pt.estgp.estgweb.domain.Course;
1351 jmachado 7
 
8
import java.lang.reflect.InvocationTargetException;
9
import java.lang.reflect.Method;
10
 
11
/**
12
 * Created by jorgemachado on 12/04/16.
13
 */
14
public class JavaAssistDynamicProxy
15
{
16
    public static void main(1.5.0/docs/api/java/lang/String.html">String[] args) throws 1.5.0/docs/api/java/lang/reflect/InvocationTargetException.html">InvocationTargetException, 1.5.0/docs/api/java/lang/NoSuchMethodException.html">NoSuchMethodException, 1.5.0/docs/api/java/lang/InstantiationException.html">InstantiationException, 1.5.0/docs/api/java/lang/IllegalAccessException.html">IllegalAccessException {
17
 
18
 
19
        ProxyFactory factory = new ProxyFactory();
1353 jmachado 20
        factory.setSuperclass(Course.class);
1351 jmachado 21
        factory.setFilter(
22
                new MethodFilter() {
23
                    @1.5.0/docs/api/java/lang/Override.html">Override
24
                    public boolean isHandled(1.5.0/docs/api/java/lang/reflect/Method.html">Method method) {
25
                        return true;
26
                        //return Modifier.isAbstract(method.getModifiers());
27
                    }
28
                }
29
        );
30
 
31
 
1353 jmachado 32
 
33
 
34
 
1351 jmachado 35
        MethodHandler handler = new MethodHandler() {
36
            @1.5.0/docs/api/java/lang/Override.html">Override
37
            public 5+0%2Fdocs%2Fapi+Object">Object invoke(5+0%2Fdocs%2Fapi+Object">Object self, 1.5.0/docs/api/java/lang/reflect/Method.html">Method thisMethod, 1.5.0/docs/api/java/lang/reflect/Method.html">Method proceed, 5+0%2Fdocs%2Fapi+Object">Object[] args) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable {
38
                1.5.0/docs/api/java/lang/System.html">System.out.println("Handling " + thisMethod + " via the method handler");
39
                if(thisMethod.getName().startsWith("getId"))
40
                {
41
                    return proceed.invoke(self,args);
42
                }
43
                else if(thisMethod.getName().startsWith("get"))
44
                {
45
                    1.5.0/docs/api/java/lang/System.html">System.out.println("Acedendo a uma propriedade " + thisMethod.getName() + " na class " + thisMethod.getDeclaringClass().getName());
46
                    1.5.0/docs/api/java/lang/String.html">String campo  = thisMethod.getName().substring(3);
47
                    campo = ("" + campo.charAt(0)).toLowerCase() + campo.substring(1);
48
                    1.5.0/docs/api/java/lang/System.html">System.out.println("select " + campo + " from " + thisMethod.getDeclaringClass().getCanonicalName() + " where id = " +
1353 jmachado 49
                            self.getClass().getMethod("getId").invoke(self, new 5+0%2Fdocs%2Fapi+Object">Object[]{}));
1351 jmachado 50
                }
51
                if(thisMethod.getName().startsWith("set"))
52
                {
53
                    1.5.0/docs/api/java/lang/System.html">System.out.println("Alterando uma propriedade no proxy " + thisMethod.getName() + " na class " + thisMethod.getDeclaringClass().getName());
54
                   return proceed.invoke(self,args);
55
                }
56
                return null;
57
            }
58
        };
59
 
1353 jmachado 60
        Course u = (Course) factory.create(new 1.5.0/docs/api/java/lang/Class.html">Class[0], new 5+0%2Fdocs%2Fapi+Object">Object[0], handler);
1351 jmachado 61
        u.setId(3);
62
 
1353 jmachado 63
        u.getName();
1692 jmachado 64
        u.getDepartment().getCourseSchool().getInstitutionalCode();
1351 jmachado 65
        u.getId();
66
 
67
    }
68
}