1. Trang chủ
  2. » Mẫu Slide

Session6 module8 9 annotations reflection

33 606 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 33
Dung lượng 188,4 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

m mới bắt đầu học java nên chưa có kinh nghiệm gì, nên mong các tiền bối đi trước cho em vài lời khuyên. trước đây em có học qua về c++ với C và php nhưng không thực sự giỏi cái nào. hiện nay em đang có bài tập lớn về java,mà em thì không có kinh nghiệm gì về nó nên muốn chọn mảng nào dễ hơn để có thể dễ bắt đầu, nếu các anh chị nào đã học qua thì cho em 1 vài lời khuyên với.em xin cám ơn các anh chị trước.

Trang 1

JPII - Session 6

Module 8 – Annotations

Module 9 – Reflection API

Trang 2

Session 5 Review

• [pending|session ]

Trang 3

Module 8 – Annotations

ACCP I7.1 - A GUIDE TO ADVANCED JAVA

Trang 4

Module Objectives

Trang 5

What is Annotation?

information about existing pieces of data.

way a program is treated by tools and libraries which in turn can affect

program semantics

replaced by annotations in many places.

enabling tools to generate such reusable code from annotations present in the source code.

Trang 6

Java Annotations

An annotation is a meta-tag used by the programmer in the code Annotation

represents the specific use of the type

Annotation type helps to define an annotation This is used by the programmer when

a custom annotation is created

• The example below defines a Annotation and use it in consuming code:

// Definition of an Annotation type

public @interface TestAnnotation {

Trang 7

Uses of Annotations

Information for the compiler — Annotations can be used by the compiler

to detect errors or suppress warnings

Compiler-time and deployment-time processing — Software tools can

process annotation information to generate code, XML files, and so forth

Runtime processing — Some annotations are available to be examined at

runtime

fields, methods, and other program elements.

Trang 8

DO and DON’T

DO

if the metadata changes the

design of the class.

if the metadata changes the

design of code dealing with

class.

if the metadata changes the

design of the class.

if the metadata changes the

design of code dealing with

class.

DON’T

be portable between application servers and databases

a per deployment basis

be portable between application servers and databases

a per deployment basis

Trang 9

Categories of Annotations

• Marker Annotations

– Consists of name and have no values associated

– Has no additional present, such as @TestAnno

• Single-value Annotations

– Consists of single value, similar to marker ones.

– Can be represented: data= value pair

– Example: @TestAnno(“Testing”)

• Full Annotations

– Consists of multiple data members.

– Each member represented by data= value pair

@TestAnno(owner=“Stephen”, value=“class scope”)

Trang 11

// use a deprecated method and tell

// compiler not to generate a warning

@SuppressWarnings("deprecation")

void useDeprecatedMethod() { objectOne.deprecatedMethod();

//deprecation warning – suppressed

}

Annotations Used by the Compiler

// Javadoc comment follows

// mark method as a superclass method

// that has been overridden

@Override int overriddenMethod() { }

Trang 12

@Deprecated annotation

• When you design an API, carefully consider whether it

supersedes an old API.

• If it does, and you wish to encourage developers (users of the API) to migrate to the new API, then deprecate the old API Valid reasons to deprecate an API include:

Trang 13

@SuppressWarnings Annotation

• To inform the compiler to

suppress certain warnings in the

annotated code element that

would otherwise have been

generate.

13

Trang 14

@SuppressWarnings Annotation

• This annotation indicates that compiler warnings should be shielded in the annotated element and all of its sub-

elements

• The set of warnings suppressed in an element is the

superset of the warnings in all of its containing

sub-elements

• As an example, if you annotate a class to suppress one

warning and one of its methods to suppress another warning, both warnings will be suppressed at the method level only

Trang 15

@SuppressWarnings Annotation Example

public void test()

{ Thu t = new Thu();

Trang 16

@Documented Annotation

• The @Documented annotation specifies that it should be included

in the documentation, documented by the javadoc tool.

• The use of the @Documented annotation in the code will enable tools like javadoc to process it and include the annotation type information in the generated document.

Trang 19

Custom Annotation for Class

• To create an annotation type for classes, the "@" symbol

followed by the keyword, interface and the annotation name

is used.

• Parameters can be added to the Annotation Declaration.

Trang 20

Custom Annotation for Method

• While creating an Annotation for a methods, the annotation starts with @.

• The order of assigning values to the members of the

annotations is of no importance.

• The decleration of annotation type should be in the same package as the class using the Annotation

Trang 21

Module8 - Summary

Trang 22

Module 9

Reflection API

ACCP I7.1 - A GUIDE TO ADVANCED JAVA

Trang 23

Module Obectives

23

Trang 24

Introduction to Reflection API

• Reflection is the ability of classes, objects and interfaces to find information about itself.

• The reflection API are used to write development tools, such as debuggers, class browsers and GUI builder

• Reflection API is used for the following purposes:

– Analyzing classes

– Controlling objects

– Controlling with arrays

24

Trang 25

Analyzing classes

• Retrieving class name:

– If an instance of the class is available, then by invoking getName() method, the class name can be retrieved.

• Retrieving fields:

– By invoking getField() method on a class object.

• Retrieving class constructors:

– By invoking getConstructors() method on a class object.

• Method information:

– By invoking getMethods() method on a class object.

25

Trang 26

Example for Analyzing classes

import java.lang.reflect.*;

public class Test {

public int m,n;

public Test() {};

public Test(int m, int n) {this.m = m; this.n=n;}

public void xem() {System.out.println("m, n = " + m+ ", " + n);}

public void nhap(int u, int v) {m=u;n=v;}

}

public class Main{

public static void main(String[] args){

Test t = new Test();

1

2

Trang 27

Example for Analyzing classes

27

static void printMethodNames(Test t) {

String className, methodName;

String methodType, methodParaName;

Trang 28

Create object

• The newInstance() method is invoked on a class instance to create an object with the no-argument constructor.

• The newInstance() method is invoked on a Constructor object to create

an object with a parameterize constructor

• Steps are:

• Create a class instance.

• Invoke getConstructor() method.

• Invoke newInstance() method.

28

Trang 29

Retrieving Field Values

– Creating a Class object.

– Creating a Field object by invoking the getField() method in the Class object.

– Invoking one of the get() methods on the Field instance.

as well as objects.

– For example: getInt() method returns int.

Trang 30

Assigning Field Values

Creating a Class object

– Creating a Field object by invoking the getField() method on the Class object.

– Invoking one of the set() method on the Field instance.

primitive types as well as objects.

Trang 31

Array and Reflection

– Is used to dynamically create and access arrays at runtime.

Class.isArray() method is used to check whether a particular object is an

Trang 32

package reflectiondemo;

import java.lang.reflect.Array;

public class ReflectDemo1 {

public static void main(String[] args) { int[] intArray = {12, 78};

System.out.println(Array.get(source, 0));

}

}

Trang 33

Module 9: Summary

• In this module, Reflection API, you learnt about:

Ngày đăng: 22/11/2014, 16:39

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN

w