• 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 package org.objectweb.asm.util;
29 
30 import org.objectweb.asm.AnnotationVisitor;
31 import org.objectweb.asm.Attribute;
32 import org.objectweb.asm.FieldVisitor;
33 import org.objectweb.asm.Opcodes;
34 import org.objectweb.asm.TypePath;
35 import org.objectweb.asm.TypeReference;
36 
37 /**
38  * A {@link FieldVisitor} that checks that its methods are properly used.
39  *
40  * @author Eric Bruneton
41  */
42 public class CheckFieldAdapter extends FieldVisitor {
43 
44   /** Whether the {@link #visitEnd} method has been called. */
45   private boolean visitEndCalled;
46 
47   /**
48    * Constructs a new {@link CheckFieldAdapter}. <i>Subclasses must not use this constructor</i>.
49    * Instead, they must use the {@link #CheckFieldAdapter(int, FieldVisitor)} version.
50    *
51    * @param fieldVisitor the field visitor to which this adapter must delegate calls.
52    * @throws IllegalStateException If a subclass calls this constructor.
53    */
CheckFieldAdapter(final FieldVisitor fieldVisitor)54   public CheckFieldAdapter(final FieldVisitor fieldVisitor) {
55     this(/* latest api = */ Opcodes.ASM9, fieldVisitor);
56     if (getClass() != CheckFieldAdapter.class) {
57       throw new IllegalStateException();
58     }
59   }
60 
61   /**
62    * Constructs a new {@link CheckFieldAdapter}.
63    *
64    * @param api the ASM API version implemented by this visitor. Must be one of the {@code
65    *     ASM}<i>x</i> values in {@link Opcodes}.
66    * @param fieldVisitor the field visitor to which this adapter must delegate calls.
67    */
CheckFieldAdapter(final int api, final FieldVisitor fieldVisitor)68   protected CheckFieldAdapter(final int api, final FieldVisitor fieldVisitor) {
69     super(api, fieldVisitor);
70   }
71 
72   @Override
visitAnnotation(final String descriptor, final boolean visible)73   public AnnotationVisitor visitAnnotation(final String descriptor, final boolean visible) {
74     checkVisitEndNotCalled();
75     // Annotations can only appear in V1_5 or more classes.
76     CheckMethodAdapter.checkDescriptor(Opcodes.V1_5, descriptor, false);
77     return new CheckAnnotationAdapter(super.visitAnnotation(descriptor, visible));
78   }
79 
80   @Override
visitTypeAnnotation( final int typeRef, final TypePath typePath, final String descriptor, final boolean visible)81   public AnnotationVisitor visitTypeAnnotation(
82       final int typeRef, final TypePath typePath, final String descriptor, final boolean visible) {
83     checkVisitEndNotCalled();
84     int sort = new TypeReference(typeRef).getSort();
85     if (sort != TypeReference.FIELD) {
86       throw new IllegalArgumentException(
87           "Invalid type reference sort 0x" + Integer.toHexString(sort));
88     }
89     CheckClassAdapter.checkTypeRef(typeRef);
90     CheckMethodAdapter.checkDescriptor(Opcodes.V1_5, descriptor, false);
91     return new CheckAnnotationAdapter(
92         super.visitTypeAnnotation(typeRef, typePath, descriptor, visible));
93   }
94 
95   @Override
visitAttribute(final Attribute attribute)96   public void visitAttribute(final Attribute attribute) {
97     checkVisitEndNotCalled();
98     if (attribute == null) {
99       throw new IllegalArgumentException("Invalid attribute (must not be null)");
100     }
101     super.visitAttribute(attribute);
102   }
103 
104   @Override
visitEnd()105   public void visitEnd() {
106     checkVisitEndNotCalled();
107     visitEndCalled = true;
108     super.visitEnd();
109   }
110 
checkVisitEndNotCalled()111   private void checkVisitEndNotCalled() {
112     if (visitEndCalled) {
113       throw new IllegalStateException("Cannot call a visit method after visitEnd has been called");
114     }
115   }
116 }
117