Cuối cùng cũng đã chuẩn bị slide xong cho seminar ngày mai. Mai seminar đầu tiên luôn
Upcoming SlideShare
Loading in...5
×
 

Cuối cùng cũng đã chuẩn bị slide xong cho seminar ngày mai. Mai seminar đầu tiên luôn

on

  • 1,152 views

 

Statistics

Views

Total Views
1,152
Views on SlideShare
1,152
Embed Views
0

Actions

Likes
2
Downloads
21
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Cuối cùng cũng đã chuẩn bị slide xong cho seminar ngày mai. Mai seminar đầu tiên luôn Cuối cùng cũng đã chuẩn bị slide xong cho seminar ngày mai. Mai seminar đầu tiên luôn Presentation Transcript

  • HoChiMinh University of Science
    Java Programming Course
    Java Reflection API
    & Reference Object
    December 2010
  • Agenda
  • Introduction
    What does “Reflection” mean ?
    What’s Java Reflection API ?
    Java Reflection API
    Usage
    “Hello Reflection” Demo
    3
  • Introduction…
    What does “Reflection” mean ?
    In computer science, reflection is the process by which a computer program can observe and modify its own structure and behavior at runtime[1].
    4
  • Introduction…
    5
    What’s Java Reflection API?
    An Java program contains many objects. Java Reflection API includes classes, interfaces support developer can retrieve and manipulate its object at runtime.
  • Introduction …
    Java Reflection API
    • Interface
    • java.lang.reflect.AnnotatedElement
    • java.lang.reflect.GenericDeclaration
    • java.lang.reflect.InvocationHandler
    • java.lang.reflect.Member
    • java.lang.reflect.Type
    6
  • Introduction …
    Java Reflection API …
    • Class
    java.lang.reflect.AccessibleObject
    • java.lang.reflect.Constructor
    • java.lang.reflect.Field
    • java.lang.reflect.Method
    java.lang.reflect.Array
    java.lang.reflect.Modifier
    java.lang.reflect.Proxy
    7
  • Introduction …
    Java Reflection API …
    • Exception
    java.lang.reflect.InvocationTargetException java.lang.reflect.MalformedParameterizedTypeException
    java.lang.reflect.UndeclaredThrowableException
    • Error
    java.lang.reflect.GenericSignatureFormatError
    8
  • Introduction…
    Usage
    • Program Extensibility
    • IDE’s Visual Assistant
    • Visual Debugger & Tester Tools
    • Object Relational Mapping
    • Application Server Deploy Configuration
    9
  • Agenda
  • java.lang.Class class
    Kind of Java types
    Class object
    How to get a Class object
    How to get Class’ members object
    Instantiate a new object from its Class
    11
  • java.lang.Class class …
    Kind of Java types
    Primitive type
    e.g : int, double, float, short, long, byte, char, void
    Reference type
    e.g : class, interface, array, enum, annotation
    12
  • java.lang.Class class…
    Class object
    Instances of the class Class represent classes and interfaces in a running Java application.
    Has no public constructor.
    Are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to the defineClass method in the class loader.
    13
  • java.lang.Class class…
    How to get a Class object
    getClass() method
    .class syntax
    forName() static method
    .TYPE syntax
    Other methods
    14
  • java.lang.Class class…
    How to get Class’ members object
    15
    Get Constructors
    Constructor<T> getConstructor(Class<T>… parameterTypes)
    Constructor<?> [] getConstructors()
    Constructor<T> getDeclaredConstructor(Class<T>… parameterTypes)
    Constructor<?> [] getDeclaredConstructors()
    GetFields
    Field getField(String fieldName)
    Field [] getFields()
    Field getDeclaredField(String fieldName)
    Field [] getDeclaredFields()
  • java.lang.Class class…
    How to get Class’ members object
    16
    Get Methods
    Method getMethod(String name, Class<T>… parameterTypes)
    Method [] getMethods()
    Method getDeclaredMethod(String name, Class<T>… parameterTypes)
    Method [] getDeclaredMethods();
    GetModifiers
    int getModifiers()
  • java.lang.Class class…
    How to get Class’ members object
    17
    Get Package
    Package getPackage()
    Other methods
    Ref : JDK Document
  • Agenda
  • Class member
    java.lang.reflect
    • Constructor
    • Field
    • Method
    19
  • Class member…
    Constructor
    • Get information
    Class<T> getDeclaringClass()
    Class<T> [] getParameterTypes()
    int getModifiers()
    • Instantiate new object
    newInstance(Object … initArgs)
    20
  • Class member…
    Field
    • Get information
    • Class<T> getDeclaringClass()
    • Class<T> getType()
    • int getModifiers()
    • Get field’s value of an object
    • Object get(Object obj)
    • Set field’s value of an object
    set(Object obj, Object value)
    21
  • Class member…
    Getting Field’s Value Example
    22
    Class c = Integer.class;
    //get a private instant field
    Field field = c.getDeclaredField("value");
    field.setAccessible(true);
    System.out.format(field.get(new Integer(10)));
    //get a public static field
    field = c.getField("MAX_VALUE");
    System.out.format(field.get(null));
  • Class member…
    Setting Field’s Value Example
    23
    Class c = Integer.class;
    Integer i = new Integer(10);
    //get a private instant field
    Field field = c.getDeclaredField("value");
    field.setAccessible(true);
    System.out.format(field.set(i, new Integer(20)));
    //get a public static field
    field = c.getField("MAX_VALUE");
    System.out.format(field.set(null, new Integer(20)));
  • Class member…
    Method
    Get information
    Class<T> getDeclaringClass()
    Class<T> [] getParameterTypes()
    int getModifiers()
    Class<T> getReturnType()
    Invoke method
    Object invoke(Object obj, Object … initArgs)
    24
  • Class member…
    Invoke a method example
    25
    Method f = Math.class.getMethod("sqrt", new Class[]{double.class});
    Object[] args = {new Double(10)};
    //invoke a static method
    Double d = (Double) f.invoke(null, args); double y = d.doubleValue(); System.out.println(y);
  • Agenda
  • Array and Enum
    Array (java.lang.reflect package)
    • Get array’s information
    • Instantiate a new array dynamically
    • Set/Get array component’s value
    Enum
    • Get enum’s information
    • Set/Get enum component’s value
    27
  • Agenda
  • Plug-in Programming
    29
    Application
    Plugin Container
    SQL
    MySQL
    Oracle
  • Advantage & Disadvantage
    Advantage
    Dynamic Programming
    Avoid hard-coding
    Disadvantage
    Performance
    Security
    30
  • 31
    Q & A
    Thanks you!