SlideShare a Scribd company logo
1 of 60
Download to read offline
Java Reflection
             Explained Simply




  CiaranMcHale.com —
Copyright License

Copyright © 2008 Ciaran McHale

Permission is hereby granted, free of charge, to any person obtaining a copy of this training course and
associated documentation files (the quot;Training Coursequot;), to deal in the Training Course without
restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Training Course, and to permit persons to whom the Training
Course is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Training Course.

THE TRAINING COURSE IS PROVIDED quot;AS ISquot;, WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
TRAINING COURSE OR THE USE OR OTHER DEALINGS IN THE TRAINING COURSE.




About the Author

Ciaran McHale has a Ph.D. in computer science from Trinity College Dublin. He has
been working for IONA Technologies (www.iona.com) since 1995, where he is a
principal consultant. His primary talent is the ability to digest complex ideas and re-
explain them in simpler ways. He applies this talent to subjects that stir his passion,
such as multi-threading, distributed middleware, code generation, configuration-file
parsers, and writing training courses. You can find details of some of his work at his
personal web site: www.CiaranMcHale.com. You can email him at
Ciaran@CiaranMcHale.com.




Acknowledgements

Ciaran McHale’s employer, IONA Technologies (www.iona.com) generously gave
permission for this training material to be released under the stated open-source
license.
Table of Contents

1. Introduction to Java Reflection

2. Dynamic Proxies

3. Example Uses of Java Reflection
Chapter 1: Introduction to Java Reflection




                   Introduction to Java Reflection


            Java Reflection
                                             Explained Simply




      CiaranMcHale.com
                                                                1
Chapter 1: Introduction to Java Reflection

                                             License
       Copyright © 2008 Ciaran McHale.
       Permission is hereby granted, free of charge, to any person obtaining a copy of this
       training course and associated documentation files (the “Training Coursequot;), to deal in
       the Training Course without restriction, including without limitation the rights to use,
       copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Training
       Course, and to permit persons to whom the Training Course is furnished to do so,
       subject to the following conditions:
       The above copyright notice and this permission notice shall be included in all copies
       or substantial portions of the Training Course.
       THE TRAINING COURSE IS PROVIDED quot;AS ISquot;, WITHOUT WARRANTY OF ANY
       KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
       WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
       AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
       COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
       LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
       ARISING FROM, OUT OF OR IN CONNECTION WITH THE TRAINING COURSE
       OR THE USE OR OTHER DEALINGS IN THE TRAINING COURSE.



       Introduction to Java Reflection                                                         2
Chapter 1: Introduction to Java Reflection




                                             1. Introduction




                                                               3
Chapter 1: Introduction to Java Reflection

                                      What is reflection?
       n   When you look in a mirror:
            -   You can see your reflection
            -   You can act on what you see, for example, straighten your tie

       n   In computer programming:
            -   Reflection is infrastructure enabling a program can see and manipulate
                itself
            -   It consists of metadata plus operations to manipulate the metadata

       n   Meta means self-referential
            -   So metadata is data (information) about oneself




       Introduction to Java Reflection                                               4
Chapter 1: Introduction to Java Reflection

                  Widespread ignorance of Java reflection
       n   Typical way a developer learns Java:
            -   Buys a large book on Java
            -   Starts reading it
            -   Stops reading about half-way through due to project deadlines
            -   Starts coding (to meet deadlines) with what he has learned so far
            -   Never finds the time to read the rest of the book

       n   Result is widespread ignorance of many “advanced” Java
           features:
            -   Many such features are not complex
            -   People just assume they are because they never read that part of the
                manual
            -   Reflection is one “advanced” issue that is not complex




       Introduction to Java Reflection                                                 5
Chapter 1: Introduction to Java Reflection

                                    Is reflection difficult?
       n   When learning to program:
            -   First learn iterative programming with if-then-else, while-loop, …
            -   Later, learn recursive programming

       n   Most people find recursion difficult at first
            -   Because it is an unusual way of programming
            -   But it becomes much easier once you “get it”

       n   Likewise, many people find reflection difficult at first
            -   It is an unusual way of programming
            -   But it becomes much easier once you “get it”
            -   Reflection seems natural to people who have written compilers
                (a parse tree is conceptually similar to metadata in reflection)

       n   A lot of reflection-based programming uses recursion


       Introduction to Java Reflection                                               6
Chapter 1: Introduction to Java Reflection




                                             2. Metadata




                                                           7
Chapter 1: Introduction to Java Reflection

                                     Accessing metadata
       n   Java stores metadata in classes
            -   Metadata for a class:         java.lang.Class
            -   Metadata for a constructor:   java.lang.reflect.Constructor
            -   Metadata for a field:         java.lang.reflect.Field
            -   Metadata for a method:        java.lang.reflect.Method

       n   Two ways to access a Class object for a class:
                Class c1 = Class.forName(“java.util.Properties”);
                Object obj = ...;
                Class c2 = obj.getClass();

       n   Reflection classes are inter-dependent
            -   Examples are shown on the next slide




       Introduction to Java Reflection                                    8
Chapter 1: Introduction to Java Reflection

        Examples of inter-relatedness of reflection classes
              class Class {
                  Constructor[]              getConstructors();
                  Field                      getDeclaredField(String name);
                  Field[]                    getDeclaredFields();
                  Method[]                   getDeclaredMethods();
                  ...
              }

              class Field {
                  Class getType();
                  ...
              }

              class Method {
                  Class[] getParameterTypes();
                  Class   getReturnType();
                  ...
              }


       Introduction to Java Reflection                                        9
Chapter 1: Introduction to Java Reflection

                   Metadata for primitive types and arrays
       n   Java associates a Class instance with each primitive type:
                Class c1 = int.class;
                Class c2 = boolean.class;                   Might be returned by
                                                            Might be returned by
                                                        Method.getReturnType()
                                                         Method.getReturnType()
                Class c3 = void.class;

       n   Use Class.forName() to access the Class object for an array
                Class   c4    =   byte.class;           // byte
                Class   c5    =   Class.forName(“[B”); // byte[]
                Class   c6    =   Class.forName(“[[B”); // byte[][]
                Class   c7    =   Class.forName(“[Ljava.util.Properties”);

       n   Encoding scheme used by Class.forName()
            -   B à byte; C à char; D à double; F à float; I à int; J à long;
                Lclass-name à class-name[]; S à short; Z à boolean
            -   Use as many “[”s as there are dimensions in the array



       Introduction to Java Reflection                                             10
Chapter 1: Introduction to Java Reflection

                           Miscellaneous Class methods
       n   Here are some useful methods defined in Class
              class Class {
                public String getName(); // fully-qualified name
                public boolean isArray();
                public boolean isInterface();
                public boolean isPrimitive();
                public Class getComponentType(); // only for arrays
                ...
              }




       Introduction to Java Reflection                           11
Chapter 1: Introduction to Java Reflection




                                     3. Calling constructors




                                                               12
Chapter 1: Introduction to Java Reflection

                           Invoking a default constructor
       n   Use Class.newInstance() to call the default constructor
           Example:
             abstract class Foo {
                  public static Foo create() throws Exception {
        Name of
         Name of      String className = System.getProperty(
        property
         property                “foo.implementation.class”,
         Default
                                 “com.example.myproject.FooImpl”);
          Default     Class c = Class.forName(className);
          value
           value
                      return (Foo)c.newInstance();
                  }
                  abstract void op1(...);
                  abstract void op2(...);
             }
             ...
             Foo obj = Foo.create();
             obj.op1(...);


       Introduction to Java Reflection                               13
Chapter 1: Introduction to Java Reflection

                    Invoking a default constructor (cont’)
       n   This technique is used in CORBA:
            -   CORBA is an RPC (remote procedure call) standard
            -   There are many competing implementations of CORBA
            -   Factory operation is called ORB.init()
            -   A system property specifies which implementation of CORBA is used

       n   A CORBA application can be written in a portable way
            -   Specify the implementation you want to use via a system property
                (pass –D<name>=<value> command-line option to the Java
                interpreter)

       n   Same technique is used for J2EE:
            -   J2EE is a collection of specifications
            -   There are many competing implementations
            -   Use a system property to specify which implementation you are using


       Introduction to Java Reflection                                                14
Chapter 1: Introduction to Java Reflection

                                   A plug-in architecture
       n   Use a properties file to store a mapping for
           plugin name à class name
            -   Many tools support plugins: Ant, Maven, Eclipse, …

                abstract class Plugin {
                    abstract void op1(...);
                    abstract void op1(...);
                }
                abstract class PluginManager {
                    public static Plugin load(String name)
                                                 throws Exception {
                        String className = props.getProperty(name);
                        Class c = Class.forName(className);
                        return (Plugin)c.newInstance();
                    }
                }
                ...
                Plugin obj = PluginManager.load(“...”);

       Introduction to Java Reflection                                15
Chapter 1: Introduction to Java Reflection

                       Invoking a non-default constructor
       n   Slightly more complex than invoking the default constructor:
            -   Use Class.getConstructor(Class[] parameterTypes)
            -   Then call Constructor.newInstance(Object[] parameters)

                abstract class PluginManager {
                    public static Plugin load(String name)
                                                 throws Exception {
                        String className = props.getProperty(name);
                        Class c = Class.forName(className);
                        Constructor cons = c.getConstructor(
                           new Class[]{String.class, String.class});
                        return (Plugin)cons.newInstance(
                                            new Object[]{“x”, “y”});
                    }
                }
                ...
                Plugin obj = PluginManager.load(“...”);


       Introduction to Java Reflection                                    16
Chapter 1: Introduction to Java Reflection

                    Passing primitive types as parameters
       n   If you want to pass a primitive type as a parameter:
            -   Wrap the primitive value in an object wrapper
            -   Then use the object wrapper as the parameter

       n   Object wrappers for primitive types:
            -   boolean     à   java.lang.Boolean
            -   byte        à   java.lang.Byte
            -   char        à   java.lang.Character
            -   int         à   java.lang.Integer
            -   ...




       Introduction to Java Reflection                            17
Chapter 1: Introduction to Java Reflection




                                             4. Methods




                                                          18
Chapter 1: Introduction to Java Reflection

                                      Invoking a method
       n   Broadly similar to invoking a non-default constructor:
            -   Use Class.getMethod(String name,
                                      Class[]parameterTypes)
            -   Then call Method.invoke(Object target,
                                        Object[] parameters)

                Object obj = ...
                Class c = obj.getClass();
                Method m = c.getMethod(“doWork”,
                           new Class[]{String.class, String.class});
                Object result= m.invoke(obj, new Object[]{“x”,“y”});




       Introduction to Java Reflection                              19
Chapter 1: Introduction to Java Reflection

                                    Looking up methods
       n   The API for looking up methods is fragmented:
            -   You can lookup a public method in a class or its ancestor classes
            -   Or, lookup a public or non-public method declared in the specified class

                                                                  A better name
                                                                  A better name
                                                                 would have been
                                                                 would have been
                                                              getPublicMethod()
                                                               getPublicMethod()
                class Class {
                    public Method getMethod(String name,
                                            Class[] parameterTypes);
                    public Method[] getMethods();
                    public Method getDeclaredMethod(String name,
                                            Class[] parameterTypes);
                    public Method[] getDeclaredMethods();
                    ...
                }



       Introduction to Java Reflection                                                20
Chapter 1: Introduction to Java Reflection

                             Finding an inherited method
       n   This code searches up a class hierarchy for a method
            -   Works for both public and non-public methods

           Method findMethod(Class cls, String methodName,
                              Class[] paramTypes)
           {
               Method method = null;
               while (cls != null) {
                   try {
                       method = cls.getDeclaredMethod(methodName,
                                                      paramTypes);
                       break;
                   } catch (NoSuchMethodException ex) {
                       cls = cls.getSuperclass();
                   }
               }
               return method;
           }

       Introduction to Java Reflection                            21
Chapter 1: Introduction to Java Reflection




                                             5. Fields




                                                         22
Chapter 1: Introduction to Java Reflection

                                        Accessing a field
       n   There are two ways to access a field:
            -   By invoking get- and set-style methods (if the class defines them)
            -   By using the code shown below

                Object obj = ...
                Class c = obj.getClass();
                Field f = c.getField(“firstName”);
                f.set(obj, “John”);
                Object value = f.get(obj);




       Introduction to Java Reflection                                               23
Chapter 1: Introduction to Java Reflection

                                        Looking up fields
       n   The API for looking up fields is fragmented:
            -   You can lookup a public field in a class or its ancestor classes
            -   Or, lookup a public or non-public field declared in the specified class

                                                                   A better name
                                                                    A better name
                                                                  would have been
                                                                   would have been
                                                                getPublicField()
                                                                 getPublicField()
                class Class {
                    public Field             getField(String name);
                    public Field[]           getFields();
                    public Field             getDeclaredField(String name);
                    public Field[]           getDeclaredFields();
                    ...
                }




       Introduction to Java Reflection                                                    24
Chapter 1: Introduction to Java Reflection

                                Finding an inherited field
       n   This code searches up a class hierarchy for a field
            -   Works for both public and non-public fields

           Field findField(Class cls, String fieldName)
           {
               Field field = null;
               while (cls != null) {
                   try {
                       field = cls.getDeclaredField(fieldName);
                       break;
                   } catch (NoSuchFieldException ex) {
                       cls = cls.getSuperclass();
                   }
               }
               return field;
           }



       Introduction to Java Reflection                            25
Chapter 1: Introduction to Java Reflection




                                             6. Modifiers




                                                            26
Chapter 1: Introduction to Java Reflection

                                             Java modifiers
       n   Java defines 11 modifiers:
            -   abstract, final, native, private, protected, public, static,
                strictfp, synchronized, transient and volatile

       n   Some of the modifiers can be applied to a class, method or
           field:
            -   Set of modifiers is represented as bit-fields in an integer
            -   Access set of modifiers by calling int getModifiers()

       n   Useful static methods on java.lang.reflect.Modifier:
                static    boolean       isAbstract(int modifier);
                static    boolean       isFinal(int modifier);
                static    boolean       isNative(int modifier);
                static    boolean       isPrivate(int modifier);
                ...



       Introduction to Java Reflection                                        27
Chapter 1: Introduction to Java Reflection

                Accessing non-public fields and methods
       n   Both Field and Method define the following methods
           (inherited from java.lang.reflect.AccessibleObject):
              boolean isAccessible();
              void setAccessible(boolean flag);
              static void setAccessible(AccessibleObject[] array,
                                        boolean flag);

       n   Better terminology might have been
           “SuppressSecurityChecks” instead of “Accessible”

       n   Example of use:
              if (!Modifier.isPublic(field.getModifiers()) {
                  field.setAccessible(true);
              }
                                             Hibernate uses this technique
                                             Hibernate uses this technique
              Object obj = field.get(obj);   so it can serialize non-public
                                                    so it can serialize non-public
                                                 fields of an object to a database
                                                  fields of an object to a database

       Introduction to Java Reflection                                            28
Chapter 1: Introduction to Java Reflection




                            7. Further reading and summary




                                                             29
Chapter 1: Introduction to Java Reflection

                                         Further reading
       n   There are very few books that discuss Java reflection
            -   An excellent one is Java Reflection in Action
                by Ira R. Forman and Nate Forman
            -   It is concise and easy to understand

       n   Main other source of information is Javadoc documentation




       Introduction to Java Reflection                                 30
Chapter 1: Introduction to Java Reflection

                                             Summary
       n   This chapter has introduced the basics of Java reflection:
            -   Metadata provides information about a program
            -   Methods on the metadata enable a program to examine itself and take
                actions

       n   Reflection is an unusual way to program:
            -   Its “meta” nature can cause confusion at first
            -   It is simple to use once you know how

       n   The next chapter looks at a reflection feature called
           dynamic proxies




       Introduction to Java Reflection                                            31
Chapter 2: Dynamic Proxies




                             Dynamic Proxies


           Java Reflection
                               Explained Simply




     CiaranMcHale.com
                                                  1
Chapter 2: Dynamic Proxies

                                            License
       Copyright © 2008 Ciaran McHale.
       Permission is hereby granted, free of charge, to any person obtaining a copy of this
       training course and associated documentation files (the “Training Coursequot;), to deal in
       the Training Course without restriction, including without limitation the rights to use,
       copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Training
       Course, and to permit persons to whom the Training Course is furnished to do so,
       subject to the following conditions:
       The above copyright notice and this permission notice shall be included in all copies
       or substantial portions of the Training Course.
       THE TRAINING COURSE IS PROVIDED quot;AS ISquot;, WITHOUT WARRANTY OF ANY
       KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
       WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
       AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
       COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
       LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
       ARISING FROM, OUT OF OR IN CONNECTION WITH THE TRAINING COURSE
       OR THE USE OR OTHER DEALINGS IN THE TRAINING COURSE.



      Dynamic Proxies                                                                          2
Chapter 2: Dynamic Proxies

                                   What is a proxy?
       n   Dictionary definition: “a person authorized to act for another”
           -   Example: if you ask a friend to vote on your behalf then you are
               “voting by proxy”

       n   In computer terms, a proxy is a delegation object (or process)
       n   Used in remote procedure call (RPC) mechanisms:
           -   Client invokes on a (local) proxy object
           -   Proxy object sends request across the network to a server and waits for
               a reply

       n   Some companies set up a HTTP proxy server:
           -   Firewall prevents outgoing connections to port 80
           -   So web browsers cannot connect to remote web sites directly
           -   Web browsers are configured to connect via the company’s proxy
               server
           -   Proxy server can be configured to disallow access to eBay, Amazon, …


      Dynamic Proxies                                                               3
Chapter 2: Dynamic Proxies

                             Dynamic proxies in Java
       n   Java 1.3 introduced dynamic proxies
           -   The API is defined in the java.lang.reflect package

               class Proxy {
                   public static Object newProxyInstance(
                                  ClassLoader loader,
                                  Class[] interfaces,
                                  InvocationHandler h) throws ...
                   ...
               }
               interface InvocationHandler {
                   Object invoke(Object proxy,
                                 Method m,
                                 Object[] args) throws Throwable;
               }




      Dynamic Proxies                                                4
Chapter 2: Dynamic Proxies

                Steps required to create a dynamic proxy
       n   Step 1:
           -   Write a class that implements InvocationHandler
           -   Your implementation of invoke() should:
                - Use Method.invoke() to delegate to the target object
               - Provide some “added value” logic

       n   Step 2:
           -   Call Proxy.newInstance(), with the following parameters:
                - targetObj.getClass().getClassLoader()
                - targetObj.getClass.getInterfaces()
                - InvocationHandler object “wrapper” around the target object

       n   Step 3:
           -   Typecast the result of Proxy.newInstance() to an interface
               implemented by the target object


      Dynamic Proxies                                                           5
Chapter 2: Dynamic Proxies

                               How does this work?
       n   The Proxy.newProxyInstance() method:
           -   Uses runtime code generation techniques
           -   Generates a “hidden” class with a name of the form $Proxy<int>
               (Use of “$” prevents namespace pollution)
           -   Generated class:
                - Implements the specified interfaces
                - Each method puts parameters into Object[] and calls
                  InvocationHandler.invoke()

       n   Can use a dynamic proxy only if a class implements 1+
           interfaces
           -   Use of interfaces is a good programming practice
           -   So this requirement is not a problem in practice




      Dynamic Proxies                                                           6
Chapter 2: Dynamic Proxies

                               Sample code
          public class Handler implements InvocationHandler {
              private Object target;
                private Handler(Object target) {    The proxy parameter
                                                     The proxy parameter
                    this.target = target;             is usually ignored
                                                       is usually ignored
                }
                public Object invoke(Object proxy, Method m,
                                   Object[] args) throws Throwable
                {
                    Object result = null;
                    try {
                        ... // added-value code
                        result = m.invoke(target, args);
                    } catch(InvocationTargetException ex) {
                        ... // added-value code
                        throw ex.getCause();
                    }
                    return result;
                }
                ... // continued on the next slide

      Dynamic Proxies                                                 7
Chapter 2: Dynamic Proxies

                             Sample code (cont’)
               ... // continued from the previous slide
               public static Object createProxy(Object target)
               {
                   return Proxy.newProxyInstance(
                            target.getClass().getClassLoader(),
                            target.getClass().getInterfaces(),
                            new Handler(target));
               }
          }




      Dynamic Proxies                                             8
Chapter 2: Dynamic Proxies

                      Example uses for dynamic proxies
       n   Added-value code might:
           -   Enforce security checks
           -   Begin and commit or rollback a transaction
           -   Use reflection & recursion to print details of all parameters
               (for debugging)

       n   In a testing system, a proxy might “pretend” to be target object
           -   Returns “test” values instead of delegating to a real object
           -   EasyMock (www.easymock.org) makes it easy to write tests in this way




      Dynamic Proxies                                                             9
Chapter 3: Example Uses of Java Reflection




               Example Uses of Java Reflection


            Java Reflection
                                         Explained Simply




     CiaranMcHale.com
                                                            1
Chapter 3: Example Uses of Java Reflection

                                             License
       Copyright © 2008 Ciaran McHale.
       Permission is hereby granted, free of charge, to any person obtaining a copy of this
       training course and associated documentation files (the “Training Coursequot;), to deal in
       the Training Course without restriction, including without limitation the rights to use,
       copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Training
       Course, and to permit persons to whom the Training Course is furnished to do so,
       subject to the following conditions:
       The above copyright notice and this permission notice shall be included in all copies
       or substantial portions of the Training Course.
       THE TRAINING COURSE IS PROVIDED quot;AS ISquot;, WITHOUT WARRANTY OF ANY
       KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
       WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
       AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
       COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
       LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
       ARISING FROM, OUT OF OR IN CONNECTION WITH THE TRAINING COURSE
       OR THE USE OR OTHER DEALINGS IN THE TRAINING COURSE.



       Example Uses of Java Reflection                                                         2
Chapter 3: Example Uses of Java Reflection




                           1. Basic uses of Java reflection




                                                              3
Chapter 3: Example Uses of Java Reflection

                                               Ant
       n   Ant reads build (compilation) instructions from an XML file

       n   Ant is hard-coded to know how to process top-level elements
           -   property, target, taskdef and so on

       n   Each Ant task (used inside target elements) is a plug-in
           -   See example Ant build file on the next slide for examples of tasks

       n   Many task plug-ins are bundled with the Ant distribution
           (jar, javac, mkdir, …)
           -   A properties file provides a mapping for
               task-name à class-that-implements-task

       n   Users can use taskdef to tell Ant about user-written tasks
           -   See example on the next slide



       Example Uses of Java Reflection                                              4
Chapter 3: Example Uses of Java Reflection

                                 Example Ant build file
          <?xml version=“1.0”?>
          <project name=“example build file” ...>
              <property name=“src.dir” value=“...”/>
              <property name=“build.dir” value=“...”/>
              <property name=“lib.dir” value=“...”/>
                <target name=“do-everything”>
                    <mkdir dir=“...”/>
                    <mkdir dir=“...”/>
                    <javac srcdir=“...” destdir=“...” excludes=“...”/>
                    <jar jarfile=“...” basedir=“...” excludes=“...”/>
                    <foo .../>
                </target>
              <taskdef name=“foo” classname=“com.example.tools.Foo”/>
          </project>




       Example Uses of Java Reflection                                   5
Chapter 3: Example Uses of Java Reflection

                         Auto-completion in a text editor
       n   Some Java editors and IDEs provide auto-completion
           -   Example: you type “someObj.” and a pop-up menu lists fields and
               methods for the object’s type

       n   The pop-up menu is populated by using Java reflection




       Example Uses of Java Reflection                                           6
Chapter 3: Example Uses of Java Reflection

                                             JUnit
       n   JUnit 3 uses reflection to find methods whose names start
           with “test”

       n   The algorithm was changed in JUnit 4
           -   Test methods are identified by an annotation
               (Annotations were introduced in Java 1.5)
           -   Reflection is used to find methods with the appropriate annotation




       Example Uses of Java Reflection                                              7
Chapter 3: Example Uses of Java Reflection

                                             Spring
       n   Below is an extract from a Spring configuration file:
             <?xml version=“1.0”?>
             <beans ...>
               <bean id=“employee1”
                 class=“com.example.xyz.Employee”>
                 <property name=“firstName” value=“John”/>
                 <property name=“lastName” value=“Smith”/>
                 <property name=“manager” ref=“manager”/>
               </bean>
                 <bean id=“manager”
                   class=“com.example.xyz.Employee”>
                   <property name=“firstName” value=“John”/>
                   <property name=“lastName” value=“Smith”/>
                   <property name=“manager” ref=“manager”/>
                 </bean>
               ...
             </beans>


       Example Uses of Java Reflection                             8
Chapter 3: Example Uses of Java Reflection

                                         Spring (cont’)
       n   Spring uses reflection to create an object for each bean
           -   The object’s type is specified by the class attribute

       n   By default, the object is created with its default constructor
           -   You can use constructor-arg elements (nested inside bean) to use
               a non-default constructor

       n   After an object is constructed, each property is examined
           -   Spring uses reflection to invoke obj.setXxx(value)
                - Where Xxx is the capitalized name of property xxx
           -   Spring uses reflection to determine the type of the parameter passed to
               obj.setXxx()
           -   Spring can support primitive types and common Collection types
           -   The ref attribute refers to another bean identified by its id




       Example Uses of Java Reflection                                              9
Chapter 3: Example Uses of Java Reflection




              2. Code generation and bytecode manipulation




                                                             10
Chapter 3: Example Uses of Java Reflection

                                      Code generators
       n   Most compilers have the following architecture

                                              compiler                Generated files

                                             parse tree
                             parser                       Back-end
               input
                                                            code
                file
                                                          generator



       n   Java’s reflection metadata is conceptually similar to a parse
           tree

       n   You can build a Java code generation tool as follows:
           -   Do not write a Java parser. Instead run the Java compiler
           -   Treat generated .class files as your parse tree
           -   Use reflection to navigate over this “parse tree”

       Example Uses of Java Reflection                                                  11
Chapter 3: Example Uses of Java Reflection

                                Code generators (cont’)
       n   Compile-time code generation in a project:
           -   Use technique described on previous slide to generate code
           -   Then run Java compiler to compile generated code
           -   Use Ant to automate the code generation and compilation

       n   Runtime code generation:
           -   Use techniques described on previous slide to generate code
           -   Then invoke a Java compiler from inside your application:
                - Can use (non-standard) API to Sun Java compiler
                   - Provided in tools.jar, which is shipped with the Sun JDK
                - Or can use Janino (an open-source, embeddable, Java compiler)
                   - Hosted at www.janino.net
           -   Finally, use Class.forName() to load the compiled code




       Example Uses of Java Reflection                                            12
Chapter 3: Example Uses of Java Reflection

                       Uses for runtime code generation
       n   Runtime code generation is used…

       n   By JSP (Java Server Pages)
           -   To generate servlets from .jsp files

       n   By IDEs and debuggers
           -   To evaluate Java expressions entered by user




       Example Uses of Java Reflection                        13
Chapter 3: Example Uses of Java Reflection

                    Uses for Java bytecode manipulation
       n   Compilers:
           -   Write a compiler for a scripting language and generate Java bytecode
                - Result: out-of-the-box integration between Java and the language
           -   Groovy (groovy.codehaus.org) uses this technique

       n   Optimization:
           -   Read a .class file, optimize bytecode and rewrite the .class file

       n   Code analysis:
           -   Read a .class file, analyze bytecode and generate a report

       n   Code obfuscation:
           -   Mangle names of methods and fields in .class files

       n   Aspect-oriented programming (AOP):
           -   Modify bytecode to insert “interception” code
           -   Generate proxies for classes or interfaces
           -   Spring uses this technique
       Example Uses of Java Reflection                                                14
Chapter 3: Example Uses of Java Reflection

                        Tools for bytecode manipulation
       n   Example open-source projects for bytecode manipulation:
           -   ASM (http://asm.objectweb.org/)
           -   BCEL (http://jakarta.apache.org/bcel/)
           -   SERP (serp.sourceforge.net)

       n   CGLIB (Code Generation LIBrary):
           -   Built on top of BCEL
           -   Provides a higher-level API for generating dynamic proxies
           -   Used by other tools, such as Spring and Hibernate




       Example Uses of Java Reflection                                      15
Chapter 3: Example Uses of Java Reflection




                                             3. Summary




                                                          16
Chapter 3: Example Uses of Java Reflection

                                             Summary
       n   A lot of tools use Java reflection:
           -   Plugins to extend functionality of an application (Ant)
           -   Auto-completion in Java editors and IDEs
           -   Use naming conventions of methods to infer semantics (JUnit test
               methods)
           -   Tie components together (Spring)
           -   Compile-time code generation
           -   Runtime code generation
                - Generate proxies
                - Generate servlets from a markup language (JSP)
           -   Evaluate Java expressions entered interactively by a user




       Example Uses of Java Reflection                                            17

More Related Content

What's hot

Java interview questions 1
Java interview questions 1Java interview questions 1
Java interview questions 1Sherihan Anver
 
Most Asked Java Interview Question and Answer
Most Asked Java Interview Question and AnswerMost Asked Java Interview Question and Answer
Most Asked Java Interview Question and AnswerTOPS Technologies
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Javabackdoor
 
Core java notes with examples
Core java notes with examplesCore java notes with examples
Core java notes with examplesbindur87
 
Object+oriented+programming+in+java
Object+oriented+programming+in+javaObject+oriented+programming+in+java
Object+oriented+programming+in+javaYe Win
 
Understanding And Using Reflection
Understanding And Using ReflectionUnderstanding And Using Reflection
Understanding And Using ReflectionGanesh Samarthyam
 
Basic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a jobBasic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a jobGaruda Trainings
 
Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes Uzair Salman
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaCPD INDIA
 
Java questions for viva
Java questions for vivaJava questions for viva
Java questions for vivaVipul Naik
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Sagar Verma
 
9781111530532 ppt ch10
9781111530532 ppt ch109781111530532 ppt ch10
9781111530532 ppt ch10Terry Yoast
 
Introduction to Ruby’s Reflection API
Introduction to Ruby’s Reflection APIIntroduction to Ruby’s Reflection API
Introduction to Ruby’s Reflection APINiranjan Sarade
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesSunil Kumar Gunasekaran
 

What's hot (20)

Java reflection
Java reflectionJava reflection
Java reflection
 
Java interview questions 1
Java interview questions 1Java interview questions 1
Java interview questions 1
 
Most Asked Java Interview Question and Answer
Most Asked Java Interview Question and AnswerMost Asked Java Interview Question and Answer
Most Asked Java Interview Question and Answer
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
 
Core java notes with examples
Core java notes with examplesCore java notes with examples
Core java notes with examples
 
Java Reflection Concept and Working
Java Reflection Concept and WorkingJava Reflection Concept and Working
Java Reflection Concept and Working
 
Object+oriented+programming+in+java
Object+oriented+programming+in+javaObject+oriented+programming+in+java
Object+oriented+programming+in+java
 
Understanding And Using Reflection
Understanding And Using ReflectionUnderstanding And Using Reflection
Understanding And Using Reflection
 
Basic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a jobBasic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a job
 
Java API, Exceptions and IO
Java API, Exceptions and IOJava API, Exceptions and IO
Java API, Exceptions and IO
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
Basics of Java
Basics of JavaBasics of Java
Basics of Java
 
Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
Java questions for viva
Java questions for vivaJava questions for viva
Java questions for viva
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3
 
9781111530532 ppt ch10
9781111530532 ppt ch109781111530532 ppt ch10
9781111530532 ppt ch10
 
Introduction to Ruby’s Reflection API
Introduction to Ruby’s Reflection APIIntroduction to Ruby’s Reflection API
Introduction to Ruby’s Reflection API
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examples
 
inheritance
inheritanceinheritance
inheritance
 

Viewers also liked

A review & summary of sexual strangers by
A review & summary of sexual strangers byA review & summary of sexual strangers by
A review & summary of sexual strangers bynicole_buccalo
 
Java Exploit Analysis .
Java Exploit Analysis .Java Exploit Analysis .
Java Exploit Analysis .Rahul Sasi
 
Easy rest service using PHP reflection api
Easy rest service using PHP reflection apiEasy rest service using PHP reflection api
Easy rest service using PHP reflection apiMatthieu Aubry
 
Active learning and the Internet and Reflections - University of Memphis, Cel...
Active learning and the Internet and Reflections - University of Memphis, Cel...Active learning and the Internet and Reflections - University of Memphis, Cel...
Active learning and the Internet and Reflections - University of Memphis, Cel...Celia Pruitt
 
SPIntersection 2016 - MICROSOFT CLOUD IDENTITIES IN AZURE AND OFFICE 365
SPIntersection 2016 - MICROSOFT CLOUD IDENTITIES IN AZURE AND OFFICE 365SPIntersection 2016 - MICROSOFT CLOUD IDENTITIES IN AZURE AND OFFICE 365
SPIntersection 2016 - MICROSOFT CLOUD IDENTITIES IN AZURE AND OFFICE 365Scott Hoag
 
Implementation of azure active directory authentication with cross platform d...
Implementation of azure active directory authentication with cross platform d...Implementation of azure active directory authentication with cross platform d...
Implementation of azure active directory authentication with cross platform d...Alexander Meijers
 
The Nature of Assessment: The student perspective on the purpose of assessmen...
The Nature of Assessment: The student perspective on the purpose of assessmen...The Nature of Assessment: The student perspective on the purpose of assessmen...
The Nature of Assessment: The student perspective on the purpose of assessmen...SHU Learning & Teaching
 
Reflection Paper
Reflection PaperReflection Paper
Reflection Papergraysonw
 
Reflection and Introspection
Reflection and IntrospectionReflection and Introspection
Reflection and Introspectionadil raja
 
Java Serialization
Java SerializationJava Serialization
Java Serializationimypraz
 
Global Big Data Conference Sept 2014 AWS Kinesis Spark Streaming Approximatio...
Global Big Data Conference Sept 2014 AWS Kinesis Spark Streaming Approximatio...Global Big Data Conference Sept 2014 AWS Kinesis Spark Streaming Approximatio...
Global Big Data Conference Sept 2014 AWS Kinesis Spark Streaming Approximatio...Chris Fregly
 
Introduction to Pepsi
Introduction to PepsiIntroduction to Pepsi
Introduction to PepsiFahad Ali
 

Viewers also liked (20)

A review & summary of sexual strangers by
A review & summary of sexual strangers byA review & summary of sexual strangers by
A review & summary of sexual strangers by
 
Java Exploit Analysis .
Java Exploit Analysis .Java Exploit Analysis .
Java Exploit Analysis .
 
Easy rest service using PHP reflection api
Easy rest service using PHP reflection apiEasy rest service using PHP reflection api
Easy rest service using PHP reflection api
 
Active learning and the Internet and Reflections - University of Memphis, Cel...
Active learning and the Internet and Reflections - University of Memphis, Cel...Active learning and the Internet and Reflections - University of Memphis, Cel...
Active learning and the Internet and Reflections - University of Memphis, Cel...
 
Java notes(OOP) jkuat IT esection
Java notes(OOP) jkuat IT esectionJava notes(OOP) jkuat IT esection
Java notes(OOP) jkuat IT esection
 
SPIntersection 2016 - MICROSOFT CLOUD IDENTITIES IN AZURE AND OFFICE 365
SPIntersection 2016 - MICROSOFT CLOUD IDENTITIES IN AZURE AND OFFICE 365SPIntersection 2016 - MICROSOFT CLOUD IDENTITIES IN AZURE AND OFFICE 365
SPIntersection 2016 - MICROSOFT CLOUD IDENTITIES IN AZURE AND OFFICE 365
 
Implementation of azure active directory authentication with cross platform d...
Implementation of azure active directory authentication with cross platform d...Implementation of azure active directory authentication with cross platform d...
Implementation of azure active directory authentication with cross platform d...
 
The Nature of Assessment: The student perspective on the purpose of assessmen...
The Nature of Assessment: The student perspective on the purpose of assessmen...The Nature of Assessment: The student perspective on the purpose of assessmen...
The Nature of Assessment: The student perspective on the purpose of assessmen...
 
Java beans
Java beansJava beans
Java beans
 
Reflection Paper
Reflection PaperReflection Paper
Reflection Paper
 
Reflection and Introspection
Reflection and IntrospectionReflection and Introspection
Reflection and Introspection
 
Java Serialization
Java SerializationJava Serialization
Java Serialization
 
javabeans
javabeansjavabeans
javabeans
 
Global Big Data Conference Sept 2014 AWS Kinesis Spark Streaming Approximatio...
Global Big Data Conference Sept 2014 AWS Kinesis Spark Streaming Approximatio...Global Big Data Conference Sept 2014 AWS Kinesis Spark Streaming Approximatio...
Global Big Data Conference Sept 2014 AWS Kinesis Spark Streaming Approximatio...
 
Java Beans
Java BeansJava Beans
Java Beans
 
Introduction to Pepsi
Introduction to PepsiIntroduction to Pepsi
Introduction to Pepsi
 
.NET Vs J2EE
.NET Vs J2EE.NET Vs J2EE
.NET Vs J2EE
 
Apache Spark & Streaming
Apache Spark & StreamingApache Spark & Streaming
Apache Spark & Streaming
 
Javabeans
JavabeansJavabeans
Javabeans
 
Java beans
Java beansJava beans
Java beans
 

Similar to Java Reflection Explained Simply

OOP in Java Presentation.pptx
OOP in Java Presentation.pptxOOP in Java Presentation.pptx
OOP in Java Presentation.pptxmrxyz19
 
JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)Prof. Erwin Globio
 
Top 100 Java Interview Questions with Detailed Answers
Top 100 Java Interview Questions with Detailed AnswersTop 100 Java Interview Questions with Detailed Answers
Top 100 Java Interview Questions with Detailed AnswersWhizlabs
 
Java Classes | Java Tutorial for Beginners | Java Classes and Objects | Java ...
Java Classes | Java Tutorial for Beginners | Java Classes and Objects | Java ...Java Classes | Java Tutorial for Beginners | Java Classes and Objects | Java ...
Java Classes | Java Tutorial for Beginners | Java Classes and Objects | Java ...Edureka!
 
Support formation vidéo : OCA Java SE 8 Programmer (1Z0-808) (2)
Support formation vidéo : OCA Java SE 8 Programmer (1Z0-808) (2)Support formation vidéo : OCA Java SE 8 Programmer (1Z0-808) (2)
Support formation vidéo : OCA Java SE 8 Programmer (1Z0-808) (2)SmartnSkilled
 
Mcs 024 assignment solution (2020-21)
Mcs 024 assignment solution (2020-21)Mcs 024 assignment solution (2020-21)
Mcs 024 assignment solution (2020-21)smumbahelp
 
Dev labs alliance top 20 basic java interview questions for sdet
Dev labs alliance top 20 basic java interview questions for sdetDev labs alliance top 20 basic java interview questions for sdet
Dev labs alliance top 20 basic java interview questions for sdetDevLabs Alliance
 
Nitish Chaulagai Java1.pptx
Nitish Chaulagai Java1.pptxNitish Chaulagai Java1.pptx
Nitish Chaulagai Java1.pptxNitishChaulagai
 
Top 20 basic java interview questions for SDET
Top 20 basic java interview questions for SDETTop 20 basic java interview questions for SDET
Top 20 basic java interview questions for SDETDevLabs Alliance
 
Dev labs alliance top 20 basic java interview question for sdet
Dev labs alliance top 20 basic java interview question for sdetDev labs alliance top 20 basic java interview question for sdet
Dev labs alliance top 20 basic java interview question for sdetdevlabsalliance
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritanceArjun Shanka
 
Top 10 java oops interview questions
Top 10 java oops interview questionsTop 10 java oops interview questions
Top 10 java oops interview questionsnishajj
 
Top 10 java oops interview questions
Top 10 java oops interview questionsTop 10 java oops interview questions
Top 10 java oops interview questionsnishajj
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsnishajj
 

Similar to Java Reflection Explained Simply (20)

OOP in Java Presentation.pptx
OOP in Java Presentation.pptxOOP in Java Presentation.pptx
OOP in Java Presentation.pptx
 
JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)
 
Top 100 Java Interview Questions with Detailed Answers
Top 100 Java Interview Questions with Detailed AnswersTop 100 Java Interview Questions with Detailed Answers
Top 100 Java Interview Questions with Detailed Answers
 
Java Classes | Java Tutorial for Beginners | Java Classes and Objects | Java ...
Java Classes | Java Tutorial for Beginners | Java Classes and Objects | Java ...Java Classes | Java Tutorial for Beginners | Java Classes and Objects | Java ...
Java Classes | Java Tutorial for Beginners | Java Classes and Objects | Java ...
 
Support formation vidéo : OCA Java SE 8 Programmer (1Z0-808) (2)
Support formation vidéo : OCA Java SE 8 Programmer (1Z0-808) (2)Support formation vidéo : OCA Java SE 8 Programmer (1Z0-808) (2)
Support formation vidéo : OCA Java SE 8 Programmer (1Z0-808) (2)
 
PPT Lecture-1.4.pptx
PPT Lecture-1.4.pptxPPT Lecture-1.4.pptx
PPT Lecture-1.4.pptx
 
java basic .pdf
java basic .pdfjava basic .pdf
java basic .pdf
 
Java scjp-part1
Java scjp-part1Java scjp-part1
Java scjp-part1
 
Mcs 024 assignment solution (2020-21)
Mcs 024 assignment solution (2020-21)Mcs 024 assignment solution (2020-21)
Mcs 024 assignment solution (2020-21)
 
Ppt chapter10
Ppt chapter10Ppt chapter10
Ppt chapter10
 
DAY_1.1.pptx
DAY_1.1.pptxDAY_1.1.pptx
DAY_1.1.pptx
 
Dev labs alliance top 20 basic java interview questions for sdet
Dev labs alliance top 20 basic java interview questions for sdetDev labs alliance top 20 basic java interview questions for sdet
Dev labs alliance top 20 basic java interview questions for sdet
 
Nitish Chaulagai Java1.pptx
Nitish Chaulagai Java1.pptxNitish Chaulagai Java1.pptx
Nitish Chaulagai Java1.pptx
 
Top 20 basic java interview questions for SDET
Top 20 basic java interview questions for SDETTop 20 basic java interview questions for SDET
Top 20 basic java interview questions for SDET
 
Dev labs alliance top 20 basic java interview question for sdet
Dev labs alliance top 20 basic java interview question for sdetDev labs alliance top 20 basic java interview question for sdet
Dev labs alliance top 20 basic java interview question for sdet
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
 
inheritance.pptx
inheritance.pptxinheritance.pptx
inheritance.pptx
 
Top 10 java oops interview questions
Top 10 java oops interview questionsTop 10 java oops interview questions
Top 10 java oops interview questions
 
Top 10 java oops interview questions
Top 10 java oops interview questionsTop 10 java oops interview questions
Top 10 java oops interview questions
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questions
 

Recently uploaded

SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetEnjoy Anytime
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 

Recently uploaded (20)

SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 

Java Reflection Explained Simply

  • 1. Java Reflection Explained Simply CiaranMcHale.com —
  • 2. Copyright License Copyright © 2008 Ciaran McHale Permission is hereby granted, free of charge, to any person obtaining a copy of this training course and associated documentation files (the quot;Training Coursequot;), to deal in the Training Course without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Training Course, and to permit persons to whom the Training Course is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Training Course. THE TRAINING COURSE IS PROVIDED quot;AS ISquot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE TRAINING COURSE OR THE USE OR OTHER DEALINGS IN THE TRAINING COURSE. About the Author Ciaran McHale has a Ph.D. in computer science from Trinity College Dublin. He has been working for IONA Technologies (www.iona.com) since 1995, where he is a principal consultant. His primary talent is the ability to digest complex ideas and re- explain them in simpler ways. He applies this talent to subjects that stir his passion, such as multi-threading, distributed middleware, code generation, configuration-file parsers, and writing training courses. You can find details of some of his work at his personal web site: www.CiaranMcHale.com. You can email him at Ciaran@CiaranMcHale.com. Acknowledgements Ciaran McHale’s employer, IONA Technologies (www.iona.com) generously gave permission for this training material to be released under the stated open-source license.
  • 3. Table of Contents 1. Introduction to Java Reflection 2. Dynamic Proxies 3. Example Uses of Java Reflection
  • 4. Chapter 1: Introduction to Java Reflection Introduction to Java Reflection Java Reflection Explained Simply CiaranMcHale.com 1
  • 5. Chapter 1: Introduction to Java Reflection License Copyright © 2008 Ciaran McHale. Permission is hereby granted, free of charge, to any person obtaining a copy of this training course and associated documentation files (the “Training Coursequot;), to deal in the Training Course without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Training Course, and to permit persons to whom the Training Course is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Training Course. THE TRAINING COURSE IS PROVIDED quot;AS ISquot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE TRAINING COURSE OR THE USE OR OTHER DEALINGS IN THE TRAINING COURSE. Introduction to Java Reflection 2
  • 6. Chapter 1: Introduction to Java Reflection 1. Introduction 3
  • 7. Chapter 1: Introduction to Java Reflection What is reflection? n When you look in a mirror: - You can see your reflection - You can act on what you see, for example, straighten your tie n In computer programming: - Reflection is infrastructure enabling a program can see and manipulate itself - It consists of metadata plus operations to manipulate the metadata n Meta means self-referential - So metadata is data (information) about oneself Introduction to Java Reflection 4
  • 8. Chapter 1: Introduction to Java Reflection Widespread ignorance of Java reflection n Typical way a developer learns Java: - Buys a large book on Java - Starts reading it - Stops reading about half-way through due to project deadlines - Starts coding (to meet deadlines) with what he has learned so far - Never finds the time to read the rest of the book n Result is widespread ignorance of many “advanced” Java features: - Many such features are not complex - People just assume they are because they never read that part of the manual - Reflection is one “advanced” issue that is not complex Introduction to Java Reflection 5
  • 9. Chapter 1: Introduction to Java Reflection Is reflection difficult? n When learning to program: - First learn iterative programming with if-then-else, while-loop, … - Later, learn recursive programming n Most people find recursion difficult at first - Because it is an unusual way of programming - But it becomes much easier once you “get it” n Likewise, many people find reflection difficult at first - It is an unusual way of programming - But it becomes much easier once you “get it” - Reflection seems natural to people who have written compilers (a parse tree is conceptually similar to metadata in reflection) n A lot of reflection-based programming uses recursion Introduction to Java Reflection 6
  • 10. Chapter 1: Introduction to Java Reflection 2. Metadata 7
  • 11. Chapter 1: Introduction to Java Reflection Accessing metadata n Java stores metadata in classes - Metadata for a class: java.lang.Class - Metadata for a constructor: java.lang.reflect.Constructor - Metadata for a field: java.lang.reflect.Field - Metadata for a method: java.lang.reflect.Method n Two ways to access a Class object for a class: Class c1 = Class.forName(“java.util.Properties”); Object obj = ...; Class c2 = obj.getClass(); n Reflection classes are inter-dependent - Examples are shown on the next slide Introduction to Java Reflection 8
  • 12. Chapter 1: Introduction to Java Reflection Examples of inter-relatedness of reflection classes class Class { Constructor[] getConstructors(); Field getDeclaredField(String name); Field[] getDeclaredFields(); Method[] getDeclaredMethods(); ... } class Field { Class getType(); ... } class Method { Class[] getParameterTypes(); Class getReturnType(); ... } Introduction to Java Reflection 9
  • 13. Chapter 1: Introduction to Java Reflection Metadata for primitive types and arrays n Java associates a Class instance with each primitive type: Class c1 = int.class; Class c2 = boolean.class; Might be returned by Might be returned by Method.getReturnType() Method.getReturnType() Class c3 = void.class; n Use Class.forName() to access the Class object for an array Class c4 = byte.class; // byte Class c5 = Class.forName(“[B”); // byte[] Class c6 = Class.forName(“[[B”); // byte[][] Class c7 = Class.forName(“[Ljava.util.Properties”); n Encoding scheme used by Class.forName() - B à byte; C à char; D à double; F à float; I à int; J à long; Lclass-name à class-name[]; S à short; Z à boolean - Use as many “[”s as there are dimensions in the array Introduction to Java Reflection 10
  • 14. Chapter 1: Introduction to Java Reflection Miscellaneous Class methods n Here are some useful methods defined in Class class Class { public String getName(); // fully-qualified name public boolean isArray(); public boolean isInterface(); public boolean isPrimitive(); public Class getComponentType(); // only for arrays ... } Introduction to Java Reflection 11
  • 15. Chapter 1: Introduction to Java Reflection 3. Calling constructors 12
  • 16. Chapter 1: Introduction to Java Reflection Invoking a default constructor n Use Class.newInstance() to call the default constructor Example: abstract class Foo { public static Foo create() throws Exception { Name of Name of String className = System.getProperty( property property “foo.implementation.class”, Default “com.example.myproject.FooImpl”); Default Class c = Class.forName(className); value value return (Foo)c.newInstance(); } abstract void op1(...); abstract void op2(...); } ... Foo obj = Foo.create(); obj.op1(...); Introduction to Java Reflection 13
  • 17. Chapter 1: Introduction to Java Reflection Invoking a default constructor (cont’) n This technique is used in CORBA: - CORBA is an RPC (remote procedure call) standard - There are many competing implementations of CORBA - Factory operation is called ORB.init() - A system property specifies which implementation of CORBA is used n A CORBA application can be written in a portable way - Specify the implementation you want to use via a system property (pass –D<name>=<value> command-line option to the Java interpreter) n Same technique is used for J2EE: - J2EE is a collection of specifications - There are many competing implementations - Use a system property to specify which implementation you are using Introduction to Java Reflection 14
  • 18. Chapter 1: Introduction to Java Reflection A plug-in architecture n Use a properties file to store a mapping for plugin name à class name - Many tools support plugins: Ant, Maven, Eclipse, … abstract class Plugin { abstract void op1(...); abstract void op1(...); } abstract class PluginManager { public static Plugin load(String name) throws Exception { String className = props.getProperty(name); Class c = Class.forName(className); return (Plugin)c.newInstance(); } } ... Plugin obj = PluginManager.load(“...”); Introduction to Java Reflection 15
  • 19. Chapter 1: Introduction to Java Reflection Invoking a non-default constructor n Slightly more complex than invoking the default constructor: - Use Class.getConstructor(Class[] parameterTypes) - Then call Constructor.newInstance(Object[] parameters) abstract class PluginManager { public static Plugin load(String name) throws Exception { String className = props.getProperty(name); Class c = Class.forName(className); Constructor cons = c.getConstructor( new Class[]{String.class, String.class}); return (Plugin)cons.newInstance( new Object[]{“x”, “y”}); } } ... Plugin obj = PluginManager.load(“...”); Introduction to Java Reflection 16
  • 20. Chapter 1: Introduction to Java Reflection Passing primitive types as parameters n If you want to pass a primitive type as a parameter: - Wrap the primitive value in an object wrapper - Then use the object wrapper as the parameter n Object wrappers for primitive types: - boolean à java.lang.Boolean - byte à java.lang.Byte - char à java.lang.Character - int à java.lang.Integer - ... Introduction to Java Reflection 17
  • 21. Chapter 1: Introduction to Java Reflection 4. Methods 18
  • 22. Chapter 1: Introduction to Java Reflection Invoking a method n Broadly similar to invoking a non-default constructor: - Use Class.getMethod(String name, Class[]parameterTypes) - Then call Method.invoke(Object target, Object[] parameters) Object obj = ... Class c = obj.getClass(); Method m = c.getMethod(“doWork”, new Class[]{String.class, String.class}); Object result= m.invoke(obj, new Object[]{“x”,“y”}); Introduction to Java Reflection 19
  • 23. Chapter 1: Introduction to Java Reflection Looking up methods n The API for looking up methods is fragmented: - You can lookup a public method in a class or its ancestor classes - Or, lookup a public or non-public method declared in the specified class A better name A better name would have been would have been getPublicMethod() getPublicMethod() class Class { public Method getMethod(String name, Class[] parameterTypes); public Method[] getMethods(); public Method getDeclaredMethod(String name, Class[] parameterTypes); public Method[] getDeclaredMethods(); ... } Introduction to Java Reflection 20
  • 24. Chapter 1: Introduction to Java Reflection Finding an inherited method n This code searches up a class hierarchy for a method - Works for both public and non-public methods Method findMethod(Class cls, String methodName, Class[] paramTypes) { Method method = null; while (cls != null) { try { method = cls.getDeclaredMethod(methodName, paramTypes); break; } catch (NoSuchMethodException ex) { cls = cls.getSuperclass(); } } return method; } Introduction to Java Reflection 21
  • 25. Chapter 1: Introduction to Java Reflection 5. Fields 22
  • 26. Chapter 1: Introduction to Java Reflection Accessing a field n There are two ways to access a field: - By invoking get- and set-style methods (if the class defines them) - By using the code shown below Object obj = ... Class c = obj.getClass(); Field f = c.getField(“firstName”); f.set(obj, “John”); Object value = f.get(obj); Introduction to Java Reflection 23
  • 27. Chapter 1: Introduction to Java Reflection Looking up fields n The API for looking up fields is fragmented: - You can lookup a public field in a class or its ancestor classes - Or, lookup a public or non-public field declared in the specified class A better name A better name would have been would have been getPublicField() getPublicField() class Class { public Field getField(String name); public Field[] getFields(); public Field getDeclaredField(String name); public Field[] getDeclaredFields(); ... } Introduction to Java Reflection 24
  • 28. Chapter 1: Introduction to Java Reflection Finding an inherited field n This code searches up a class hierarchy for a field - Works for both public and non-public fields Field findField(Class cls, String fieldName) { Field field = null; while (cls != null) { try { field = cls.getDeclaredField(fieldName); break; } catch (NoSuchFieldException ex) { cls = cls.getSuperclass(); } } return field; } Introduction to Java Reflection 25
  • 29. Chapter 1: Introduction to Java Reflection 6. Modifiers 26
  • 30. Chapter 1: Introduction to Java Reflection Java modifiers n Java defines 11 modifiers: - abstract, final, native, private, protected, public, static, strictfp, synchronized, transient and volatile n Some of the modifiers can be applied to a class, method or field: - Set of modifiers is represented as bit-fields in an integer - Access set of modifiers by calling int getModifiers() n Useful static methods on java.lang.reflect.Modifier: static boolean isAbstract(int modifier); static boolean isFinal(int modifier); static boolean isNative(int modifier); static boolean isPrivate(int modifier); ... Introduction to Java Reflection 27
  • 31. Chapter 1: Introduction to Java Reflection Accessing non-public fields and methods n Both Field and Method define the following methods (inherited from java.lang.reflect.AccessibleObject): boolean isAccessible(); void setAccessible(boolean flag); static void setAccessible(AccessibleObject[] array, boolean flag); n Better terminology might have been “SuppressSecurityChecks” instead of “Accessible” n Example of use: if (!Modifier.isPublic(field.getModifiers()) { field.setAccessible(true); } Hibernate uses this technique Hibernate uses this technique Object obj = field.get(obj); so it can serialize non-public so it can serialize non-public fields of an object to a database fields of an object to a database Introduction to Java Reflection 28
  • 32. Chapter 1: Introduction to Java Reflection 7. Further reading and summary 29
  • 33. Chapter 1: Introduction to Java Reflection Further reading n There are very few books that discuss Java reflection - An excellent one is Java Reflection in Action by Ira R. Forman and Nate Forman - It is concise and easy to understand n Main other source of information is Javadoc documentation Introduction to Java Reflection 30
  • 34. Chapter 1: Introduction to Java Reflection Summary n This chapter has introduced the basics of Java reflection: - Metadata provides information about a program - Methods on the metadata enable a program to examine itself and take actions n Reflection is an unusual way to program: - Its “meta” nature can cause confusion at first - It is simple to use once you know how n The next chapter looks at a reflection feature called dynamic proxies Introduction to Java Reflection 31
  • 35. Chapter 2: Dynamic Proxies Dynamic Proxies Java Reflection Explained Simply CiaranMcHale.com 1
  • 36. Chapter 2: Dynamic Proxies License Copyright © 2008 Ciaran McHale. Permission is hereby granted, free of charge, to any person obtaining a copy of this training course and associated documentation files (the “Training Coursequot;), to deal in the Training Course without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Training Course, and to permit persons to whom the Training Course is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Training Course. THE TRAINING COURSE IS PROVIDED quot;AS ISquot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE TRAINING COURSE OR THE USE OR OTHER DEALINGS IN THE TRAINING COURSE. Dynamic Proxies 2
  • 37. Chapter 2: Dynamic Proxies What is a proxy? n Dictionary definition: “a person authorized to act for another” - Example: if you ask a friend to vote on your behalf then you are “voting by proxy” n In computer terms, a proxy is a delegation object (or process) n Used in remote procedure call (RPC) mechanisms: - Client invokes on a (local) proxy object - Proxy object sends request across the network to a server and waits for a reply n Some companies set up a HTTP proxy server: - Firewall prevents outgoing connections to port 80 - So web browsers cannot connect to remote web sites directly - Web browsers are configured to connect via the company’s proxy server - Proxy server can be configured to disallow access to eBay, Amazon, … Dynamic Proxies 3
  • 38. Chapter 2: Dynamic Proxies Dynamic proxies in Java n Java 1.3 introduced dynamic proxies - The API is defined in the java.lang.reflect package class Proxy { public static Object newProxyInstance( ClassLoader loader, Class[] interfaces, InvocationHandler h) throws ... ... } interface InvocationHandler { Object invoke(Object proxy, Method m, Object[] args) throws Throwable; } Dynamic Proxies 4
  • 39. Chapter 2: Dynamic Proxies Steps required to create a dynamic proxy n Step 1: - Write a class that implements InvocationHandler - Your implementation of invoke() should: - Use Method.invoke() to delegate to the target object - Provide some “added value” logic n Step 2: - Call Proxy.newInstance(), with the following parameters: - targetObj.getClass().getClassLoader() - targetObj.getClass.getInterfaces() - InvocationHandler object “wrapper” around the target object n Step 3: - Typecast the result of Proxy.newInstance() to an interface implemented by the target object Dynamic Proxies 5
  • 40. Chapter 2: Dynamic Proxies How does this work? n The Proxy.newProxyInstance() method: - Uses runtime code generation techniques - Generates a “hidden” class with a name of the form $Proxy<int> (Use of “$” prevents namespace pollution) - Generated class: - Implements the specified interfaces - Each method puts parameters into Object[] and calls InvocationHandler.invoke() n Can use a dynamic proxy only if a class implements 1+ interfaces - Use of interfaces is a good programming practice - So this requirement is not a problem in practice Dynamic Proxies 6
  • 41. Chapter 2: Dynamic Proxies Sample code public class Handler implements InvocationHandler { private Object target; private Handler(Object target) { The proxy parameter The proxy parameter this.target = target; is usually ignored is usually ignored } public Object invoke(Object proxy, Method m, Object[] args) throws Throwable { Object result = null; try { ... // added-value code result = m.invoke(target, args); } catch(InvocationTargetException ex) { ... // added-value code throw ex.getCause(); } return result; } ... // continued on the next slide Dynamic Proxies 7
  • 42. Chapter 2: Dynamic Proxies Sample code (cont’) ... // continued from the previous slide public static Object createProxy(Object target) { return Proxy.newProxyInstance( target.getClass().getClassLoader(), target.getClass().getInterfaces(), new Handler(target)); } } Dynamic Proxies 8
  • 43. Chapter 2: Dynamic Proxies Example uses for dynamic proxies n Added-value code might: - Enforce security checks - Begin and commit or rollback a transaction - Use reflection & recursion to print details of all parameters (for debugging) n In a testing system, a proxy might “pretend” to be target object - Returns “test” values instead of delegating to a real object - EasyMock (www.easymock.org) makes it easy to write tests in this way Dynamic Proxies 9
  • 44. Chapter 3: Example Uses of Java Reflection Example Uses of Java Reflection Java Reflection Explained Simply CiaranMcHale.com 1
  • 45. Chapter 3: Example Uses of Java Reflection License Copyright © 2008 Ciaran McHale. Permission is hereby granted, free of charge, to any person obtaining a copy of this training course and associated documentation files (the “Training Coursequot;), to deal in the Training Course without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Training Course, and to permit persons to whom the Training Course is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Training Course. THE TRAINING COURSE IS PROVIDED quot;AS ISquot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE TRAINING COURSE OR THE USE OR OTHER DEALINGS IN THE TRAINING COURSE. Example Uses of Java Reflection 2
  • 46. Chapter 3: Example Uses of Java Reflection 1. Basic uses of Java reflection 3
  • 47. Chapter 3: Example Uses of Java Reflection Ant n Ant reads build (compilation) instructions from an XML file n Ant is hard-coded to know how to process top-level elements - property, target, taskdef and so on n Each Ant task (used inside target elements) is a plug-in - See example Ant build file on the next slide for examples of tasks n Many task plug-ins are bundled with the Ant distribution (jar, javac, mkdir, …) - A properties file provides a mapping for task-name à class-that-implements-task n Users can use taskdef to tell Ant about user-written tasks - See example on the next slide Example Uses of Java Reflection 4
  • 48. Chapter 3: Example Uses of Java Reflection Example Ant build file <?xml version=“1.0”?> <project name=“example build file” ...> <property name=“src.dir” value=“...”/> <property name=“build.dir” value=“...”/> <property name=“lib.dir” value=“...”/> <target name=“do-everything”> <mkdir dir=“...”/> <mkdir dir=“...”/> <javac srcdir=“...” destdir=“...” excludes=“...”/> <jar jarfile=“...” basedir=“...” excludes=“...”/> <foo .../> </target> <taskdef name=“foo” classname=“com.example.tools.Foo”/> </project> Example Uses of Java Reflection 5
  • 49. Chapter 3: Example Uses of Java Reflection Auto-completion in a text editor n Some Java editors and IDEs provide auto-completion - Example: you type “someObj.” and a pop-up menu lists fields and methods for the object’s type n The pop-up menu is populated by using Java reflection Example Uses of Java Reflection 6
  • 50. Chapter 3: Example Uses of Java Reflection JUnit n JUnit 3 uses reflection to find methods whose names start with “test” n The algorithm was changed in JUnit 4 - Test methods are identified by an annotation (Annotations were introduced in Java 1.5) - Reflection is used to find methods with the appropriate annotation Example Uses of Java Reflection 7
  • 51. Chapter 3: Example Uses of Java Reflection Spring n Below is an extract from a Spring configuration file: <?xml version=“1.0”?> <beans ...> <bean id=“employee1” class=“com.example.xyz.Employee”> <property name=“firstName” value=“John”/> <property name=“lastName” value=“Smith”/> <property name=“manager” ref=“manager”/> </bean> <bean id=“manager” class=“com.example.xyz.Employee”> <property name=“firstName” value=“John”/> <property name=“lastName” value=“Smith”/> <property name=“manager” ref=“manager”/> </bean> ... </beans> Example Uses of Java Reflection 8
  • 52. Chapter 3: Example Uses of Java Reflection Spring (cont’) n Spring uses reflection to create an object for each bean - The object’s type is specified by the class attribute n By default, the object is created with its default constructor - You can use constructor-arg elements (nested inside bean) to use a non-default constructor n After an object is constructed, each property is examined - Spring uses reflection to invoke obj.setXxx(value) - Where Xxx is the capitalized name of property xxx - Spring uses reflection to determine the type of the parameter passed to obj.setXxx() - Spring can support primitive types and common Collection types - The ref attribute refers to another bean identified by its id Example Uses of Java Reflection 9
  • 53. Chapter 3: Example Uses of Java Reflection 2. Code generation and bytecode manipulation 10
  • 54. Chapter 3: Example Uses of Java Reflection Code generators n Most compilers have the following architecture compiler Generated files parse tree parser Back-end input code file generator n Java’s reflection metadata is conceptually similar to a parse tree n You can build a Java code generation tool as follows: - Do not write a Java parser. Instead run the Java compiler - Treat generated .class files as your parse tree - Use reflection to navigate over this “parse tree” Example Uses of Java Reflection 11
  • 55. Chapter 3: Example Uses of Java Reflection Code generators (cont’) n Compile-time code generation in a project: - Use technique described on previous slide to generate code - Then run Java compiler to compile generated code - Use Ant to automate the code generation and compilation n Runtime code generation: - Use techniques described on previous slide to generate code - Then invoke a Java compiler from inside your application: - Can use (non-standard) API to Sun Java compiler - Provided in tools.jar, which is shipped with the Sun JDK - Or can use Janino (an open-source, embeddable, Java compiler) - Hosted at www.janino.net - Finally, use Class.forName() to load the compiled code Example Uses of Java Reflection 12
  • 56. Chapter 3: Example Uses of Java Reflection Uses for runtime code generation n Runtime code generation is used… n By JSP (Java Server Pages) - To generate servlets from .jsp files n By IDEs and debuggers - To evaluate Java expressions entered by user Example Uses of Java Reflection 13
  • 57. Chapter 3: Example Uses of Java Reflection Uses for Java bytecode manipulation n Compilers: - Write a compiler for a scripting language and generate Java bytecode - Result: out-of-the-box integration between Java and the language - Groovy (groovy.codehaus.org) uses this technique n Optimization: - Read a .class file, optimize bytecode and rewrite the .class file n Code analysis: - Read a .class file, analyze bytecode and generate a report n Code obfuscation: - Mangle names of methods and fields in .class files n Aspect-oriented programming (AOP): - Modify bytecode to insert “interception” code - Generate proxies for classes or interfaces - Spring uses this technique Example Uses of Java Reflection 14
  • 58. Chapter 3: Example Uses of Java Reflection Tools for bytecode manipulation n Example open-source projects for bytecode manipulation: - ASM (http://asm.objectweb.org/) - BCEL (http://jakarta.apache.org/bcel/) - SERP (serp.sourceforge.net) n CGLIB (Code Generation LIBrary): - Built on top of BCEL - Provides a higher-level API for generating dynamic proxies - Used by other tools, such as Spring and Hibernate Example Uses of Java Reflection 15
  • 59. Chapter 3: Example Uses of Java Reflection 3. Summary 16
  • 60. Chapter 3: Example Uses of Java Reflection Summary n A lot of tools use Java reflection: - Plugins to extend functionality of an application (Ant) - Auto-completion in Java editors and IDEs - Use naming conventions of methods to infer semantics (JUnit test methods) - Tie components together (Spring) - Compile-time code generation - Runtime code generation - Generate proxies - Generate servlets from a markup language (JSP) - Evaluate Java expressions entered interactively by a user Example Uses of Java Reflection 17