get Class Declared Fields - Java Reflection

Java examples for Reflection:Field Get

Description

get Class Declared Fields

Demo Code


//package com.java2s;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class Main {
    public static void main(String[] argv) throws Exception {
        Class classInstance = String.class;
        getClassDeclaredFields(classInstance);
    }//from  w  w w .  java2s.  co  m

    public static void getClassDeclaredFields(Class<?> classInstance) {

        Field[] ff = classInstance.getDeclaredFields();
        String x;

        for (int i = 0; i < ff.length; i++) {
            x = ff[i].getType().getName();
            System.out.println("???????:" + x);
        }

        System.out.println("----------------------------=");

        Constructor<?>[] cn = classInstance.getDeclaredConstructors();
        for (int i = 0; i < cn.length; i++) {
            Class cx[] = cn[i].getParameterTypes();
            System.out.println("???? :" + cn[i].getName());
            for (int j = 0; j < cx.length; j++) {
                x = cx[j].getName();
                System.out.println("---------???? ?????? :" + x);
            }
        }

        System.out.println("----------------------------=");
        Method[] mm = classInstance.getDeclaredMethods();

        for (int i = 0; i < mm.length; i++) {
            System.out.println("????? :" + mm[i].getName());

            int md = mm[i].getModifiers();
            System.out.print("??? :" + Modifier.toString(md));

            Class cx[] = mm[i].getParameterTypes();
            for (int j = 0; j < cx.length; j++) {
                x = cx[j].getName();
                System.out.println("---------????????:" + x);
            }

            x = mm[i].getReturnType().getName();
            System.out.println("---------???????:" + x);
        }
        //  classRef.remove(c.getName()); //???????????import???
    }
}

Related Tutorials