1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. Oracle designates this 7 * particular file as subject to the "Classpath" exception as provided 8 * by Oracle in the LICENSE file that accompanied this code. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 */ 24 25 /* 26 * This file is available under and governed by the GNU General Public 27 * License version 2 only, as published by the Free Software Foundation. 28 * However, the following notice accompanied the original version of this 29 * file: 30 * 31 * ASM: a very small and fast Java bytecode manipulation framework 32 * Copyright (c) 2000-2011 INRIA, France Telecom 33 * All rights reserved. 34 * 35 * Redistribution and use in source and binary forms, with or without 36 * modification, are permitted provided that the following conditions 37 * are met: 38 * 1. Redistributions of source code must retain the above copyright 39 * notice, this list of conditions and the following disclaimer. 40 * 2. Redistributions in binary form must reproduce the above copyright 41 * notice, this list of conditions and the following disclaimer in the 42 * documentation and/or other materials provided with the distribution. 43 * 3. Neither the name of the copyright holders nor the names of its 44 * contributors may be used to endorse or promote products derived from 45 * this software without specific prior written permission. 46 * 47 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 48 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 50 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 51 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 52 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 53 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 54 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 55 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 56 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 57 * THE POSSIBILITY OF SUCH DAMAGE. 58 */ 59 package jdk.internal.org.objectweb.asm.util; 60 61 import jdk.internal.org.objectweb.asm.Opcodes; 62 import jdk.internal.org.objectweb.asm.signature.SignatureVisitor; 63 64 /** 65 * A {@link SignatureVisitor} that checks that its methods are properly used. 66 * 67 * @author Eric Bruneton 68 */ 69 public class CheckSignatureAdapter extends SignatureVisitor { 70 71 /** 72 * Type to be used to check class signatures. See 73 * {@link #CheckSignatureAdapter(int, SignatureVisitor) 74 * CheckSignatureAdapter}. 75 */ 76 public static final int CLASS_SIGNATURE = 0; 77 78 /** 79 * Type to be used to check method signatures. See 80 * {@link #CheckSignatureAdapter(int, SignatureVisitor) 81 * CheckSignatureAdapter}. 82 */ 83 public static final int METHOD_SIGNATURE = 1; 84 85 /** 86 * Type to be used to check type signatures.See 87 * {@link #CheckSignatureAdapter(int, SignatureVisitor) 88 * CheckSignatureAdapter}. 89 */ 90 public static final int TYPE_SIGNATURE = 2; 91 92 private static final int EMPTY = 1; 93 94 private static final int FORMAL = 2; 95 96 private static final int BOUND = 4; 97 98 private static final int SUPER = 8; 99 100 private static final int PARAM = 16; 101 102 private static final int RETURN = 32; 103 104 private static final int SIMPLE_TYPE = 64; 105 106 private static final int CLASS_TYPE = 128; 107 108 private static final int END = 256; 109 110 /** 111 * Type of the signature to be checked. 112 */ 113 private final int type; 114 115 /** 116 * State of the automaton used to check the order of method calls. 117 */ 118 private int state; 119 120 /** 121 * <tt>true</tt> if the checked type signature can be 'V'. 122 */ 123 private boolean canBeVoid; 124 125 /** 126 * The visitor to which this adapter must delegate calls. May be 127 * <tt>null</tt>. 128 */ 129 private final SignatureVisitor sv; 130 131 /** 132 * Creates a new {@link CheckSignatureAdapter} object. <i>Subclasses must 133 * not use this constructor</i>. Instead, they must use the 134 * {@link #CheckSignatureAdapter(int, int, SignatureVisitor)} version. 135 * 136 * @param type 137 * the type of signature to be checked. See 138 * {@link #CLASS_SIGNATURE}, {@link #METHOD_SIGNATURE} and 139 * {@link #TYPE_SIGNATURE}. 140 * @param sv 141 * the visitor to which this adapter must delegate calls. May be 142 * <tt>null</tt>. 143 */ 144 public CheckSignatureAdapter(final int type, final SignatureVisitor sv) { 145 this(Opcodes.ASM5, type, sv); 146 } 147 148 /** 149 * Creates a new {@link CheckSignatureAdapter} object. 150 * 151 * @param api 152 * the ASM API version implemented by this visitor. Must be one 153 * of {@link Opcodes#ASM4} or {@link Opcodes#ASM5}. 154 * @param type 155 * the type of signature to be checked. See 156 * {@link #CLASS_SIGNATURE}, {@link #METHOD_SIGNATURE} and 157 * {@link #TYPE_SIGNATURE}. 158 * @param sv 159 * the visitor to which this adapter must delegate calls. May be 160 * <tt>null</tt>. 161 */ 162 protected CheckSignatureAdapter(final int api, final int type, 163 final SignatureVisitor sv) { 164 super(api); 165 this.type = type; 166 this.state = EMPTY; 167 this.sv = sv; 168 } 169 170 // class and method signatures 171 172 @Override 173 public void visitFormalTypeParameter(final String name) { 174 if (type == TYPE_SIGNATURE 175 || (state != EMPTY && state != FORMAL && state != BOUND)) { 176 throw new IllegalStateException(); 177 } 178 CheckMethodAdapter.checkIdentifier(name, "formal type parameter"); 179 state = FORMAL; 180 if (sv != null) { 181 sv.visitFormalTypeParameter(name); 182 } 183 } 184 185 @Override 186 public SignatureVisitor visitClassBound() { 187 if (state != FORMAL) { 188 throw new IllegalStateException(); 189 } 190 state = BOUND; 191 SignatureVisitor v = sv == null ? null : sv.visitClassBound(); 192 return new CheckSignatureAdapter(TYPE_SIGNATURE, v); 193 } 194 195 @Override 196 public SignatureVisitor visitInterfaceBound() { 197 if (state != FORMAL && state != BOUND) { 198 throw new IllegalArgumentException(); 199 } 200 SignatureVisitor v = sv == null ? null : sv.visitInterfaceBound(); 201 return new CheckSignatureAdapter(TYPE_SIGNATURE, v); 202 } 203 204 // class signatures 205 206 @Override 207 public SignatureVisitor visitSuperclass() { 208 if (type != CLASS_SIGNATURE || (state & (EMPTY | FORMAL | BOUND)) == 0) { 209 throw new IllegalArgumentException(); 210 } 211 state = SUPER; 212 SignatureVisitor v = sv == null ? null : sv.visitSuperclass(); 213 return new CheckSignatureAdapter(TYPE_SIGNATURE, v); 214 } 215 216 @Override 217 public SignatureVisitor visitInterface() { 218 if (state != SUPER) { 219 throw new IllegalStateException(); 220 } 221 SignatureVisitor v = sv == null ? null : sv.visitInterface(); 222 return new CheckSignatureAdapter(TYPE_SIGNATURE, v); 223 } 224 225 // method signatures 226 227 @Override 228 public SignatureVisitor visitParameterType() { 229 if (type != METHOD_SIGNATURE 230 || (state & (EMPTY | FORMAL | BOUND | PARAM)) == 0) { 231 throw new IllegalArgumentException(); 232 } 233 state = PARAM; 234 SignatureVisitor v = sv == null ? null : sv.visitParameterType(); 235 return new CheckSignatureAdapter(TYPE_SIGNATURE, v); 236 } 237 238 @Override 239 public SignatureVisitor visitReturnType() { 240 if (type != METHOD_SIGNATURE 241 || (state & (EMPTY | FORMAL | BOUND | PARAM)) == 0) { 242 throw new IllegalArgumentException(); 243 } 244 state = RETURN; 245 SignatureVisitor v = sv == null ? null : sv.visitReturnType(); 246 CheckSignatureAdapter cv = new CheckSignatureAdapter(TYPE_SIGNATURE, v); 247 cv.canBeVoid = true; 248 return cv; 249 } 250 251 @Override 252 public SignatureVisitor visitExceptionType() { 253 if (state != RETURN) { 254 throw new IllegalStateException(); 255 } 256 SignatureVisitor v = sv == null ? null : sv.visitExceptionType(); 257 return new CheckSignatureAdapter(TYPE_SIGNATURE, v); 258 } 259 260 // type signatures 261 262 @Override 263 public void visitBaseType(final char descriptor) { 264 if (type != TYPE_SIGNATURE || state != EMPTY) { 265 throw new IllegalStateException(); 266 } 267 if (descriptor == 'V') { 268 if (!canBeVoid) { 269 throw new IllegalArgumentException(); 270 } 271 } else { 272 if ("ZCBSIFJD".indexOf(descriptor) == -1) { 273 throw new IllegalArgumentException(); 274 } 275 } 276 state = SIMPLE_TYPE; 277 if (sv != null) { 278 sv.visitBaseType(descriptor); 279 } 280 } 281 282 @Override 283 public void visitTypeVariable(final String name) { 284 if (type != TYPE_SIGNATURE || state != EMPTY) { 285 throw new IllegalStateException(); 286 } 287 CheckMethodAdapter.checkIdentifier(name, "type variable"); 288 state = SIMPLE_TYPE; 289 if (sv != null) { 290 sv.visitTypeVariable(name); 291 } 292 } 293 294 @Override 295 public SignatureVisitor visitArrayType() { 296 if (type != TYPE_SIGNATURE || state != EMPTY) { 297 throw new IllegalStateException(); 298 } 299 state = SIMPLE_TYPE; 300 SignatureVisitor v = sv == null ? null : sv.visitArrayType(); 301 return new CheckSignatureAdapter(TYPE_SIGNATURE, v); 302 } 303 304 @Override 305 public void visitClassType(final String name) { 306 if (type != TYPE_SIGNATURE || state != EMPTY) { 307 throw new IllegalStateException(); 308 } 309 CheckMethodAdapter.checkInternalName(name, "class name"); 310 state = CLASS_TYPE; 311 if (sv != null) { 312 sv.visitClassType(name); 313 } 314 } 315 316 @Override 317 public void visitInnerClassType(final String name) { 318 if (state != CLASS_TYPE) { 319 throw new IllegalStateException(); 320 } 321 CheckMethodAdapter.checkIdentifier(name, "inner class name"); 322 if (sv != null) { 323 sv.visitInnerClassType(name); 324 } 325 } 326 327 @Override 328 public void visitTypeArgument() { 329 if (state != CLASS_TYPE) { 330 throw new IllegalStateException(); 331 } 332 if (sv != null) { 333 sv.visitTypeArgument(); 334 } 335 } 336 337 @Override 338 public SignatureVisitor visitTypeArgument(final char wildcard) { 339 if (state != CLASS_TYPE) { 340 throw new IllegalStateException(); 341 } 342 if ("+-=".indexOf(wildcard) == -1) { 343 throw new IllegalArgumentException(); 344 } 345 SignatureVisitor v = sv == null ? null : sv.visitTypeArgument(wildcard); 346 return new CheckSignatureAdapter(TYPE_SIGNATURE, v); 347 } 348 349 @Override 350 public void visitEnd() { 351 if (state != CLASS_TYPE) { 352 throw new IllegalStateException(); 353 } 354 state = END; 355 if (sv != null) { 356 sv.visitEnd(); 357 } 358 } 359 }