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 
  60 package jdk.internal.org.objectweb.asm;
  61 
  62 /**
  63  * A reference to a field or a method.
  64  *
  65  * @author Remi Forax
  66  * @author Eric Bruneton
  67  */
  68 public final class Handle {
  69 
  70     /**
  71      * The kind of field or method designated by this Handle. Should be
  72      * {@link Opcodes#H_GETFIELD}, {@link Opcodes#H_GETSTATIC},
  73      * {@link Opcodes#H_PUTFIELD}, {@link Opcodes#H_PUTSTATIC},
  74      * {@link Opcodes#H_INVOKEVIRTUAL}, {@link Opcodes#H_INVOKESTATIC},
  75      * {@link Opcodes#H_INVOKESPECIAL}, {@link Opcodes#H_NEWINVOKESPECIAL} or
  76      * {@link Opcodes#H_INVOKEINTERFACE}.
  77      */
  78     final int tag;
  79 
  80     /**
  81      * The internal name of the class that owns the field or method designated
  82      * by this handle.
  83      */
  84     final String owner;
  85 
  86     /**
  87      * The name of the field or method designated by this handle.
  88      */
  89     final String name;
  90 
  91     /**
  92      * The descriptor of the field or method designated by this handle.
  93      */
  94     final String desc;
  95 
  96     
  97     /**
  98      * Indicate if the owner is an interface or not.
  99      */
 100     final boolean itf;
 101 
 102     /**
 103      * Constructs a new field or method handle.
 104      *
 105      * @param tag
 106      *            the kind of field or method designated by this Handle. Must be
 107      *            {@link Opcodes#H_GETFIELD}, {@link Opcodes#H_GETSTATIC},
 108      *            {@link Opcodes#H_PUTFIELD}, {@link Opcodes#H_PUTSTATIC},
 109      *            {@link Opcodes#H_INVOKEVIRTUAL},
 110      *            {@link Opcodes#H_INVOKESTATIC},
 111      *            {@link Opcodes#H_INVOKESPECIAL},
 112      *            {@link Opcodes#H_NEWINVOKESPECIAL} or
 113      *            {@link Opcodes#H_INVOKEINTERFACE}.
 114      * @param owner
 115      *            the internal name of the class that owns the field or method
 116      *            designated by this handle.
 117      * @param name
 118      *            the name of the field or method designated by this handle.
 119      * @param desc
 120      *            the descriptor of the field or method designated by this
 121      *            handle.
 122      *
 123      * @deprecated this constructor has been superseded
 124      *             by {@link #Handle(int, String, String, String, boolean)}.
 125      */
 126     @Deprecated
 127     public Handle(int tag, String owner, String name, String desc) {
 128         this(tag, owner, name, desc, tag == Opcodes.H_INVOKEINTERFACE);
 129     }
 130 
 131     /**
 132      * Constructs a new field or method handle.
 133      *
 134      * @param tag
 135      *            the kind of field or method designated by this Handle. Must be
 136      *            {@link Opcodes#H_GETFIELD}, {@link Opcodes#H_GETSTATIC},
 137      *            {@link Opcodes#H_PUTFIELD}, {@link Opcodes#H_PUTSTATIC},
 138      *            {@link Opcodes#H_INVOKEVIRTUAL},
 139      *            {@link Opcodes#H_INVOKESTATIC},
 140      *            {@link Opcodes#H_INVOKESPECIAL},
 141      *            {@link Opcodes#H_NEWINVOKESPECIAL} or
 142      *            {@link Opcodes#H_INVOKEINTERFACE}.
 143      * @param owner
 144      *            the internal name of the class that owns the field or method
 145      *            designated by this handle.
 146      * @param name
 147      *            the name of the field or method designated by this handle.
 148      * @param desc
 149      *            the descriptor of the field or method designated by this
 150      *            handle.
 151      * @param itf
 152      *            true if the owner is an interface.
 153      */
 154     public Handle(int tag, String owner, String name, String desc, boolean itf) {
 155         this.tag = tag;
 156         this.owner = owner;
 157         this.name = name;
 158         this.desc = desc;
 159         this.itf = itf;
 160     }
 161 
 162     /**
 163      * Returns the kind of field or method designated by this handle.
 164      *
 165      * @return {@link Opcodes#H_GETFIELD}, {@link Opcodes#H_GETSTATIC},
 166      *         {@link Opcodes#H_PUTFIELD}, {@link Opcodes#H_PUTSTATIC},
 167      *         {@link Opcodes#H_INVOKEVIRTUAL}, {@link Opcodes#H_INVOKESTATIC},
 168      *         {@link Opcodes#H_INVOKESPECIAL},
 169      *         {@link Opcodes#H_NEWINVOKESPECIAL} or
 170      *         {@link Opcodes#H_INVOKEINTERFACE}.
 171      */
 172     public int getTag() {
 173         return tag;
 174     }
 175 
 176     /**
 177      * Returns the internal name of the class that owns the field or method
 178      * designated by this handle.
 179      *
 180      * @return the internal name of the class that owns the field or method
 181      *         designated by this handle.
 182      */
 183     public String getOwner() {
 184         return owner;
 185     }
 186 
 187     /**
 188      * Returns the name of the field or method designated by this handle.
 189      *
 190      * @return the name of the field or method designated by this handle.
 191      */
 192     public String getName() {
 193         return name;
 194     }
 195 
 196     /**
 197      * Returns the descriptor of the field or method designated by this handle.
 198      *
 199      * @return the descriptor of the field or method designated by this handle.
 200      */
 201     public String getDesc() {
 202         return desc;
 203     }
 204 
 205     /**
 206      * Returns true if the owner of the field or method designated
 207      * by this handle is an interface.
 208      *
 209      * @return true if the owner of the field or method designated
 210      *         by this handle is an interface.
 211      */
 212     public boolean isInterface() {
 213         return itf;
 214     }
 215 
 216     @Override
 217     public boolean equals(Object obj) {
 218         if (obj == this) {
 219             return true;
 220         }
 221         if (!(obj instanceof Handle)) {
 222             return false;
 223         }
 224         Handle h = (Handle) obj;
 225         return tag == h.tag && itf == h.itf && owner.equals(h.owner)
 226                 && name.equals(h.name) && desc.equals(h.desc);
 227     }
 228 
 229     @Override
 230     public int hashCode() {
 231         return tag + (itf? 64: 0) + owner.hashCode() * name.hashCode() * desc.hashCode();
 232     }
 233 
 234     /**
 235      * Returns the textual representation of this handle. The textual
 236      * representation is:
 237      *
 238      * <pre>
 239      * for a reference to a class:
 240      * owner '.' name desc ' ' '(' tag ')'
 241      * for a reference to an interface:
 242      * owner '.' name desc ' ' '(' tag ' ' itf ')'
 243      * </pre>
 244      *
 245      * . As this format is unambiguous, it can be parsed if necessary.
 246      */
 247     @Override
 248     public String toString() {
 249         return owner + '.' + name + desc + " (" + tag + (itf? " itf": "") + ')';
 250     }
 251 }