Skip to content

Commit

Permalink
Add java-dynamic-proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
Trinea committed Jan 31, 2015
1 parent 40ebd92 commit 604d7d2
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 0 deletions.
45 changes: 45 additions & 0 deletions java-dynamic-proxy/src/com/codekk/java/test/dynamicproxy/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.codekk.java.test.dynamicproxy;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import com.codekk.java.test.dynamicproxy.util.ProxyUtils;

/**
* Dynamic Proxy Demo
*
* @author [email protected]
*/
public class Main {
public static void main(String[] args) {

TimingInvocationHandler timingInvocationHandler = new TimingInvocationHandler(new OperateImpl());
Operate operate = (Operate)(Proxy.newProxyInstance(Operate.class.getClassLoader(), new Class[] {Operate.class},
timingInvocationHandler));

// call proxy instance method
operate.operateMethod1();
System.out.println();
operate.operateMethod2();
System.out.println();
operate.operateMethod3();

// print info of proxy class
System.out.println("proxy class is: " + operate.getClass().getName());
System.out.println("\r\nsuper class of proxy class is: " + operate.getClass().getSuperclass().getName());
System.out.println("\r\ninterfaces of proxy class are: ");
for (Class inter : operate.getClass().getInterfaces()) {
System.out.println("\t" + inter.getName());
}
System.out.println("\r\nmethods of proxy class are: ");
for (Method method : operate.getClass().getMethods()) {
System.out.println("\t" + method.getName());
}

// save proxy class to root of this project, you can use jd-gui to see content of the saved file
String saveFileName = "$Proxy0.class";
ProxyUtils.saveProxyClass(saveFileName, operate.getClass().getSimpleName(), operate.getClass().getInterfaces());
System.out.println("\r\nContent of " + operate.getClass().getSimpleName() + ".class has saved to file "
+ saveFileName + " at root of this project");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.codekk.java.test.dynamicproxy;

/**
* Operate Interface, Proxy Class need to implements this
*
* @author [email protected]
*/
public interface Operate {

public void operateMethod1();

public void operateMethod2();

public void operateMethod3();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.codekk.java.test.dynamicproxy;

/**
* Proxy class, implements {@link Operate}
*
* @author [email protected]
*/
public class OperateImpl implements Operate {

@Override
public void operateMethod1() {
System.out.println("Invoke operateMethod1");
sleep(110);
}

@Override
public void operateMethod2() {
System.out.println("Invoke operateMethod2");
sleep(120);
}

@Override
public void operateMethod3() {
System.out.println("Invoke operateMethod3");
sleep(130);
}

private static void sleep(long millSeconds) {
try {
Thread.sleep(millSeconds);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.codekk.java.test.dynamicproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
* InvocationHandler to timing proxy class
*
* @author [email protected]
*/
public class TimingInvocationHandler implements InvocationHandler {

private Object target;

public TimingInvocationHandler() {}

public TimingInvocationHandler(Object target) {
this.target = target;
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
long start = System.currentTimeMillis();
Object obj = method.invoke(target, args);
System.out.println(method.getName() + " cost time is:" + (System.currentTimeMillis() - start));
return obj;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.codekk.java.test.dynamicproxy.util;

import java.io.FileOutputStream;
import java.io.IOException;

import sun.misc.ProxyGenerator;

/**
* Utils of proxy
*
* @author [email protected]
* @thx to <a href="http://rejoy.iteye.com/blog/1627405">Rejoy's Blog</a>
*/
public class ProxyUtils {

/**
* Save proxy class to path
*
* @param path path to save proxy class
* @param proxyClassName name of proxy class
* @param interfaces interfaces of proxy class
* @return
*/
public static boolean saveProxyClass(String path, String proxyClassName, Class[] interfaces) {
if (proxyClassName == null || path == null) {
return false;
}

// get byte of proxy class
byte[] classFile = ProxyGenerator.generateProxyClass(proxyClassName, interfaces);
FileOutputStream out = null;
try {
out = new FileOutputStream(path);
out.write(classFile);
out.flush();
return true;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
}

0 comments on commit 604d7d2

Please sign in to comment.