This preview shows page 1-2-3-4-5-6-7-8-9-64-65-66-67-68-69-70-71-72-129-130-131-132-133-134-135-136-137 out of 137 pages.
Session 2289Steve OdendahlMember of Technical StaffGlobal eServices EngineeringSun Microsystems, Inc.ReflectionJava™ Technology’s Secret WeaponSession 22892GoalLearn the What, How, When, Why and Where of the Java™ Reflection APISession 22893Learning ObjectivesWhen we are done, you will be able to:Use the Reflection API in your own code Understand the engineering tradeoffs in using ReflectionApply some common Reflection PatternsAvoid inappropriate use of ReflectionSession 22894Speaker’s QualificationsSteve Odendahl has been employed as a Java™ technology applications programmer since the exciting early days of the JDK™ 1.02 releaseAs a member of the Global eServices Engineering team in Enterprise Services, developing Java technology-based solutions for service and support, he takes an interest in writing effective, efficient, and maintainable Java codeSession 22895What Is Reflection?Reflection is Java™ technology’s crowbar—a blunt instrument for dirty jobs that can’t be done any other way.Session 22896AgendaIntroduction to the Reflection APIEngineering tradeoffsReflection patternsReal World ReflectionWhen not to use ReflectionSession 2289Introduction to the Reflection APISession 22898Introduction to the Reflection APIWhat can Reflection do?The classes in the Reflection APISample code using the APISession 22899What Can Reflection Do?Provide runtime information on the fields and methods of a classSession 228910What Can Reflection Do?Provide runtime information on the fields and methods of a classInstantiate objects and arrays given the class nameSession 228911What Can Reflection Do?Provide runtime information on the fields and methods of a classInstantiate objects and arrays given the class nameInvoke static and instance methods given the method nameSession 228912What Can Reflection Do?Provide runtime information on the fields and methods of a classInstantiate objects and arrays given the class nameInvoke static and instance methods given the method nameCreate a class at runtime that implements one or more interfacesSession 228913The Reflection API—Major Playersjava.lang.Classj.l.r.Field j.l.r.Methodjava.lang.reflect.Memberj.l.r.Constructor0 .. n 0 .. n 0 .. nSession 228914The Reflection API—Classjava.lang.Classj.l.r.Field j.l.r.Methodjava.lang.reflect.Memberj.l.r.Constructor0 .. n 0 .. n 0 .. n1 1 1Session 228915The Reflection API—Classjava.lang.Class+ getName() : String+ getFields() : Field[]+ getField(String) : Field+ getMethods() : Method[]+ getMethod(String, Class[]) : Field+ getConstructors() : Constructor[]+ getConstructor(Class[]) : ConstructorEntry point to the Reflection APISession 228916The Reflection API—Memberjava.lang.Classj.l.r.Field j.l.r.Methodjava.lang.reflect.Memberj.l.r.Constructor0 .. n 0 .. n 0 .. n1 1 1Session 228917The Reflection API—Memberjava.lang.reflect.Member+ getName() : String+ getDeclaringClass() : Class+ getModifiers() : intName (of the field or method)Modifiers (public, static, transient…)Session 228918The Reflection API—Fieldjava.lang.Classj.l.r.Field j.l.r.Methodjava.lang.reflect.Memberj.l.r.Constructor0 .. n 0 .. n 0 .. n1 1 1Session 228919The Reflection API—Fieldjava.lang.reflect.Field+ get(Object) : Object+ set(Object, Object) + getInt(Object) : int+ setInt(Object, int)...Get and set the value of the fieldSession 228920The Reflection API—Methodjava.lang.Classj.l.r.Field j.l.r.Methodjava.lang.reflect.Memberj.l.r.Constructor0 .. n 0 .. n 0 .. n1 1 1Session 228921The Reflection API—Methodjava.lang.reflect.Method+ invoke(Object, Object[]) : Object + getParameterTypes() : Class[]+ getExceptionTypes() : Class[]Call the method—invoke(target, args)Static method—invoke(null, args)No arguments—invoke(target, null)Session 228922The Reflection API—Constructorjava.lang.Classj.l.r.Field j.l.r.Methodjava.lang.reflect.Memberj.l.r.Constructor0 .. n 0 .. n 0 .. n1 1 1Session 228923The Reflection API—Constructorjava.lang.reflect.Constructor+ newInstance(Object[]) : Object + getParameterTypes() : Class[]+ getExceptionTypes() : Class[]Create an instance—newInstance(args)Session 228924The Reflection API—Minor Playersjava.lang.reflect.Arrayjava.lang.reflect.Proxyjava.lang.reflect.InvocationHandlerjava.lang.reflect.Modifier1Session 228925The Reflection API—Arrayjava.lang.reflect.Arrayjava.lang.reflect.Proxyjava.lang.reflect.InvocationHandlerjava.lang.reflect.Modifier1Session 228926The Reflection API—Arrayjava.lang.reflect.Array+ newInstance(Class, int) : Object[]+ newInstance(Class, int[]) : Object[]+ get(Object, int) : Object+ set(Object, int, Object)+ getInt(Object, int) : int...Create an arrayGet/set items in the arraySession 228927The Reflection API—Proxyjava.lang.reflect.Arrayjava.lang.reflect.Proxyjava.lang.reflect.InvocationHandlerjava.lang.reflect.Modifier1Session 228928The Reflection API—Proxyjava.lang.reflect.Proxy+ newProxyInstance (ClassLoader, Class[], InvocationHandler) : ObjectCreate a dynamic proxy—an object that implements one or more interfacesjava.lang.reflect.InvocationHandler+ invoke (Object, Method, Object[]) : ObjectSession 228929It All Begins With ClassAn instance of java.lang.Class for every reference type (classes, interfaces, and arrays)An instance of java.lang.Class for every primitive typeMost reflective techniques start with retrieval of a Class instanceSession 228930How to…Get an Instance of ClassUse the class nametry { Class strcl = Class.forName ("java.lang.String"); Class strarray = Class.forName ("[Ljava.lang.String;");} catch (ClassNotFoundException cnfex){ // handle it}Session 228931How to…Get an Instance of ClassUse the class nameUse a class literalClass strcl = String.class;Class intcl = int.class;Class strarray = String[].class;Session 228932How to…Get an Instance of ClassUse the class nameUse a class literalRetrieve it from an instanceClass strcl = "hello".getClass ();Class strarray = new String[] {}.getClass ();Session 228933How to…Get an Instance of ClassUse the class nameUse a class literalRetrieve it from an instanceUse the constant defined in the wrapper classClass intcl = Integer.TYPE;Session 228934How to…Create an ObjectUse the Class objecttry { Class foocl = Class.forName ("Foo"); Foo f = (Foo) foocl.newInstance ();} catch
View Full Document