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

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

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

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

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/801605.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-07
下一篇 2022-05-07

发表评论

登录后才能评论

评论列表(0条)