Docsity

Reflection in Java: Understanding Objects, Classes, and the Reflection API, Slides of Java Programming

Partial preview of the text

Download Reflection in Java: Understanding Objects, Classes, and the Reflection API and more Slides Java Programming in PDF only on Docsity!

Reflection Overview

` Reflection API

` The “Class" class

` Class methods

` Array class

` Member interface

` Method class

` invoking a method, throwing exceptions

` Field class

` accessible objects

docsity.com

Background

` Programs are just another kind of data

` Source code is text

` Manipulate it line by line, or by parsing expressions

` Compiled programs are data, too

` Integers and strings are bytes in memory that you

interpret a certain way

` Instructions in methods are just bytes too

` No reason why a program can't inspect itself

docsity.com

Defining Reflection

` Introduced in Java 1.1. Called the "Java Core Reflection

API"API

` 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

` Allows you to find out information about any object,

including its methods and fields, at run time.

` Can no longer get away with this called an enabling

technology because it supports Java language elements

such as:

` Java Beans, Serialization, and Remote Method Invocation

5

(RMI).

` JBoss, Tomcat, Eclipse, etc. are reflection-based

docsity.com

Reflection Can be Used To...

` construct new class instances and new arrays

` access and modify fields of objects and classes

` invoke methods on objects and classes

` access and modify elements of arraysy y

docsity.com

Drawbacks of Reflection

` Reflection is powerful, but should not be used

i diindiscriminately. i i t l

` If it is possible to perform an operation without using reflection, then it is preferable to avoid using it.

` Performance Overhead

Because reflection involves types that are dynamically resolved,yp y y , certain Java virtual machine optimizations can not be performed. Slower performance than their non-reflective counterparts, ` should be avoided in sections of code which are called frequently in performance-sensitive applications.

`` Security RestrictionsSecurity Restrictions

` Reflection requires a runtime permission which may not be present when running under a security manager.

Code which has to run in a restricted security context, such as in an Applet.

docsity.com

Drawbacks of Reflection

` Exposure of Internals

` Since reflection allows code to perform operations that would be illegal in non-reflective code,

Accessing private fields and methods, the use of reflection can result in unexpected side-effects,

Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.h b h i ith d f th l tf

docsity.com

Running Example

` Most typical use of reflection:

T kTake a class name, make a l k **ClCl ass** objectbj t Create object of that class, cast and use it

**1. String className = ...;

  1. Class c = Class.forName(className);
  2. Object o = c.newInstance(); 4 T t (T)**

new T 1 (); new T ();

` Statically convert

4. T t = (T) o; new^ T 2 (); ...

Class.newInstance Æ new T()

docsity.com

Reflection API

` Package java.lang.reflect

With the exception of java.lang.reflect.ReflectPermission, none of the classes in java.lang.reflect have public

` Field class

` get name and access for an arbitrary field

` Method class

` get info about an arbitrary method (and invoke it)

` Constructor class

constructors.

` Constructor class

` get info about an arbitrary constructor (and invoke it)

` Class class

` creates new instances of Field, Method, and Constructor

` Array class

12

ay c ass

` static methods to create and get info about arbitrary arrays

` …..

docsity.com

Obtaining a Class Object

` To get to these classes, it is necessary to invoke

appropriate methods on Class.

1.1. At compile time, using the symbolic class name:At compile time, using the symbolic class name:

Class c1 = String.class; Class c2 = Employee[].class;

222 2.. At runtime by callingAt runtime by callingAt runtime, by callingAt runtime, by calling getClass( )getClass( )getClass(getClass( ) ) on any object:on any object:onon any object: any object:

Class c3 = obj.getClass( );

3.3. At runtime, by passing a class name (string) to theAt runtime, by passing a class name (string) to the

forNameforName( ) static method:( ) static method:

Class c = Class.forName( "java.util.Date" );

14

docsity.com