Lecture 28 Old
Lecture 28 Old
Computer Science and Engineering College of Engineering The Ohio State University
Lecture 28
Motivating Problem
Computer Science and Engineering The Ohio State University
Debugger/visualization tool
Takes an object, any object…
Displays the methods one can invoke on
that object
int countMethods(Object o) {
//return the number of methods o has…
//how?
}
Important library elements
Class java.lang.Class
Package java.lang.reflect
The java.lang.Class Class
Computer Science and Engineering The Ohio State University
int m = c.getModifiers();
System.out.println("Class is public: " + Modifier.isPublic(m));
System.out.println("Class is final: " + Modifier.isFinal(m));
System.out.println("Class is abstract: "+Modifier.isAbstract(m));
}
}
$ java GetClassExample
Class name: Employee
Class super class: class java.lang.Object
Class is public: true
Class is final: false
Class is abstract: false
Calling the 0-Argument Constructor
Computer Science and Engineering The Ohio State University
Two steps:
Get an object representing the constructor
See java.lang.reflect.Constructor<T>
Use getConstructor method in Class
Constructor<T>
getConstructor(Class<?>... pTypes);
Call newInstance on that constructor
T newInstance(Object... args);
Varargs syntax (...) allows for an
arbitrary number of arguments of that
type
Syntactic sugar for an array of that type
Example
Computer Science and Engineering The Ohio State University
Class<?> c = Class.forName("Employee");
Class<?>[] paramTypes = {
String.class,
String.class,
int.class };
Object[] args = {
"Fred",
"Flintstone",
new Integer(9000) };
Object o = cons.newInstance(args);
System.out.println("Just made: " + o);
Discovering a Class’s Members
Computer Science and Engineering The Ohio State University
Class<?> c = Class.forName("Employee");
Method[] methods = c.getMethods();
for(Method m : methods) {
System.out.println("Found: " + m );
}
getDeclaredMethod() no no yes
getMethod() no yes no
Class<?> c = Class.forName("Employee");
Class<?>[] pTypes = {int.class};
Method m = c.getMethod("setSalary", pTypes);
System.out.println("Found: " + m);
Accessing Members
Computer Science and Engineering The Ohio State University
Class<?> c = Class.forName("Employee");
Class<?>[] pTypes = {int.class};
Method m = c.getMethod("setSalary", pTypes);
Object theObject = c.newInstance();
Object[] parameters = {new Integer(90000)};
m.invoke(theObject, parameters);
Note:
Above code works (without recompilation) for any
class with:
1. A 0-argument constructor (for newInstance call)
2. A single-parameter (int) method
Just use different strings (eg “Student”, and
“addCredits”)
Does not rely on inheritance relationship or
polymorphism
Security
Computer Science and Engineering The Ohio State University
Class objects
Correspond to types in the program
Obtained from instances, class names, or strings
Provide basic information about class
Provide members of class
Instantiation
Through Class’s newInstance() – no arguments
Through Constructor’s newInstance()
Members
Lists or individual members
Methods can then be invoked
Fields can be accessed
Privacy can be undermined