The Carbon Java Framework  

The Carbon Utilities Subsystem

Reflection Usage

Printer Friendly Version

Author: Anand Raman (araman at sapient.com)
Version: $Revision: 1.2 $($Author: araman $ / $Date: 2003/05/02 09:07:06 $)
Created: May 2003

Introduction

The reflection utility package contains convenience classes which makes it easier to access / set bean properties implement invocation handlers retrieve object hierarchies

Usage

Some of the common usages of the reflection package is showcased here

Access bean attributes

Refer to the java doc of org.sape.carbon.core.util.relection.BeanUtil getObjectAttribute and setObjectAttribute

Implementing invocation handler

GenericProxy reflection handler provides a cleaner way to implement dynamic proxy invocation handlers. The generic proxy extends from the InvocationHandler interface and provides implementations for common methods defined in java.lang.Object such as hashCode(), equals(), toString()
    public class MyInvocationHandler extends GenericProxy {
        protected Object handleInvoke(Object proxy, Method m, Object[] args) throws Throwable {
            Object result = null;

            //my specific invocation handler stuff comes here
            ....
            ....
            ....
            ....

            return result;
        }

    }
    
A thing to note is that the overridden equals() invocation does a instance comparison of the underlying proxy objects and not a object equality check

Locate classes in the classpath

ClassFinder can be used to search for classes over the classpath implementing a certain interface. The list of classes which are retrieved can also be filtered by specifying the package name under which the classes should lie The following code snippets show examples where all classes all implementing a certain interface are retrieved class is retrieved
        public Set findAllClasses(Class classNode) throws Exception {
            ClassFinder cf = new ClassFinder(classNode);

            return cf.getClasses();
        }
        
all classes all implementing a certain interface and lying under a certain package is retrieved
        public Set findClassInPackage(Class classNode, String packageName) {
            ClassFinder cf = new ClassFinder(classNode, packageName);
            
            return cf.getClasses();
        }
        

Copyright © 2001-2003, Sapient Corporation