© Luxoft Training 2013 Using Reflection API in Java.

Презентация:



Advertisements
Похожие презентации
© Luxoft Training 2013 Annotations. © Luxoft Training 2013 Java reflection / RTTI // given the name of a class, get a "Class" object that // has all info.
Advertisements

1 © Luxoft Training 2012 Inner and anonymous classes.
2005 Pearson Education, Inc. All rights reserved. 1 Object-Oriented Programming: Interface.
© Luxoft Training 2013 Java Collections API. © Luxoft Training 2013 Collections hierarchy.
Inner Classes. 2 Simple Uses of Inner Classes Inner classes are classes defined within other classes The class that includes the inner class is called.
Unit II Constructor Cont… Destructor Default constructor.
1/27 Chapter 9: Template Functions And Template Classes.
1/30 Chapter 8: Dynamic Binding And Abstract classes.
2005 Pearson Education, Inc. All rights reserved. 1 Object-Oriented Programming: Polymorphism.
2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
1 © Luxoft Training 2012 Java basics Module 2. 2 © Luxoft Training 2012 Running Java application.
Evgeniy Krivosheev Vyacheslav Yakovenko Last update: Feb, 2012 Spring Framework Module 4 – JNDI.
In mathematics, the notion of permutation is used with several slightly different meanings, all related to the act of permuting (rearranging) objects.
Initialization & Cleanup Dr. Ramzi Saifan Slides adapted from Prof. Steven Roehrig.
Arrays Dr. Ramzi Saifan Slides adapted from Prof. Steven Roehrig.
Section 2.1: Use Inductive Reasoning Conjecture: A conjecture is an unproven statement that is based on observations; an educated guess. Inductive Reasoning:
Operator Overloading Customised behaviour of operators Chapter: 08 Lecture: 26 & 27 Date:
Exception Handling 1. Introduction Users may use our programs in an unexpected ways. Due to design errors or coding errors, our programs may fail in unexpected.
© 2005 Cisco Systems, Inc. All rights reserved.INTRO v Module Summary The Cisco Discovery Protocol is an information-gathering tool used by network.
Java Collections Framework (JCF) in Java Tutorial for students of universities Author: Oxana Dudnik.
Транксрипт:

© Luxoft Training 2013 Using Reflection API in Java

© Luxoft Training 2013 Introduction Reflection or introspection is the ability of a program to examine and modify its structure and behavior Programming paradigm in the foundation of reflection is called reflective programming Java Reflection API can view information about classes, interfaces, methods, fields, constructors, and annotations during Java program runtime. You dont need to know names of inspected elements to do that 7-2 What is Reflection

© Luxoft Training 2013 Class introspection By using reflection you can retrieve the following information about a class: Class name Access modifiers Package information Superclasses Implemented interfaces If you know the class name you can also load and instantiate it 7-3 Introduction

© Luxoft Training 2013 Class introspection Before you inspect the class, you need to get the java.lang.Class object that describes the class All java classes (including primitives and arrays) have associated java.lang.Class java.lang.Class can be retrieved in three ways: Class myObjectClass = myObject.getClass() Class myClass = MyClass.class If you know the class name you can also load it: 7-4 The Class object

© Luxoft Training 2013 Class introspection Full class name: Class name without package: 7-5 Getting class name

© Luxoft Training 2013 Class introspection Using you can get a bitmask that defines access modifiers of a class. Using the group of methods Modifiers.isAbstract(int modifiers), Modifiers.isFinal(int modifiers), Modifiers.isInterface(int modifiers) etc., you can check the byte mask and find the corresponding modifier 7-6 Getting class modifiers

© Luxoft Training 2013 Class introspection Package package = aClass.getPackage() The package object describes package information such as package name 7-7 Getting the information about package

© Luxoft Training 2013 Class introspection Class superclass = aClass.getSuperclass(); You can go up the hierarchy to the Object class. 7-8 Getting the superclass

© Luxoft Training 2013 Class introspection Class[] interfaces = aClass.getInterfaces(); If this object represents a class, the return value is an array containing objects representing all interfaces implemented by the class. The order of the interface objects in the array corresponds to the order of the interface names in the implements clause of the declaration of the class represented by this object. For example, given the declaration: class Shimmer implements FloorWax, DessertTopping {... } suppose the value of s is an instance of Shimmer; the value of the expression: s.getClass().getInterfaces()[0] is the Class object that represents interface FloorWax; the value of: s.getClass().getInterfaces()[1] is the Class object represents interface DessertTopping. 7-9 Getting the implemented interfaces

© Luxoft Training 2013 Class introspection Constructors, methods, fields, and annotations can be received using 7-10 Getting other class elements The Constructor, Method, Field, Annotation classes are used to describe corresponding elements

© Luxoft Training 2013 Creating New Class Instance The Class newInstance() class method creates a class instance that describes this Class This is similar to creating an object using the new statement and to calling a no-argument constructor: 7-11 Class.newInstance()

© Luxoft Training 2013 Creating New Class Instance May throw exceptions: IllegalAccessException when a class or its no-argument constructor are not available InstantiationException if Class object represents an abstract class, an interface, an array class, a primitive or void ExceptionInInitializerError if an error occurred in the static initializer SecurityException if a security violation to create a class instance takes place 7-12 Class.newInstance()

© Luxoft Training 2013 Introspection of class fields Class.getFields() Class c = obj.getClass(); Field[] publicFields = c.getFields(); for (Field field : publicFields) { Class fieldType = field.getType(); System.out.println(Name: " + field.getName()); System.out.println(Type: " + fieldType.getName()); } Class c = obj.getClass(); Field field = c.getField("name"); String nameValue = (String) field.get(obj); field.set(obj, "New name");

© Luxoft Training 2013 Class c = obj.getClass(); Method[] methods = c.getMethods(); for (Method method : methods) { System.out.println(Name: " + method.getName()); System.out.println(Returning type: " + method.getReturnType().getName()); Class[] paramTypes = method.getParameterTypes(); System.out.print(Parameter types: "); for (Class paramType : paramTypes) { System.out.print(" " + paramType.getName()); } System.out.println(); } Introspection of class methods Class.getMethods()

© Luxoft Training 2013 Class method invocation Method.invoke(obj,args) Class c = obj.getClass(); Class[] paramTypes = new Class[] { String.class, int.class }; Method method = c.getMethod( "getCalculateRating", paramTypes); Object[] args = new Object[] { new String("First Calculate"), new Integer(10) }; Double d = (Double) method.invoke(obj, args);

© Luxoft Training 2013Example 7-16 ClassSpyTutor

© Luxoft Training 2013Example 7-17 ReflectionTutor