语言小结
  • program-language-note
  • contact
  • common
    • 代码风格
    • 概念语法
      • 类型
      • 注释
      • 字符
      • 语句
      • 操作符
      • 函数
        • 递归
    • 格式化参数
    • 源码结构
    • 数据结构
    • 名词
  • 电路
    • 内存地址
    • Untitled
    • Code
  • C
    • note
    • overview
      • helloworld.c
      • c标准
      • 关键字
      • tips
      • util.c
    • 语法
      • 函数
        • main
      • const
      • static
      • 作用域
    • 编译和运行
      • c代码内存模型
      • 预处理
        • include
        • define
    • 头文件
    • 基本数据类型
      • 整型
      • 枚举
      • 浮点型
      • 指针
      • 数组
      • 结构和联合
    • 指针&数组、指针&函数
    • API
    • 存储结构
    • 操作符
      • sizeof
    • typedef
    • 输入输出
    • 格式化参数
    • 左値右値
    • 性能思考
    • volatile
    • 字符串
      • find_char.c
    • 动态分配
      • alloc.h
      • alloc.c
      • alloc_usage.c
    • note
  • cpp
    • 资源
    • note
    • 数据结构
    • 智能指针
    • 编译过程
  • shell
    • usage
    • Untitled
  • Rust
    • overview
  • Lisp
    • Untitled
  • web
    • overview
      • index
      • 软件工具
      • ARIA规范
      • SEO
    • style
    • html
      • 标签、元素
        • 标签快记
        • 联系信息
        • 引用
        • 列表
        • 语言设置
        • meta
      • 页面结构
        • 图片
        • 视频
        • 引用css、js文件
      • 等价字符
      • 链接
        • 邮件
      • 表单
        • note
      • 表格
    • css
      • 字体
      • 布局
        • position
        • float
        • display
        • flexbox
    • js
    • note
  • java
    • note
    • java语言程序设计
    • 设计模式
      • 大话设计模式-吴强-2010
      • 大话设计模式-程杰
      • 设计模式-gof
      • 设计模式解析
      • 原则
      • 单例
    • java程序设计第10版-基础
    • java程序设计第10版-进阶
    • java核心技术第9版-I
    • jar包
    • 安全
    • 反射
  • python
    • note
    • index
    • 个人记忆点
    • 疑惑
    • simple
    • 精通Python爬虫框架scrapy
    • 语法
    • scrapy
      • notice
      • index
  • 汇编
    • Untitled
  • kotlin
    • index
    • note
    • by android
      • note
      • index
  • groovy
    • gradle
Powered by GitBook
On this page

Was this helpful?

  1. java

反射

/**
 * just for quick practice
 */
public class Test {

    public static void main(String[] args) {
        ReflectClass reflectClass = new ReflectClass();
        //常规使用
        Class cl = reflectClass.getClass();
        //result is : Test$ReflectClass
        System.out.println(cl.getName());

        //throw exception if use "Test.ReflectClass", that is for package path
        String className = "Test$ReflectClass";
        try {
            Class cl1 = Class.forName(className);
            System.out.println(cl1.getName());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        //for history reason, Double[].class.getName() equals [Ljava.lang.Double; int[] .. return [I
        Class cl2 = ReflectClass.class;
        cl2 = int.class;
        cl2 = Double[].class;
        System.out.println(cl2.getName());

        //create instance
        Class cl;
        try {
            cl.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }

        //access
        try {
            Field f = reflectClass.getClass().getDeclaredField("field");
            //without control of safety manager
            f.setAccessible(true);
            Object obj = f.get(reflectClass);
            //for double is a primary type
            f.getDouble(reflectClass);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }

    }

    static class ReflectClass {

        public static void reflectClass(Class cl) {
            Class supreCl = cl.getSuperclass();
            //the modifiers : public , protected,
            String modifiers = Modifier.toString(cl.getModifiers());
            if (modifiers.length() > 0) {
                System.out.print(" " + modifiers);
            }
            printConstructors(cl);
            printMethods(cl);
            printFields(cl);
        }

        private static void printConstructors(Class cl) {
            Constructor[] constructors = cl.getDeclaredConstructors();
            for (Constructor c : constructors) {
                String name = c.getName();
                System.out.print("    ");
                //
                Class[] paramTypes = c.getParameterTypes();

            }
        }

        private static void printMethods(Class cl) {
            Method[] methods = cl.getMethods();
            //
            methods[0].getReturnType();
        }

        private static void printFields(Class cl) {
            Field[] fields = cl.getDeclaredFields();
            //
            fields[0].getType();
        }

        private ArrayList<Object> reflected = new ArrayList<>();

        //universal toString by reflect
        public String toString(Object obj) {
            if (obj == null) return "null";
            if (reflected.contains(obj)) return "...";
            reflected.add(obj);

            Class cl = obj.getClass();
            if (cl == String.class) return (String) obj;
            if (cl.isArray()) {
                String r = cl.getComponentType() + "[]{";
                for (int i = 0; i < Array.getLength(obj); i++) {
                    if (i > 0) r += ",";
                    Object o = Array.get(obj, i);
                    if (cl.getComponentType().isPrimitive()) r += o;
                    else r += toString(o);
                }
                return r + "}";
            }

            String str = cl.getName();
            do {
                str += "[";
                Field[] fields = cl.getDeclaredFields();
                AccessibleObject.setAccessible(fields, true);
                for (Field f : fields) {
                    if (!Modifier.isStatic(f.getModifiers())) {
                        if (!str.endsWith("[")) {
                            str += ",";
                        }
                        str += f.getName() + "= ";

                        try {
                            Class c = f.getType();
                            Object o = f.get(this);
                            if (c.isPrimitive()) {
                                str += o;
                            } else {
                                str += toString(o);
                            }
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                    }
                }
                str += "]";
                cl = cl.getSuperclass();
            } while (cl != null);
            return str;
        }
    }

}
Previous安全Nextnote

Last updated 5 years ago

Was this helpful?

这句话其实不是很理解,也许以后做到java的时候会理解吧。

201848-163013.jpg