diff --git a/asm/src/main/java/org/objectweb/asm/AnnotationVisitor.java b/asm/src/main/java/org/objectweb/asm/AnnotationVisitor.java index 0c696844ed1a5f2ecd9e261db8ef91aa34b5d40a..ad5e96344a239abf48d9bd41655520e180d8c197 100644 --- a/asm/src/main/java/org/objectweb/asm/AnnotationVisitor.java +++ b/asm/src/main/java/org/objectweb/asm/AnnotationVisitor.java @@ -84,6 +84,17 @@ public abstract class AnnotationVisitor { this.av = annotationVisitor; } + /** + * The annotation visitor to which this visitor must delegate method calls. May be {@literal + * null}. + * + * @return the annotation visitor to which this visitor must delegate method calls, or {@literal + * null}. + */ + public AnnotationVisitor getDelegate() { + return av; + } + /** * Visits a primitive value of the annotation. * diff --git a/asm/src/main/java/org/objectweb/asm/ClassVisitor.java b/asm/src/main/java/org/objectweb/asm/ClassVisitor.java index a557f9e4c81c816631786f988cea1f21bd173ac5..6adf6d2395472902dabad5d3ff2a3b08026901b7 100644 --- a/asm/src/main/java/org/objectweb/asm/ClassVisitor.java +++ b/asm/src/main/java/org/objectweb/asm/ClassVisitor.java @@ -83,6 +83,15 @@ public abstract class ClassVisitor { this.cv = classVisitor; } + /** + * The class visitor to which this visitor must delegate method calls. May be {@literal null}. + * + * @return the class visitor to which this visitor must delegate method calls, or {@literal null}. + */ + public ClassVisitor getDelegate() { + return cv; + } + /** * Visits the header of the class. * diff --git a/asm/src/main/java/org/objectweb/asm/FieldVisitor.java b/asm/src/main/java/org/objectweb/asm/FieldVisitor.java index fc271156b6436ecc9ff0a208a7928b6f63341ec0..2893d9f55d2d5f6af0966bde2112abb3c6e84cce 100644 --- a/asm/src/main/java/org/objectweb/asm/FieldVisitor.java +++ b/asm/src/main/java/org/objectweb/asm/FieldVisitor.java @@ -80,6 +80,15 @@ public abstract class FieldVisitor { this.fv = fieldVisitor; } + /** + * The field visitor to which this visitor must delegate method calls. May be {@literal null}. + * + * @return the field visitor to which this visitor must delegate method calls, or {@literal null}. + */ + public FieldVisitor getDelegate() { + return fv; + } + /** * Visits an annotation of the field. * diff --git a/asm/src/main/java/org/objectweb/asm/MethodVisitor.java b/asm/src/main/java/org/objectweb/asm/MethodVisitor.java index 92068e862b59bab6334f29d4f965eb6ce6eb2b1f..7211e6561a405da59ec6a9fb35c8b9d5487b0ba0 100644 --- a/asm/src/main/java/org/objectweb/asm/MethodVisitor.java +++ b/asm/src/main/java/org/objectweb/asm/MethodVisitor.java @@ -96,6 +96,16 @@ public abstract class MethodVisitor { this.mv = methodVisitor; } + /** + * The method visitor to which this visitor must delegate method calls. May be {@literal null}. + * + * @return the method visitor to which this visitor must delegate method calls, or {@literal + * null}. + */ + public MethodVisitor getDelegate() { + return mv; + } + // ----------------------------------------------------------------------------------------------- // Parameters, annotations and non standard attributes // ----------------------------------------------------------------------------------------------- diff --git a/asm/src/main/java/org/objectweb/asm/ModuleVisitor.java b/asm/src/main/java/org/objectweb/asm/ModuleVisitor.java index 6ef58e714b910c3d80e9b549b5e7fade735871fe..2dd0a4c3ae10243fd55caddab529bbcb118938c1 100644 --- a/asm/src/main/java/org/objectweb/asm/ModuleVisitor.java +++ b/asm/src/main/java/org/objectweb/asm/ModuleVisitor.java @@ -82,6 +82,16 @@ public abstract class ModuleVisitor { this.mv = moduleVisitor; } + /** + * The module visitor to which this visitor must delegate method calls. May be {@literal null}. + * + * @return the module visitor to which this visitor must delegate method calls, or {@literal + * null}. + */ + public ModuleVisitor getDelegate() { + return mv; + } + /** * Visit the main class of the current module. * diff --git a/asm/src/main/java/org/objectweb/asm/RecordComponentVisitor.java b/asm/src/main/java/org/objectweb/asm/RecordComponentVisitor.java index 3f040819fd1ad92407cba8fcac3d3e8f5468efb6..32784e2a089253cb32b2a9ac35e68e0befcc04a3 100644 --- a/asm/src/main/java/org/objectweb/asm/RecordComponentVisitor.java +++ b/asm/src/main/java/org/objectweb/asm/RecordComponentVisitor.java @@ -45,7 +45,7 @@ public abstract class RecordComponentVisitor { /** * The record visitor to which this visitor must delegate method calls. May be {@literal null}. */ - /*package-private*/ RecordComponentVisitor delegate; + protected RecordComponentVisitor delegate; /** * Constructs a new {@link RecordComponentVisitor}. @@ -85,7 +85,8 @@ public abstract class RecordComponentVisitor { /** * The record visitor to which this visitor must delegate method calls. May be {@literal null}. * - * @return the record visitor to which this visitor must delegate method calls or {@literal null}. + * @return the record visitor to which this visitor must delegate method calls, or {@literal + * null}. */ public RecordComponentVisitor getDelegate() { return delegate; diff --git a/asm/src/test/java/org/objectweb/asm/AnnotationVisitorTest.java b/asm/src/test/java/org/objectweb/asm/AnnotationVisitorTest.java index fc4d763467df52fc7f3e77f51e69723ac0e04b3d..6b06d0813f78f415b00c215c66a6c04799d3b16f 100644 --- a/asm/src/test/java/org/objectweb/asm/AnnotationVisitorTest.java +++ b/asm/src/test/java/org/objectweb/asm/AnnotationVisitorTest.java @@ -29,6 +29,7 @@ package org.objectweb.asm; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -61,6 +62,14 @@ class AnnotationVisitorTest extends AsmTest { assertEquals("Unsupported api 0", exception.getMessage()); } + @Test + void testGetDelegate() { + AnnotationVisitor delegate = new AnnotationVisitor(Opcodes.ASM4) {}; + AnnotationVisitor visitor = new AnnotationVisitor(Opcodes.ASM4, delegate) {}; + + assertSame(delegate, visitor.getDelegate()); + } + /** * Tests that ClassReader accepts visitor which return null AnnotationVisitor, and that returning * null AnnotationVisitor is equivalent to returning an EmptyAnnotationVisitor. diff --git a/asm/src/test/java/org/objectweb/asm/ClassVisitorTest.java b/asm/src/test/java/org/objectweb/asm/ClassVisitorTest.java index fb6e71151a3abda87d6e505a3af83f50865452e4..5c04868f7a3ee19d16ae812b6b4693fdcca38cd9 100644 --- a/asm/src/test/java/org/objectweb/asm/ClassVisitorTest.java +++ b/asm/src/test/java/org/objectweb/asm/ClassVisitorTest.java @@ -30,6 +30,7 @@ package org.objectweb.asm; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -68,6 +69,14 @@ class ClassVisitorTest extends AsmTest { assertEquals("Unsupported api 0", exception.getMessage()); } + @Test + void testGetDelegate() { + ClassVisitor delegate = new ClassVisitor(Opcodes.ASM4) {}; + ClassVisitor visitor = new ClassVisitor(Opcodes.ASM4, delegate) {}; + + assertSame(delegate, visitor.getDelegate()); + } + /** * Tests that classes are unchanged when transformed with a ClassReader -> class adapter -> * ClassWriter chain, where "class adapter" is a ClassVisitor which returns FieldVisitor, diff --git a/asm/src/test/java/org/objectweb/asm/ClassWriterTest.java b/asm/src/test/java/org/objectweb/asm/ClassWriterTest.java index 0871e90c23a11b035ad2d7ee2426da79a3beb9ca..fb9034edb23e62353ef0ad5f170246a57b8d4d2d 100644 --- a/asm/src/test/java/org/objectweb/asm/ClassWriterTest.java +++ b/asm/src/test/java/org/objectweb/asm/ClassWriterTest.java @@ -141,8 +141,10 @@ class ClassWriterTest extends AsmTest { Method classWriterMethod = ClassWriter.class.getMethod( classVisitorMethod.getName(), classVisitorMethod.getParameterTypes()); - assertTrue( - Modifier.isFinal(classWriterMethod.getModifiers()), classWriterMethod + " is final"); + if (!classWriterMethod.getName().equals("getDelegate")) { + assertTrue( + Modifier.isFinal(classWriterMethod.getModifiers()), classWriterMethod + " is final"); + } } catch (NoSuchMethodException e) { fail("ClassWriter must override " + classVisitorMethod); } diff --git a/asm/src/test/java/org/objectweb/asm/FieldVisitorTest.java b/asm/src/test/java/org/objectweb/asm/FieldVisitorTest.java index 0473a0cb43fb5ce524f7861762ccd860bd078f82..b7f37cd7d6455f2cd41e2886e659627408b802a5 100644 --- a/asm/src/test/java/org/objectweb/asm/FieldVisitorTest.java +++ b/asm/src/test/java/org/objectweb/asm/FieldVisitorTest.java @@ -29,6 +29,7 @@ package org.objectweb.asm; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.Test; @@ -55,4 +56,12 @@ class FieldVisitorTest { Exception exception = assertThrows(IllegalArgumentException.class, constructor); assertEquals("Unsupported api 0", exception.getMessage()); } + + @Test + void testGetDelegate() { + FieldVisitor delegate = new FieldVisitor(Opcodes.ASM4) {}; + FieldVisitor visitor = new FieldVisitor(Opcodes.ASM4, delegate) {}; + + assertSame(delegate, visitor.getDelegate()); + } } diff --git a/asm/src/test/java/org/objectweb/asm/MethodVisitorTest.java b/asm/src/test/java/org/objectweb/asm/MethodVisitorTest.java index 2a2051229cf0134e47ced46227802174f0d2c033..915b1b4da20053d87f31bc36138473e4aae88f28 100644 --- a/asm/src/test/java/org/objectweb/asm/MethodVisitorTest.java +++ b/asm/src/test/java/org/objectweb/asm/MethodVisitorTest.java @@ -29,6 +29,7 @@ package org.objectweb.asm; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -59,6 +60,14 @@ class MethodVisitorTest extends AsmTest { assertEquals("Unsupported api 0", exception.getMessage()); } + @Test + void testGetDelegate() { + MethodVisitor delegate = new MethodVisitor(Opcodes.ASM4) {}; + MethodVisitor visitor = new MethodVisitor(Opcodes.ASM4, delegate) {}; + + assertSame(delegate, visitor.getDelegate()); + } + @Test void testVisitParameter_asm4Visitor() { MethodVisitor methodVisitor = new MethodVisitor(Opcodes.ASM4, null) {}; diff --git a/asm/src/test/java/org/objectweb/asm/ModuleVisitorTest.java b/asm/src/test/java/org/objectweb/asm/ModuleVisitorTest.java new file mode 100644 index 0000000000000000000000000000000000000000..c9eb3382afd128bc1425a93250a53fb95c8249fd --- /dev/null +++ b/asm/src/test/java/org/objectweb/asm/ModuleVisitorTest.java @@ -0,0 +1,67 @@ +// ASM: a very small and fast Java bytecode manipulation framework +// Copyright (c) 2000-2011 INRIA, France Telecom +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// 1. Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// 3. Neither the name of the copyright holders nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF +// THE POSSIBILITY OF SUCH DAMAGE. +package org.objectweb.asm; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; + +/** + * Unit tests for {@link ModuleVisitor}. + * + * @author Eric Bruneton + */ +class ModuleVisitorTest { + + @Test + void testConstructor_validApi() { + Executable constructor = () -> new ModuleVisitor(Opcodes.ASM4) {}; + + assertDoesNotThrow(constructor); + } + + @Test + void testConstructor_invalidApi() { + Executable constructor = () -> new ModuleVisitor(0) {}; + + Exception exception = assertThrows(IllegalArgumentException.class, constructor); + assertEquals("Unsupported api 0", exception.getMessage()); + } + + @Test + void testGetDelegate() { + ModuleVisitor delegate = new ModuleVisitor(Opcodes.ASM4) {}; + ModuleVisitor visitor = new ModuleVisitor(Opcodes.ASM4, delegate) {}; + + assertSame(delegate, visitor.getDelegate()); + } +} diff --git a/asm/src/test/java/org/objectweb/asm/RecordComponentVisitorTest.java b/asm/src/test/java/org/objectweb/asm/RecordComponentVisitorTest.java new file mode 100644 index 0000000000000000000000000000000000000000..0e3ef7a3bd9cc317eb5de495d2368db46071b197 --- /dev/null +++ b/asm/src/test/java/org/objectweb/asm/RecordComponentVisitorTest.java @@ -0,0 +1,67 @@ +// ASM: a very small and fast Java bytecode manipulation framework +// Copyright (c) 2000-2011 INRIA, France Telecom +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// 1. Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// 3. Neither the name of the copyright holders nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF +// THE POSSIBILITY OF SUCH DAMAGE. +package org.objectweb.asm; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; + +/** + * Unit tests for {@link RecordComponentVisitor}. + * + * @author Eric Bruneton + */ +class RecordComponentVisitorTest { + + @Test + void testConstructor_validApi() { + Executable constructor = () -> new RecordComponentVisitor(Opcodes.ASM4) {}; + + assertDoesNotThrow(constructor); + } + + @Test + void testConstructor_invalidApi() { + Executable constructor = () -> new RecordComponentVisitor(0) {}; + + Exception exception = assertThrows(IllegalArgumentException.class, constructor); + assertEquals("Unsupported api 0", exception.getMessage()); + } + + @Test + void testGetDelegate() { + RecordComponentVisitor delegate = new RecordComponentVisitor(Opcodes.ASM4) {}; + RecordComponentVisitor visitor = new RecordComponentVisitor(Opcodes.ASM4, delegate) {}; + + assertSame(delegate, visitor.getDelegate()); + } +}