Treemap-use reflection to get the tree structure in treemap, that is, get the root

Source: Internet
Author: User

If you want to get the member attribute of an object, the member type is package-level private internal class, and the member is private, how can you get this member?

Ideas:

Suppose we want to study treemap, and we want to get the root of the treemap object.

First, treemap. Class.Getdeclaredclasses () to get all internal classesFor example, if root is an internal class entry, that is, clazz = treemap. Class. getdeclaredclasses () [3];, the internal class type can be obtained.

2. Obtain the private member variable field rootf =Treemap. Class. getdeclaredfield ("root ");Because it is private, you need to set Access PermissionsRootf. setaccessible (true );

Rootf is only a field type in treemap. It has no relationship with the object we created. We need to associate it with the physical object.Object root = rootf. Get (tree)(Tree is a treemap object)

3. We have all types and variables, so it is easy to access the attributes of the type,

Next we wantReflection entry typeWhat is in it?MemberAnd then useBind filed to rootYou can get the member value in root.

Entrys = clazz. getdeclaredfields ();
For (field F: entrys ){
F. setaccessible (true );
}

That isEntrys [0]. Get (Root)You can get the root key value, and entrys [2]. Get (Root) will get the root left child.

This method is written during the study of building sequences in treemap, that is, the buildfromsorted method in treemap.

At the beginning, I didn't know how it was established, so I wanted to get the node of the tree. Because the entry root is private, and the internal class entry is package-level private, it cannot be accessed.

Because reflection can only be used, and the access level of members can be set. For more information, see annotations.

Package trees; import java. Lang. Reflect. Field; public class treemapreflectroot {static treemap <integer, integer> tree; static sortedmap <integer, integer> Fill; static class <?> Clazz; // use getdeclaredclasses to obtain the inner Question class entrystatic field rootf in treemap; // use getdeclaredfield to obtain the root member static object root in treemap; // obtain the rootf. get (tree) to get the root rootstatic field [] entrys; // Private Static void Init () throws exception {tree = new treemap <integer, integer> (); fill = new treemap <integer, integer> (); fill. put (1, 1); fill. put (2, 2); fill. put (3, 3); fill. put (4, 4); fill. put (5, 5); fil L. put (6, 6); fill. put (7, 7); fill. put (8, 8); fill. put (9,9); fill. put (10, 10); tree. putall (fill); clazz = treemap. class. getdeclaredclasses () [3]; // get entry classrootf = treemap. class. getdeclaredfield ("root"); // get field root; rootf. setaccessible (true); entrys = clazz. getdeclaredfields (); // get entry inner field; For (field F: entrys) {f. setaccessible (true);} root = rootf. get (tree);} public static object key (obje Ct obj) throws exception {return entrys [0]. get (OBJ);} public static object value (Object OBJ) throws exception {return entrys [1]. get (OBJ);} public static object left (Object OBJ) throws exception {return entrys [2]. get (OBJ);} public static object right (Object OBJ) throws exception {return entrys [3]. get (OBJ);} public static object parent (Object OBJ) throws exception {return entrys [4]. get (OBJ);} public static object Color (Object OBJ) throws exception {return (Boolean) entrys [5]. Get (OBJ) = true? "Black": "red";} public static void main (string ARGs []) throws exception {Init (); system. out. println (color (right (left (Root ))));}}

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.