Java Reflection Get Aggregation property fields __java

Source: Internet
Author: User
Tags addall reflection

You need to dynamically pass in a class type, and then reflect to the list of launches, knowing the various attributes of the list's specific objects, based on the generics defined in the list.

Code

Import Java.lang.reflect.Field;
Import Java.lang.reflect.ParameterizedType;
Import Java.lang.reflect.Type;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import Java.util.HashSet;
Import java.util.List;
Import Java.util.Map;

Import Java.util.Set;

Import Com.meyacom.ruleapp.dto.Ztree; public class Test {public static void main (string[] args) throws ClassNotFoundException {class<?> apply = Clas
		S.forname ("Com.chac.ruleapp.bom.ApplyTest");
		field[] fields = Apply.getfields ();
		Gets all properties list> allfieldlist = getBomFields1 (New ArrayList (), fields);
		for (List list:allfieldlist) {System.out.println (list.tostring ());
		} protected static list> Getbomfields (List chain, field[] fields) {list> result = new arraylist> ();
			for (Field field:fields) {class<?> fieldclass = Field.gettype (); if (Fieldclass.getname (). StartsWith ("java") | | | fieldclass.getname (). StartsWith ("Javax") | | fieldclass.getname ().
				StartsWith ("Com.sun")	|| Fieldclass.getname (). StartsWith ("boolean") | | Fieldclass.getname (). StartsWith ("double") | |
				Fieldclass.getname (). StartsWith ("int")) {List Endchain = new ArrayList (chain);
				Endchain.add (field);
				Result.add (Endchain);
			Continue
				else {List Thischain = new ArrayList (chain);
				Thischain.add (field);
			Result.addall (Getbomfields (New Java.util.ArrayList (Thischain), Fieldclass.getdeclaredfields ()));
	} return result;
		public static list> GetBomFields1 (List chain, field[] fields) {list> result = new arraylist> ();
			for (Field field:fields) {class<?> fieldclass = Field.gettype (); if (fieldclass.isprimitive () | | | fieldclass.getname (). StartsWith ("Java.lang") | | fieldclass.getname (). StartsWith ( "Java.util.Date") | | Fieldclass.getname (). StartsWith ("Javax") | | Fieldclass.getname (). StartsWith ("Com.sun") | | Fieldclass.getname (). StartsWith ("Sun") | | Fieldclass.getname (). StartsWith ("Boolean ") | | Fieldclass.getname (). StartsWith ("double") | |
				Fieldclass.getname (). StartsWith ("int")) {List Endchain = new ArrayList (chain);
				Endchain.add (field);
				Result.add (Endchain);
			Continue else {if (Fieldclass.isassignablefrom (List.class))//"2" {Type FC = Field.getgenerictype ();//Key place, if it is List type, get its generic type if (FC instanceof Parameterizedtype)//"3" If it is the type of generic parameter {parameterizedtype pt = (Param
						Eterizedtype) FC; Class Genericclazz = (Class) pt.getactualtypearguments () [0]; ' 4 ' if (Genericclazz.getname (). StartsWith ("Java.lang")//Set End type of List | | genericclazz.getname (). Startswit H ("Java.util.Date") | | Genericclazz.getname (). StartsWith ("Javax") | | Genericclazz.getname (). StartsWith ("Com.sun") | | Genericclazz.getname (). StartsWith ("Sun") | | Genericclazz.getname (). StartsWith ("boolean") | | Genericclazz.getname (). StartsWith ("double") | | Genericclazz.getname (). Startswith ("int")) {continue;
																}//system.out.println (Genericclazz);
						Gets the class type object in the generic. 
List Thischain = new ArrayList (chain);
						SYSTEM.OUT.PRINTLN (chain); Thischain.add (field);
						//!!
					Result.addall (GETBOMFIELDS1 (New ArrayList (Thischain), Genericclazz.getdeclaredfields ()));
					} else {List thischain = new ArrayList (chain);
					Thischain.add (field);
				Result.addall (GETBOMFIELDS1 (New ArrayList (Thischain), Fieldclass.getdeclaredfields ()));
	}} return result; }
The individual judgments for the list type refer to someone else's code snippet as follows    field[] fs = clazz.getdeclaredfields (); //  Get all fields      for (FIELD&NBSP;F&NBSP;:&NBSP;FS)     {         class fieldclazz = f.gettype (); //  Gets the Class and type full path of field           if (fieldclazz.isprimitive ())   continue;  //"1"  // Determine if the basic type           if (Fieldclazz.getname (). StartsWith ("Java.lang"))  continue; //getname () returns the type full path of the field;          if ( Fieldclazz.isassignablefrom (List.class))  //"2"        {                 Type fc =  F.getgenerictype (); //  key Place, if the list type, get its generic type                &nBsp;    if (fc == null)  continue;                    if (Fc instanceof parameterizedtype)  //  "3" if the type of the generic parameter                  {                        ParameterizedType pt =  (Parameterizedtype)  fc;                           Class genericClazz =  (Class) pt.getactualtypearguments () [0]; //"4"   Gets the class type object in the generic.                           m.put (F.getname (),  genericclazz);      &NBSP;&NBSP;&NBsp;                map< String, class> m1 = preparemap (genericclazz);                          m.putall (M1);                  }           }   }  


"Explanation":
1, Isprimitive

The public boolean isprimitive () determines whether the specified Class object represents a basic type.
There are nine predefined Class objects that represent eight basic types and void. These class objects are created by the Java virtual machine with the same name as the base type they represent, namely Boolean, Byte, char, short, int, long, float, and double. "Note: Packing classes like Integer,boolean are not basic types. 】

These objects can only be accessed by variables declared as public static final by the following, and only a few Class objects that make this method return True.

Return: Returns True if and only if the class represents a base type
Start with the following version: JDK1.1


Reference http://lorry1113.iteye.com/blog/973903 http://www.360doc.com/content/11/1231/14/1954236_176297236.shtml

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.