最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Java Reflection - Loading a field with dependency - Stack Overflow

programmeradmin4浏览0评论

I want to get the field names of a class through reflection, but one of the fields is from a class that is not in the jar file and was added with a dependency, and it throws NoClassDefFoundError exception.

Is there a way I can skip that field or load it without needing that dependency?

@Nullable
public Class<?> loadClass(String name) {
    try {
        return Class.forName(name, false, classLoader);
    } catch (ClassNotFoundException e) {
        LOGGER.error("Class {} is not loaded.", name);
        return null;
    }
}
Class<?> clazz = mcLoader.loadClass("className");
List<String> fieldNames = new ArrayList<>();
for (Field field: clazz.getDeclaredFields()) {
    field.setAccessible(true);
    fieldNames.add(field.getName() + ":" + field.getType().getName());
}

try-catch didn't help because the exception is coming from clazz.getDeclaredFields() and if I use try-catch, non of fields will load as the field with dependency is the first field.

I want to get the field names of a class through reflection, but one of the fields is from a class that is not in the jar file and was added with a dependency, and it throws NoClassDefFoundError exception.

Is there a way I can skip that field or load it without needing that dependency?

@Nullable
public Class<?> loadClass(String name) {
    try {
        return Class.forName(name, false, classLoader);
    } catch (ClassNotFoundException e) {
        LOGGER.error("Class {} is not loaded.", name);
        return null;
    }
}
Class<?> clazz = mcLoader.loadClass("className");
List<String> fieldNames = new ArrayList<>();
for (Field field: clazz.getDeclaredFields()) {
    field.setAccessible(true);
    fieldNames.add(field.getName() + ":" + field.getType().getName());
}

try-catch didn't help because the exception is coming from clazz.getDeclaredFields() and if I use try-catch, non of fields will load as the field with dependency is the first field.

Share Improve this question asked Mar 25 at 19:41 BaRiBoDBaRiBoD 133 bronze badges 6
  • 1 I'm not sure I follow. When you say "one of the fields is from a class that is not in the jar file", do you mean that the data type of the field is a class type, and that class has not been loaded? That would make some sense, but what you seem actually to have said is that you are trying to reflect a field belonging to a class that has not been loaded, and that's definitely impossible. – John Bollinger Commented Mar 25 at 19:53
  • Did you try inspecting the class this way: ClassLoader.getSystemClassLoader().loadClass("className").getDeclaredFields() ? – Arno Commented Mar 25 at 19:57
  • Given you seem to only want the name of the field and the name of its type, perhaps you could inspect the byte-code instead of using reflection. – Slaw Commented Mar 25 at 20:03
  • A NoClassDefFoundError can occur if the class you are loading depends on other classes that are missing. The error should tell you which class is missing. Check that class and include the jar containing that class to fix the error. – sanurah Commented Mar 25 at 20:20
  • @JohnBollinger Yes, I meant the data type of the field is a class type that has not been loaded. – BaRiBoD Commented Mar 25 at 21:30
 |  Show 1 more comment

1 Answer 1

Reset to default 2

I want to get the field names of a class through reflection

If you don't already have the field names, then your alternatives for getting them by reflection are Class.getDeclaredFields() and Class.getFields(), depending on whether you want to include inherited fields.

try-catch didn't help because the exception is coming from clazz.getDeclaredFields() and if I use try-catch, non of fields will load as the field with dependency is the first field.

These methods will attempt to load any unloaded classes representing the fields' data types, and if they cannot do so then they will throw instead of returning a result. They return all the results at once, as an array, so there is no provision for selective or partial results, and a problem with any field will scuttle the whole thing.

Overall, there's not much you can do with a Class object if you cannot load all the reflected class's dependencies. Only when lazy linking is performed can you even get your hands on such an object in the first place.

Is there a way I can skip that field or load it without needing that dependency?

Not with reflection. You could conceivably analyze the class file directly, without performing Java class loading at all, but that's a different thing. The Java standard library has no provision for it before Java 22, but there are third-party tools that might help. Java 24 has has added a classfile API (previewed starting in Java 22) that looks like it provides an alternative to reflection for what you describe [thanks @Slaw].

发布评论

评论列表(0)

  1. 暂无评论