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 ,"");
}
} |