Java Dynamic Agent Code

Source: Internet
Author: User

Java Dynamic Agent code recently learned Java agent, it feels very magical, especially dynamic agent, the function is really powerful, if there is a dynamic agent, then the hacker is not a more development step ... Implementation of Java Dynamic Agent, the main implementation of several steps
1. Implement the Invocationhandler interface, and then implement the public object invoke (object proxy, Method M, object[] args) method, the code is as follows
Package Com.chapter1.dynamicproxy;import Java.lang.reflect.invocationhandler;import Java.lang.reflect.Method; public class Proxyhandler implements Invocationhandler{private object Proxied;public Proxyhandler (object proxied) { this.proxied = proxied;} @Overridepublic object Invoke (object proxy, Method m, object[] args) throws Throwable {//TODO auto-generated Method Stubsy Stem.out.println ("before"); M.invoke (this.proxied, args); return null;}}

2. Application steps
Package Com.chapter1.dynamicproxy;import Java.lang.reflect.proxy;public class Test {public static void main (string[] args) {Realcoder rc = new Realcoder (); Proxyhandler ph = new Proxyhandler (RC); Coder PROXYC = (coder) proxy.newproxyinstance (Rc.getclass (). getClassLoader (), Rc.getclass (). Getinterfaces (), ph); Proxyc.coding ();}}

RC is the object that is being proxied, the ph is the object that you create, and then you create the proxy instance with a proxy and then call the method.

Java Dynamic Agent Code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.