Reflections - Java 8 - Invalid Constant Type

Reflections - Java 8 - invalid constant type

If you look at this table, you’ll see that “constant type: 18” refers to the CONSTANT_InvokeDynamic attribute whose tag value is 18.

So you are using a library which has a class parser which is not Java 8 compatible. Actually, this class parser even isn’t Java 7 compatible as this constant value is specified since Java 7. It just got away with that as ordinary Java code doesn’t use this feature in Java 7. But when interacting with code produced by different programming languages for the JVM, it could even fail with Java 7.

There’s an item in the bug tracker of Reflections describing your problem. At the bottom, you will find the notice:

With this fix: https://issues.jboss.org/browse/JASSIST-174 javassist got support for this constant. So with 3.18.2-GA this error doesn't occur.

When using Reflections on a class containing Scanner.next() I get invalid constant type 18 error

Ive found the answer!

Update your javassist class, I chose to do so through my build.gradle class using this line

implementation 'org.javassist:javassist:3.29.0-GA'

If there is a new version by the time of reading this replace "29.0" with the latest version number

Why javaassist throws invalid constant type: 18 when loading Entitymanager only when lambdas are used in project

It fails to create the EntityManager instance, because a class the manager is loading fails. Then it is not a javassist problem with the manager, but with the class loaded by it. Thus it is entirely possible to have one time no problem creating the manager (without lambdas in the usage) and one time failing (with lambdas). The error message suggests that the old javaassist version does not understand invokedynamic instruction, which means it is a pre Java7 version. Only that in Java7 java itself did not use the instruction, which makes it entirely possible for the problem only appearing with java8 code. And lambdas are making use of invokedynamic.

Caused by: java.lang.RuntimeException: java.io.IOException: invalid constant type: 18

This is caused because of the conflict of dependencies. Resolving the dependency conflicts is one way to resolve this issue and the other way is to reorder <dependency></dependency> elements in your pom.xml. Move the powermock dependency declarations to the top of the <dependencies></dependencies> section. This is a complete hack and the right way to resolve it would be to resolve the dependency conflicts.

To identify those conflict you may use the command "mvn dependency:tree". Here is the dependency tree for my application for which I came across this same problem. Notice that there are two dependencies of "javassist" in the tree.

In my case, I am adding so many maven modules/components into my project, I added that dependency below the powermock dependency that start working the things.

Upgrade to Java 8 causes Orika mapper in unittest java.io.IOException: invalid constant type: 15 at 142

Your idea goes into the right direction.

the java.lang.RuntimeException: java.io.IOException: invalid constant type: 15 at 142
shows that the the application has problems with java 8 - as explained inn your link.

The ma.glasnost.orika mapper depends on javassist as you can see in the Stack Trace.
This is a transitive dependency of orika.

You could use mvn dependency:tree -verbose to build a dependency tree.
There you can look up the what library is depending eg on javassist and the exact version.

To use a javassist version that is Java 8 compatible use that dependency to overwrite the implicit dependency of orika:

  <dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.18.2-GA</version>
</dependency>

But for this example it might by better to upgrade the orika version to 1.4.6 as this one is Java 8 ready.

java.io.IOException: invalid constant type: 19 at 5

Though this doesn't answer on how to fix this but as stated in JDK-8161256 about the Constant pool tags:

Java 9 uses codes 19 and 20 for the module system.

and the way currently javassist creates ClassFile is by looking up in the ConstantPool (table until Java8) using the readOne method which certainly lacks something like a Module for the latest java version. Your dependency hierarchy for javassist:

  • org.springframework.boot:spring-boot-starter-actuator:jar:2.0.0.M3:compile

    • org.springframework.boot:spring-boot-starter-data-jpa:jar:2.0.0.M3:compile

      • org.hibernate:hibernate-core:jar:5.2.10.Final:compile

        • org.javassist:javassist:jar:3.21.0-GA:compile

Looking at the javassist side of things:

  • There seems to be a similar bug registered on javassist/issues#147.

  • The rel_3_22_0_cr2 (last release) from them
    reads Compatible with Java 9-ea+164. Same issue occurs with this version of javassist as well.

  • So you can probably wait for javassist to come back over this with a solution.

Adding mock breaks database tests

As indicated in the comments, the answer lies with Reflections - Java 8 - invalid constant type.

It turns out that constant: 18 refers to an issue when using java 8 style lambdas with libraries not compiled under java 8.

So to fix the issue, I had to write more code as follows:

Mockito.when(udfConfigurationRepository.save(any(UDFConfiguration.class))).thenAnswer(new Answer<UDFConfiguration>() {
@Override
public UDFConfiguration answer(InvocationOnMock invocationOnMock) throws Throwable {
UDFConfiguration config = (UDFConfiguration) invocationOnMock.getArguments()[0];
return config;
}
});

Doing so fixed this unit test and all of the others.



Related Topics



Leave a reply



Submit