forked from android-cn/android-open-project-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
java-dynamic-proxy/src/com/codekk/java/test/dynamicproxy/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
java-dynamic-proxy/src/com/codekk/java/test/dynamicproxy/Operate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
35 changes: 35 additions & 0 deletions
35
java-dynamic-proxy/src/com/codekk/java/test/dynamicproxy/OperateImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
java-dynamic-proxy/src/com/codekk/java/test/dynamicproxy/TimingInvocationHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
java-dynamic-proxy/src/com/codekk/java/test/dynamicproxy/util/ProxyUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |