The reflection of Java Basic Learning notes _java

Source: Internet
Author: User

Reflection

Reflection: Maps the properties and methods of a class to the appropriate class.

Reflection Basic Use

Get the three methods of class classes:

    1. Class name. class
    2. Object name. GetClass ()
    3. Class.forName ("Class name to load")

It is written according to the API, and the general process is:

    1. To get the class class of a particular class in one of three ways, that is, the byte code for that class
    2. Invokes the GetConstructor (Class<?>. parametertypes) of the Class object to get the constructor method object
    3. The call is a new object created by the Newinstance (Object ... Initargs) method of the construction method class constructor
    4. Invokes the GetMethod (String name, Class<?> ... parametertypes) of the Class object to get the method object
    5. Invokes the Invoke (Object obj, Object ... args) method of method object class methods, calling the corresponding method on the object

Uniquely identifies a method with the parameter type of the method, based on: The overload of the method

Reflection of an array

The following example illustrates a few points:

    1. For an array of the same type of elements, the same dimension array, class
    2. Different dimension, different class
    3. Different dimensions, the parent class is object, same
    4. The base type is that the array cannot be directly converted to object[]
    5. Java.util.Arrays's Aslist method API look at
public class Reflecttest {public static void main (string[] args) {int [] a1 = new int[]{1,2,3};
    int [] A2 = new Int[5];
    int [] [] a3 = new Int[2][3]; System.out.println (a1.getclass () = = A2.getclass ());//true System.out.println (A1.getclass ());//class [I System.out. println (A3.getclass ());//class [[I System.out.println (A1.getclass (). Getsuperclass () = A3.getclass (). Getsuperclass ());//true System.out.println (A2.getclass (). Getsuperclass ())//class java.lang.Object//Next sentence compilation does not pass: Error: (MB) ja VA: Non-comparable types: java.lang.class<capture#1, total? Extends int[]> and java.lang.class<capture#2, altogether?

    Extends Int[][]>//system.out.println (a1.getclass () = = A3.getclass ());

    Object []B3 = a3;//through//The following sentence is compiled without Error: (a) Java: Incompatible type: int[] cannot be converted to java.lang.object[]//object [] b1 = A1;
    String S1 = "abc"; System.out.println (arrays.aslist (A1));//[[i@1540e19d] System.out.println (arrays.aslist (S1));//[ABC]}

Output:

True
class [I
class [[I
True
class Java.lang.Object
[[i@1540e19d] [
ABC]

Random entry: Hashcode and memory leak issues reference Java API:

    1. Hashcode once generated, do not change
    2. Object Equals method returns True, the Hascode should be consistent
    3. Conversely, the Equals method returns False,hascode does not necessarily differ from each other

If the member variable that participates in the Hascode calculation changes Midway, the subsequent remove fails, causing a memory leak

Configuration file Loading

class loader load read-only configuration file
Class name. Class.getclassloader (). getResourceAsStream (str);

Class name. Class.getresourceasstream (str), or call the class loader in essence. SOURCE interception (Class.java under Java.lang package):

 Public InputStream getResourceAsStream (String name) {
  name = ResolveName (name);
  ClassLoader cl = GetClassLoader0 ();
  if (cl==null) {
    //A system class.
    return Classloader.getsystemresourceasstream (name);
  return Cl.getresourceasstream (name);

With regard to path STR, the wording is somewhat fastidious.

    1. No slash, relative path: str = "Config.properties";
    2. Plus slash, from classpath root path: str = "/org/iot/ui/config.properties";

Before compiling Java code, some conf/folders are added to rely on or marked as the source folder, which is clearly XML files, no Java source code. From here, I now know that it is the use of reflection to load a configuration file.

Introspection (instropector) & JavaBean

JavaBean the process of reading the value of property x: Capitalize, prefix, get method.

"X"--> "x"--> "GetX"--> "Methodgetx"
Self-operation with introspection
I'm not working on it right now, so I'm not going to post code, just attach the core class

Simple implementation: Using the Java.beans.PropertyDescriptor class

Trouble implementation: Use the Java.beans.Introspector class to traverse the return value of the Getbeaninfo method

JavaBean must have a constructor with no parameters

Using the Beanutils Toolkit

    1. String and Integer conversions (propertyutils)
    2. Attribute Cascade operation
    3. Manipulating map

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.