简体   繁体   English

声明库包的类时出现IllegalAccessError

[英]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. 我正在为一个未记录的代码进行维护,并且在Spring Boot中运行类时偶然发现了一个奇怪的错误。 The code raises an IllegalAccessError on execution, but compiles properly. 代码在执行时引发IllegalAccessError,但编译正确。

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). 似乎原始团队试图访问Kafka Streams的私有方法的方式是“MacGyver方式”,但我不确定它的行为,因为IntelliJ正确地导入代码并处理它应该的继承(如果我声明class作为本地包,它突出显示AbstractStream和KTableImpl类不存在)。

The project is managed with Gradle and it is a micro-service. 该项目由Gradle管理,它是一项微服务。 The execution is made through a Spring Boot and the error raises on startup. 执行是通过Spring Boot执行的,启动时错误会增加。

One possible solution was to use Java's Reflection library, but it does not seem the correct approach to solve this error. 一种可能的解决方案是使用Java的Reflection库,但它似乎不是解决此错误的正确方法。 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). 不确定您要完成的任务,但AbstractStreamKTableImpl来自包org.apache.kafka.streams.kstream.internals而不是公共API的一部分(如名称所示)。 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. 也许enableSendingOldValues()的可见性是从public更改为打包私有代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在JMock中访问包私有类时出现IllegalAccessError - IllegalAccessError when accessing package private class in JMock 通过另一个包的公共子类使用包私有类的公共方法引用时出现IllegalAccessError - IllegalAccessError when using a public method reference of a package-private class through a public subclass from another package 将类添加到Swing包中-结果是可见函数上的IllegalAccessError - Adding a class to the Swing package — result is an IllegalAccessError on a visible function 在同一个包中对sun。*类进行子类化会产生IllegalAccessError - Subclassing sun.* class in same package gives IllegalAccessError IllegalAccessError 与 RenderScript 支持库 - IllegalAccessError with RenderScript Support library 为什么在远程加载类时出现IllegalAccessError? - Why do I get an IllegalAccessError when loading a class remotely? 我在尝试使用 main() 方法运行 class 时收到 IllegalAccessError - I am receiving an IllegalAccessError when trying to run the class with the main() method in it IllegalAccessError:类无法访问方法 - IllegalAccessError: Method is inaccessible to class 将Spring Data Repo软件包设为本地时出现java.lang.IllegalAccessError - java.lang.IllegalAccessError when making Spring Data repo package local Kotlin Java抽象类IllegalAccessError - Kotlin java abstract class IllegalAccessError
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM