My Java Learning Note (12) about reflection (Part 1)

Source: Internet
Author: User
Tags modifiers string format

1. A program capable of analyzing class capabilities is called reflection.

2. During a program run, the Java Runtime system always maintains a type identity known as the runtime for all objects. This information tracks the classes that each object belongs to. The virtual machine uses the run-time type information to select the appropriate method to execute.

3. The classes that hold the information are called class.

The GetClass () method in the 4.Object class will return an instance of class type.

String s = "Hello"; Class C1 = S.getclass ();

5. If the class is in a package, the name of the package is also part of the class name.

6. Three ways to get the class name

A.

Date d = new Date (); Class C1 = D.getclass (); String name = V1.getname ();

B.

String className = "java.util.Date";//This string must be stored in the class name or interface name Class C1 = Class.forName (className); String name = C1.getname ();


c.

Class C1 = string.class;string name = C1.getname ();

7. A class object actually represents a type, and this type may not necessarily be a class. For example, int is not a class, but Int.class is an object of type class.

8. The virtual machine manages a class object for each type.

The 9.newInstance method can create an instance object, but note that the method returns an object type.

10. When an error occurs while the program is running, it "throws an exception". Throwing an exception is much more flexible than terminating the program because it can provide a "catch" exception handler to handle the exception.

11. There are two types of exceptions: no Check out exception and checked exception.

12. Place one or more method call code that might throw a checked exception in a try block, and then provide the processing code in the catch clause.

try{...    } catch (Exception e) {    e.printstracktrace ();}

13. In the Java.lang.reflect package, there are three classes of fields, methods, and Constuctor, respectively, that describe the domain, method, and constructor of a class. Three classes have a GetName method that returns the name of the project.

The 14.Field class has a GetType method that returns a class object that describes the type that the domain belongs to.

The GetFields, GetMethods, and GetConstructors methods in the 15.Class class will return the public fields, methods, and classes provided by the class, respectively.

the Getdeclaredfields, getdeclaredmethods, and getdeclaredconstructors methods in the class class will return all the fields provided by the classes, Methods and classes.

Instance Code

Import Java.util.*;import java.lang.reflect.*;p ublic class Test {public static void main (string[] args) {String name; Scanner in = new Scanner (system.in); System.out.println ("Please enter class name:"); name = In.next (); Try{class C1 = class.forname (name); Class Superc1 = C1.getsuperclass (); String modifiers = modifier.tostring (C1.getmodifiers ()), if (Modifiers.length () > 0) System.out.println (modifiers + " "); System.out.print ("Class" + name); if (superc1! = NULL && SUPERC1! = object.class) System.out.println ("extends" + S Uperc1.getname ()); System.out.print ("\n{\n");p rintconstructors (C1); System.out.println ();p rintmethods (C1); System.out.println ();p Rintfields (C1); System.out.println ("}");} catch (ClassNotFoundException e) {e.printstacktrace ();} System.exit (0);} public static void Printconstructors (Class C1) {constructor[] con = c1.getdeclaredconstructors (); for (Constructor C:con) {String name = C.getname (); System.out.print (""); String modifiers = modifier.tostring (C.getmodifiers ()); if (Modifiers.length () > 0) System.out.print (modifiers + ""); System.out.print (name + "("); class[] Paramtype = C.getparametertypes (); for (int i = 0;i < Paramtype.length; i++) {if (i > 0) System.out.print (","); System.out.print (Paramtype[i].getname ());} System.out.println (")");}} public static void Printmethods (class C1) {method[] methods = C1.getdeclaredmethods (), for (Method m:methods) {class Rettyp E = M.getreturntype (); String name = M.getname (); System.out.print (""); String modifiers = modifier.tostring (M.getmodifiers ()), if (Modifiers.length () > 0) System.out.print (modifiers + ""); System.out.print (Rettype.getname () + "+ name +" ("); class[] Paramtypes = M.getparametertypes (); for (int i = 0; i < paramtypes.length;i++) {if (i > 0) System.out.print (",") ; System.out.print (Paramtypes[i].getname ());} System.out.println (")");}} public static void Printfields (class C1) {field[] field = C1.getdeclaredfields (); for (Field f:fields) {Class type = F.getty PE (); String name = F.getname (); System.out.print (""); String Modifiers = modifier.tostring (F.getmodifiers ()), if (Modifiers.length () > 0) System.out.print (modifiers + ""); System.out.print (Type.getname () + "" + Name);}}}



Output results


Please enter the class name:
Java.lang.String
Public final
Class java.lang.String
{
Public java.lang.String ([B,int,int)
Public java.lang.String ([B,java.nio.charset.charset)
Public java.lang.String ([b,java.lang.string)
Public java.lang.String ([B,int,int,java.nio.charset.charset)
Public java.lang.String ([b,int,int,java.lang.string)
Java.lang.String ([C,boolean)
Public java.lang.String (Java.lang.StringBuilder)
Public java.lang.String (Java.lang.StringBuffer)
Public java.lang.String ([B)
Public java.lang.String ([I,int,int)
Public java.lang.String ()
Public java.lang.String ([C)
Public java.lang.String (java.lang.String)
Public java.lang.String ([C,int,int)
Public java.lang.String ([B,int)
Public java.lang.String ([B,int,int,int)


public boolean equals (Java.lang.Object)
Public java.lang.String toString ()
public int hashcode ()
public int CompareTo (java.lang.String)
public volatile int CompareTo (java.lang.Object)
public int indexOf (java.lang.string,int)
public int indexOf (java.lang.String)
public int indexOf (int,int)
public int indexOf (int)
static int indexOf ([c,int,int,[c,int,int,int)
static int indexOf ([c,int,int,java.lang.string,int)
public static java.lang.String valueOf (int)
public static java.lang.String valueOf (Long)
public static java.lang.String valueOf (float)
public static Java.lang.String ValueOf (Boolean)
public static java.lang.String valueOf ([C)
public static java.lang.String valueOf ([C,int,int)
public static java.lang.String valueOf (Java.lang.Object)
public static java.lang.String valueOf (char)
public static java.lang.String valueOf (double)
public char charAt (int)
private static void Checkbounds ([b,int,int)
public int Codepointat (int)
public int Codepointbefore (int)
public int Codepointcount (int,int)
public int comparetoignorecase (java.lang.String)
Public java.lang.String concat (java.lang.String)
Public Boolean contains (java.lang.CharSequence)
public boolean contentequals (java.lang.CharSequence)
public boolean contentequals (Java.lang.StringBuffer)
public static java.lang.String copyvalueof ([C)
public static java.lang.String copyvalueof ([C,int,int)
public boolean endsWith (java.lang.String)
public boolean equalsignorecase (java.lang.String)
public static transient java.lang.String format (java.util.locale,java.lang.string,[ljava.lang.object;)
public static transient java.lang.String format (java.lang.string,[ljava.lang.object;)
public void GetBytes (Int,int,[b,int)
public [B getBytes (Java.nio.charset.Charset)
public [B getBytes (java.lang.String)
public [B getBytes ()
public void GetChars (Int,int,[c,int)
void GetChars ([c,int)
private int indexofsupplementary (int,int)
Public native java.lang.String intern ()
public boolean isEmpty ()
public static transient java.lang.String join (java.lang.charsequence,[ljava.lang.charsequence;)
public static java.lang.String Join (java.lang.charsequence,java.lang.iterable)
public int lastIndexOf (int)
public int lastIndexOf (java.lang.String)
static int lastIndexOf ([c,int,int,java.lang.string,int)
public int lastIndexOf (java.lang.string,int)
public int lastIndexOf (int,int)
static int lastIndexOf ([c,int,int,[c,int,int,int)
private int lastindexofsupplementary (int,int)
public int Length ()
Public boolean matches (java.lang.String)
Private Boolean nonsynccontentequals (Java.lang.AbstractStringBuilder)
public int offsetbycodepoints (int,int)
public boolean regionmatches (Int,java.lang.string,int,int)
public boolean regionmatches (Boolean,int,java.lang.string,int,int)
Public java.lang.String replace (Char,char)
Public java.lang.String replace (java.lang.charsequence,java.lang.charsequence)
Public java.lang.String ReplaceAll (java.lang.string,java.lang.string)
Public java.lang.String Replacefirst (java.lang.string,java.lang.string)
public [Ljava.lang.String; split (java.lang.String)
public [Ljava.lang.String; split (Java.lang.string,int)
public boolean startsWith (Java.lang.string,int)
public boolean startsWith (java.lang.String)
Public java.lang.CharSequence subsequence (int,int)
Public java.lang.String substring (int)
Public java.lang.String substring (int,int)
public [C ToCharArray ()
Public java.lang.String toLowerCase (Java.util.Locale)
Public java.lang.String toLowerCase ()
Public java.lang.String toUpperCase ()
Public java.lang.String toUpperCase (Java.util.Locale)
Public java.lang.String trim ()


Private final [C value private int hash private static final long serialversionuid private static final [Ljava.io.Ob Jectstreamfield; Serialpersistentfields public static final Java.util.Comparator Case_insensitive_order}

My Java Learning Note (12) about reflection (Part 1)

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.