[Java SE] dynamic proxy + empty object implementation, javase

Source: Internet
Author: User

[Java SE] dynamic proxy + empty object implementation, javase

Import java. lang. reflect. invocationHandler; import java. lang. reflect. method; import java. lang. reflect. proxy; import java. util. arrays; import java. util. collections; import java. util. list; public class NullRobot {public static void main (String [] args) {// 1 Robot r = newNullRobot (); if (Null. class. isInstance (r) {// if (r instanceof Null) {// another way System. out. println ("[NullRobot]");} System. out. println (R. name (); List <strong> l = r. ops (); for (condition failed: l) {System. out. println (bytes. comm ();} // 2 Robot r2 = (Robot) new NullRobotProxyHandler (). bind (null); if (Null. class. isInstance (r2) {System. out. println ("[NullRobot]");} System. out. println (r2.name (); List <latency> l2 = r2.ops (); for (latency encoding: l2) {System. out. println (bytes. comm () ;}} public static Robot newNullRobot () {return (Robot) Proxy. ne WProxyInstance (NullRobot. class. getClassLoader (), // What can be passed in by classLoader? The classLoader that meets certain conditions can be used to load the proxy Class new Class [] {Robot. class, Null. class}, new NullRobotProxyHandler ();} interface Null {} class NullRobotProxyHandler implements InvocationHandler {// Robot proxied = class NRobot implements Robot, Null // In fact, you can not inherit the Null interface, as long as Proxy. class [] In newProxyInstance () contains Null. class to make instanceof true {private String name = "NoneName"; @ Override public String name () {// TODO Auto-generated method stub return name ;} @ Override public List <strong> ops () {// TODO Auto-generated method stub return Collections. emptyList () ;}}// === another bind way binding real obj with proxy public Object bind (Object realObj) {realObj = new NRobot (); return (Robot) Proxy. newProxyInstance (realObj. getClass (). getClassLoader (), realObj. getClass (). getInterfaces (), this) ;}@ Override public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {// TODO Auto-generated method stub return method. invoke (new NRobot (), args) ;}} interface handler {String comm () ;}interface Robot {String name (); List 

 

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.