Download presentation
Presentation is loading. Please wait.
Published byEthel Welch Modified over 9 years ago
1
© Keren Kalif Advanced Java Topics Written by Keren Kalif, Edited by Liron Blecher
2
Agenda Reflection Annotations Dynamic Proxy
3
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
4
The example class Person 4
5
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
6
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
7
Get all data members Getting data members descriptions Get fields type Get field variable name Check if a field is static
8
Get array of all constructors Get array of all the c’tor parameter types Getting constructors descriptions
9
Get array of all methods Get method return type Getting methods descriptions
10
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
11
Method Invocation - Example Get method “foo” without parameters Invoke method on object ‘a’
12
Method invocation – NoSuchMethodException
13
Method invocation - IllegalArgumentException Method expects to have only 2 int parameters
14
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
15
Interactive Invocation Example
16
Interactive Invocation Example – cont.
18
Temporary support for “int” and “String” only. the rest can be added later.
19
Output examples 19
20
Links http://www.javacodegeeks.com/2013/07/javas- reflection-api.html 20
21
DEMO examples.reflection 21
22
Agenda Reflection Annotations Dynamic Proxy
23
23 Annotations A mechanism to add meta-data on fields, methods and classes Several annotations already exists out-of-the-box: Override Deprecated SuppressWarnings - http://www.thebuzzmedia.com/supported- values-for-suppresswarningshttp://www.thebuzzmedia.com/supported- values-for-suppresswarnings
24
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
25 Annotations Example 1: public @interface MyAnnotation { } Usage: public class MyClass { @MyAnnotation public void foo() { }
26
26 Annotations Example 2: public @interface MyAnnotation { String myValue(); } Usage: @MyAnnotation (myValue = “winter is coming”) public class MyClass { @MyAnnotation (“winter is coming”) public void foo() { }
27
27 Annotations Example 3: public @interface MyAnnotation { String myValue(); int myNumber(); } Usage: public class MyClass { @MyAnnotation (myValue = “winter is coming”) public void foo() { } @MyAnnotation (myValue=“winter is coming”,myNumber=5) public void boo() { }
28
28 Annotations Example 4: public @interface MyAnnotation { String myValue(); int myNumber() default 5; } Usage: public class MyClass { @MyAnnotation (myValue = “winter is coming”) public void foo() { } @MyAnnotation (myValue = “winter is coming”,myNumber=5) public void boo() { }
29
29 Annotations Example 5: @Target ({ElementType.Method}) @Retention (RetentionPolicy.Runtime) public @interface MyAnnotation { String myValue(); int myNumber() default 5; } Usage: public class MyClass { @MyAnnotation (myValue = “winter is coming”) public void foo() { } @MyAnnotation (myValue = “winter is coming”,myNumber=5) public void boo() { }
30
DEMO examples.annotations 30
31
Agenda Reflection Annotations Dynamic Proxy
32
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
33
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
34
DEMO examples.dynamicproxy 34
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.