简体   繁体   中英

IllegalAccessError when declaring a class for a library package

I'm currently working on a maintenance for an undocumented code and have stumbled upon a weird error when running a class in Spring Boot. The code raises an IllegalAccessError on execution, but compiles properly.

It seems that the way the original team tried to access the Kafka Streams' private methods was the "MacGyver way", but I'm unsure about its behaviour since IntelliJ imports the code properly and handles the inheritance as it should (if I declare the class as a local package it highlights saying that the AbstractStream and KTableImpl classes do not exist).

The project is managed with Gradle and it is a micro-service. The execution is made through a Spring Boot and the error raises on startup.

One possible solution was to use Java's Reflection library, but it does not seem the correct approach to solve this error. Maybe a boot setting is wrong?

package org.apache.kafka.streams.kstream.internals;

import org.apache.kafka.streams.kstream.KTable;

public class KTableSpy {

  public static <K> String getName(AbstractStream<K> stream) {
    return stream.name;
  }

  public static <K, V> void enableSendingOldValues(KTable<K, V> table) {
    ((KTableImpl<K, ?, V>) table).enableSendingOldValues();
  }
}

This class should've worked properly and do not interfere on the service startup. Instead, we have the following error

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'AVCompanyStateProjector': Invocation of init method failed; nested exception is java.lang.IllegalAccessError: tried to access method org.apache.kafka.streams.kstream.internals.KTableImpl.enableSendingOldValues()V from class org.apache.kafka.streams.kstream.internals.KTableSpy

Not sure what you try to accomplish, but AbstractStream and KTableImpl are from package org.apache.kafka.streams.kstream.internals and not part of public API (as the name indicates). This implies that methods (or even the whole class) can be added/removed without notice.

Maybe the visibility from enableSendingOldValues() was changes from public to package-private breaking your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM