Title: Java Reflection and Beans
1Java Reflection and Beans
- Andrew A. Chien
- achien_at_cs.uiuc.edu
- High Performance Distributed Objects
- Concurrent Systems Architecture Group
- Course WWW http//www-csag.cs.uiuc.edu/achien/cs4
91-f97/
2Outline
- Java Reflection APIs
- What is reflection?
- What is it good for?
- Security, Behavior, Typing and other issues
- Java Beans
- Goals
- Elements Standardized by Beans
- Relation to other Software Architectures
3Java Reflection APIs
Find Next Procedure
Execute Procedure
- What is reflection?
- The capability to look at the model or
implementation of the system, from within the
system. - The capability to manipulate the model or
implementation, from within the system.
4A Powerful form of Reflection
Program Counter
Memory
Reg 1
Reg 2
Reg 3
Reg 4
- Basic computing engine
- Reflection Modify address interpretation, trap
particular addresses when accessed. - Can be used to implement a wealth of selective
semantic manipulations. - Trap 640K and below addresses, and emulate
- Trap remote addresses, implement RMI
5What information needed?
- Execution model -- addresses, address
interpretation, basic operation - And then change the definition of the machine
interpreter in order to implement the address
checking - Is this type of information available in a
typical system? - Scientific Fortran code, does it know about the
machine? Fortran execution model? - C program, does it know about method
resolution, scoping rules? - Most systems are not self aware, much less able
to modify their own definitions.
6Powerful Reflection (continued)
- Modify semantics of a programming language
- Change from lexical to dynamic scoping
- Change resolution order for ambiguous multiple
inheritance methods - Change the for looping construct to have
different semantics
7Problems with Reflection
- Hard to implement.
- Potential infinite tower of meta-interpreters
or reflective layers - Hard to implement efficiently too much can
change! - Instruction interpreter changes require new
hardware - Software model changes require source programs,
recompilation, and even change to the data - Allowing change of execution model opens all
kinds of security problems. - Access control
- What range of reflective changes are safe
8Java Implements a Weak form of Reflection
Transparent, can look inside!
- Can examine the fields of objects
- Can examine the methods
- Can build new objects (instances)
- Can access and modify array elements (unnamed
fields)
9Purpose
- Allow applications to discover and use all of
the public members of a target object based on
its runtime class (a la COM) - Applications which need to discover the members
of a class to manipulate it (meta-programs
such as debuggers, class browsers, object
serialization, etc.)
10How does it work?
- Class Objects (instantiated by JVM)
- describe the structure of objects of this class
(a la Smalltalk) - Represent types and contain information for each
type about - Fields, Methods, Constructors, etc.
- Superclasses, NewInstance,
- Three final classes
- Field
- Method
- Constructor
- and some special support for Arrays
11Fields and Methods
- These classes described objects which can be
extracted from class objects - final classes (cant extend types of fields and
methods) - Implement member interface (same for both)
- Allow query and exploration of the interface of
an object - methods and fields of an unknown object can be
determined - thus, can determine procedural correctness of
composition - Behavior cannot be determined
- Unique type cannot be determined (many
implementations of an interface) - How does this compare to IUKnown COM interface?
12Java Serialization
- Packages such as serialization require
reflective information - Dynamic class loading model
- Stubs cannot be statically compiled
- How can serialization be done?
- Exploit reflective interface, obtain information
needed, and interpret serialization (or compile
stubs) - 1. Identify object fields and serialize
- 2. Identify object methods and serialize each
method object (code travels with data) - 3. Serialize the class object and send it
13Serializing (Pickling) an Unknown Object
Source Program class apples int number color
mycolor
State
Class Object
Methods
- Runtime representation preserves information
about class structure (to support reflection) - Reflection allows generic software to serialize
- Class object at receiver is enough to unpack
14Summary
- Java Reflection provides the ability to identify
and explore the structure of objects and methods. - This can be used to build intelligent systems
that extend their functionality as needed to
support the classes they encounter. - Similar in some ways to CORBA dynamic features,
but this is a Language feature, leveraged for
distributed computing. - What else can it be used for within the language?
15Java Beans
- The goal of the JavaBeans APIs is to define a
software component model for Java, so that third
party ISVs can create and ship Java components
that can be composed together into applications
by end users. - What this means is unclear.
- Components, End users, Composed? Direct
manipulation? - The rest of the document is a bit more focused.
16Java Bean Types
- Building blocks composed by tools
- think a GUI builder with direct manipulation
- Java Bean components
- think composition of user-visible elements into
larger entities, such as a form into a web page,
a table into a larger interactive document, a
spreadsheet figure into a presentation, an
animation into a presentation - Distinction these are not general class
libraries. Java Beans are supposed to be
visually configurable and may have a visual
element.
17Elements of the Jbeans Interface
- Introspection
- Customization allow configuration and extension
- Events
- Properties simple attributes
- Persistence beans can be serialized and stored
18Introspection
getX, setX, isX (properties) addListenerType,
removeListenerType (events)
- Low level reflection to acquire basic information
and bootstrap the high level analysis - structure, method names, properties
- Naming conventions for common patterns and
interface structures design patterns - Properties, events, methods
- Find standard interfaces, guess what theyre
used for - Build a framework for reuse and sharing
- Explicit specification using BeanInfo class
19Introspection (cont.)
Interfaces and names define plug
types Low-level reflection enables probing -gt
tells little about actual behavior
- Claim behavior, but really only simple hacks on
likely uses - gt allow generic builder tools for composition,
stylized end user composition
20Events
Object
Object
Object
Object
EventSource
Object
Register as listener Handle events
Events
EventAdaptor
- Defines
- Event Listeners
- Event Sources
- Plumbing adaptors
- Structure for building generic plumbing
- Support for Unicast and Multicast
- gt similar to most user interface environments
(e.g. Win32, X windows)
Filter, Label, Direct
21Summary
- JavaBeans provide a modest framework for reusable
components, focus on graphical widgets - General interface query mechanisms already
supported in base Reflection APIs - No typing of the class interfaces beyond class
identity (no disocciation -- factoring of
interfaces, multiple implementation, and sharing) - Will anything with complex interface behavior and
semantics will work without source code
exchanges. (beyond similar plug in elements)