My Java Learning Note (13) about reflection (Part 2)

Source: Internet
Author: User

Woo-hoo ... Today because of the evening something ... Results in the lab without Eclipse, with the command line, the result environment variables are not well-equipped, engaged in a half day. Get it now. Hey..


1. The key method to view the object domain is the Get method in the field class.

Field f = c1.getdeclarefields ("name"); Object value = F.get (obj); Value is the current value of the Name field in obj.


2. The default behavior of the reflection mechanism is limited by Java access control.

3. If a Java program is not under the control of the security manager, access control can be overridden.

The Setaccessible method of the 4.field,method and constructor objects can be accessed over security management.

F.setacceessible (TRUE);

The 5.setAccessible method is a method in the AccessibleObject class, which is a public superclass of the Field,method and constructor classes.

6. You can also set the value of the F field of an Obj object by using the Set method.

F.set (Obj,value);

Related Instance Code

Test class

Import Java.util.arraylist;public class Testee{public static void Main (string[] args) {arraylist<integer> s = new Ar Raylist<> (); for (int i = 1; i <=5; i++) {s.add (i*i);} System.out.println (New Objectanalyzer (). ToString (s));}}

function Class


Import Java.lang.reflect.*;import java.util.*;p ublic class objectanalyzer{private arraylist<object> v = new Arraylist<> ();p ublic String toString (Object obj) {if (obj = = null) return "NULL"; if (V.contains (obj)) return "..."; V.add (obj); Class C1 = Obj.getclass (), if (c1 = = String.class) return (string) obj;if (C1.isarray ()) {string r = C1.getcomponenttype () + "[ ]{"; for (int i = 0; I < array.getlength (obj); i++) {if (i > 0) R + = ","; Object val = Array.get (obj,i); if (C1.getcomponenttype (). Isprimitive ()) R + = Val;elser + = toString (val);} return r + "}";} String r = c1.getname ();d o{r + = "["; field[] fields = C1.getdeclaredfields (); Accessibleobject.setaccessible (fields,true); for (Field f:fields) {if (! Modifier.isstatic (F.getmodifiers ())) {if (!r.endswith ("[")) R + = ","; r + = F.getname () + "="; Try{class t = F.gettype (); O Bject val = f.get (obj), if (T.isprimitive ()) R + = Val;elser + = toString (val);} catch (Exception e) {e.printstacktrace ();}}} R + = "]"; c1 = C1.getsuperclass ();} while (c1! = null); return r;}}

Print Results




Hey.. No eclipse console looks good.




My Java Learning Note (13) about reflection (Part 2)

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.