1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the GNU General Public License v3+, or later.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package net
.multiphasicapps
.classfile
;
12 import java
.lang
.ref
.Reference
;
13 import java
.lang
.ref
.SoftReference
;
14 import java
.lang
.ref
.WeakReference
;
17 * This represents a method's class, name, and type.
21 public final class MethodHandle
22 implements Comparable
<MethodHandle
>
24 /** The class the method is in. */
25 protected final ClassName outerclass
;
27 /** The name of the method. */
28 protected final MethodName name
;
30 /** The descriptor of the method. */
31 protected final MethodDescriptor descriptor
;
33 /** Method name and type. */
34 private Reference
<MethodNameAndType
> _nat
;
36 /** String representation. */
37 private Reference
<String
> _string
;
40 * Initializes the method handle.
42 * @param __cl The class the method is in.
43 * @param __n The name of the method.
44 * @param __d The descriptor of the method.
45 * @throws NullPointerException On null arguments.
48 public MethodHandle(ClassName __cl
, MethodName __n
, MethodDescriptor __d
)
49 throws NullPointerException
52 if (__cl
== null || __n
== null || __d
== null)
53 throw new NullPointerException("NARG");
55 this.outerclass
= __cl
;
57 this.descriptor
= __d
;
65 public int compareTo(MethodHandle __o
)
68 if ((rv
= this.outerclass
.compareTo(__o
.outerclass
)) != 0)
70 if ((rv
= this.name
.compareTo(__o
.name
)) != 0)
72 return this.descriptor
.toString().compareTo(__o
.descriptor
.toString());
76 * Returns the descriptor of the method.
78 * @return The method descriptor.
81 public MethodDescriptor
descriptor()
83 return this.descriptor
;
91 public boolean equals(Object __o
)
96 if (!(__o
instanceof MethodHandle
))
99 MethodHandle o
= (MethodHandle
)__o
;
100 return this.outerclass
.equals(o
.outerclass
) &&
101 this.name
.equals(o
.name
) &&
102 this.descriptor
.equals(o
.descriptor
);
110 public int hashCode()
112 return this.outerclass
.hashCode() ^
113 this.name
.hashCode() ^
114 this.descriptor
.hashCode();
118 * Returns {@code true} if this represents the instance initializer.
120 * @return {@code true} if this is the instance initializer.
123 public boolean isInstanceInitializer()
125 return this.name
.isInstanceInitializer();
129 * Returns the Java type stack for this handle.
131 * @param __i If {@code true} then this is an instance invocation.
132 * @return The handle as it appears on the Java Stack.
135 public JavaType
[] javaStack(boolean __i
)
137 // No need to add current class type
138 JavaType
[] djs
= this.descriptor
.javaStack();
144 JavaType
[] rv
= new JavaType
[dn
+ 1];
145 rv
[0] = new JavaType(this.outerclass
);
146 for (int i
= 0, o
= 1; i
< dn
; i
++, o
++)
153 * Returns the name of the method.
155 * @return The method name.
158 public MethodName
name()
164 * Returns the name and type of the method.
166 * @return The method name and type.
169 public final MethodNameAndType
nameAndType()
171 Reference
<MethodNameAndType
> ref
= this._nat
;
172 MethodNameAndType rv
;
174 if (ref
== null || null == (rv
= ref
.get()))
175 this._nat
= new SoftReference
<>((rv
= new MethodNameAndType(
176 this.name
, this.descriptor
)));
182 * Returns the class this is contained within.
184 * @return The outer class.
187 public ClassName
outerClass()
189 return this.outerclass
;
197 public String
toString()
199 Reference
<String
> ref
= this._string
;
202 if (ref
== null || null == (rv
= ref
.get()))
203 this._string
= new WeakReference
<>((rv
= String
.format("%s::%s%s",
204 this.outerclass
, this.name
, this.descriptor
)));