Presentation is loading. Please wait.

Presentation is loading. Please wait.

EE 422C Java Reflection re·flec·tion rəˈflekSH(ə)n/ noun

Similar presentations


Presentation on theme: "EE 422C Java Reflection re·flec·tion rəˈflekSH(ə)n/ noun"— Presentation transcript:

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

2 Agenda Reflection

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: Modifier requiring override:
public, protected, and private Modifier requiring override: abstract Modifier restricting to one instance: static Modifier prohibiting value modification: final

5 Class members -- 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
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 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
Class<?> c = "foo".getClass(); c points to the String Class 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.


Download ppt "EE 422C Java Reflection re·flec·tion rəˈflekSH(ə)n/ noun"

Similar presentations


Ads by Google