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)