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) CheckSignatureAdapter}.
  74      */
  75     public static final int CLASS_SIGNATURE = 0;
  76 
  77     /**
  78      * Type to be used to check method signatures. See
  79      * {@link #CheckSignatureAdapter(int, SignatureVisitor) CheckSignatureAdapter}.
  80      */
  81     public static final int METHOD_SIGNATURE = 1;
  82 
  83     /**
  84      * Type to be used to check type signatures.See
  85      * {@link #CheckSignatureAdapter(int, SignatureVisitor) CheckSignatureAdapter}.
  86      */
  87     public static final int TYPE_SIGNATURE = 2;
  88 
  89     private static final int EMPTY = 1;
  90 
  91     private static final int FORMAL = 2;
  92 
  93     private static final int BOUND = 4;
  94 
  95     private static final int SUPER = 8;
  96 
  97     private static final int PARAM = 16;
  98 
  99     private static final int RETURN = 32;
 100 
 101     private static final int SIMPLE_TYPE = 64;
 102 
 103     private static final int CLASS_TYPE = 128;
 104 
 105     private static final int END = 256;
 106 
 107     /**
 108      * Type of the signature to be checked.
 109      */
 110     private final int type;
 111 
 112     /**
 113      * State of the automaton used to check the order of method calls.
 114      */
 115     private int state;
 116 
 117     /**
 118      * <tt>true</tt> if the checked type signature can be 'V'.
 119      */
 120     private boolean canBeVoid;
 121 
 122     /**
 123      * The visitor to which this adapter must delegate calls. May be
 124      * <tt>null</tt>.
 125      */
 126     private final SignatureVisitor sv;
 127 
 128     /**
 129      * Creates a new {@link CheckSignatureAdapter} object. <i>Subclasses must
 130      * not use this constructor</i>. Instead, they must use the
 131      * {@link #CheckSignatureAdapter(int, int, SignatureVisitor)} version.
 132      *
 133      * @param type the type of signature to be checked. See
 134      *        {@link #CLASS_SIGNATURE}, {@link #METHOD_SIGNATURE} and
 135      *        {@link #TYPE_SIGNATURE}.
 136      * @param sv the visitor to which this adapter must delegate calls. May be
 137      *        <tt>null</tt>.
 138      */
 139     public CheckSignatureAdapter(final int type, final SignatureVisitor sv) {
 140         this(Opcodes.ASM4, type, sv);
 141     }
 142 
 143     /**
 144      * Creates a new {@link CheckSignatureAdapter} object.
 145      *
 146      * @param api the ASM API version implemented by this visitor. Must be one
 147      *        of {@link Opcodes#ASM4}.
 148      * @param type the type of signature to be checked. See
 149      *        {@link #CLASS_SIGNATURE}, {@link #METHOD_SIGNATURE} and
 150      *        {@link #TYPE_SIGNATURE}.
 151      * @param sv the visitor to which this adapter must delegate calls. May be
 152      *        <tt>null</tt>.
 153      */
 154     protected CheckSignatureAdapter(
 155         final int api,
 156         final int type,
 157         final SignatureVisitor sv)
 158     {
 159         super(api);
 160         this.type = type;
 161         this.state = EMPTY;
 162         this.sv = sv;
 163     }
 164 
 165     // class and method signatures
 166 
 167     @Override
 168     public void visitFormalTypeParameter(final String name) {
 169         if (type == TYPE_SIGNATURE
 170                 || (state != EMPTY && state != FORMAL && state != BOUND))
 171         {
 172             throw new IllegalStateException();
 173         }
 174         CheckMethodAdapter.checkIdentifier(name, "formal type parameter");
 175         state = FORMAL;
 176         if (sv != null) {
 177             sv.visitFormalTypeParameter(name);
 178         }
 179     }
 180 
 181     @Override
 182     public SignatureVisitor visitClassBound() {
 183         if (state != FORMAL) {
 184             throw new IllegalStateException();
 185         }
 186         state = BOUND;
 187         SignatureVisitor v = sv == null ? null : sv.visitClassBound();
 188         return new CheckSignatureAdapter(TYPE_SIGNATURE, v);
 189     }
 190 
 191     @Override
 192     public SignatureVisitor visitInterfaceBound() {
 193         if (state != FORMAL && state != BOUND) {
 194             throw new IllegalArgumentException();
 195         }
 196         SignatureVisitor v = sv == null ? null : sv.visitInterfaceBound();
 197         return new CheckSignatureAdapter(TYPE_SIGNATURE, v);
 198     }
 199 
 200     // class signatures
 201 
 202     @Override
 203     public SignatureVisitor visitSuperclass() {
 204         if (type != CLASS_SIGNATURE || (state & (EMPTY | FORMAL | BOUND)) == 0)
 205         {
 206             throw new IllegalArgumentException();
 207         }
 208         state = SUPER;
 209         SignatureVisitor v = sv == null ? null : sv.visitSuperclass();
 210         return new CheckSignatureAdapter(TYPE_SIGNATURE, v);
 211     }
 212 
 213     @Override
 214     public SignatureVisitor visitInterface() {
 215         if (state != SUPER) {
 216             throw new IllegalStateException();
 217         }
 218         SignatureVisitor v = sv == null ? null : sv.visitInterface();
 219         return new CheckSignatureAdapter(TYPE_SIGNATURE, v);
 220     }
 221 
 222     // method signatures
 223 
 224     @Override
 225     public SignatureVisitor visitParameterType() {
 226         if (type != METHOD_SIGNATURE
 227                 || (state & (EMPTY | FORMAL | BOUND | PARAM)) == 0)
 228         {
 229             throw new IllegalArgumentException();
 230         }
 231         state = PARAM;
 232         SignatureVisitor v = sv == null ? null : sv.visitParameterType();
 233         return new CheckSignatureAdapter(TYPE_SIGNATURE, v);
 234     }
 235 
 236     @Override
 237     public SignatureVisitor visitReturnType() {
 238         if (type != METHOD_SIGNATURE
 239                 || (state & (EMPTY | FORMAL | BOUND | PARAM)) == 0)
 240         {
 241             throw new IllegalArgumentException();
 242         }
 243         state = RETURN;
 244         SignatureVisitor v = sv == null ? null : sv.visitReturnType();
 245         CheckSignatureAdapter cv = new CheckSignatureAdapter(TYPE_SIGNATURE, v);
 246         cv.canBeVoid = true;
 247         return cv;
 248     }
 249 
 250     @Override
 251     public SignatureVisitor visitExceptionType() {
 252         if (state != RETURN) {
 253             throw new IllegalStateException();
 254         }
 255         SignatureVisitor v = sv == null ? null : sv.visitExceptionType();
 256         return new CheckSignatureAdapter(TYPE_SIGNATURE, v);
 257     }
 258 
 259     // type signatures
 260 
 261     @Override
 262     public void visitBaseType(final char descriptor) {
 263         if (type != TYPE_SIGNATURE || state != EMPTY) {
 264             throw new IllegalStateException();
 265         }
 266         if (descriptor == 'V') {
 267             if (!canBeVoid) {
 268                 throw new IllegalArgumentException();
 269             }
 270         } else {
 271             if ("ZCBSIFJD".indexOf(descriptor) == -1) {
 272                 throw new IllegalArgumentException();
 273             }
 274         }
 275         state = SIMPLE_TYPE;
 276         if (sv != null) {
 277             sv.visitBaseType(descriptor);
 278         }
 279     }
 280 
 281     @Override
 282     public void visitTypeVariable(final String name) {
 283         if (type != TYPE_SIGNATURE || state != EMPTY) {
 284             throw new IllegalStateException();
 285         }
 286         CheckMethodAdapter.checkIdentifier(name, "type variable");
 287         state = SIMPLE_TYPE;
 288         if (sv != null) {
 289             sv.visitTypeVariable(name);
 290         }
 291     }
 292 
 293     @Override
 294     public SignatureVisitor visitArrayType() {
 295         if (type != TYPE_SIGNATURE || state != EMPTY) {
 296             throw new IllegalStateException();
 297         }
 298         state = SIMPLE_TYPE;
 299         SignatureVisitor v = sv == null ? null : sv.visitArrayType();
 300         return new CheckSignatureAdapter(TYPE_SIGNATURE, v);
 301     }
 302 
 303     @Override
 304     public void visitClassType(final String name) {
 305         if (type != TYPE_SIGNATURE || state != EMPTY) {
 306             throw new IllegalStateException();
 307         }
 308         CheckMethodAdapter.checkInternalName(name, "class name");
 309         state = CLASS_TYPE;
 310         if (sv != null) {
 311             sv.visitClassType(name);
 312         }
 313     }
 314 
 315     @Override
 316     public void visitInnerClassType(final String name) {
 317         if (state != CLASS_TYPE) {
 318             throw new IllegalStateException();
 319         }
 320         CheckMethodAdapter.checkIdentifier(name, "inner class name");
 321         if (sv != null) {
 322             sv.visitInnerClassType(name);
 323         }
 324     }
 325 
 326     @Override
 327     public void visitTypeArgument() {
 328         if (state != CLASS_TYPE) {
 329             throw new IllegalStateException();
 330         }
 331         if (sv != null) {
 332             sv.visitTypeArgument();
 333         }
 334     }
 335 
 336     @Override
 337     public SignatureVisitor visitTypeArgument(final char wildcard) {
 338         if (state != CLASS_TYPE) {
 339             throw new IllegalStateException();
 340         }
 341         if ("+-=".indexOf(wildcard) == -1) {
 342             throw new IllegalArgumentException();
 343         }
 344         SignatureVisitor v = sv == null ? null : sv.visitTypeArgument(wildcard);
 345         return new CheckSignatureAdapter(TYPE_SIGNATURE, v);
 346     }
 347 
 348     @Override
 349     public void visitEnd() {
 350         if (state != CLASS_TYPE) {
 351             throw new IllegalStateException();
 352         }
 353         state = END;
 354         if (sv != null) {
 355             sv.visitEnd();
 356         }
 357     }
 358 }