Java Reflection correlation-Gets the entire schema of a class through class

Source: Internet
Author: User

Package Com.huawei.pia;

Import Java.lang.reflect.Constructor;
Import Java.lang.reflect.Field;
Import Java.lang.reflect.Modifier;

public class person extends SuperClass1 implements Inter1,inter2 {

private String name;
private int age;

Public person (String Name,int age) {
THIS.name = name;
This.age = age;
}

Public person () {};

Public String GetName () {
return name;
}

public void SetName (String name) {
THIS.name = name;
}

public int getage () {
return age;
}

public void Setage (int.) {
This.age = age;
}

@Override
Public String toString () {
TODO auto-generated method stubs
Return "[" + THIS.name + ":" + This.age + "]";
}

Static Class hello{

public static void Main (string[] args) {
Class<?> demo = null;
try {
Demo = Class.forName ("Com.huawei.pia.Person");
} catch (ClassNotFoundException e) {
E.printstacktrace ();
}

field[] field = Demo.getdeclaredfields ();
for (int i = 0; i < field.length; i++) {
Get modifier
int mo = Field[i].getmodifiers ();
String priv = modifier.tostring (MO);
Property type
class<?> type = Field[i].gettype ();
System.out.println ("modifier:" +priv+ ", \ n attribute type:" +type+ ", \ n Property Name:" +field[i].getname () ");
}

Get the construction method
Constructor<?>[] cons = Demo.getconstructors ();
for (int i = 0; i < cons.length; i++) {
System.out.println ("Construction Method:" +cons[i].getname ());
}

Get interface
class<?> inter[] = demo.getinterfaces ();
for (int i = 0; i < inter.length; i++) {
SYSTEM.OUT.PRINTLN ("interface" + (i+1) + ":" +inter[i].getname ());
}

Get the parent class
Class<?> superclass = Demo.getsuperclass ();
System.out.println ("Parent class:" +superclass.getname ());
}
}
}

Java Reflection correlation-Gets the entire schema of a class through class

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.