diff options
author | Eric Bruneton <ebruneton@free.fr> | 2022-10-01 15:53:54 +0000 |
---|---|---|
committer | Eric Bruneton <ebruneton@free.fr> | 2022-10-01 15:53:54 +0000 |
commit | ae10e89b2185675178612551813299100d5ec5c1 (patch) | |
tree | 2e767efdee55527bc65a3d2396d684a4b64c4a06 | |
parent | 5829657c560945404e395dcdd597fd8b87715aa6 (diff) | |
parent | f44c6c0cd407201829653bbc930ebec374b4c507 (diff) | |
download | ow2-asm-ae10e89b2185675178612551813299100d5ec5c1.tar.gz |
Merge branch '317981-add-get-delegate-methods' into 'master'
Add a getDelegate() method to all visitors. Also make the delegate field protected in all cases.
Closes #317981
See merge request asm/asm!359
13 files changed, 226 insertions, 4 deletions
diff --git a/asm/src/main/java/org/objectweb/asm/AnnotationVisitor.java b/asm/src/main/java/org/objectweb/asm/AnnotationVisitor.java index 0c696844..ad5e9634 100644 --- a/asm/src/main/java/org/objectweb/asm/AnnotationVisitor.java +++ b/asm/src/main/java/org/objectweb/asm/AnnotationVisitor.java @@ -85,6 +85,17 @@ public abstract class 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. * * @param name the value name. diff --git a/asm/src/main/java/org/objectweb/asm/ClassVisitor.java b/asm/src/main/java/org/objectweb/asm/ClassVisitor.java index 95fd38e3..b4cba80b 100644 --- a/asm/src/main/java/org/objectweb/asm/ClassVisitor.java +++ b/asm/src/main/java/org/objectweb/asm/ClassVisitor.java @@ -84,6 +84,15 @@ public abstract class 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. * * @param version the class version. The minor version is stored in the 16 most significant bits, diff --git a/asm/src/main/java/org/objectweb/asm/FieldVisitor.java b/asm/src/main/java/org/objectweb/asm/FieldVisitor.java index fc271156..2893d9f5 100644 --- a/asm/src/main/java/org/objectweb/asm/FieldVisitor.java +++ b/asm/src/main/java/org/objectweb/asm/FieldVisitor.java @@ -81,6 +81,15 @@ public abstract class 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. * * @param descriptor the class descriptor of the annotation class. diff --git a/asm/src/main/java/org/objectweb/asm/MethodVisitor.java b/asm/src/main/java/org/objectweb/asm/MethodVisitor.java index a5365a21..f4b9d95e 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 70496fb1..9a566f3d 100644 --- a/asm/src/main/java/org/objectweb/asm/ModuleVisitor.java +++ b/asm/src/main/java/org/objectweb/asm/ModuleVisitor.java @@ -83,6 +83,16 @@ public abstract class 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. * * @param mainClass the internal name of the main class of the current module (see {@link diff --git a/asm/src/main/java/org/objectweb/asm/RecordComponentVisitor.java b/asm/src/main/java/org/objectweb/asm/RecordComponentVisitor.java index 3f040819..32784e2a 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 fc4d7634..6b06d081 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 fb6e7115..5c04868f 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 0871e90c..fb9034ed 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 0473a0cb..b7f37cd7 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 2a205122..915b1b4d 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; @@ -60,6 +61,14 @@ class MethodVisitorTest extends AsmTest { } @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 00000000..c9eb3382 --- /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 00000000..0e3ef7a3 --- /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()); + } +} |