EE 422C Java Reflection re·flec·tion rəˈflekSH(ə)n/ noun

Slides:



Advertisements
Similar presentations
Java Virtual Machine (JVM). Lecture Objectives Learn about the Java Virtual Machine (JVM) Understand the functionalities of the class loader subsystem.
Advertisements

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.
Slides 4/22 COP Topics Final Exam Review Final Exam The final exam is Friday, April 29 th at 10:00 AM in the usual room No notes, books, calculators,
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Problem Solving #3: JVM ICS Outline Review of Key Topics Review of Key Topics Problem 1 Problem 1 Problem 2 Problem 2 Problem 3 Problem 3 Problem.
Unit 061 Java Virtual Machine (JVM) What is Java Virtual Machine? The Class Loader Subsystem Linking oVerification oPreparation oResolution Class Initialization.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Advanced Java Programming Lecture 5 Reflection dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Object Oriented Programming in Java Lecture 13. Java Reflection Reflection is a feature unique to Java that allows an executing program to examine or.
Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes.
Lecture objectives  Differences between Java and C#  Understand and use abstract classes  Casting in Java: why is it important?  Master exceptions.
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
© Keren Kalif Advanced Java Topics Written by Keren Kalif, Edited by Liron Blecher.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Sadegh Aliakbary Sharif University of Technology Fall 2012.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CSCE 314 Programming Languages Reflection Dr. Hyunyoung Lee 1.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Generics.
Chapter 7: Cloning and RTTI
Advanced Programming in Java
Advanced Programming in Java
Chapter 11 Inheritance and Polymorphism
Objects as a programming concept
Inheritance and Polymorphism
Dynamic Proxy Proxy: Addition to the Java 1.3 reflection package:
Chapter 3: Using Methods, Classes, and Objects
University of Central Florida COP 3330 Object Oriented Programming
Generics, Lambdas, Reflections
Presentation on Object Oriented programming Topic
Continuing Chapter 11 Inheritance and Polymorphism
Chapter 9 Inheritance and Polymorphism
null, true, and false are also reserved.
Advanced Programming in Java
Java Programming Language
Classes & Objects: Examples
Java Programming Course
Advanced Programming in Java
Generics, Lambdas, Reflections
Chapter 14 Abstract Classes and Interfaces
CMPE212 – Reminders Assignment 3 due next Friday.
CIS 199 Final Review.
Chapter 8 Class Inheritance and Interfaces
Chapter 11 Inheritance and Polymorphism Part 2
Some Examples Reflection is used for many things:
CSCE 314: Programming Languages Dr. Dylan Shell
Review for Midterm 3.
Presentation transcript:

EE 422C Java Reflection re·flec·tion rəˈflekSH(ə)n/ noun noun: reflection 1. Image of oneself, useful for self-examination.

Agenda Reflection https://docs.oracle.com/javase/tutorial/reflect/index.html

Classes Every object is a reference type or primitive Reference types inherit from java.lang.Object. classes, interfaces, arrays, enumerated types are reference types. int, double, long, boolean etc. are primitive types. The JVM instantiates an instance of java.lang.Class for every type of object. Can examine the runtime properties of the object.

Modifier Properties Access modifiers: Modifier requiring override: public, protected, and private Modifier requiring override: abstract Modifier restricting to one instance: static Modifier prohibiting value modification: final

Class members -- fields getting field modifiers, field types, field values private etc. String, double etc. “foo”, 3.0 etc. setting field values

Class members – methods & constructors List the methods and their types name, modifiers, parameters, return type, and list of throwable exceptions. Invoke method with specified parameters Constructors List the constructors and their types modifiers (private, protected constructors also allowed) instantiate an object

Why do we need reflection? We will use reflection in Critters Find a class matching a name. The name is provided as a String at runtime. Instantiate the class makeCritter method Invoke methods on the instantiated class runstats

Starting point -- Class For every type of object, the Java virtual machine instantiates an instance of java.lang.Class There is only one instance of Class per type of object.

How to get a constructor? How do we usually know what methods are available for a class, and what their parameters are? We look at the documentation. How can we determine if a method is static or not? We can do it using reflection.

Retrieving Class Objects Depends on whether the code has access to A class name A class object A class type An existing Class

Class object retrieval from name Static method using fully-qualified name e.g. Class c = Class.forName(”project4.Algae"); }

Class object retrieval from object instance Class<?> c = "foo".getClass(); c points to the String Class object that the JVM has already created. e.g. Find all instances of Critter myCrit in population Critter myCrit = …; for (Critter crit: population) { if (myCrit.getClass().isInstance(crit)) { myList.add(crit); }

Class object retrieval from type or primitive Use when Class type is available, or use for primitive types. Class c = boolean.class; e.g. Get a method called runstats that has the parameter List<of something>. List is the type. Method method = myClass.getMethod("runStats", List.class);

Generics and Class All types of List have only 1 Class object. List<String>, List<Integer> have the same Class object. For example, the runstats method takes a List of Critters as a parameter. So when getting that method using <Critter’s class>.getMethod, you must pass in List.class as the parameter to getMethod, not List<Critter>.class. If you have a List object, use listObject.getClass() instead of List.class All types of ArrayList have a Class object that is different from List’s class object.

Check if your named class is a concrete Critter Is it a Critter? Use instanceof (if you have instance available) or Subclass.class.isAssignableFrom(Critter.class). Throw InvalidCritterException if not. Is it concrete (not abstract)? Catch InstantiationException while trying to instantiate the class. Throw InvalidCritterException in catch block.