Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当使用getDeclaredField()和字段存在时,NoSuchFieldException_Java_Reflection - Fatal编程技术网

Java 当使用getDeclaredField()和字段存在时,NoSuchFieldException

Java 当使用getDeclaredField()和字段存在时,NoSuchFieldException,java,reflection,Java,Reflection,java.lang.NoSuchFieldException:c 位于java.lang.Class.getDeclaredField(未知源) 在ru.onlymc.OnlyMZ.CustomEntityType.getPrivateStatic(CustomEntityType.java:177) 方法: private static Object getPrivateStatic(Class clazz, String f) throws Exception { Field fie

java.lang.NoSuchFieldException:c
位于java.lang.Class.getDeclaredField(未知源)
在ru.onlymc.OnlyMZ.CustomEntityType.getPrivateStatic(CustomEntityType.java:177)

方法:

private static Object getPrivateStatic(Class clazz, String f) throws Exception {
    Field field = clazz.getDeclaredField(f);
    field.setAccessible(true);
    return field.get(null);
}
private static void a(Class paramClass, String paramString, int paramInt) {
    try {
        ((Map) getPrivateStatic(sg.class, "c")).put(paramString, paramClass);
        //...
    } catch (Exception exc) {
        exc.printStackTrace();
    }
}
private static Map c = new HashMap();
private static Map d = new HashMap();
private static Map e = new HashMap();
private static Map f = new HashMap();
private static Map g = new HashMap();
public static HashMap a = new LinkedHashMap();
呼叫:

private static Object getPrivateStatic(Class clazz, String f) throws Exception {
    Field field = clazz.getDeclaredField(f);
    field.setAccessible(true);
    return field.get(null);
}
private static void a(Class paramClass, String paramString, int paramInt) {
    try {
        ((Map) getPrivateStatic(sg.class, "c")).put(paramString, paramClass);
        //...
    } catch (Exception exc) {
        exc.printStackTrace();
    }
}
private static Map c = new HashMap();
private static Map d = new HashMap();
private static Map e = new HashMap();
private static Map f = new HashMap();
private static Map g = new HashMap();
public static HashMap a = new LinkedHashMap();
sg.class(从反编译到确定必填字段确实存在)

private static Object getPrivateStatic(Class clazz, String f) throws Exception {
    Field field = clazz.getDeclaredField(f);
    field.setAccessible(true);
    return field.get(null);
}
private static void a(Class paramClass, String paramString, int paramInt) {
    try {
        ((Map) getPrivateStatic(sg.class, "c")).put(paramString, paramClass);
        //...
    } catch (Exception exc) {
        exc.printStackTrace();
    }
}
private static Map c = new HashMap();
private static Map d = new HashMap();
private static Map e = new HashMap();
private static Map f = new HashMap();
private static Map g = new HashMap();
public static HashMap a = new LinkedHashMap();

您正在尝试将对象的字段设置为null。请参阅有关的文档

您需要提供一个对象来获取其字段内容。或者您需要提供一个类来获取静态字段内容

所以你应该写以下内容:

private static Object getPrivateStatic(Class clazz, String f) throws Exception {
  Field field = clazz.getDeclaredField(f);
  field.setAccessible(true);
  return field.get(clazz);
}
顺便说一句,在生产代码中使用反射并不被认为是好的编程风格,因为它会使重构成为问题

如果您仍然需要使用反射(或者不在处理生产代码),请为此类内容使用框架(访问私有静态字段)。例如,这种方法将是一种1-衬板:

PA.getValue(clazz, f);

对不起,我不能复制这个

以下是我运行的完整源代码:

import java.lang.reflect.*;
import java.util.*;

@SuppressWarnings("unchecked")
public class ReflectionTest {

    private static Object getPrivateStatic(Class clazz, String f) throws Exception {
        Field field = clazz.getDeclaredField(f);
        field.setAccessible(true);
        return field.get(null);
    }

    private static void a(Class paramClass, String paramString, int paramInt) {
        try {
            ((Map) getPrivateStatic(sg.class, "c")).put(paramString, paramClass);
            //...
        } catch (Exception exc) {
            exc.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
        a(String.class, "test", 0);
        sg.printC();
    }
}

class sg {
    private static Map c = new HashMap();
    private static Map d = new HashMap();
    private static Map e = new HashMap();
    private static Map f = new HashMap();
    private static Map g = new HashMap();
    public static HashMap a = new LinkedHashMap();

    public static void printC() {
        System.out.println(c);
    }
}
这是我运行它时得到的输出:

{test=class java.lang.String}
鉴于您尚未指定
sg
类的完整反编译源代码,我只能猜测可能发生的几件事:

  • 有多个名为
    sg
    的类,您的代码正在使用其中一个类,但您的反编译输出来自另一个类
  • 这些字段存在于
    sg
    内部的一个内部类中
EDIT:您链接到下面的类
sg
似乎包含一个静态字段
c
,并且不包含内部类,因此我希望能够使用反射来访问此字段。我不能将您的
sg
类与我上面编写的
ReflectionTest
类一起使用,因为它依赖于许多其他名称模糊的类,例如
xk

我只能得出结论,您对试图从哪个类访问字段
c
有些困惑。我建议将您的
getPrivateStatic
方法更改为以下内容,这可能会提供更有用的错误消息,包括类的名称和其中的所有字段:

    private static Object getPrivateStatic(Class clazz, String f) throws Exception {
        try {
            Field field = clazz.getDeclaredField(f);
            field.setAccessible(true);
            return field.get(null);
        }
        catch (NoSuchFieldException e) {
            // Throw a more helpful exception.
            throw new NoSuchFieldException(
                "Could not find field named '" + f + "' in class '" + clazz +
                "'.  All fields: " + Arrays.asList(clazz.getDeclaredFields()));
        }
    }

从链接到的文档页面:'如果基础字段是静态字段,则忽略obj参数;它可能是空的。这里的问题肯定不是带有反射源代码的line
field.get(null)
.sg.class:您无法使用getDEclaredField获取静态字段,原因还不完全清楚。以前对此有过疑问。可能是重复的