8000 Merge branch '317981-add-get-delegate-methods' into 'master' · scala/scala-asm@ae10e89 · GitHub
[go: up one dir, main page]

Skip to content

Commit ae10e89

Browse files
committed
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
2 parents 5829657 + f44c6c0 commit ae10e89

13 files changed

+226
-4
lines changed

asm/src/main/java/org/objectweb/asm/AnnotationVisitor.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ protected AnnotationVisitor(final int api, final AnnotationVisitor annotationVis
8484
this.av = annotationVisitor;
8585
}
8686

87+
/**
88+
* The annotation visitor to which this visitor must delegate method calls. May be {@literal
89+
* null}.
90+
*
91+
* @return the annotation visitor to which this visitor must delegate method calls, or {@literal
92+
* null}.
93+
*/
94+
public AnnotationVisitor getDelegate() {
95+
return av;
96+
}
97+
8798
/**
8899
* Visits a primitive value of the annotation.
89100
*

asm/src/main/java/org/objectweb/asm/ClassVisitor.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ protected ClassVisitor(final int api, final ClassVisitor classVisitor) {
8383
this.cv = classVisitor;
8484
}
8585

86+
/**
87+
* The class visitor to which this visitor must delegate method calls. May be {@literal null}.
88+
*
89+
* @return the class visitor to which this visitor must delegate method calls, or {@literal null}.
90+
*/
91+
public ClassVisitor getDelegate() {
92+
return cv;
93+
}
94+
8695
/**
8796
* Visits the header of the class.
8897
*

asm/src/main/java/org/objectweb/asm/FieldVisitor.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ protected FieldVisitor(final int api, final FieldVisitor fieldVisitor) {
8080
this.fv = fieldVisitor;
8181
}
8282

83+
/**
84+
* The field visitor to which this visitor must delegate method calls. May be {@literal null}.
85+
*
86+
* @return the field visitor to which this visitor must delegate method calls, or {@literal null}.
87+
*/
88+
public FieldVisitor getDelegate() {
89+
return fv;
90+
}
91+
8392
/**
8493
* Visits an annotation of the field.
8594
*

asm/src/main/java/org/objectweb/asm/MethodVisitor.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@ protected MethodVisitor(final int api, final MethodVisitor methodVisitor) {
9696
this.mv = methodVisitor;
9797
}
9898

99+
/**
100+
* The method visitor to which this visitor must delegate method calls. May be {@literal null}.
101+
*
102+
* @return the method visitor to which this visitor must delegate method calls, or {@literal
103+
* null}.
104+
*/
105+
public MethodVisitor getDelegate() {
106+
return mv;
107+
}
108+
99109
// -----------------------------------------------------------------------------------------------
100110
// Parameters, annotations and non standard attributes
101111
// -----------------------------------------------------------------------------------------------

asm/src/main/java/org/objectweb/asm/ModuleVisitor.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ protected ModuleVisitor(final int api, final ModuleVisitor moduleVisitor) {
8282
this.mv = moduleVisitor;
8383
}
8484

85+
/**
86+
* The module visitor to which this visitor must delegate method calls. May be {@literal null}.
87+
*
88+
* @return the module visitor to which this visitor must delegate method calls, or {@literal
89+
* null}.
90+
*/
91+
public ModuleVisitor getDelegate() {
92+
return mv;
93+
}
94+
8595
/**
8696
* Visit the main class of the current module.
8797
*

asm/src/main/java/org/objectweb/asm/RecordComponentVisitor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public abstract class RecordComponentVisitor {
4545
/**
4646
* The record visitor to which this visitor must delegate method calls. May be {@literal null}.
4747
*/
48-
/*package-private*/ RecordComponentVisitor delegate;
48+
protected RecordComponentVisitor delegate;
4949

5050
/**
5151
* Constructs a new {@link RecordComponentVisitor}.
@@ -85,7 +85,8 @@ protected RecordComponentVisitor(
8585
/**
8686
* The record visitor to which this visitor must delegate method calls. May be {@literal null}.
8787
*
88-
* @return the record visitor to which this visitor must delegate method calls or {@literal null}.
88+
* @return the record visitor to which this visitor must delegate method calls, or {@literal
89+
* null}.
8990
*/
9091
public RecordComponentVisitor getDelegate() {
9192
return delegate;

asm/src/test/java/org/objectweb/asm/AnnotationVisitorTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
3131
import static org.junit.jupiter.api.Assertions.assertEquals;
32+
import static org.junit.jupiter.api.Assertions.assertSame;
3233
import static org.junit.jupiter.api.Assertions.assertThrows;
3334
import static org.junit.jupiter.api.Assertions.assertTrue;
3435

@@ -61,6 +62,14 @@ void testConstructor_invalidApi() {
6162
assertEquals("Unsupported api 0", exception.getMessage());
6263
}
6364

65+
@Test
66+
void testGetDelegate() {
67+
AnnotationVisitor delegate = new AnnotationVisitor(Opcodes.ASM4) {};
68+
AnnotationVisitor visitor = new AnnotationVisitor(Opcodes.ASM4, delegate) {};
69+
70+
assertSame(delegate, visitor.getDelegate());
71+
}
72+
6473
/**
6574
* Tests that ClassReader accepts visitor which return null AnnotationVisitor, and that returning
6675
* null AnnotationVisitor is equivalent to returning an EmptyAnnotationVisitor.

asm/src/test/java/org/objectweb/asm/ClassVisitorTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
3131
import static org.junit.jupiter.api.Assertions.assertEquals;
3232
import static org.junit.jupiter.api.Assertions.assertFalse;
33+
import static org.junit.jupiter.api.Assertions.assertSame;
3334
import static org.junit.jupiter.api.Assertions.assertThrows;
3435
import static org.junit.jupiter.api.Assertions.assertTrue;
3536

@@ -68,6 +69,14 @@ void testConstructor_invalidApi() {
6869
assertEquals("Unsupported api 0", exception.getMessage());
6970
}
7071

72+
@Test
73+
void testGetDelegate() {
74+
ClassVisitor delegate = new ClassVisitor(Opcodes.ASM4) {};
75+
ClassVisitor visitor = new ClassVisitor(Opcodes.ASM4, delegate) {};
76+
77+
assertSame(delegate, visitor.getDelegate());
78+
}
79+
7180
/**
7281
* Tests that classes are unchanged when transformed with a ClassReader -> class adapter ->
7382
* ClassWriter chain, where "class adapter" is a ClassVisitor which returns FieldVisitor,

asm/src/test/java/org/objectweb/asm/ClassWriterTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,10 @@ void testVisitMethods_final() {
141141
Method classWriterMethod =
142142
ClassWriter.class.getMethod(
143143
classVisitorMethod.getName(), classVisitorMethod.getParameterTypes());
144-
assertTrue(
145-
Modifier.isFinal(classWriterMethod.getModifiers()), classWriterMethod + " is final");
144+
if (!classWriterMethod.getName().equals("getDelegate")) {
145+
assertTrue(
146+
Modifier.isFinal(classWriterMethod.getModifiers()), classWriterMethod + " is final");
147+
}
146148
} catch (NoSuchMethodException e) {
147149
fail("ClassWriter must override " + classVisitorMethod);
148150
}

asm/src/test/java/org/objectweb/asm/FieldVisitorTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
3131
import static org.junit.jupiter.api.Assertions.assertEquals;
32+
import static org.junit.jupiter.api.Assertions.assertSame;
3233
import static org.junit.jupiter.api.Assertions.assertThrows;
3334

3435
import org.junit.jupiter.api.Test;
@@ -55,4 +56,12 @@ void testConstructor_invalidApi() {
5556
Exception exception = assertThrows(IllegalArgumentException.class, constructor);
5657
assertEquals("Unsupported api 0", exception.getMessage());
5758
}
59+
60+
@Test
61+
void testGetDelegate() {
62+
FieldVisitor delegate = new FieldVisitor(Opcodes.ASM4) {};
63+
FieldVisitor visitor = new FieldVisitor(Opcodes.ASM4, delegate) {};
64+
65+
assertSame(delegate, visitor.getDelegate());
66+
}
5867
}

0 commit comments

Comments
 (0)
0