Skip to content

Mocking of methods in enum inner classes fails when classfile version is >= 53 (Java 9+) #616

@nebel

Description

@nebel
  • Version of JMockit that was used:

1.46

  • Description of the problem or enhancement request:

For Java code compiled with classfile version >= 53 (Java 9+), mocking of enum inner classes fails due to access flag changes in Java 9. I believe this change was made in the Java compiler due to this bug: https://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8034044

As a result of the above change, the inner class of a specific enum constant may have access flags ENUM + FINAL (new) instead of ENUM + STATIC (old). But when MockedClassModifier#visitInnerClass visits these inner classes, it expects only ENUM + STATIC. As a result, mocks are not applied correctly.

As for the fix, maybe looking for the enum flag alone is enough, or perhaps both flag combinations could be checked.

This should be reproducible using the existing test suite if MockedEnumsTest#mockEnumWithValueSpecificMethods is compiled in a recent version of Java. You can also inspect the generated flags directly using javap or IDE bytecode analysis tools (I did so and it confirmed that the above change happened in Java 9).

  • Check the following:
  • If a defect or unexpected result, JMockit project members should be able to reproduce it.
    For that, include an example test (perhaps accompanied by a Maven/Gradle build script) which
    can be executed without changes and reproduces the failure.

  • If an enhancement or new feature request, it should be justified by an example test
    demonstrating the validity and usefulness of the desired enhancement or new feature.

  • The issue does not fall outside the scope of the project (for example, attempting to use
    JMockit APIs from Groovy or Scala code, or with an Android runtime).

  • The JDK where the problem occurs is a final release, not a development build.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions