简体   繁体   English

Java 反射无法实例化没有 arg 构造函数的私有嵌套类

[英]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.

相关问题 使用java反射实例化私有内部类 - Instantiate private inner class with java reflection 为什么Java Reflection无法在另一个包中找到类的包私有构造函数? - Why can't Java Reflection find the package-private constructor of a class in another package? 构造函数 <Class> 未定义。 不能使用 <super(arg)> 在java中 - The constructor <Class> is undefined. Can't use <super(arg)> in java 无法通过反思实例化课程 - Can't instantiate a class through reflection java.lang.InstantiationException:无法实例化类; 没有空的构造函数 - java.lang.InstantiationException: can't instantiate class; no empty constructor Java通用方法,使用反射使用任何构造函数实例化任何类 - Java Generic Method to Instantiate any Class with any Constructor using Reflection 使用Java反射,我可以访问类型为private静态嵌套类的private字段吗? - Using Java reflection, can I access a private field of type private static nested class? 无法实例化类......; 没有空的构造函数 - Can't instantiate class …; no empty constructor Java反射-调用具有私有构造函数的泛型类的静态方法 - Java reflection - Call a static method of a generic class that has private constructor 无法实例化课程; 没有空的构造函数 - can't instantiate class; no empty constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM