• 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 
29 package org.objectweb.asm;
30 
31 /**
32  * A reference to a field or a method.
33  *
34  * @author Remi Forax
35  * @author Eric Bruneton
36  */
37 public final class Handle {
38 
39   /**
40    * The kind of field or method designated by this Handle. Should be {@link Opcodes#H_GETFIELD},
41    * {@link Opcodes#H_GETSTATIC}, {@link Opcodes#H_PUTFIELD}, {@link Opcodes#H_PUTSTATIC}, {@link
42    * Opcodes#H_INVOKEVIRTUAL}, {@link Opcodes#H_INVOKESTATIC}, {@link Opcodes#H_INVOKESPECIAL},
43    * {@link Opcodes#H_NEWINVOKESPECIAL} or {@link Opcodes#H_INVOKEINTERFACE}.
44    */
45   private final int tag;
46 
47   /** The internal name of the class that owns the field or method designated by this handle. */
48   private final String owner;
49 
50   /** The name of the field or method designated by this handle. */
51   private final String name;
52 
53   /** The descriptor of the field or method designated by this handle. */
54   private final String descriptor;
55 
56   /** Whether the owner is an interface or not. */
57   private final boolean isInterface;
58 
59   /**
60    * Constructs a new field or method handle.
61    *
62    * @param tag the kind of field or method designated by this Handle. Must be {@link
63    *     Opcodes#H_GETFIELD}, {@link Opcodes#H_GETSTATIC}, {@link Opcodes#H_PUTFIELD}, {@link
64    *     Opcodes#H_PUTSTATIC}, {@link Opcodes#H_INVOKEVIRTUAL}, {@link Opcodes#H_INVOKESTATIC},
65    *     {@link Opcodes#H_INVOKESPECIAL}, {@link Opcodes#H_NEWINVOKESPECIAL} or {@link
66    *     Opcodes#H_INVOKEINTERFACE}.
67    * @param owner the internal name of the class that owns the field or method designated by this
68    *     handle (see {@link Type#getInternalName()}).
69    * @param name the name of the field or method designated by this handle.
70    * @param descriptor the descriptor of the field or method designated by this handle.
71    * @deprecated this constructor has been superseded by {@link #Handle(int, String, String, String,
72    *     boolean)}.
73    */
74   @Deprecated
Handle(final int tag, final String owner, final String name, final String descriptor)75   public Handle(final int tag, final String owner, final String name, final String descriptor) {
76     this(tag, owner, name, descriptor, tag == Opcodes.H_INVOKEINTERFACE);
77   }
78 
79   /**
80    * Constructs a new field or method handle.
81    *
82    * @param tag the kind of field or method designated by this Handle. Must be {@link
83    *     Opcodes#H_GETFIELD}, {@link Opcodes#H_GETSTATIC}, {@link Opcodes#H_PUTFIELD}, {@link
84    *     Opcodes#H_PUTSTATIC}, {@link Opcodes#H_INVOKEVIRTUAL}, {@link Opcodes#H_INVOKESTATIC},
85    *     {@link Opcodes#H_INVOKESPECIAL}, {@link Opcodes#H_NEWINVOKESPECIAL} or {@link
86    *     Opcodes#H_INVOKEINTERFACE}.
87    * @param owner the internal name of the class that owns the field or method designated by this
88    *     handle (see {@link Type#getInternalName()}).
89    * @param name the name of the field or method designated by this handle.
90    * @param descriptor the descriptor of the field or method designated by this handle.
91    * @param isInterface whether the owner is an interface or not.
92    */
Handle( final int tag, final String owner, final String name, final String descriptor, final boolean isInterface)93   public Handle(
94       final int tag,
95       final String owner,
96       final String name,
97       final String descriptor,
98       final boolean isInterface) {
99     this.tag = tag;
100     this.owner = owner;
101     this.name = name;
102     this.descriptor = descriptor;
103     this.isInterface = isInterface;
104   }
105 
106   /**
107    * Returns the kind of field or method designated by this handle.
108    *
109    * @return {@link Opcodes#H_GETFIELD}, {@link Opcodes#H_GETSTATIC}, {@link Opcodes#H_PUTFIELD},
110    *     {@link Opcodes#H_PUTSTATIC}, {@link Opcodes#H_INVOKEVIRTUAL}, {@link
111    *     Opcodes#H_INVOKESTATIC}, {@link Opcodes#H_INVOKESPECIAL}, {@link
112    *     Opcodes#H_NEWINVOKESPECIAL} or {@link Opcodes#H_INVOKEINTERFACE}.
113    */
getTag()114   public int getTag() {
115     return tag;
116   }
117 
118   /**
119    * Returns the internal name of the class that owns the field or method designated by this handle.
120    *
121    * @return the internal name of the class that owns the field or method designated by this handle
122    *     (see {@link Type#getInternalName()}).
123    */
getOwner()124   public String getOwner() {
125     return owner;
126   }
127 
128   /**
129    * Returns the name of the field or method designated by this handle.
130    *
131    * @return the name of the field or method designated by this handle.
132    */
getName()133   public String getName() {
134     return name;
135   }
136 
137   /**
138    * Returns the descriptor of the field or method designated by this handle.
139    *
140    * @return the descriptor of the field or method designated by this handle.
141    */
getDesc()142   public String getDesc() {
143     return descriptor;
144   }
145 
146   /**
147    * Returns true if the owner of the field or method designated by this handle is an interface.
148    *
149    * @return true if the owner of the field or method designated by this handle is an interface.
150    */
isInterface()151   public boolean isInterface() {
152     return isInterface;
153   }
154 
155   @Override
equals(final Object object)156   public boolean equals(final Object object) {
157     if (object == this) {
158       return true;
159     }
160     if (!(object instanceof Handle)) {
161       return false;
162     }
163     Handle handle = (Handle) object;
164     return tag == handle.tag
165         && isInterface == handle.isInterface
166         && owner.equals(handle.owner)
167         && name.equals(handle.name)
168         && descriptor.equals(handle.descriptor);
169   }
170 
171   @Override
hashCode()172   public int hashCode() {
173     return tag
174         + (isInterface ? 64 : 0)
175         + owner.hashCode() * name.hashCode() * descriptor.hashCode();
176   }
177 
178   /**
179    * Returns the textual representation of this handle. The textual representation is:
180    *
181    * <ul>
182    *   <li>for a reference to a class: owner "." name descriptor " (" tag ")",
183    *   <li>for a reference to an interface: owner "." name descriptor " (" tag " itf)".
184    * </ul>
185    */
186   @Override
toString()187   public String toString() {
188     return owner + '.' + name + descriptor + " (" + tag + (isInterface ? " itf" : "") + ')';
189   }
190 }
191