0% found this document useful (0 votes)
72 views6 pages

Reflections: Obj Instanceof Dog Dog D Dog Obj D

The document discusses Java reflection and introspection. It defines introspection as examining an object's type or properties at runtime, while reflection is examining and modifying an object's structure and behavior at runtime. Introspection is a subset of reflection. It provides examples of using reflection to examine classes, constructors, methods, and fields at runtime. Key reflection classes like Class are used to obtain runtime information about classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views6 pages

Reflections: Obj Instanceof Dog Dog D Dog Obj D

The document discusses Java reflection and introspection. It defines introspection as examining an object's type or properties at runtime, while reflection is examining and modifying an object's structure and behavior at runtime. Introspection is a subset of reflection. It provides examples of using reflection to examine classes, constructors, methods, and fields at runtime. Key reflection classes like Class are used to obtain runtime information about classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Reflections

Reflection is commonly used by programs which require the ability to


examine or modify the runtime behavior of applications running in the Java
virtual machine. This concept is often mixed with introspection.
1.

Introspection is the ability of a program to examine the type or


properties of an object at runtime.
2.
Reflection is the ability of a program to examine and modify the
structure and behavior of an object at runtime. Introspection is the
subset of reflections.

Introspection Example: The instanceof operator determines whether an


object belongs to a particular class.
if(obj instanceof Dog){
Dog d = (Dog)obj;
d.bark();
}
Reflection Example: The Class.forName() method returns the Class object
associated with the class/interface with the given name(a string and full
qualified name). The forName method causes the class with the name to be
initialized.

Class<?> c = Class.forName("classpath.and.classname");
Object dog = c.newInstance();
Method m = c.getDeclaredMethod("bark", new Class<?>[0]);
m.invoke(dog);

In Java, reflection is more about introspection, because you can not change
structure of an object. There are some APIs to change accessibilities of
methods and fields, but not structures.
Reflection enables us to:

Examine an object's class at runtime

Construct an object for a class at runtime

Examine a class's field and method at runtime

Invoke any method of an object at runtime

Change accessibility flag of Constructor, Method and Field

Reflection Classes:
Using Java Reflection you can inspect Java classes at runtime. Inspecting
classes is often the first thing you do when using Reflection. From the classes
you can obtain information about

Class Name

Class Modifies (public, private, synchronized etc.)

Package Info

Super class

Implemented Interfaces

Constructors

Methods

Fields

Annotations

plus a lot more information related to Java classes. For a full list you should
consult the JavaDoc for java.lang.Class. This text will briefly touch upon all
accessing of the above mentioned information. Some of the topics will also

be examined in greater detail in separate texts. For instance, this text will
show you how to obtain all methods or a specific method, but a separate text
will show you how to invoke that method, how to find the method matching a
given set of arguments if more than one method exists with the same name,
what exceptions are thrown from method invocation via reflection, how to
spot a getter/setter etc. The purpose of this text is primarily to introduce
the Class object and the information you can obtain from it.

The Class Object


Before you can do any inspection on a class you need to obtain
its java.lang.Class object. All types in Java including the primitive types (int,
long, float etc.) including arrays have an associated Class object. If you know
the name of the class at compile time you can obtain a Class object like this:
Class myObjectClass = MyObject.class
If you don't know the name at compile time, but have the class name as a
string at runtime, you can do like this:
String className = ... //obtain class name as string at runtime Class class =
Class.forName(className);
When using the Class.forName() method you must supply the fully qualified
class name. That is the class name including all package names. For
instance, if MyObject is located in package com.jenkov.myapp then the fully
qualified class name is com.jenkov.myapp.MyObject
The Class.forName() method may throw a ClassNotFoundException if the
class cannot be found on the classpath at runtime.
Class Name
From a Class object you can obtain its name in two versions. The fully
qualified class name (including package name) is obtained using
the getName() method like this:
Class aClass = ... //obtain Class object. See prev. section

String className = aClass.getName();


If you want the class name without the pacakge name you can obtain it using
the getSimpleName() method, like this:
Class aClass

= ... //obtain Class object. See prev. section

String simpleClassName = aClass.getSimpleName();

Superclass
From the Class object you can access the superclass of the class. Here is
how:
Class superclass = aClass.getSuperclass();
The superclass class object is a Class object like any other, so you can
continue doing class reflection on that too.
Implemented Interfaces
It is possible to get a list of the interfaces implemented by a given class.
Here is how:
Class aClass = ... //obtain Class object. See prev. section
Class[] interfaces = aClass.getInterfaces();
A class can implement many interfaces. Therefore an array of Class is
returned. Interfaces are also represented byClass objects in Java Reflection.
NOTE: Only the interfaces specifically declared implemented by a given class
is returned. If a superclass of the class implements an interface, but the class
doesn't specifically state that it also implements that interface, that interface
will not be returned in the array. Even if the class in practice implements that
interface, because the superclass does.
To get a complete list of the interfaces implemented by a given class you will
have to consult both the class and its superclasses recursively.

Constructors
You can access the constructors of a class like this:
Constructor[] constructors = aClass.getConstructors();
Constructors are covered in more detail in the text on Constructors.

Methods
You can access the methods of a class like this:
Method[] method = aClass.getMethods();
Methods are covered in more detail in the text on Methods.
Constructor Parameters
You can read what parameters a given constructor takes like this:
Constructor constructor = ... // obtain constructor - see above
Class[] parameterTypes = constructor.getParameterTypes();
Method Parameters and Return Types
You can read what parameters a given method takes like this:
Method method = ... // obtain method - see above
Class[] parameterTypes = method.getParameterTypes();
You can access the return type of a method like this:
Method method = ... // obtain method - see above

Class returnType = method.getReturnType();


Invoking Methods using Method Object
You can invoke a method like this:
//get method that takes a String as argument
Method method = MyObject.class.getMethod("doSomething", String.class);

Object returnValue = method.invoke(null, "parameter-value1");


The null parameter is the object you want to invoke the method on. If the
method is static you supply null instead of an object instance. In this
example, if doSomething(String.class) is not static, you need to supply a
validMyObject instance instead of null;
The Method.invoke(Object target, Object ... parameters) method takes an
optional amount of parameters, but you must supply exactly one parameter
per argument in the method you are invoking. In this case it was a method
taking a String, so one String must be supplied.

You might also like