Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Java Platform, Standard Edition
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==== java.lang.reflect ==== [[Reflection (computer science)|Reflection]] is a constituent of the [[Java (programming language)|Java]] API that lets Java code examine and "reflect" on Java components at runtime and use the reflected members. Classes in the {{Javadoc:SE|package=java.lang.reflect|java/lang/reflect}} package, along with <code>java.lang.Class</code> and {{Javadoc:SE|package=java.lang|java/lang|Package}} accommodate applications such as [[debugger]]s, [[interpreter (computing)|interpreters]], object inspectors, [[class browser]]s, and services such as object [[serialization]] and [[JavaBeans]] that need access to either the public members of a target object (based on its runtime class) or the members declared by a given class. This package was added in JDK 1.1. Reflection is used to instantiate classes and invoke methods using their names, a concept that allows for dynamic programming. Classes, interfaces, methods, [[field (computer science)|fields]], and [[constructor (computer science)|constructor]]s can all be discovered and used at runtime. Reflection is supported by [[metadata]] that the JVM has about the program. ===== Techniques ===== There are basic techniques involved in reflection: * Discovery β this involves taking an object or class and discovering the members, superclasses, implemented interfaces, and then possibly using the discovered elements. * Use by name β involves starting with the symbolic name of an element and using the named element. ====== Discovery ====== Discovery typically starts with an object and calling the {{Javadoc:SE|java/lang|Object|getClass()}} method to get the object's <code>Class</code>. The <code>Class</code> object has several methods for discovering the contents of the class, for example: * {{Javadoc:SE|name=getMethods()|java/lang|Class|getMethods()}} β returns an array of {{Javadoc:SE|java/lang/reflect|Method}} objects representing all the public methods of the class or interface * {{Javadoc:SE|name=getConstructors()|java/lang|Class|getConstructors()}} β returns an array of {{Javadoc:SE|java/lang/reflect|Constructor}} objects representing all the public constructors of the class * {{Javadoc:SE|name=getFields()|java/lang|Class|getFields()}} β returns an array of {{Javadoc:SE|java/lang/reflect|Field}} objects representing all the public fields of the class or interface * {{Javadoc:SE|name=getClasses()|java/lang|Class|getClasses()}} β returns an array of <code>Class</code> objects representing all the public classes and interfaces that are members (e.g. [[inner class]]es) of the class or interface * {{Javadoc:SE|name=getSuperclass()|java/lang|Class|getSuperclass()}} β returns the <code>Class</code> object representing the superclass of the class or interface (<code>null</code> is returned for interfaces) * {{Javadoc:SE|name=getInterfaces()|java/lang|Class|getInterfaces()}} β returns an array of <code>Class</code> objects representing all the interfaces that are implemented by the class or interface ====== Use by name ====== The <code>Class</code> object can be obtained either through discovery, by using the ''class literal'' (e.g. <code>MyClass.class</code>) or by using the name of the class (e.g. {{Javadoc:SE|name=Class.forName("mypackage.MyClass")|java/lang|Class|forName(java.lang.String)}}). With a <code>Class</code> object, member <code>Method</code>, <code>Constructor</code>, or <code>Field</code> objects can be obtained using the symbolic name of the member. For example: * {{Javadoc:SE|name=getMethod("methodName", Class...)|java/lang|Class|getMethod(java.lang.String,java.lang.Class...)}} β returns the <code>Method</code> object representing the public method with the name "methodName" of the class or interface that accepts the parameters specified by the <code>Class...</code> parameters. * {{Javadoc:SE|name=getConstructor(Class...)|java/lang|Class|getConstructor(java.lang.Class...)}} β returns the <code>Constructor</code> object representing the public constructor of the class that accepts the parameters specified by the <code>Class...</code> parameters. * {{Javadoc:SE|name=getField("fieldName")|java/lang|Class|getField(java.lang.String)}} β returns the <code>Field</code> object representing the public field with the name "fieldName" of the class or interface. <code>Method</code>, <code>Constructor</code>, and <code>Field</code> objects can be used to dynamically access the represented member of the class. For example: * {{Javadoc:SE|name=Field.get(Object)|java/lang/reflect|Field|get(java.lang.Object)}} β returns an <code>Object</code> containing the value of the field from the instance of the object passed to <code>get()</code>. (If the <code>Field</code> object represents a static field then the <code>Object</code> parameter is ignored and may be <code>null</code>.) * {{Javadoc:SE|name=Method.invoke(Object, Object...)|java/lang/reflect|Method|invoke(java.lang.Object,java.lang.Object...)}} β returns an <code>Object</code> containing the result of invoking the method for the instance of the first <code>Object</code> parameter passed to <code>invoke()</code>. The remaining <code>Object...</code> parameters are passed to the method. (If the <code>Method</code> object represents a [[static method]] then the first <code>Object</code> parameter is ignored and may be <code>null</code>.) * {{Javadoc:SE|name=Constructor.newInstance(Object...)|java/lang/reflect|Constructor|newInstance(java.lang.Object...)}} β returns the new <code>Object</code> instance from invoking the constructor. The <code>Object...</code> parameters are passed to the constructor. (Note that the parameterless constructor for a class can also be invoked by calling {{Javadoc:SE|name=newInstance()|java/lang|Class|newInstance()}}.) ===== Arrays and proxies ===== The <code>java.lang.reflect</code> package also provides an {{Javadoc:SE|java/lang/reflect|Array}} class that contains static methods for creating and manipulating array objects, and since J2SE 1.3, a {{Javadoc:SE|java/lang/reflect|Proxy}} class that supports dynamic creation of proxy classes that implement specified interfaces. The implementation of a <code>Proxy</code> class is provided by a supplied object that implements the {{Javadoc:SE|java/lang/reflect|InvocationHandler}} interface. The <code>InvocationHandler</code>'s {{Javadoc:SE|name=invoke(Object, Method, Object[])|java/lang/reflect|InvocationHandler|invoke(java.lang.Object,java.lang.reflect.Method,java.lang.Object[])}} method is called for each method invoked on the proxy objectβthe first parameter is the proxy object, the second parameter is the <code>Method</code> object representing the method from the interface implemented by the proxy, and the third parameter is the array of parameters passed to the interface method. The <code>invoke()</code> method returns an <code>Object</code> result that contains the result returned to the code that called the proxy interface method.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)