Reflection - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Reflection

Description:

Reflection. CSC335. By Craig Barber. What is it? Reflection: A means of extracting information about classes and objects during runtime ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 12
Provided by: cra9151
Category:

less

Transcript and Presenter's Notes

Title: Reflection


1
Reflection
  • CSC335
  • By Craig Barber

2
What is it?
  • Reflection
  • A means of extracting information about classes
    and objects during runtime
  • A means of using functionality from foreign
    classes during runtime
  • A means of having references (pointers) to
    methods, for arbitrary use during runtime

3
How does it work?
  • java.lang.Class
  • a Class object is an object which represents a
    type in java
  • It allows you to get information about a certain
    class during run time
  • general info, variables, methods
  • It allows you to construct instances of objects
    within the class and thereby allowing you to use
    methods/fields within that class

4
java.lang.Class
  • methods in Class
  • public static Class forName( String )
  • returns a reference to the class at the specified
    package address
  • public String getName( )
  • returns the full name of a class (package
    included) ie. java.util.ArrayList
  • public int getModifiers( )
  • returns an int flag set with information about
    the class
  • abstract, interface, etc.
  • public Object newInstance( )
  • returns a new instance of the object in the
    class. Can only be used if constructor takes no
    arguments

5
java.lang.Class contd
  • public Class getClasses( )
  • returns an array of all the inner classes within
    a class
  • public Constructor getConstructor( Class
    params)
  • returns a constructor object defined by params
    for the class
  • public Field getField( String name )
  • returns a Field object for the instance variable
    with the given name in the class
  • public Field getFields( )
  • returns an array of the fields for the instance
    variables in a class
  • public Method getMethod( String name, Class
    params)
  • returns a reference to the method in the class
    with the given name and parameter types
  • public Method getMethods( )
  • returns an array of all of the methods within the
    class

6
java.lang.reflect
  • Constructor
  • public Object newInstance( Object args )
  • creates a new instance of a class by calling the
    constructor from a class, using the given
    arguments (args)
  • Method
  • public Object invoke( Object source, Object
    args )
  • calls a method from the given object( source),
    using the given arguments (args)

7
Getting class references
  • To get a class from an existing object
  • Object object new Object( )
  • Class aClass object.getClass()
  • To get a class statically
  • Class stringClass String.class
  • To get a class using Class.forName( String )
  • Class aClass Class.forName( class name )
  • To get a class from an arbitrary file system
  • Use Class Loader
  • ClassLoader cl ClassLoader.getSystemClassLoade
    r( )

8
Getting info about classes
  • public class Bob
  • public void bob1( int n)
  • public void bob2( )
  • public void printMethods( )
  • try
  • Class aClass Class.forName( Bob )
  • System.out.println( aClass.getName( ) )
  • Method methods aClass.getMethods( )
  • for ( int i 0 i lt methods.length i )
  • System.out.println( methodsi )
  • catch( Exception e )
  • Printed result
  • Bob
  • public void Bob.bob1(int)
  • public void Bob.bob2()
  • public native int java.lang.Object.hasCode()
  • .

9
Using foreign classes at run time
  • public class Bob
  • String name
  • public Bob( String value ) name value
  • public void printNum( int n )
  • System.out.println( name n )
  • Public class Test
  • try
  • Class aClass Class.forName( Bob )
  • Class params new Class String.class
  • Constructor cst aClass.getConstructor(
    params)
  • Object args new Object Joe
  • Bob bob (Bob)cst.newInstance( args )
  • bob.printNum( 2 )
  • catch( Exception e )
  • Printed Result
  • Joe 2

10
Using Method References (function pointers)
  • Normal ActionListener
  • public void actionPerformed( ActionEvent e )
  • String command e.getActionCommand( )
  • if ( command.equals( Redo) )
  • performRedo( )
  • else if( command.equals( Undo ) )
  • performUndo( )
  • else if
  • else if
  • .
  • ActionListener with Reflection
  • Public void actionPerformed( ActionEvent e )
  • String command e.getActionCommand( )
  • String methodName perform command
  • Method method
  • method getClass( ).getMethod( methodName,
    null )
  • method.invoke( this, null )

11
Working Examples of Reflection
  • JUnit
  • public void test()
  • Eclipse
  • field/method listing pop-up boxes
  • real-time compiler error checking
  • Runtime Plug-ins
  • Parsers
Write a Comment
User Comments (0)
About PowerShow.com