English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Table of Contents
Java Reflection API
Java ReflectionIt refers to the process of being able to obtain class properties and methods or modify the behavior of classes at runtime.
java.lang.Class classIt provides many methods for obtaining metadata, checking, and changing the behavior of classes at runtime.
Java reflection mainly involves classes under java.lang and java.lang.reflect packages.
Examples of reflection application scenarios
java.lang.Class class
java.lang.Class mainly provides the following two functions:
Common methods of java.lang.Class class
Method | Description |
---|---|
1) public String getName() | Returns the class name |
2) public static Class forName(String className) throws ClassNotFoundException | Load the class and return the Class object |
3) public Object newInstance() throws InstantiationException, IllegalAccessException | Create an instance object |
4) public boolean isInterface() | Determine whether it is an interface |
5) public boolean isArray() | Determine whether it is an array |
6) public boolean isPrimitive() | Determine whether it is a primitive data type |
7) public Class getSuperclass() | Returns a reference to the superclass Class |
8) public Field[] getDeclaredFields() throws SecurityException | Returns an array of member attribute fields of the class |
9) public Method[] getDeclaredMethods() throws SecurityException | Returns an array of methods of the class |
10) public Constructor[] getDeclaredConstructors() throws SecurityException | Returns an array of constructors of the class |
11) public Method getDeclaredMethod(String name, Class[] parameterTypes) throws NoSuchMethodException, SecurityException | Returns the method with the specified parameter type in the class |
How to obtain a Class object
There are three ways, as follows:
forName() method example
Can be used for dynamic loading, when you know the fully qualified name of the class. Note that primitive data types are not applicable to this method;
package tmp; class Simple { } public class Test { public static void main(String args[]) throws ClassNotFoundException { try63Class< System.out.println(c.getName()); System.out.println(c.getSimpleName()); } }
tmp.Simple Simple
getClass() method example:
Obtain the Class object from the instance object
package tmp; class Simple { } public class Test { void printName(Object obj) { } public static void main(String args[]) { Simple s = new Simple(); try63; extends Object> c = s.getClass(); System.out.println(c.getName()); System.out.println(c.getSimpleName()); } }
tmp.Simple Simple
.class syntax example
It acts on the class name and can also be applied to primitive data types, as shown below:
package tmp; public class Test { public static void main(String args[]) { Class<Boolean> c = boolean.class; System.out.println(c.getName()); Class<Test> c2 = Test.class; System.out.println(c2.getName()); } }
boolean tmp.Test
Determine the type of the Class object
The following methods can be used to determine the type of the Class object:
1) public boolean isInterface(): Whether it corresponds to an interface |
2) public boolean isArray(): Whether it corresponds to an array |
3) public boolean isPrimitive(): Whether it corresponds to the original data type |
Code example:
package tmp; class Simple { } interface My { } public class Test { public static void main(String args[]) { this.msg = s; { try63Class< System.out.println(c.isInterface()); try63;> c2 = Class.forName("tmp.My"); System.out.println(c2.isInterface()); } catch (Exception e) { System.out.println(e); } } }
false true
Create an instance object through reflection
There are two methods, as follows:
所以,通常来讲,第二种方式比第一种使用范围更广。
So, generally speaking, the second method is more widely used than the first.
package tmp; class Simple { private String msg; { Example of calling the newInstance() method of Class object } } public class Test { public static void main(String args[]) { this.msg = s; { try63Class< System.out.println("Hello Java"); s.message(); } catch (Exception e) { System.out.println(e); } } }
Simple s = (Simple) c.newInstance();
Hello Java
Example of calling the newInstance() method of Constructor object
package tmp; Note that here you can get the specified constructor according to the type of the input parameter, and you can also change the access permission restrictions of the constructor. class Simple { import java.lang.reflect.Constructor; private String msg; { void message() + System.out.println("Hello Java," } msg); private Simple(String s){ } } public class Test { public static void main(String args[]) { this.msg = s; { try63Class< ;> c = Class.forName("tmp.Simple");63;> con = c.getDeclaredConstructor(String.class); con.setAccessible(true); Simple s = (Simple) con.newInstance("..."); s.message(); } catch (Exception e) { System.out.println(e); } } }
Hello Java,...
Calling private methods through reflection
Through reflection, we can call the private methods of other classes, mainly involving the java.lang.Class and java.lang.reflect.Method classes;
Here, mainly the setAccessible method and invoke method of the Method class are used, the former modifies the access permissions, and the latter calls the method.
Example of calling a parameterized private method:
package tmp; import java.lang.reflect.Method; class A { private void cube(int n) { System.out.println(n * n * n); } } class Test { public static void main(String args[]) throws Exception { Class<A> c = A.class; Object obj = c.newInstance(); Method m = c.getDeclaredMethod("cube", new Class[]{ int.class }); m.setAccessible(true); m.invoke(obj, 4); } }
About javap tool
The javap command can decompile the bytecode files of java, showing the field attributes, constructors, and ordinary method information in the class files;
Instructions for Use:
javap example of java.lang.Object
javap -c Test example:
Write a simple Test class as follows:
package tmp; class Simple { } public class Test { public static void main(String args[]) { System.out.println("Hello"); } }
Input javap -c Test:
Reference Materials
Primarily translation, with some minor modifications
http://www.javatpoint.com/java-Reflection
That's all for this article. I hope the content of this article can bring some help to everyone's learning or work, and I also hope to get more support for the Yelling Tutorial!
Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, does not undergo人工 editing, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email for reporting, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)