• 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.FieldVisitor;
33 import org.objectweb.asm.Opcodes;
34 import org.objectweb.asm.TypePath;
35 
36 /**
37  * A {@link FieldVisitor} that remaps types with a {@link Remapper}.
38  *
39  * @author Eugene Kuleshov
40  */
41 public class FieldRemapper extends FieldVisitor {
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 FieldRemapper}. <i>Subclasses must not use this constructor</i>.
48    * Instead, they must use the {@link #FieldRemapper(int,FieldVisitor,Remapper)} version.
49    *
50    * @param fieldVisitor the field visitor this remapper must delegate to.
51    * @param remapper the remapper to use to remap the types in the visited field.
52    */
FieldRemapper(final FieldVisitor fieldVisitor, final Remapper remapper)53   public FieldRemapper(final FieldVisitor fieldVisitor, final Remapper remapper) {
54     this(/* latest api = */ Opcodes.ASM9, fieldVisitor, remapper);
55   }
56 
57   /**
58    * Constructs a new {@link FieldRemapper}.
59    *
60    * @param api the ASM API version supported by this remapper. Must be one of the {@code
61    *     ASM}<i>x</i> values in {@link Opcodes}.
62    * @param fieldVisitor the field visitor this remapper must delegate to.
63    * @param remapper the remapper to use to remap the types in the visited field.
64    */
FieldRemapper(final int api, final FieldVisitor fieldVisitor, final Remapper remapper)65   protected FieldRemapper(final int api, final FieldVisitor fieldVisitor, final Remapper remapper) {
66     super(api, fieldVisitor);
67     this.remapper = remapper;
68   }
69 
70   @Override
visitAnnotation(final String descriptor, final boolean visible)71   public AnnotationVisitor visitAnnotation(final String descriptor, final boolean visible) {
72     AnnotationVisitor annotationVisitor =
73         super.visitAnnotation(remapper.mapDesc(descriptor), visible);
74     return annotationVisitor == null
75         ? null
76         : createAnnotationRemapper(descriptor, annotationVisitor);
77   }
78 
79   @Override
visitTypeAnnotation( final int typeRef, final TypePath typePath, final String descriptor, final boolean visible)80   public AnnotationVisitor visitTypeAnnotation(
81       final int typeRef, final TypePath typePath, final String descriptor, final boolean visible) {
82     AnnotationVisitor annotationVisitor =
83         super.visitTypeAnnotation(typeRef, typePath, remapper.mapDesc(descriptor), visible);
84     return annotationVisitor == null
85         ? null
86         : createAnnotationRemapper(descriptor, annotationVisitor);
87   }
88 
89   /**
90    * Constructs a new remapper for annotations. The default implementation of this method returns a
91    * new {@link AnnotationRemapper}.
92    *
93    * @param annotationVisitor the AnnotationVisitor the remapper must delegate to.
94    * @return the newly created remapper.
95    * @deprecated use {@link #createAnnotationRemapper(String, AnnotationVisitor)} instead.
96    */
97   @Deprecated
createAnnotationRemapper(final AnnotationVisitor annotationVisitor)98   protected AnnotationVisitor createAnnotationRemapper(final AnnotationVisitor annotationVisitor) {
99     return new AnnotationRemapper(api, /* descriptor = */ null, annotationVisitor, remapper);
100   }
101 
102   /**
103    * Constructs a new remapper for annotations. The default implementation of this method returns a
104    * new {@link AnnotationRemapper}.
105    *
106    * @param descriptor the descriptor of the visited annotation.
107    * @param annotationVisitor the AnnotationVisitor the remapper must delegate to.
108    * @return the newly created remapper.
109    */
createAnnotationRemapper( final String descriptor, final AnnotationVisitor annotationVisitor)110   protected AnnotationVisitor createAnnotationRemapper(
111       final String descriptor, final AnnotationVisitor annotationVisitor) {
112     return new AnnotationRemapper(api, descriptor, annotationVisitor, remapper)
113         .orDeprecatedValue(createAnnotationRemapper(annotationVisitor));
114   }
115 }
116