1 / 15

EE 422C Java Reflection

EE 422C Java Reflection. re·flec·tion rəˈflekSH(ə)n/ noun noun: reflection 1 . Image of oneself, useful for self-examination. Agenda. Reflection https://docs.oracle.com/javase/tutorial/reflect/index.html. Classes. Every object is a reference type or primitive

ibowers
Download Presentation

EE 422C Java Reflection

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. EE 422CJava Reflection re·flec·tion rəˈflekSH(ə)n/ noun noun: reflection 1. Image of oneself, useful for self-examination.

  2. Agenda • Reflection https://docs.oracle.com/javase/tutorial/reflect/index.html

  3. Classes • Every object is a reference type or primitive • Reference types inherit from java.lang.Object. • classes, interfaces, arrays, enumerated types are reference types. • int, double, long, boolean etc. are primitive types. • The JVM instantiates an instance of java.lang.Class for every type of object. • Can examine the runtime properties of the object.

  4. Modifier Properties • Access modifiers: • public, protected, and private • Modifier requiring override: • abstract • Modifier restricting to one instance: • static • Modifier prohibiting value modification: • final

  5. Class members -- fields • Fields • getting field modifiers, field types, field values • private etc. • String, double etc. • “foo”, 3.0 etc. • setting field values

  6. Class members – methods & constructors • Methods • List the methods and their types • name, modifiers, parameters, return type, and list of throwable exceptions. • Invoke method with specified parameters • Constructors • List the constructors and their types • modifiers (private, protected constructors also allowed) • instantiate an object

  7. Why do we need reflection? • We will use reflection in Critters • Find a class matching a name. • The name is provided as a String at runtime. • Instantiate the class • makeCritter method • Invoke methods on the instantiated class • runstats

  8. Starting point -- Class • For every type of object, the Java virtual machine instantiates an instance of java.lang.Class • There is only one instance of Class per type of object.

  9. How to get a constructor? • How do we usually know what methods are available for a class, and what their parameters are? • We look at the documentation. • How can we determine if a method is static or not? • We look at the documentation. • We can do it using reflection.

  10. Retrieving Class Objects • Depends on whether the code has access to • A class name • A class object • A class type • An existing Class

  11. Class object retrieval from name • Static method using fully-qualified name e.g. Class c = Class.forName(”project4.Algae"); } }

  12. Class object retrieval from object instance • From object instance Class<?> c = "foo".getClass(); c points to the StringClass object that the JVM has already created. e.g. Find all instances of Critter myCrit in population Critter myCrit = …; for (Critter crit: population) { if (myCrit.getClass().isInstance(crit)) { myList.add(crit); } }

  13. Class object retrieval from type or primitive • Use when Class type is available, or use for primitive types. Class c = boolean.class; e.g. Get a method called runstats that has the parameter List<of something>. List is the type. Method method = myClass.getMethod("runStats", List.class);

  14. Generics and Class • All types of List have only 1 Class object. • List<String>, List<Integer> have the same Class object. • For example, the runstats method takes a List of Critters as a parameter. So when getting that method using <Critter’s class>.getMethod, you must pass in List.class as the parameter to getMethod, not List<Critter>.class. • If you have a List object, use listObject.getClass() instead of List.class • All types of ArrayList have a Class object that is different from List’s class object.

  15. Check if your named class is a concrete Critter • Is it a Critter? • Use instanceof (if you have instance available) or Subclass.class.isAssignableFrom(Critter.class). Throw InvalidCritterException if not. • Is it concrete (not abstract)? • Catch InstantiationException while trying to instantiate the class. Throw InvalidCritterException in catch block.

More Related