[英]Java reflection can't instantiate a private nested class with no arg constructor
I need to create an instance of a private static class
which doesn't have a constructor (only the default no-arg constructor).我需要创建一个没有构造函数的
private static class
的实例(只有默认的无参数构造函数)。 The class is located in a 3d party library.该课程位于 3d 派对库中。
However, Class.getDeclaredConstructors()
returns an empty list for some reason.但是,
Class.getDeclaredConstructors()
出于某种原因返回一个空列表。 I created a sample with a private static class
within the same codebase, and in this case Class.getDeclaredConstructors()
returned a list with 1 constructor (the default one) as expected.我在同一个代码库中创建了一个带有
private static class
的示例,在这种情况下, Class.getDeclaredConstructors()
) 按预期返回了一个带有 1 个构造函数(默认构造函数)的列表。
Also calling Class.newInstance()
directly results in an exception like还直接调用
Class.newInstance()
会导致异常,例如
Caused by: java.lang.IllegalAccessException: java.lang.Class<MyClassA> is not accessible from java.lang.Class<MyClassB>
What might be the reason for this strange behavior?这种奇怪行为的原因可能是什么? Are there any other ways to instantiate a
private static class
without non-default constructors?有没有其他方法可以在没有非默认构造函数的情况下实例化
private static class
?
实际上,我正在尝试在 Android API 32 上执行此操作,这可能与这些限制有关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.