150 likes | 165 Views
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
E N D
EE 422CJava 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 • 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.
Modifier Properties • Access modifiers: • public, protected, and private • Modifier requiring override: • abstract • Modifier restricting to one instance: • static • Modifier prohibiting value modification: • final
Class members -- fields • Fields • getting field modifiers, field types, field values • private etc. • String, double etc. • “foo”, 3.0 etc. • setting field values
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
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
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.
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.
Retrieving Class Objects • Depends on whether the code has access to • A class name • A class object • A class type • An existing Class
Class object retrieval from name • Static method using fully-qualified name e.g. Class c = Class.forName(”project4.Algae"); } }
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); } }
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);
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.
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.