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