• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // ASM: a very small and fast Java bytecode manipulation framework
2 // Copyright (c) 2000-2011 INRIA, France Telecom
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions
7 // are met:
8 // 1. Redistributions of source code must retain the above copyright
9 //    notice, this list of conditions and the following disclaimer.
10 // 2. Redistributions in binary form must reproduce the above copyright
11 //    notice, this list of conditions and the following disclaimer in the
12 //    documentation and/or other materials provided with the distribution.
13 // 3. Neither the name of the copyright holders nor the names of its
14 //    contributors may be used to endorse or promote products derived from
15 //    this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 // THE POSSIBILITY OF SUCH DAMAGE.
28 
29 package org.objectweb.asm.commons;
30 
31 import org.objectweb.asm.AnnotationVisitor;
32 import org.objectweb.asm.Opcodes;
33 import org.objectweb.asm.RecordComponentVisitor;
34 import org.objectweb.asm.TypePath;
35 
36 /**
37  * A {@link RecordComponentVisitor} that remaps types with a {@link Remapper}.
38  *
39  * @author Remi Forax
40  */
41 public class RecordComponentRemapper extends RecordComponentVisitor {
42 
43   /** The remapper used to remap the types in the visited field. */
44   protected final Remapper remapper;
45 
46   /**
47    * Constructs a new {@link RecordComponentRemapper}. <i>Subclasses must not use this
48    * constructor</i>. Instead, they must use the {@link
49    * #RecordComponentRemapper(int,RecordComponentVisitor,Remapper)} version.
50    *
51    * @param recordComponentVisitor the record component visitor this remapper must delegate to.
52    * @param remapper the remapper to use to remap the types in the visited record component.
53    */
RecordComponentRemapper( final RecordComponentVisitor recordComponentVisitor, final Remapper remapper)54   public RecordComponentRemapper(
55       final RecordComponentVisitor recordComponentVisitor, final Remapper remapper) {
56     this(/* latest api = */ Opcodes.ASM9, recordComponentVisitor, remapper);
57   }
58 
59   /**
60    * Constructs a new {@link RecordComponentRemapper}.
61    *
62    * @param api the ASM API version supported by this remapper. Must be one of {@link
63    *     org.objectweb.asm.Opcodes#ASM8} or {@link org.objectweb.asm.Opcodes#ASM9}.
64    * @param recordComponentVisitor the record component visitor this remapper must delegate to.
65    * @param remapper the remapper to use to remap the types in the visited record component.
66    */
RecordComponentRemapper( final int api, final RecordComponentVisitor recordComponentVisitor, final Remapper remapper)67   protected RecordComponentRemapper(
68       final int api, final RecordComponentVisitor recordComponentVisitor, final Remapper remapper) {
69     super(api, recordComponentVisitor);
70     this.remapper = remapper;
71   }
72 
73   @Override
visitAnnotation(final String descriptor, final boolean visible)74   public AnnotationVisitor visitAnnotation(final String descriptor, final boolean visible) {
75     AnnotationVisitor annotationVisitor =
76         super.visitAnnotation(remapper.mapDesc(descriptor), visible);
77     return annotationVisitor == null
78         ? null
79         : createAnnotationRemapper(descriptor, annotationVisitor);
80   }
81 
82   @Override
visitTypeAnnotation( final int typeRef, final TypePath typePath, final String descriptor, final boolean visible)83   public AnnotationVisitor visitTypeAnnotation(
84       final int typeRef, final TypePath typePath, final String descriptor, final boolean visible) {
85     AnnotationVisitor annotationVisitor =
86         super.visitTypeAnnotation(typeRef, typePath, remapper.mapDesc(descriptor), visible);
87     return annotationVisitor == null
88         ? null
89         : createAnnotationRemapper(descriptor, annotationVisitor);
90   }
91 
92   /**
93    * Constructs a new remapper for annotations. The default implementation of this method returns a
94    * new {@link AnnotationRemapper}.
95    *
96    * @param annotationVisitor the AnnotationVisitor the remapper must delegate to.
97    * @return the newly created remapper.
98    * @deprecated use {@link #createAnnotationRemapper(String, AnnotationVisitor)} instead.
99    */
100   @Deprecated
createAnnotationRemapper(final AnnotationVisitor annotationVisitor)101   protected AnnotationVisitor createAnnotationRemapper(final AnnotationVisitor annotationVisitor) {
102     return new AnnotationRemapper(api, /* descriptor = */ null, annotationVisitor, remapper);
103   }
104 
105   /**
106    * Constructs a new remapper for annotations. The default implementation of this method returns a
107    * new {@link AnnotationRemapper}.
108    *
109    * @param descriptor the descriptor sof the visited annotation.
110    * @param annotationVisitor the AnnotationVisitor the remapper must delegate to.
111    * @return the newly created remapper.
112    */
createAnnotationRemapper( final String descriptor, final AnnotationVisitor annotationVisitor)113   protected AnnotationVisitor createAnnotationRemapper(
114       final String descriptor, final AnnotationVisitor annotationVisitor) {
115     return new AnnotationRemapper(api, descriptor, annotationVisitor, remapper)
116         .orDeprecatedValue(createAnnotationRemapper(annotationVisitor));
117   }
118 }
119