DOC PREVIEW
UMD CMSC 433 - Reflection

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.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 137 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 137 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 137 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 137 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 137 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 137 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 137 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 137 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 137 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 137 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 137 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 137 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 137 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 137 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 137 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 137 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 137 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 137 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 137 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 137 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 137 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 137 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 137 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 137 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 137 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 137 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 137 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 137 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 ObjectivesWhen we are done, you will be able to:Use the Reflection API in your own code Understand the engineering tradeoffs in using ReflectionApply some common Reflection PatternsAvoid inappropriate use of ReflectionSession 22894Speaker’s QualificationsSteve Odendahl has been employed as a Java™ technology applications programmer since the exciting early days of the JDK™ 1.02 releaseAs 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 22896AgendaIntroduction to the Reflection APIEngineering tradeoffsReflection patternsReal World ReflectionWhen not to use ReflectionSession 2289Introduction to the Reflection APISession 22898Introduction to the Reflection APIWhat can Reflection do?The classes in the Reflection APISample 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 classInstantiate objects and arrays given the class nameSession 228911What Can Reflection Do?Provide runtime information on the fields and methods of a classInstantiate objects and arrays given the class nameInvoke static and instance methods given the method nameSession 228912What Can Reflection Do?Provide runtime information on the fields and methods of a classInstantiate objects and arrays given the class nameInvoke static and instance methods given the method nameCreate 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[]) : ConstructorEntry 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() : intName (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 arrayGet/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) : ObjectCreate a dynamic proxy—an object that implements one or more interfacesjava.lang.reflect.InvocationHandler+ invoke (Object, Method, Object[]) : ObjectSession 228929It All Begins With ClassAn instance of java.lang.Class for every reference type (classes, interfaces, and arrays)An instance of java.lang.Class for every primitive typeMost reflective techniques start with retrieval of a Class instanceSession 228930How to…Get an Instance of ClassUse 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 ClassUse the class nameUse a class literalClass strcl = String.class;Class intcl = int.class;Class strarray = String[].class;Session 228932How to…Get an Instance of ClassUse the class nameUse a class literalRetrieve it from an instanceClass strcl = "hello".getClass ();Class strarray = new String[] {}.getClass ();Session 228933How to…Get an Instance of ClassUse the class nameUse a class literalRetrieve it from an instanceUse the constant defined in the wrapper classClass intcl = Integer.TYPE;Session 228934How to…Create an ObjectUse the Class objecttry { Class foocl = Class.forName ("Foo"); Foo f = (Foo) foocl.newInstance ();} catch


View Full Document

UMD CMSC 433 - Reflection

Documents in this Course
Trace 1

Trace 1

62 pages

Testing

Testing

25 pages

Paradigms

Paradigms

10 pages

Testing

Testing

17 pages

Java RMI

Java RMI

17 pages

Java RMI

Java RMI

17 pages

Java RMI

Java RMI

17 pages

Trace 1

Trace 1

46 pages

Jini

Jini

4 pages

Final

Final

15 pages

Java RMI

Java RMI

13 pages

Testing

Testing

16 pages

Load more
Download Reflection
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Reflection and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Reflection 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?