From 6fcc3ab0c5b60446e3267e31882e85f038dce342 Mon Sep 17 00:00:00 2001 From: Thomas Behr Date: Tue, 16 Jul 2024 15:26:49 +0200 Subject: [PATCH] fixed UnsupportedOperationException: PermittedSubclasses requires ASM9 (#156) --- CHANGELOG.md | 4 ++- .../yworks/yshrink/core/OutputVisitor.java | 13 +++++----- .../yworks/yshrink/model/ModelVisitor.java | 2 +- .../yworks/yguard/obf/KeepExtendsTest.java | 25 +++++++++---------- .../yworks/yguard/obf/asm/SealedClassImpl.txt | 7 ++++++ .../obf/asm/SealedSerializableClass.txt | 15 +++++++++++ .../com/yworks/yguard/obf/asm/SimpleClass.txt | 13 ++++++++++ 7 files changed, 58 insertions(+), 21 deletions(-) create mode 100644 src/test/resources/com/yworks/yguard/obf/asm/SealedClassImpl.txt create mode 100644 src/test/resources/com/yworks/yguard/obf/asm/SealedSerializableClass.txt create mode 100644 src/test/resources/com/yworks/yguard/obf/asm/SimpleClass.txt diff --git a/CHANGELOG.md b/CHANGELOG.md index cab755fa..343ea502 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Process files in the order they appear in the respective input archives or directories. This fixes a regression that was introduced with directory support in version 3.0.0. +- Fixed `UnsupportedOperationException` that occurred for Java 17 class files when excluding classes from obfuscation + using the `extends` or `implements` attributes of the `rename.keep.class` element in the yGuard ANT task. ## [4.1.0] @@ -117,7 +119,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [2.7.2] - 2019-09-26 ### Fixed -- Fixed `UnsupportedOperationException` that occurred for Java 11 class files when excluding classes from obfuscation using the `extends` or `implements` attributes of the `rename.keep.class` element in the yGuard ANT task. +- Fixed `UnsupportedOperationException` that occurred for Java 11 class files when excluding classes from obfuscation using the `extends` or `implements` attributes of the `rename.keep.class` element in the yGuard ANT task. - Fixed yGuard's `language-conformity` mode `illegal` to no longer produce unqualified names that contain dots or spaces. ## [2.7.1] - 2019-02-26 diff --git a/src/main/java/com/yworks/yshrink/core/OutputVisitor.java b/src/main/java/com/yworks/yshrink/core/OutputVisitor.java index a19cb9a2..c0fd90f0 100644 --- a/src/main/java/com/yworks/yshrink/core/OutputVisitor.java +++ b/src/main/java/com/yworks/yshrink/core/OutputVisitor.java @@ -13,6 +13,7 @@ * @author Michael Schroeder, yWorks GmbH http://www.yworks.com */ public class OutputVisitor extends ClassVisitor { + static final int OPCODES_ASM = Opcodes.ASM9; private ClassVisitor cv; private Model model; @@ -33,7 +34,7 @@ public class OutputVisitor extends ClassVisitor { * @param createStubs the create stubs */ public OutputVisitor( final ClassVisitor cv, final Model model, boolean createStubs ) { - super(Opcodes.ASM7); + super(OPCODES_ASM); this.createStubs = createStubs; this.cv = cv; this.model = model; @@ -215,7 +216,7 @@ class OutputMethodVisitor extends MethodVisitor { * @param delegate the delegate */ public OutputMethodVisitor( MethodVisitor delegate ) { - super(Opcodes.ASM7); + super(OPCODES_ASM); this.delegate = delegate; } @@ -429,7 +430,7 @@ class OutputFieldVisitor extends FieldVisitor { * @param delegate the delegate */ public OutputFieldVisitor(FieldVisitor delegate) { - super(Opcodes.ASM7); + super(OPCODES_ASM); this.delegate = delegate; } @@ -487,7 +488,7 @@ class StubOutputMethodVisitor extends MethodVisitor { * @param visitStub the visit stub */ public StubOutputMethodVisitor(MethodVisitor delegate, boolean visitStub) { - super(Opcodes.ASM7); + super(OPCODES_ASM); this.delegate = delegate; this.visitStub = visitStub; } @@ -608,7 +609,7 @@ static class DoNothingAnnotationVisitor extends AnnotationVisitor { * Instantiates a new Do nothing annotation visitor. */ public DoNothingAnnotationVisitor() { - super(Opcodes.ASM7); + super(OPCODES_ASM); } public void visit( String name, Object value ) { @@ -647,7 +648,7 @@ class OutputAnnotationVisitor extends AnnotationVisitor { * @param delegate the delegate */ public OutputAnnotationVisitor( AnnotationVisitor delegate ) { - super(Opcodes.ASM7); + super(OPCODES_ASM); this.delegate = delegate; } diff --git a/src/main/java/com/yworks/yshrink/model/ModelVisitor.java b/src/main/java/com/yworks/yshrink/model/ModelVisitor.java index 63e80bae..55bd9940 100644 --- a/src/main/java/com/yworks/yshrink/model/ModelVisitor.java +++ b/src/main/java/com/yworks/yshrink/model/ModelVisitor.java @@ -21,7 +21,7 @@ public class ModelVisitor extends ClassVisitor { /** * The Opcodes asm. */ - static final int OPCODES_ASM = Opcodes.ASM7; + static final int OPCODES_ASM = Opcodes.ASM9; private Model model; diff --git a/src/test/java/com/yworks/yguard/obf/KeepExtendsTest.java b/src/test/java/com/yworks/yguard/obf/KeepExtendsTest.java index a4197e34..51d19be5 100644 --- a/src/test/java/com/yworks/yguard/obf/KeepExtendsTest.java +++ b/src/test/java/com/yworks/yguard/obf/KeepExtendsTest.java @@ -30,17 +30,9 @@ * @author Thomas Behr */ public class KeepExtendsTest extends AbstractObfuscationTest { - /** - * The Name. - */ @Rule public TestName name = new TestName(); - /** - * Test extends. - * - * @throws Exception the exception - */ @Test public void testExtends() throws Exception { impl(new TypeStruct("asm/AbstractBaseClass.txt", "com.yworks.ext.test.AbstractBaseClass"), @@ -49,16 +41,23 @@ public void testExtends() throws Exception { new TypeStruct("asm/Sample.txt", "com.yworks.impl.test.Sample")); } - /** - * Test nests. - * - * @throws Exception the exception - */ @Test public void testNests() throws Exception { impl(new TypeStruct("asm/OuterClass.txt", "com.yworks.yguard.obf.asm.OuterClass")); } + @Test + public void testPermittedSubclasses() throws Exception { + // PermittedSubclasses attribute is used only in Java 17 and newer + if (17 <= getMajorVersion()) { + impl(new TypeStruct("asm/SealedClassImpl.txt", "com.yworks.yguard.obf.asm.SealedClassImpl"), + new TypeStruct("asm/SealedSerializableClass.txt", "com.yworks.yguard.obf.asm.SealedSerializableClass"), + new TypeStruct("asm/SimpleClass.txt", "com.yworks.yguard.obf.asm.SimpleClass")); + } else { + System.err.println("Run test with Java 17 or newer."); + } + } + private void impl( final TypeStruct... types ) throws Exception { assertTrue("Invalid Java version", 11 <= getMajorVersion()); diff --git a/src/test/resources/com/yworks/yguard/obf/asm/SealedClassImpl.txt b/src/test/resources/com/yworks/yguard/obf/asm/SealedClassImpl.txt new file mode 100644 index 00000000..1346c257 --- /dev/null +++ b/src/test/resources/com/yworks/yguard/obf/asm/SealedClassImpl.txt @@ -0,0 +1,7 @@ +package com.yworks.yguard.obf.asm; + +public final class SealedClassImpl extends SealedSerializableClass { + public SealedClassImpl() { + super(666); + } +} diff --git a/src/test/resources/com/yworks/yguard/obf/asm/SealedSerializableClass.txt b/src/test/resources/com/yworks/yguard/obf/asm/SealedSerializableClass.txt new file mode 100644 index 00000000..ec555561 --- /dev/null +++ b/src/test/resources/com/yworks/yguard/obf/asm/SealedSerializableClass.txt @@ -0,0 +1,15 @@ +package com.yworks.yguard.obf.asm; + +import java.io.Serializable; + +public sealed class SealedSerializableClass implements Serializable permits SealedClassImpl { + private int data; + + public SealedSerializableClass( final int data ) { + this.data = data; + } + + public int getData() { + return data; + } +} \ No newline at end of file diff --git a/src/test/resources/com/yworks/yguard/obf/asm/SimpleClass.txt b/src/test/resources/com/yworks/yguard/obf/asm/SimpleClass.txt new file mode 100644 index 00000000..31ecc973 --- /dev/null +++ b/src/test/resources/com/yworks/yguard/obf/asm/SimpleClass.txt @@ -0,0 +1,13 @@ +package com.yworks.yguard.obf.asm; + +public class SimpleClass { + private int data; + + public SimpleClass( final int data ) { + this.data = data; + } + + public int getData() { + return data; + } +} \ No newline at end of file