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

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

import java.lang.reflect.*;
import java.util.*;
//本程序演示 使用反射来在对象上从事间谍活动,刺探一些关于object的信息
public class ObjectAnalyzerTest {
    public static void main(String [] args)
    {
        ArrayList<Integer> squares=new ArrayList<Integer>();
        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;i<Array.getLength(obj);i++)
            {
                if(i>0) 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);/*Convenience method to set the accessible flag for an array of
            reflected objects with a single security check (for efficiency).This method may be used to enable access to all
            reflected objects in the array when access to each reflected object can be enabled as specified by setAccessible(boolean).
            If there is a security manager, its checkPermission method is first called with a ReflectPermission("suppressAccessChecks") permission.
            A SecurityException is also thrown if any of the elements of the input array is a Constructor object for
            the class java.lang.Class and flag is 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<Object> visited=new ArrayList<Object>();
}

运行的时候出现:

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 这个功能点有关,我是解决不来了,求解啊!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值