







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of reflection in java, explaining how objects and classes work, the concept of reflection, and its uses and drawbacks. It covers the reflection api, including the class class, methods, fields, and constructors, and provides examples of reflection in action.
Typology: Slides
1 / 13
This page cannot be seen from the preview
Don't miss anything!








` Class methods
` invoking a method, throwing exceptions
` accessible objects
docsity.com
` Programs are just another kind of data
` Compiled programs are data, too
` No reason why a program can't inspect itself
docsity.com
` Java Beans, Serialization, and Remote Method Invocation
5
` JBoss, Tomcat, Eclipse, etc. are reflection-based
docsity.com
` 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
` If it is possible to perform an operation without using reflection, then it is preferable to avoid using it.
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.
` 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
` 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
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 = ...;
new T 1 (); new T ();
4. T t = (T) o; new^ T 2 (); ...
docsity.com
` Package java.lang.reflect
With the exception of java.lang.reflect.ReflectPermission, none of the classes in java.lang.reflect have public
` get name and access for an arbitrary field
` get info about an arbitrary method (and invoke it)
constructors.
` get info about an arbitrary constructor (and invoke it)
` creates new instances of Field, Method, and Constructor
12
` static methods to create and get info about arbitrary arrays
docsity.com
` To get to these classes, it is necessary to invoke
appropriate methods on Class.
Class c1 = String.class; Class c2 = Employee[].class;
Class c3 = obj.getClass( );
Class c = Class.forName( "java.util.Date" );
14
docsity.com