The type org.objectweb.asm.ClassVisitor cannot be resolved. It is indirectly referenced from required .class files

删除回忆录丶 提交于 2021-01-29 07:18:39

问题


I am using ASM 7.0 in a short Java 11 project, which convert a class file to a text using the following code:

final var charset = StandardCharsets.UTF_8;
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
try (var pw = new PrintWriter(bos, false, charset)) {
  final org.objectweb.asm.ClassVisitor traceClassVisitor = new TraceClassVisitor(null,
      new org.objectweb.asm.util.Textifier(), new PrintWriter(bos));
  new ClassReader(stream.getStream()).accept(traceClassVisitor,
      ClassReader.SKIP_DEBUG | ClassReader.SKIP_CODE | ClassReader.SKIP_FRAMES);
}

This works, however from times to times, Eclipse produce an error: The type org.objectweb.asm.ClassVisitor cannot be resolved. It is indirectly referenced from required .class files. Clean and compile fixes the issue.

When built with Maven 3.6.0 + Java 11, there are no such errors.

My module-info.java is the following:

module stackoverflow.asm {
  exports stackoverflow.asm;
  // requires ransitive org.objectweb.asm;
  // requires transitive org.objectweb.asm.tree;
  requires transitive org.objectweb.asm.util;
  // requires transitive org.objectweb.asm.tree.analysis;
  uses java.nio.file.spi.FileSystemProvider;
}

In maven, I have the following dependencies:

<dependencies>
  <dependency>
    <groupId>org.ow2.asm</groupId>
    <artifactId>asm</artifactId>
    <version>7.0</version>
  </dependency>
  <dependency>
    <groupId>org.ow2.asm</groupId>
    <artifactId>asm-util</artifactId>
    <version>7.0</version>
  </dependency>
</dependencies>

While I understand the problem reported by Eclipse, org.objectweb.asm.util.Textifier requires the class org.objectweb.asm.ClassVisitor, but Eclipse fail to find it in the classpath (or should I say module path ?).

Is it a bug of Eclipse (I am using latest) or am I missing something in my module-info.java ?

Note: as said, I understand the error; I already had it in the past, when using maven dependencies with bad transitive or no dependencies, or simply when uninstalling the JDK in an already compiled project. This was relatively easy to fix, but here I don't know how.

来源:https://stackoverflow.com/questions/54245536/the-type-org-objectweb-asm-classvisitor-cannot-be-resolved-it-is-indirectly-ref

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!