Reflections on Reflection Objects from the inside out...

Slides:



Advertisements
Similar presentations
Reflection Reflection is the ability of a program to examine and modify the structure and behavior of an object at runtime.
Advertisements

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.
Inheritance Inheritance Reserved word protected Reserved word super
JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Java Virtual Machine (JVM). Lecture Objectives Learn about the Java Virtual Machine (JVM) Understand the functionalities of the class loader subsystem.
1 Java (vs. C++). 2 Object-oriented Programming Java and C++ are the most popular object-oriented programming languages Java Sun Microsystems in 1990.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Java object model Part 3: Serialization & Reflection.
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.
Programming Languages and Paradigms Object-Oriented Programming.
SEG4110 – Advanced Software Design and Reengineering TOPIC H Java Reflection.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
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.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
The Reflection Lucielle Mendez Antonio Bologna.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Reflection in Java Bibliografie: Sun: The Java Tutorials – The Reflection API Java programming.
© Keren Kalif Advanced Java Topics Written by Keren Kalif, Edited by Liron Blecher.
10-Nov-15 Java Object Oriented Programming What is it?
Programming in Java CSCI-2220 Object Oriented Programming.
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
2-Dec-15 Inner Classes. 2 Inner classes All the classes so far have been “top level” It is possible (and useful) to define a class inside another class.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
CSCE 314 Programming Languages Reflection Dr. Hyunyoung Lee 1.
Classes, Interfaces and Packages
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Georgia Institute of Technology More on Creating Classes part 3 Barb Ericson Georgia Institute of Technology Nov 2005.
CSE 331 Reflection slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
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.”
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
Inner Classes.
Topic: Inner Classes Course : JAVA PROGRAMMING Paper Code: ETCS-307 Faculty : Dr. Prabhjot Kaur Reader, Dept. of IT 1.
Inner Classes 27-Dec-17.
Chapter 7: Cloning and RTTI
NESTED CLASSES REFLECTION PROXIES.
CSE 413, Autumn 2002 Programming Languages
Presentation on Object Oriented programming Topic
Winter 2018 CMPE212 9/21/2018 CMPE212 – Stuff…
EE 422C Java Reflection re·flec·tion rəˈflekSH(ə)n/ noun
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
Inner Classes 11/14/ Dec-04 inner_classes.ppt.
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
MSIS 655 Advanced Business Applications Programming
Interfaces.
Inheritance Inheritance is a fundamental Object Oriented concept
Java IDE Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
Inner Classes 17-Apr-19.
Inner Classes 21-Apr-19.
Object Oriented Programming
Some Examples Reflection is used for many things:
Inner Classes 11-May-19.
Inner Classes 18-May-19.
CSCE 314: Programming Languages Dr. Dylan Shell
Inner Classes 25-Oct-19.
Presentation transcript:

Reflections on Reflection Objects from the inside out...

Administrivia Course surveys ICES 351-specific survey (help me make the class better) Both anonymous Final exam: Wed, May 11, 3:00-5:00 Usual classroom (Tapy 201) P3 demos Fri, May 13, noon FEC 3rd floor lab I will provide pizza (RSVP w/ requests)

P3: Group evaluations Each individual will assess: Her/his role and performance in group Other group members’ roles/performance Things that went well in P3 Things that went not so well (obstructions, setbacks, confusions, etc.) I will send forms in You mail back to me and Jason Responses are not shared w/ your group members Please be honest

Reflection

What is it/why do we care? Java’s mechanism to allow code to examine types (runtime) Class of object Methods/constructors in class (declared/inherited) Fields in class (declared/inherited) Modifiers for all of above ( private, protected, synchronized, native, static, etc.) Useful for: Development tools Runtime class loading (dynamic loading)

The getClass() call All Object s have a getClass() method Returns a Class (bleh) object Class object gives access to Class name ( getName() ) Constructors ( getConstructors(), etc.) Methods ( getMethods(), etc.) Fields ( getFields(), etc.) Interfaces ( getInterfaces() ) Package membership ( getPackage() ) Etc. (check JDK API)

Using class info Simplest use: inspect class and structures Useful for tools (Eclipse, Ant, etc.) Allows them to load/parse class hierarchy dynamically getConstructors() returns Constructor[] getFields() returns Field[] Etc. Can eventually recurse all the way down and get effectively all info on all classes available at runtime

Class info: basic use public void analyzeObject(Object o) { System.out.println(”class=” + o.getClass().getName());

Class info: basic use public void analyzeObject(Object o) { System.out.println(”class=” + o.getClass().getName()); System.out.println(”public fields:”); Field[] oFields=o.getClass().getFields();

Class info: basic use public void analyzeObject(Object o) { System.out.println(”class=” + o.getClass().getName()); System.out.println(”public fields:”); Field[] oFields=o.getClass().getFields(); for (int i=0;i<oFields.length;++i) { System.out.println(oFields[i].getName() + “:” + oFields[i].getType().getName()); } // etc... }

Declared vs. accessible Many Class getters are split into “declared” and “accessible” versions getDeclaredConstructors() vs. getConstructors() getDeclaredFields() vs. getFields() etc. Declared == all things declared by this class Regardless of accessibility Doesn’t include inherited stuff Non-declared (accessible) == all public stuff accessible in this class Includes inherited stuff Excludes private/protected stuff

AnalyzeDemo

Twiddling bits Reflection is much more powerful than just inspection Can also Create new objects (invoke constructors) Call methods Modify fields Load classes dynamically at runtime

Basic object creation Class.newInstance() Creates a new object of the class represented by this Class object Uses the no-arg constructor Fails (exception) if there is no no-arg constructor or if no-arg constructor is private Can use to duplicate an object if you don’t know its runtime type

TypeDup

Advanced object creation No public no-arg constructor ⇒ can’t use Class.newInstance() Have to use Class.getConstructor() to find publicly accessible constructor Then build argument list for constructor Object[] Then call Constructor.newInstance(argList)

Runtime class loading Want to write a really extensible game Be able to offer “expansion packs” that include: New unit types New terrain types New commands New building types New resource types etc. Can use reflection to load/bind types at runtime

Class.forName() Class offers mechanism for loading a class at runtime.class file need not even exist when program starts Could receive.class file over network, be compiled on the fly, etc. Similar in concept to dynamic libraries/DLLs Java makes it much less painful, though... Class c=Class.forName(”nameOfClass”); Need to use full name of class, incl. package Class must exist on CLASSPATH

DynLoadDemo