Null objects + cglib2 (Author: cunzhi)

Source: Internet
Author: User

I added the null objects implementing concrete class, which is implemented using cglib2 (so if you use it in the project, you need to download cglib. Jar ). Take a look at the source code, pay attention to the bold part (Note: constructor can be declared in pojo. If constructors exists, it must be declared that the constructor without any parameters by default.):

Null. Java

---------------------

Import java. Lang. Reflect. array;

Import java. Lang. Reflect. invocationhandler;

Import java. Lang. Reflect. method;

Import java. Lang. Reflect. proxy;

Import java. util. hashmap;

Import java. util. Map;

Import net. SF. cglib. Proxy. enhancer;

Import net. SF. cglib. Proxy. methodinterceptor;

Import net. SF. cglib. Proxy. methodproxy;

Public class null implements invocationhandler,Methodinterceptor{

Private map resultvaluesbytype = new hashmap ();

Public null (){

Createdefaultresults ();

}

Public static object (class clazz ){

If (clazz. isinterface () // Interface

Return proxy. newproxyinstance (clazz. getclassloader (),

New Class [] {clazz}, new null ());

Else // concrete class

Return enhancer. Create (clazz, new null ());

}

// implement invocationhandler
Public object invoke (Object proxy, method, object [] ARGs)
throws throwable {
class returntype = method. getreturntype ();

If (resultvaluesbytype. containskey (returntype) {
return resultvaluesbytype. get (returntype);
} else if (returntype. isarray () {
return array. newinstance (returntype. getcomponenttype (), 0);
} else if (returntype. isinterface () {
return null. object (returntype);
} else {
return null. object (returntype);

}< br>
}

// Implement methodinterceptor

 Public object intercept (Object thisproxy, method, object [] ARGs,

Methodproxy superproxy) throws throwable {

Return invoke (thisproxy, method, argS );

}

Public void addresult (class resulttype, object resultvalue ){

Resultvaluesbytype. Put (resulttype, resultvalue );

}

Protected void createdefaultresults (){

Addresult (Boolean. Class, Boolean. False );

Addresult (void. Class, null );

Addresult (byte. Class, new byte (byte) 0 ));

Addresult (short. Class, new short (short) 0 ));

Addresult (Int. Class, new INTEGER (0 ));

Addresult (long. Class, new long (0l ));

Addresult (char. Class, new character ('\ 0 '));

Addresult (float. Class, new float (0.0f ));

Addresult (double. Class, new double (0.0 ));

Addresult (Boolean. Class, Boolean. False );

Addresult (byte. Class, new byte (byte) 0 ));

Addresult (short. Class, new short (short) 0 ));

Addresult (integer. Class, new INTEGER (0 ));

Addresult (long. Class, new long (0l ));

Addresult (character. Class, new character ('\ 0 '));

Addresult (float. Class, new float (0.0f ));

Addresult (double. Class, new double (0.0 ));

Addresult (string. class ,"");

}

}

Cunzhi Article History, gains and losses (author)

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.