Subversion Repositories bacoAlunos

Rev

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

package jomm.experiments;

import javassist.util.proxy.MethodFilter;
import javassist.util.proxy.MethodHandler;
import javassist.util.proxy.ProxyFactory;
import pt.estgp.estgweb.domain.User;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by jorgemachado on 12/04/16.
 */

public class JavaAssistDynamicProxy
{
    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 {


        ProxyFactory factory = new ProxyFactory();
        factory.setSuperclass(User.class);
        factory.setFilter(
                new MethodFilter() {
                    @1.5.0/docs/api/java/lang/Override.html">Override
                    public boolean isHandled(1.5.0/docs/api/java/lang/reflect/Method.html">Method method) {
                        return true;
                        //return Modifier.isAbstract(method.getModifiers());
                    }
                }
        );


        MethodHandler handler = new MethodHandler() {
            @1.5.0/docs/api/java/lang/Override.html">Override
            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 {
                1.5.0/docs/api/java/lang/System.html">System.out.println("Handling " + thisMethod + " via the method handler");
                if(thisMethod.getName().startsWith("getId"))
                {
                    return proceed.invoke(self,args);
                }
                else if(thisMethod.getName().startsWith("get"))
                {
                    1.5.0/docs/api/java/lang/System.html">System.out.println("Acedendo a uma propriedade " + thisMethod.getName() + " na class " + thisMethod.getDeclaringClass().getName());
                    1.5.0/docs/api/java/lang/String.html">String campo  = thisMethod.getName().substring(3);
                    campo = ("" + campo.charAt(0)).toLowerCase() + campo.substring(1);
                    1.5.0/docs/api/java/lang/System.html">System.out.println("select " + campo + " from " + thisMethod.getDeclaringClass().getCanonicalName() + " where id = " +
                            self.getClass().getMethod("getId").invoke(self,new 5+0%2Fdocs%2Fapi+Object">Object[]{}));
                }
                if(thisMethod.getName().startsWith("set"))
                {
                    1.5.0/docs/api/java/lang/System.html">System.out.println("Alterando uma propriedade no proxy " + thisMethod.getName() + " na class " + thisMethod.getDeclaringClass().getName());
                   return proceed.invoke(self,args);
                }
                return null;
            }
        };

        User u = (User) factory.create(new 1.5.0/docs/api/java/lang/Class.html">Class[0], new 5+0%2Fdocs%2Fapi+Object">Object[0], handler);
        u.setId(3);

        u.getUsername();
        u.getCourses();
        u.getId();

    }
}