Presentation is loading. Please wait.

Presentation is loading. Please wait.

Reflections on Reflection Objects from the inside out...

Similar presentations


Presentation on theme: "Reflections on Reflection Objects from the inside out..."— Presentation transcript:

1 Reflections on Reflection Objects from the inside out...

2 Administrivia Course surveys ICES 351-specific survey (help me make the class better) Both anonymous Final exam: Wed, May 11, 3:00-5:00 Usual classroom (Tapy 201) P3 demos Fri, May 13, noon FEC 3rd floor lab I will provide pizza (RSVP w/ requests)

3 P3: Group evaluations Each individual will assess: Her/his role and performance in group Other group members’ roles/performance Things that went well in P3 Things that went not so well (obstructions, setbacks, confusions, etc.) I will send forms in email You mail back to me and Jason Responses are not shared w/ your group members Please be honest

4 Reflection

5 What is it/why do we care? Java’s mechanism to allow code to examine types (runtime) Class of object Methods/constructors in class (declared/inherited) Fields in class (declared/inherited) Modifiers for all of above ( private, protected, synchronized, native, static, etc.) Useful for: Development tools Runtime class loading (dynamic loading)

6 The getClass() call All Object s have a getClass() method Returns a Class (bleh) object Class object gives access to Class name ( getName() ) Constructors ( getConstructors(), etc.) Methods ( getMethods(), etc.) Fields ( getFields(), etc.) Interfaces ( getInterfaces() ) Package membership ( getPackage() ) Etc. (check JDK API)

7 Using class info Simplest use: inspect class and structures Useful for tools (Eclipse, Ant, etc.) Allows them to load/parse class hierarchy dynamically getConstructors() returns Constructor[] getFields() returns Field[] Etc. Can eventually recurse all the way down and get effectively all info on all classes available at runtime

8 Class info: basic use public void analyzeObject(Object o) { System.out.println(”class=” + o.getClass().getName());

9 Class info: basic use public void analyzeObject(Object o) { System.out.println(”class=” + o.getClass().getName()); System.out.println(”public fields:”); Field[] oFields=o.getClass().getFields();

10 Class info: basic use public void analyzeObject(Object o) { System.out.println(”class=” + o.getClass().getName()); System.out.println(”public fields:”); Field[] oFields=o.getClass().getFields(); for (int i=0;i<oFields.length;++i) { System.out.println(oFields[i].getName() + “:” + oFields[i].getType().getName()); } // etc... }

11 Declared vs. accessible Many Class getters are split into “declared” and “accessible” versions getDeclaredConstructors() vs. getConstructors() getDeclaredFields() vs. getFields() etc. Declared == all things declared by this class Regardless of accessibility Doesn’t include inherited stuff Non-declared (accessible) == all public stuff accessible in this class Includes inherited stuff Excludes private/protected stuff

12 AnalyzeDemo

13 Twiddling bits Reflection is much more powerful than just inspection Can also Create new objects (invoke constructors) Call methods Modify fields Load classes dynamically at runtime

14 Basic object creation Class.newInstance() Creates a new object of the class represented by this Class object Uses the no-arg constructor Fails (exception) if there is no no-arg constructor or if no-arg constructor is private Can use to duplicate an object if you don’t know its runtime type

15 TypeDup

16 Advanced object creation No public no-arg constructor ⇒ can’t use Class.newInstance() Have to use Class.getConstructor() to find publicly accessible constructor Then build argument list for constructor Object[] Then call Constructor.newInstance(argList)

17 Runtime class loading Want to write a really extensible game Be able to offer “expansion packs” that include: New unit types New terrain types New commands New building types New resource types etc. Can use reflection to load/bind types at runtime

18 Class.forName() Class offers mechanism for loading a class at runtime.class file need not even exist when program starts Could receive.class file over network, be compiled on the fly, etc. Similar in concept to dynamic libraries/DLLs Java makes it much less painful, though... Class c=Class.forName(”nameOfClass”); Need to use full name of class, incl. package Class must exist on CLASSPATH

19 DynLoadDemo


Download ppt "Reflections on Reflection Objects from the inside out..."

Similar presentations


Ads by Google