Reflections CSC207 – Software Design. Background Turing's great insight: programs are just another kind of data. Source code is text that is interpreted.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Java Virtual Machine (JVM). Lecture Objectives Learn about the Java Virtual Machine (JVM) Understand the functionalities of the class loader subsystem.
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.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Objects and Classes First Programming Concepts. 14/10/2004Lecture 1a: Introduction 2 Fundamental Concepts object class method parameter data type.
Week 4 Recap CSE 115 Spring Formal Parameter Lists Comma-separated list of formal parameters Formal parameters are listed giving the type of the.
Introduction to Java Programming
1 Memory Model of A Program, Methods Overview l Memory Model of JVM »Method Area »Heap »Stack.
OOP Languages: Java vs C++
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 18: 0xCAFEBABE (Java Byte Codes)
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Java and C++, The Difference An introduction Unit - 00.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
5.0 Objects First with Java A Practical Introduction using BlueJ Introduction to Computer Science I Instructor: Allyson Anderson.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
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.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
The Reflection Lucielle Mendez Antonio Bologna.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
OBJECTS AND CLASSES CITS1001. Concepts for this lecture class; object; instance method; parameter; signature data type multiple instances; state method.
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.
Lecture 2 Exception handling Advanced Java Programming 1 dr inż. Wojciech Bieniecki
Chapter 1 Object Orientation: Objects and Classes.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
Object Persistence and Object serialization CSNB534 Asma Shakil.
Lecture 8 February 29, Topics Questions about Exercise 4, due Thursday? Object Based Programming (Chapter 8) –Basic Principles –Methods –Fields.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Polymorphism, Dynamic Typing, and Dynamic Binding Copyright © 2012 by Yong-Gu Lee
Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and web applications Very rich GUI libraries Portability (machine independence) A real Object.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Chapter 4 Software. Chapter 4: Software Generations of Languages Each computer is wired to perform certain operations in response to an instruction. An.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
CSI 3125, Preliminaries, page 1 Compiling the Program.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 15.
Parallel Processing (CS526) Spring 2012(Week 8).  Shared Memory Architecture  Shared Memory Programming & PLs  Java Threads  Preparing the Environment.
Remote Method Invocation by James Hunt, Joel Dominic, and Adam Mcculloch.
CSCE 314 Programming Languages Reflection Dr. Hyunyoung Lee 1.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Classes, Interfaces and Packages
Lesson 1 1 LESSON 1 l Background information l Introduction to Java Introduction and a Taste of Java.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Chapter 3 Implementing Classes
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
METADATA IN.NET Presented By Sukumar Manduva. INTRODUCTION  What is Metadata ? Metadata is a binary information which contains the complete description.
Chapter 1 Object Orientation: Objects and Classes.
7-Nov Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Oct lecture23-24-hll-interrupts 1 High Level Language vs. Assembly.
Java Yingcai Xiao.
JAVA MULTIPLE CHOICE QUESTION.
Introspection the process by which a builder tool finds out which properties, methods, and events a bean supports. searching for classes and methods that.
CS216: Program and Data Representation
Java Primer 1: Types, Classes and Operators
Java Programming Language
CSE 413, Autumn 2002 Programming Languages
Topic: JAVA .Class File Structure
Mobile Development Workshop
Programming paradigms
What time is it?. What time is it? Major Concepts: a data structure model: basic representation of data, such as integers, logic values, and characters.
JAVA Constructors.
CSCE 314: Programming Languages Dr. Dylan Shell
CMSC 202 Exceptions.
Threads and concurrency / Safety
Presentation transcript:

Reflections CSC207 – Software Design

Background Turing's great insight: programs are just another kind of data. Source code is text that is interpreted in a specific way, with certain conventions. Compiled programs are data, too: –Integers and strings are bytes in memory that you interpret a certain way. –Instructions in methods are just bytes, too.

Reflection If programs are just data, then we can write a program that interprets another program. That’s what a compiler or interpreter is(CSC488) We can also build reflection into our languages. –Reflection is the ability to interpret code as data while the program is running. –This leads to “meta-programming”.

Why Study Reflection? Imagine a system that uses plug-ins to change the basic behaviour of the system. Reflection allows you to invoke code without hard-coding the class name. Hence, we can get plug-in names (class names) from config files.

... Really? You probably won’t use this on a daily basis. –The code is bulkier and harder to understand. Understanding reflection will help us understand how Java works. In particular, in CSC209, you’ll work with pointers, so we’d like to understand the concept of “memory”.

How Objects Work

The Class Class Instances of the class Class store information about Java classes (types). –Class name, inheritance relationships, interfaces implemented, methods and members, etc. Can look up the instance of Class for a specific type by name or using an instance of the type.

Showing a Type

Output for Type Example

Examining Class Contents public static void showMembers(String className) throws ClassNotFoundException { Class thisClass = Class.forName(className); Field[] fields = thisClass.getDeclaredFields(); for (Field f : fields) { System.out.println("\t" + f); } Method[] methods = thisClass.getDeclaredMethods(); for (Method m : methods) { System.out.println("\t "+ m); }

Output

Accessing Members How to access members of a specific object? class Field –Encapsulates access to a particular field of instances of a class. –Knows “where the field is” in objects of that class -- so it can get() the value or set() it.

ShowField Code public static void showField( Object obj, String fieldName) { try { Class thisClass = obj.getClass(); Field field = thisClass.getField(fieldName); Object value = field.get(obj); System.out.println(fieldName + " : " + value); } catch (NoSuchFieldException e) { System.out.println("NoSuchField"); System.exit(1); } catch (IllegalAccessException e) { System.out.println("Illegal access"); System.exit(1); }

Point p = new Point("origin", 0, 0); showField( p, "name"); name : origin

Calling Methods Look up a method based on its signature: the name and list of parameter types Specify signature as a comma-separated list of Class objects Call the method, passing in parameters and capturing return value Specify parameters as a comma-separated list of Objects

Summary There is no magic to executing a program! –A class is just a data structure –A method is just a data structure, too A program just contains bytes that are interpreted as instructions to execute. The call stack is another data structure