Java Reflection: Manipulating Classes, Methods, and Fields at Runtime, Study Guides, Projects, Research of Programming Languages

An introduction to java reflection, a technology that allows classes, methods, and fields to be manipulated at runtime. It covers the basics of reflection, including how to determine fields and methods of a class, instantiate classes, invoke methods, and work with fields. The document also includes a warning about the sparing use of reflection and provides examples of reflection in action.

Typology: Study Guides, Projects, Research

Pre 2010

Uploaded on 07/30/2009

koofers-user-y3l
koofers-user-y3l 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CMSC 433 – Programming Language
Technologies and Paradigms
Spring 2004
Reflection
April 27, 2004
2
Reflection
Remember project 1?
Used reflection to find module class
http://webserver:port/HelloWorld/Foo
We’ll cover some basics of reflection
Highly condensed version of
Reflection: Java Technology’s Secret Weapon, by
Odendahl (on web page)
3
What is Reflection?
Makes classes, methods, and fields objects
that can be manipulated at run time
Can determine fields and methods of class
Can instantiate class given a String containing
its name
Can invoke methods given a String with name
Can create classes an runtime
4
What Reflection Isn’t
Doesn’t add any power to the language
Given access to all the source code
Not the solution to every problem
Use sparingly, if at all
5
java.lang.Class
Object of type Class represents a class
Useful for
Making instances of a class
Getting information about fields/methods
Most uses of reflection start with a Class
Primitive types also have a Class
e.g., int.class
6
Getting Some Class
Use Object method getClass()
Class c = “hello”.getClass();
Use class literal
Class c = String.class;
Use the class name
Class c = Class.forName(“java.lang.String”)
pf2

Partial preview of the text

Download Java Reflection: Manipulating Classes, Methods, and Fields at Runtime and more Study Guides, Projects, Research Programming Languages in PDF only on Docsity!

CMSC 433 – Programming Language

Technologies and Paradigms

Spring 2004

Reflection April 27, 2004 2

Reflection

  • Remember project 1?
    • Used reflection to find module class
      • http://webserver:port/HelloWorld/Foo
  • We’ll cover some basics of reflection
    • Highly condensed version of
      • Reflection: Java Technology’s Secret Weapon, by Odendahl (on web page) 3

What is Reflection?

  • Makes classes, methods, and fields objects that can be manipulated at run time - Can determine fields and methods of class - Can instantiate class given a String containing its name - Can invoke methods given a String with name - Can create classes an runtime 4

What Reflection Isn’t

  • Doesn’t add any power to the language
    • Given access to all the source code
  • Not the solution to every problem
    • Use sparingly, if at all 5

java.lang.Class

  • Object of type Class represents a class
    • Useful for
      • Making instances of a class
      • Getting information about fields/methods
    • Most uses of reflection start with a Class
  • Primitive types also have a Class
    • e.g., int.class 6

Getting Some Class

  • Use Object method getClass()
    • Class c = “hello”.getClass();
  • Use class literal
    • Class c = String.class;
  • Use the class name
    • Class c = Class.forName(“java.lang.String”)

7

Making Objects

  • Class object for no-arg constructor Class c; Foo f = (Foo) c.newInstance();
  • Constructor object otherwise Class c; Class[] cArg = { String.class }; Constructor cons = c.getConstructor(cArg); Object[] consArg = { “hello” }; Foo f = (Foo) cons.newInstance(consArg); 8

Working with Fields

  • Can get Field objects from Class
    • Can also get all fields in Class Class c = x.getClass(); // get class of obj x Field f = c.getField(“foo”); ...(Type-of-foo) f.get(x); ... ...f.set(x, value);... 9

Invoking Methods

  • Get from Class object
    • Invoke just like constructor Class c = x.getClass(); Class[] cArg = { String.class }; Method m = c.getMethod(“bar”, cArg); Object[] mArg = { “hello” }; Foo f = (m-result-type) m.invoke(x, mArg); 10

Putting It All Together

  • Example from JavaOne slides: public static void main(String [] args) throws Exception { Field f = System.class.getField(“out”); PrintStream out = (PrintStream) f.get(null); Class[] paramTypes = { String.class }; Method m = PrintStream.class.getMethod (“println”, paramTypes); String[] params = (String[]) Array.newInstance (String.class, 1); Array.set(params, 0, “Hello, world!”); m.invoke(out, params); }