Java Reflection: Manipulating Classes, Methods, and Fields at Runtime - Prof. Alan L. Suss, Study notes of Programming Languages

An introduction to java reflection, a powerful feature that allows classes, methods, and fields to be manipulated at runtime. Reflection enables determining fields and methods of a class, instantiating classes dynamically, invoking methods, and creating classes at runtime. This document also covers the basics of the java.lang.class object and provides examples of making objects, working with fields, and invoking methods using reflection.

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-izl
koofers-user-izl 🇺🇸

9 documents

1 / 5

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)
pf3
pf4
pf5

Partial preview of the text

Download Java Reflection: Manipulating Classes, Methods, and Fields at Runtime - Prof. Alan L. Suss and more Study notes 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

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); }