Some time ago, I wrote JSON String ParsingCodeMost of the work is to repeatedly write code with the same logic, and we plan to solve this repetitive process with generics. As a result, we can imagine that problems are everywhere along the way, however, we finally found a solution. The following is a summary. One is to forget it, and the other is to share it.
There are two ways to solve this problem. One seems to be very technical, but the effect is very good, it is very simple, the other is a little higher, and it is also very troublesome, sub-classes must be created continuously during development.
Generic Type
Jsonutil
1 Public ClassJsonutil <t>{2PublicT analytic2object (class <t>Tclass, string Str)3ThrowsInstantiationexception, illegalaccessexception {4ReturnTclass. newinstance ();5 }6}
Data
Testobject
1 Public ClassTestobject {2Public VoidPrintname (){3System. Out. println ("testobject");4 }5}
Test class
Testmain
1 Public Class Testmain { 2 Public Static Void Main (string [] ARGs) Throws Instantiationexception, 3 Illegalaccessexception { 4 Jsonutil <testobject> util = New Jsonutil <testobject> (); 5 Testobject OBJ = util. analytic2object (testobject. Class ,""); 6 OBJ. printname (); 7 } 8 }
After reading this code, you may ask, why should we transfer a class in the method? Why not use the generic t? Why do I still use generics when I want to pass the class in? Below are a few questions to answer:
Why do we need to transfer a class in the method? The reason is very simple. In our tool class, we cannot obtain its Class Based on the T generic type. The reason why we cannot get it is: generics are obtained during compilation and have an erasure mechanism. So I don't know what t is before compilation, and it will be erased after compilation. Is it necessary to use generics? The answer is no need for generics, because after getting the class, all problems are solved, but one problem is that the object returned by your method can only be an object, then, the forced type conversion is performed in the called place.
The code for passing classes without generics is as follows:
Jsonutil
1 Public ClassJsonutil {2PublicObject analytic2object (class tclass, string Str)ThrowsInstantiationexception, illegalaccessexception {3ReturnTclass. newinstance ();4 }5}
- Method for retrieving class through generics:
At this time, you may wonder: Didn't you say you can't get it through generics? How can this problem be solved? Please pay attention to the phrase "in ourIn this tool classThere is no way to obtain the class according to the generic T. "That is to say, we can use another method to obtain the class. Next I will give a general idea of this method:
- Create a generic class to make it a base class or abstract class. Why not use an interface? Because we need to write all the logic code for parsing JSON on this generic class;
- Add the method for getting class through generics;
- Write a non-generic class to inherit this generic class, and clearly write the generic type during integration so that the generic type is known during compilation, the code in step 1 takes effect. If the code in step 2 of the base class is not inherited, an exception may occur. If you do not believe it, try it;
- In our code, it is normal to call the method of the subclass of the generic class.
The biggest drawback of this method is that every time we add a data class to be parsed, we need to add a subclass of this generic class. The advantage is that this subclass does not need to write any code, because the logic is implemented in the generic base class.
Generic base class
Absjsonutil
1 Import Java. Lang. Reflect. parameterizedtype; 2 Import Java. Lang. Reflect. type; 3 4 Public Abstract Class Absjsonutil <t> { 5 Public T gett () Throws Instantiationexception, illegalaccessexception { 6 Type stype = Getclass (). getgenericsuperclass (); 7 Type [] generics = (Parameterizedtype) stype). getactualtypearguments (); 8 Class <t> mtclass = (class <t>) (generics [0 ]); 9 Return Mtclass. newinstance (); 10 } 11 }
Generic subclass
Jsonutilfortestobject
1 Public ClassJsonutilfortestobjectExtendsAbsjsonutil <testobject>{2 3}
Test class
Testmain
1 Public Class Testmain { 2 Public Static Void Main (string [] ARGs)Throws Instantiationexception, 3 Illegalaccessexception { 4 Jsonutilfortestobject go = New Jsonutilfortestobject (); 5 Testobject OBJ = Go. gett (); 6 OBJ. printname (); 7 } 8 }
The data class is the same as the preceding example.