C OMP 401 R EFLECTION AND A CTION O BJECTS Instructor: Prasun Dewan.

Slides:



Advertisements
Similar presentations
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Advertisements

Reflection Reflection is the ability of a program to examine and modify the structure and behavior of an object at runtime.
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.
Index Exception handling Exception In Java Exception Types
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
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.
Lecture 28 More on Exceptions COMP1681 / SE15 Introduction to Programming.
C OMP 401 D YNAMIC D ISPATCH AND A BSTRACT M ETHODS Instructor: Prasun Dewan.
C OMP 401 C LASS S TATE Instructor: Prasun Dewan.
C OMP 401 O BJECTS Instructor: Prasun Dewan 2 C OMPUTER VS. P ROGRAM M ODEL Processor Compiler Program (source code)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Advanced Java Course Reflection. Reflection API What if you want to access information not just about the Object, but about that Object’s Class? What.
Exceptions Three categories of errors: Syntax errors Runtime errors Logic errors Syntax errors: rules of the language have not been followed. Runtime error:
06 - Exceptions. 2 ©S. Uchitel, 2004 A familiar sight? Bluescreen.scr.
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.
Scott Grissom, copyright 2004Ch 3: Java Features Slide 1 Why Java? It is object-oriented provides many ready to use classes platform independent modern.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
1 Java object model Part 3: Serialization & Reflection.
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 One-Dimensional Arrays  What are and Why 1-D arrays?  1-D Array Declaration  Accessing elements of a 1-D Array  Initializer List  Passing Array.
16-Aug-15 Java Puzzlers From the book Java Puzzlers by Joshua Bloch and Neal Gafter.
CSC3170 Introduction to Database Systems
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Object Oriented Programming with Java (150704).  Throwable Exception (This class will catch exceptions generated by prog.) (Create your own custom exception.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
SEG4110 – Advanced Software Design and Reengineering TOPIC H Java Reflection.
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.
The Reflection Lucielle Mendez Antonio Bologna.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
C OMP 401: C ONSTRUCTORS AND P OINTERS Instructor: Prasun Dewan (FB 150,
ECE122 Feb. 22, Any question on Vehicle sample code?
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
C OMP 401 A DVANCED G ENERICS Instructor: Prasun Dewan.
WEEK 2 Introduction to Java II CSE 252 Principles of Programming Languages LAB SECTION.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
CSI 3125, Preliminaries, page 1 Compiling the Program.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
Cs2220: Engineering Software Class 12: Substitution Principle Fall 2010 University of Virginia David Evans.
CSE 331 Reflection slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1)
Methods.
Topics Instance variables, set and get methods Encapsulation
C OMP 401 E XCEPTIONS -R EMAINDER Instructor: Prasun Dewan.
Reflection Mehdi Einali Advanced Programming in Java 1.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
C OMP 110/401 D OCUMENTATION : A NNOTATIONS Instructor: Prasun Dewan.
Cs205: engineering software university of virginia fall 2006 Programming Exceptionally David Evans
Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Object Oriented Programming Lecture 2: BallWorld.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. P ART 3: A GGREGATION, I NHERITANCE AND P.
N ESTED C LASSES -1. T YPES OF N ESTED C LASSES & I NTERFACES top-level nested classes interfaces inner classes member local named anonymous.
C OMP 401 D YNAMIC D ISPATCH AND V IRTUAL AND A BSTRACT M ETHODS Instructor: Prasun Dewan.
Chapter 7: Cloning and RTTI
CSE 413, Autumn 2002 Programming Languages
RADE new features via JAVA
Instructor: Prasun Dewan
Pass by Reference, const, readonly, struct
Web Design & Development Lecture 7
class PrintOnetoTen { public static void main(String args[]) {
Generics, Lambdas, Reflections
Chapter 11 Inheritance and Polymorphism Part 2
Presentation transcript:

C OMP 401 R EFLECTION AND A CTION O BJECTS Instructor: Prasun Dewan

2 P REREQUISITES Inheritance

3 T OPICS Type Object Object representing a type of objects Action Object Object representing an action such as “Do”

4 PRINT P ROPERTIES public static void main(String[] args) { BMISpreadsheet bmi = new ABMISpreadsheet(1.77, 75); printProperties(bmi); Point point = new ACartesianPoint(50, 34); printProperties(point); } public static void printProperties(Object object) { … }

5 P ROPERTIES P RINTER

6 C ALLING PRINT P ROPERTIES public static void main(String[] args) { BMISpreadsheet bmi = new ABMISpreadsheet(1.77, 75); printProperties(bmi); Point point = new ACartesianPoint(50, 34); printProperties(point); } printProperties() accepts an argument of arbitrary type

7 C LASS R EFLECTION public static void printProperties(Object object) { System.out.println("Properties of:" + object); Class objectClass = object.getClass(); Method[] methods = objectClass.getMethods(); Object[] nullArgs = {}; for (int index = 0; index < methods.length; index++) { Method method = methods[index]; if (isGetter(method)) { Object retVal = methodInvoke(object, method, nullArgs); System.out.println(propertyName(method) + ":" + retVal); } System.out.println(); } Class Reflection: Invoking methods on a class to create new instance or learn its properties. Class is a runtime variable vs. compile time as in generics.

8 M ETHOD R EFLECTION public static String GETTER_PREFIX = "get"; public static boolean isGetter (Method method) { return method.getParameterTypes().length == 0 && method.getReturnType() != Void.TYPE && method.getName().startsWith(GETTER_PREFIX); } public static String propertyName(Method getter) { return getter.getName().substring(GETTER_PREFIX.length()); } Method Reflection: Invoking methods on a method.

9 I NVOKING T ARGET M ETHOD public static Object methodInvoke(Object object, Method method, Object[] args ) { try { return method.invoke(object, args); } catch (IllegalAccessException e) { e.printStackTrace(); return null; } catch (InvocationTargetException e ) { e.printStackTrace(); return null; } IllegalAccessException: Method not visible to caller. InvocationTargetException: Exception thrown when parameters/target object do not match method or method throws exception such as ClassCastException. Method that takes method as a parameter is 2 nd -order method.

10 Type Object = Embedded Type R EFLECTABLE T YPE O BJECT getName() getMethods() getInterfaces() Provides reflection operations to learn properties of the action such as return type, name, and parameter types. getSuperType() getSubTypes() A supertype does not reference its subtypes. Subtypes can, can be developed all over the world, and thus can never be known. Need not correspond to a compiled class. Can be an XML schema, for instance. newInstance()

11 Action Object = Embedded Operation A CTION O BJECT Provides an execute operation to perform some action. The execute operation takes as parameters the object on which the target operation is to invoked and an array of parameters of the target method. execute (targetObject, params)

12 Action Object = Embedded Operation R EFLECTABLE A CTION O BJECT Provides an execute operation to perform some action. The execute operation takes the object on which the target operation is to invoked and an array of parameters of the target method. execute (targetObject, params) getParameterTypes() getReturnType() Provides additional reflection operations to learn properties of the action such as return type, name, and parameter types. getName () Need not correspond to a compiled method. Can be a Web Ser vice action described by XML.