Remove exported everywhere.
[SquirrelJME.git] / modules / tool-classfile / src / main / java / net / multiphasicapps / classfile / MethodHandle.java
blobf5748090674f9a31f096b99a9dc38987b415ada9
1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
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;
16 /**
17 * This represents a method's class, name, and type.
19 * @since 2017/09/16
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;
39 /**
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.
46 * @since 2017/09/16
48 public MethodHandle(ClassName __cl, MethodName __n, MethodDescriptor __d)
49 throws NullPointerException
51 // Check
52 if (__cl == null || __n == null || __d == null)
53 throw new NullPointerException("NARG");
55 this.outerclass = __cl;
56 this.name = __n;
57 this.descriptor = __d;
60 /**
61 * {@inheritDoc}
62 * @since 2017/10/14
64 @Override
65 public int compareTo(MethodHandle __o)
67 int rv;
68 if ((rv = this.outerclass.compareTo(__o.outerclass)) != 0)
69 return rv;
70 if ((rv = this.name.compareTo(__o.name)) != 0)
71 return rv;
72 return this.descriptor.toString().compareTo(__o.descriptor.toString());
75 /**
76 * Returns the descriptor of the method.
78 * @return The method descriptor.
79 * @since 2017/09/16
81 public MethodDescriptor descriptor()
83 return this.descriptor;
86 /**
87 * {@inheritDoc}
88 * @since 2017/09/16
90 @Override
91 public boolean equals(Object __o)
93 if (this == __o)
94 return true;
96 if (!(__o instanceof MethodHandle))
97 return false;
99 MethodHandle o = (MethodHandle)__o;
100 return this.outerclass.equals(o.outerclass) &&
101 this.name.equals(o.name) &&
102 this.descriptor.equals(o.descriptor);
106 * {@inheritDoc}
107 * @since 2017/09/16
109 @Override
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.
121 * @since 2017/09/18
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.
133 * @since 2017/09/16
135 public JavaType[] javaStack(boolean __i)
137 // No need to add current class type
138 JavaType[] djs = this.descriptor.javaStack();
139 if (!__i)
140 return djs;
142 // Just copy over
143 int dn = djs.length;
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++)
147 rv[o] = djs[i];
149 return rv;
153 * Returns the name of the method.
155 * @return The method name.
156 * @since 2017/09/16
158 public MethodName name()
160 return this.name;
164 * Returns the name and type of the method.
166 * @return The method name and type.
167 * @since 2019/04/17
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)));
178 return rv;
182 * Returns the class this is contained within.
184 * @return The outer class.
185 * @since 2017/09/16
187 public ClassName outerClass()
189 return this.outerclass;
193 * {@inheritDoc}
194 * @since 2017/09/16
196 @Override
197 public String toString()
199 Reference<String> ref = this._string;
200 String rv;
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)));
206 return rv;