栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java 核心技术(第八版)卷1:基础知识:例5-7P193ObjectAnalyzerTest

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java 核心技术(第八版)卷1:基础知识:例5-7P193ObjectAnalyzerTest

这个程序我实现了,但是运行不了,有问题。留待这里的大佬们给看看。

import java.lang.reflect.*;
import java.util.*;
//本程序演示 使用反射来在对象上从事间谍活动,刺探一些关于object的信息
public class ObjectAnalyzerTest {
    public static void main(String [] args)
    {
        ArrayList squares=new ArrayList();
        for(int i=1;i<=5;i++)
           squares.add(i*i);
        System.out.println(new ObjectAnalyzer().toString(squares));
    }
}
class ObjectAnalyzer
{
    //转换一个对象到字符串 代表着 需要列出所有的字段
    //obj 参数 是一个对象
    //返回一个string 是对象的类名和所有字段的名字和值
    public String toString(Object obj)
    {
        if(obj==null) return "null";

        if(visited.contains(obj)) return "...";
        visited.add(obj);
        Class cl=obj.getClass();
        if(cl==String.class)return (String) obj;
        if(cl.isArray())
        {
            String r=cl.getComponentType()+"[]{";//getComponentType():Returns the Class representing the component
            // type of an array.If this class does not represent an array class this method returns null.
            for(int i=0;i0) r+=",";
                Object val=Array.get(obj,i);//Returns the value of the indexed component in the specified array object.
                // The value is automatically wrapped in an object if it has a primitive type.
                if(cl.getComponentType().isPrimitive()) r+=val;//如果是基本数据类型,r=r+val;
                else r+=toString(val);//如果不是基本类型,就调用toString()方法
            }
            return r+"}";
        }
        String r=cl.getName();
        //检查这个类和所有超类的字段
        do
        {
            r+="[";
            Field[] fields=cl.getDeclaredFields();
            AccessibleObject.setAccessible(fields,true);
            //得到所有字段的 name 和 value
            for(Field f:fields)
            {
                if(!Modifier.isStatic(f.getModifiers()))
                {
                    if(!r.endsWith("["))r+=",";
                    r+=f.getName()+"=";
                    try
                    {
                        Class t=f.getType();  //Returns a Class object that identifies the declared type for the field
                                              // represented by this Field object.
                        Object val=f.get(obj);//Returns the value of the field represented by this Field, on the specified object.
                                              // The value is automatically wrapped in an object if it has a primitive type.
                        if(t.isPrimitive()) r+=val;
                        else r+=toString(val);
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();//Prints this throwable and its backtrace to the standard error stream.
                        // This method prints a stack trace for this Throwable object on the error output stream
                        // that is the value of the field System.err.
                    }
                }
            }
            r+="]";
            cl=cl.getSuperclass();
        }
        while(cl!=null);
            return  r;
    }
    private ArrayList visited=new ArrayList();
} 

运行的时候出现:

Exception in thread "main" java.lang.reflect.InaccessibleObjectException: Unable to make field private static final long java.util.ArrayList.serialVersionUID accessible: module java.base does not "opens java.util" to unnamed module @15aeb7ab
	at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354)
	at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
	at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:180)
	at java.base/java.lang.reflect.AccessibleObject.setAccessible(AccessibleObject.java:130)
	at ObjectAnalyzer.toString(ObjectAnalyzerTest.java:46)
	at ObjectAnalyzerTest.main(ObjectAnalyzerTest.java:10)

进程已结束,退出代码1

这玩意好像和module 这个功能点有关,我是解决不来了,求解啊!

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号