Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java 17 and enums: class redefinition failed: attempted to change the class NestHost, NestMembers, Record, or PermittedSubclasses attribute #135

Open
4 tasks done
mgorovoy opened this issue Jun 30, 2023 · 1 comment
Assignees

Comments

@mgorovoy
Copy link

mgorovoy commented Jun 30, 2023

Please provide the following information:

  • Version of JMockit that was used: 1.50.0

  • Version of JDK that was used: 17.0.2

  • Version of Maven that was used: 3.9.3

  • Description of the problem or enhancement request: Execution of a test that mocks specific class results in following exception - java.lang.UnsupportedOperationException: class redefinition failed: attempted to change the class NestHost, NestMembers, Record, or PermittedSubclasses attribute

  • 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.

I've stripped the class from our product that was being mocked until I found the minimum viable example of the test (attached) that causes it to fail. I've been able to reproduce the failure both with Maven 3.9.3 (see below), as well as Eclipse 2023-6 on macOS. I've tried both OpenJDK 17.0.2 on macOS and Oracle JDK 17.0.2 on Windows with the same results.

$ mvn -v
Apache Maven 3.9.3 (21122926829f1ead511c958d89bd2f672198ae9f)
Maven home: /usr/local/Cellar/maven/3.9.3/libexec
Java version: 17.0.7, vendor: Homebrew, runtime: /usr/local/Cellar/openjdk@17/17.0.7/libexec/openjdk.jdk/Contents/Home
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "13.4.1", arch: "x86_64", family: "mac"

The issue seems to occur because the first element in static enum declaration overrides a method of the enum class as follows.

COUNT {
	@Override
	public void send(OutputStream outputStream) throws IOException {
		// do something
	}
},

Removing this override as follows makes the exception disappear.

COUNT,

Unfortunately, we use this type of construct in many places throughout our product, and the specific class that this example is borrowed from is mocked very extensively throughout our tests.

jmockit-test.tar.gz

@hazendaz
Copy link
Owner

hazendaz commented Dec 2, 2023

Please look this over mockito/mockito#2315 as it seems same issues existed with byte buddy and mockito that were fixed. I don't know what the fix was specifically but some users comment that mocking enums is a dubious process and there are other ways around it. Possibly one of those might resolve it for you or possibly you can find more info on what was fixed that we could also fix.

@hazendaz hazendaz changed the title java.lang.UnsupportedOperationException: class redefinition failed: attempted to change the class NestHost, NestMembers, Record, or PermittedSubclasses attribute java 17 and enums: class redefinition failed: attempted to change the class NestHost, NestMembers, Record, or PermittedSubclasses attribute Dec 2, 2023
@hazendaz hazendaz self-assigned this Feb 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants