CSE 413, Autumn 2002 Programming Languages

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

Reflection Reflection is the ability of a program to examine and modify the structure and behavior of an object at runtime.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Java Virtual Machine (JVM). Lecture Objectives Learn about the Java Virtual Machine (JVM) Understand the functionalities of the class loader subsystem.
10/18/08 Matt Swatzell. What is Reflection?  Some Definitions….  Reflection is the process by which a program can modify its own behavior.  A program.
Slide 11.1 Advanced Programming 2004, based on LY Stefanus’s Slides Reflection Reflection is the ability for a class or object to examine itself. Java.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Road Map Introduction to object oriented programming. Classes
1 Java Reflection. 2 Java looking at Java l One of the unusual capabilities of Java is that a program can examine itself »You can determine the class.
JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.
Java Virtual Machine (JVM). Lecture Objectives Learn about the Java Virtual Machine (JVM) Understand the functionalities of the class loader subsystem.
1 Java Object Model Part 1. 2 Type Definition: set of values – a set of values and set of operations –a set of operations that can be applied to those.
1 Java Reflection. 2 Java looking at Java l One of the unusual capabilities of Java is that a program can examine itself »You can determine the class.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Java reflections API Lecture 1. 2 Assignments Java 1.4 or Java 1.5? Both are ok, but the assignments are written for Java 1.4, and specific Java 1.5.
Unit 061 Java Virtual Machine (JVM) What is Java Virtual Machine? The Class Loader Subsystem Linking oVerification oPreparation oResolution Class Initialization.
13-Jul-15 Reflection. 2 Java looking at Java One of the unusual capabilities of Java is that a program can examine itself You can determine the class.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
OOP Languages: Java vs C++
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
SEG4110 – Advanced Software Design and Reengineering TOPIC H Java Reflection.
NSIT,Jetalpur CORE JAVA CONCEPTS SURABHI MISHRA (LCE)NSIT.
Java Reflection. Compile-Time vs Run-Time Some data you know at compile time: int area = radius*radius*3.14; The “3.14” is set – known at compile time.
Object Oriented Programming in Java Lecture 13. Java Reflection Reflection is a feature unique to Java that allows an executing program to examine or.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
The Reflection Lucielle Mendez Antonio Bologna.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
CMSC 341 Java Packages, Classes, Variables, Expressions, Flow Control, and Exceptions.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
CS884 (Prasad)java11Extn1 Java 1.1 Extensions Nested classes static public/protected/private.
CSCE 314 Programming Languages Reflection Dr. Hyunyoung Lee 1.
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
03G-1 Reflection In Java “Reflection is the ability of a program to manipulate as data something representing the state of the program during its own execution.”
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Eclipse.
Modern Programming Tools And Techniques-I
Chapter 7: Cloning and RTTI
NESTED CLASSES REFLECTION PROXIES.
JAVA MULTIPLE CHOICE QUESTION.
Java Programming Course
3 Introduction to Classes and Objects.
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
Presentation on Object Oriented programming Topic
CSC 113 Tutorial QUIZ I.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 10 Thinking in Objects
Advanced Programming in Java
The Language Package.
An Introduction to Java – Part I, language basics
Week 6 Object-Oriented Programming (2): Polymorphism
Inheritance Inheritance is a fundamental Object Oriented concept
Core Concepts.
Constructors, GUI’s(Using Swing) and ActionListner
Advanced Inheritance Concepts
Java Programming Language
Some Examples Reflection is used for many things:
CSCE 314: Programming Languages Dr. Dylan Shell
Introduction to java Part I By Shenglan Zhang.
Presentation transcript:

CSE 413, Autumn 2002 Programming Languages Reflection CSE 413, Autumn 2002 Programming Languages http://www.cs.washington.edu/education/courses/413/02au/ 27-November-2002 cse413-20-Reflection © 2002 University of Washington

Readings and References Chapter 5, Inheritance, Section: Reflection ,Core Java Volume 1, by Horstmann and Cornell Other References "The Reflection API", Java tutorial http://java.sun.com/docs/books/tutorial/reflect/index.html 27-November-2002 cse413-20-Reflection © 2002 University of Washington

Classes from another viewpoint Ordinarily we deal with specific classes that perform specific functions that are known at compile time The Scanner class is used to read through a source line in the D language and return Token objects Sometimes we want to deal with a group of classes in a general sense one or more classes that perform a particular task but whose names are not known at compile time 27-November-2002 cse413-20-Reflection © 2002 University of Washington

cse413-20-Reflection © 2002 University of Washington Reflection API The ability to treat classes as data is provided by the classes in package java.lang and java.lang.reflect The Reflection API is used to build programs that work with classes as data objects development tools such as debuggers, class browsers, and application builders programs with dynamic behavior enabled by providing additional class files and one class that knows how to discover and use the added classes 27-November-2002 cse413-20-Reflection © 2002 University of Washington

Example: User Interface builder A GUI builder may allow the end-user to select a Button from a menu of components, menu built by scanning a directory for class files create the Button object, object created by invoking the constructor, but we don't know the name of the class until runtime then click the Button to request an action. invoke a method on the newly created object 27-November-2002 cse413-20-Reflection © 2002 University of Washington

Example: Application builder 27-November-2002 cse413-20-Reflection © 2002 University of Washington

cse413-20-Reflection © 2002 University of Washington The Class class Java runtime system maintains information about each class in your program while it is running The information is maintained in objects of type java.lang.Class Class is a class, just like String, Scanner, Integer, etc. Objects of type Class store information about every class in your program 27-November-2002 cse413-20-Reflection © 2002 University of Washington

All types are represented with Class Instances of the class Class represent classes and interfaces in a running Java application. Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element type and number of dimensions. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are represented as Class objects. 27-November-2002 cse413-20-Reflection © 2002 University of Washington

cse413-20-Reflection © 2002 University of Washington Some methods Object class Class getClass() Returns the runtime class of an object. Class class String getName() Returns the name of the entity (class, interface, array class, primitive type, or void) represented by this Class object, as a String. Class getSuperclass() Returns the Class representing the superclass of the entity (class, interface, primitive type or void) represented by this Class. 27-November-2002 cse413-20-Reflection © 2002 University of Washington

Example: print class hierarchy public class SampleSuper { public static void main(String[] args) { Object o = System.out; // object to analyze Class subclass = o.getClass(); Class superclass = subclass.getSuperclass(); System.out.println(subclass.getName()); while (superclass != null) { String className = superclass.getName(); System.out.println(className); subclass = superclass; superclass = subclass.getSuperclass(); } java.io.PrintStream java.io.FilterOutputStream java.io.OutputStream java.lang.Object 27-November-2002 cse413-20-Reflection © 2002 University of Washington

More Class information is available Class[] getInterfaces() Determines the interfaces implemented by the class or interface represented by this object. Constructor[] getConstructors() Returns an array containing Constructor objects reflecting all the public constructors of the class represented by this Class object. Field[] getFields() Returns an array containing Field objects reflecting all the accessible public fields of the class or interface represented by this Class object. Method[] getMethods() Returns an array containing Method objects reflecting all the public member methods of the class or interface represented by this Class object ReflectionTest.java 27-November-2002 cse413-20-Reflection © 2002 University of Washington

Create new object from class name static Class forName(String className) Returns the Class object associated with the class or interface with the given string name. Object newInstance() Creates a new instance of the class represented by this Class object. Calls the default constructor (the zero-argument constructor) String className = "java.util.Random"; Class classDefinition = Class.forName(className); Object object = classDefinition.newInstance(); SampleNoArg.java 27-November-2002 cse413-20-Reflection © 2002 University of Washington

cse413-20-Reflection © 2002 University of Washington A note of caution The reflection capability is very handy for certain high-level applications or particular functions in a normal application eg, building a menu item list or options list Don't use it when other tools more natural to the Java programming language would suffice Specifically, provide callback objects by defining interfaces and implementing them in one or more classes. Do not use the Method objects to create elaborate C-style function pointers. 27-November-2002 cse413-20-Reflection © 2002 University of Washington