Java Reflection Basics

Source: Internet
Author: User
Tags reflection

Reprint Please indicate the source: https://i.cnblogs.com/EditPosts.aspx?opt=1
Recently, when exposed to the bottom of the frame, I encountered a reflection, and wanted to learn and summarize the reflection, to help understand the Java framework
Operating process and operating principle:
Objects are based on everything, so the class is also the object, which is the object that describes the class by reflection, that is, class. There are three ways to get:
Class name. Classes;
Example. GetClass;
Class.forName (full class name);
Once you have a class that describes classes, you can get through the methods, fields, and annotations inside the class.
In Java programming, most of the frameworks used are encapsulated in the bottom layer, and in the process of programming, how to invoke the underlying encapsulated class, attributes, or
Methods are basically obtained by means of reflection or proxy, and the underlying principles are manipulated by reflection.
If you have a class like this:
Package com.test;

public class Test {
public void Sayhi (String name)
{
System.out.println ("Hi" +name);
}

}

you can write another class to reflect the method that calls the test class:

Package com.test;

Import java.lang.reflect.InvocationTargetException;
Import Java.lang.reflect.Method;

public class Class {
public static void Main (string[] args) {
String [] Names ={"Tom", "Tim", "Allen", "Alice"};
java.lang.class<test> clazz = Test.class;
Try
{
method = Clazz.getmethod ("Sayhi", String.class);
for (String name:names)
Method.invoke (Clazz.newinstance (), name);
} catch (Nosuchmethodexception e)
{
E.printstacktrace ();
} catch (Illegalaccessexception e)
{
E.printstacktrace ();
} catch (IllegalArgumentException e)
{
E.printstacktrace ();
} catch (InvocationTargetException e)
{
E.printstacktrace ();
} catch (Instantiationexception e)
{
E.printstacktrace ();
}
}

}
Java reflection refers to the use of a class name to detect the information inside the class, such as the class's property name, property name modifier, method name,
Method return value, method modifier, and so on, except that the method body is not available, the other can be reflected by reflection, reflection can also generate an instance of the class,
This example defines a property, invoking a method, especially one that can invoke private properties and private methods.
Import java.lang.reflect.*;
public class Reflectiontest {

public static void Main (string[] args) {
try {
Class c=class.forname ("Java.util.HashSet");
Object o=c.newinstance ();
Method[] Methods=c.getdeclaredmethods ();
for (Method method:methods) {
System.out.println (method);
}
Method M1=c.getmethod ("Add", Object.class);
M1.invoke (O, "cyq");
M1.invoke (o, "hello");
M1.invoke (o, "Java");
System.out.println (o);
} catch (Exception e) {
E.printstacktrace ();
}
}

}

This is a simple reflection example of getting all the declarations in hashset, generating an instance of HashSet, and adding objects to it

Java Reflection Basics

Related Article

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.