© Keren Kalif Advanced Java Topics Written by Keren Kalif, Edited by Liron Blecher.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

Basic -2 Classes and Objects. Classes and Objects A class is a complex data TYPE An object is an instance of a class. Example: Class: Person Objects:
Java Review Interface, Casting, Generics, Iterator.
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.
Java Annotations. Annotations  Annotations are metadata or data about data. An annotation indicates that the declared element should be processed in.
JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Enhancing classes Visibility modifiers and encapsulation revisited
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
Creating Classes from Other Classes Chapter 2. 2 Chapter Contents Composition Adapters Inheritance Invoking constructors from within constructors Private.
1 Java object model Part 3: Serialization & Reflection.
Declaring and Checking Non-null Types in an Object-Oriented Language Authors: Manuel Fahndrich K. Rustan M. Leino OOPSLA’03 Presenter: Alexander Landau.
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.
Java CourseWinter 2009/10. Introduction Object oriented, imperative programming language. Developed: Inspired by C++ programming language.
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.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
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.
Java Implementation: Part 3 Software Construction Lecture 8.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Computer Science and Engineering College of Engineering The Ohio State University Interfaces The credit for these slides goes to Professor Paul Sivilotti.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
Java Dynamic Proxy Bibliography: reflection/proxy.html
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Sadegh Aliakbary Sharif University of Technology Fall 2012.
Chapter 3 Collections. Objectives  Define the concepts and terminology related to collections  Explore the basic structures of the Java Collections.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Object Oriented Programming
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
Java 1 Introduction Why annotations?  Enhance ease-of-development  Shift some code generation from programmer to compiler What are annotations?
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Java Annotations. Annotations  Annotations are metadata or data about data. An annotation indicates that the declared element should be processed in.
CSCE 314 Programming Languages Reflection Dr. Hyunyoung Lee 1.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
OOP Tirgul 10. What We’ll Be Seeing Today  Generics – A Reminder  Type Safety  Bounded Type Parameters  Generic Methods  Generics and Inner Classes.
Design issues for Object-Oriented Languages
Topic: Classes and Objects
Chapter 7: Cloning and RTTI
Objects as a programming concept
Inheritance and Polymorphism
Dynamic Proxy Proxy: Addition to the Java 1.3 reflection package:
Generics, Lambdas, Reflections
CompSci 230 Software Construction
Presentation on Object Oriented programming Topic
EE 422C Java Reflection re·flec·tion rəˈflekSH(ə)n/ noun
Overloading and Constructors
Enumerations & Annotations
Enumerations & Annotations
Polymorphism and access control
Implementing Non-Static Features
Enumerations & Annotations
Java Programming Course
Generics, Lambdas, Reflections
Winter 2019 CMPE212 5/25/2019 CMPE212 – Reminders
CSCE 314: Programming Languages Dr. Dylan Shell
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Presentation transcript:

© Keren Kalif Advanced Java Topics Written by Keren Kalif, Edited by Liron Blecher

Agenda Reflection Annotations Dynamic Proxy

What is Reflection? Reflection is a mechanism in Java that allows inspection and manipulation of an object or class without the need to know what is the type of the object or class Allows to get the structure of an object such as its fields (data members), methods, constructors, etc. Enables to run a method without knowing the exact type of the object it will run on Another ability it to change (at runtime) the access modifier of methods and fields to allow changing them 3

The example class Person 4

Class class The Class class is the root class for other reflection data classes (such as Field, Method, etc.) It’s a class that describes classes It contains the description of how an object is constructed – but not the data itself Each object (static or not) and class are linked (at runtime, by the JVM) with an instance of the Class object which describes them There are 2 ways to get the Class for an object: 1. Call getClass() on the object itself 2. Use the static method Class.forName (“…full class name”) – if the class does not exists in the class path, the ClassNotFoundException will be thrown 5

Returns the modifiers of the class (as bits) Check the modifier data (bits in the form of an int) and return true/false accordingly Class class - example 6

Get all data members Getting data members descriptions Get fields type Get field variable name Check if a field is static

Get array of all constructors Get array of all the c’tor parameter types Getting constructors descriptions

Get array of all methods Get method return type Getting methods descriptions

Method invocation Once you have a Method object (which defined a method signature) you can invoke it on any object that has such a method The class we’ll be working with: 10

Method Invocation - Example Get method “foo” without parameters Invoke method on object ‘a’

Method invocation – NoSuchMethodException

Method invocation - IllegalArgumentException Method expects to have only 2 int parameters

Instantiating new objects Class.forName does not work on primitive types (int, boolean, etc.) This is a utility class to enable dynamic conversion of primitive names to their class class) 14

Interactive Invocation Example

Interactive Invocation Example – cont.

Temporary support for “int” and “String” only. the rest can be added later.

Output examples 19

Links reflection-api.html 20

DEMO examples.reflection 21

Agenda Reflection Annotations Dynamic Proxy

23 Annotations A mechanism to add meta-data on fields, methods and classes Several annotations already exists out-of-the-box: Override Deprecated SuppressWarnings - values-for-suppresswarningshttp:// values-for-suppresswarnings

24 Annotations Examples in following slides… Annotations can be read during: Compilation time Runtime (using reflection) You can choose which types are allowed to use the annotation using: Target Retention Policy

25 Annotations Example 1: MyAnnotation { } Usage: public class MyClass public void foo() { }

26 Annotations Example 2: MyAnnotation { String myValue(); } (myValue = “winter is coming”) public class MyClass (“winter is coming”) public void foo() { }

27 Annotations Example 3: MyAnnotation { String myValue(); int myNumber(); } Usage: public class MyClass (myValue = “winter is coming”) public void foo() { (myValue=“winter is coming”,myNumber=5) public void boo() { }

28 Annotations Example 4: MyAnnotation { String myValue(); int myNumber() default 5; } Usage: public class MyClass (myValue = “winter is coming”) public void foo() { (myValue = “winter is coming”,myNumber=5) public void boo() { }

29 Annotations Example (RetentionPolicy.Runtime) MyAnnotation { String myValue(); int myNumber() default 5; } Usage: public class MyClass (myValue = “winter is coming”) public void foo() { (myValue = “winter is coming”,myNumber=5) public void boo() { }

DEMO examples.annotations 30

Agenda Reflection Annotations Dynamic Proxy

Dynamic Proxy is a mechanism that allows a class to mimic an interface without the need to explicitly implement all the interfaces methods. By using reflection, only a single method is invoked when a method is called The single method will contain the method name and parameters thus allowing the Proxy to know which method was called Dynamic Proxies are used to wrap network communications, performance testing, logging, security and more 32

Dynamic Proxy When creating a Dynamic Proxy, a new class is created in runtime that can be casted to the interface it mimics – after that, all other classes will see it as an implementation of that interface To create an instance of a Dynamic Proxy, the interface InvocationHandler should be implemented; It only has one method - invoke that is going to be called whenever an object calls a method on the proxy. To create the actual proxy object, use Proxy.newProxyInstance 33

DEMO examples.dynamicproxy 34