1 /*
2 * Copyright (c) 2000, 2026, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 package sun.jvm.hotspot.oops;
26
27 import java.io.PrintStream;
28 import sun.jvm.hotspot.utilities.Observable;
29 import sun.jvm.hotspot.utilities.Observer;
30 import sun.jvm.hotspot.utilities.U1Array;
31
32 import sun.jvm.hotspot.code.NMethod;
33 import sun.jvm.hotspot.debugger.Address;
34 import sun.jvm.hotspot.interpreter.OopMapCacheEntry;
35 import sun.jvm.hotspot.runtime.SignatureConverter;
36 import sun.jvm.hotspot.runtime.VM;
37 import sun.jvm.hotspot.runtime.VMObjectFactory;
38 import sun.jvm.hotspot.types.AddressField;
39 import sun.jvm.hotspot.types.Type;
40 import sun.jvm.hotspot.types.TypeDataBase;
41 import sun.jvm.hotspot.types.WrongTypeException;
42 import sun.jvm.hotspot.utilities.Assert;
43 import sun.jvm.hotspot.utilities.U1Array;
44
45 // A Method represents a Java method
46
47 public class Method extends Metadata {
48 static {
49 VM.registerVMInitializedObserver(new Observer() {
50 public void update(Observable o, Object data) {
51 initialize(VM.getVM().getTypeDataBase());
52 }
53 });
54 }
55
56 private static synchronized void initialize(TypeDataBase db) throws WrongTypeException {
57 type = db.lookupType("Method");
58 constMethod = type.getAddressField("_constMethod");
59 methodCounters = type.getAddressField("_method_counters");
60 accessFlags = new CIntField(type.getCIntegerField("_access_flags"), 0);
61 code = type.getAddressField("_code");
62 vtableIndex = new CIntField(type.getCIntegerField("_vtable_index"), 0);
63
64 /*
65 fromCompiledCodeEntryPoint = type.getAddressField("_from_compiled_code_entry_point");
66 interpreterEntry = type.getAddressField("_from_interpreted_entry");
67 */
68
69 objectInitializerName = null;
70 classInitializerName = null;
71 }
72
73 public Method(Address addr) {
74 super(addr);
75 }
76
77 public boolean isMethod() { return true; }
78
79 // Not a Method field, used to keep type.
80 private static Type type;
81
82 // Fields
83 private static AddressField constMethod;
84 private static AddressField methodCounters;
85 private static CIntField accessFlags;
86 private static CIntField vtableIndex;
87
88 private static AddressField code;
89 /*
90 private static AddressCField fromCompiledCodeEntryPoint;
91 private static AddressField interpreterEntry;
92 */
93
94
95 // constant method names - <init>, <clinit>
96 // Initialized lazily to avoid initialization ordering dependencies between ArrayKlass and String
97 private static String objectInitializerName;
98 private static String classInitializerName;
99 private static String objectInitializerName() {
100 if (objectInitializerName == null) {
101 objectInitializerName = "<init>";
102 }
103 return objectInitializerName;
104 }
105 private static String classInitializerName() {
106 if (classInitializerName == null) {
107 classInitializerName = "<clinit>";
108 }
109 return classInitializerName;
110 }
111
112
113 // Accessors for declared fields
114 public ConstMethod getConstMethod() {
115 Address addr = constMethod.getValue(getAddress());
116 return VMObjectFactory.newObject(ConstMethod.class, addr);
117 }
118 public ConstantPool getConstants() {
119 return getConstMethod().getConstants();
120 }
121 public boolean hasStackMapTable() {
122 return getConstMethod().hasStackMapTable();
123 }
124 public U1Array getStackMapData() {
125 return getConstMethod().getStackMapData();
126 }
127 public MethodCounters getMethodCounters() {
128 Address addr = methodCounters.getValue(getAddress());
129 return VMObjectFactory.newObject(MethodCounters.class, addr);
130 }
131 /** WARNING: this is in words, not useful in this system; use getObjectSize() instead */
132 public long getMaxStack() { return getConstMethod().getMaxStack(); }
133 public long getMaxLocals() { return getConstMethod().getMaxLocals(); }
134 public long getSizeOfParameters() { return getConstMethod().getSizeOfParameters(); }
135 public long getNameIndex() { return getConstMethod().getNameIndex(); }
136 public long getSignatureIndex() { return getConstMethod().getSignatureIndex(); }
137 public long getGenericSignatureIndex() { return getConstMethod().getGenericSignatureIndex(); }
138 public long getAccessFlags() { return accessFlags.getValue(this); }
139 public long getCodeSize() { return getConstMethod().getCodeSize(); }
140 public long getVtableIndex() { return vtableIndex.getValue(this); }
141 public long getInvocationCount() {
142 MethodCounters mc = getMethodCounters();
143 return mc == null ? 0 : mc.getInvocationCounter();
144 }
145 public long getBackedgeCount() {
146 MethodCounters mc = getMethodCounters();
147 return mc == null ? 0 : mc.getBackedgeCounter();
148 }
149
150 // get associated compiled native method, if available, else return null.
151 public NMethod getNativeMethod() {
152 Address addr = code.getValue(getAddress());
153 return VMObjectFactory.newObject(NMethod.class, addr);
154 }
155
156 // Convenience routine
157 public AccessFlags getAccessFlagsObj() {
158 return new AccessFlags(getAccessFlags());
159 }
160
161 /** Get a bytecode or breakpoint at the given bci */
162 public int getBytecodeOrBPAt(int bci) {
163 return getConstMethod().getBytecodeOrBPAt(bci);
164 }
165
166 /** Fetch the original non-breakpoint bytecode at the specified
167 bci. It is required that there is currently a bytecode at this
168 bci. */
169 public int getOrigBytecodeAt(int bci) {
170 BreakpointInfo bp = getMethodHolder().getBreakpoints();
171 for (; bp != null; bp = bp.getNext()) {
172 if (bp.match(this, bci)) {
173 return bp.getOrigBytecode();
174 }
175 }
176 System.err.println("Requested bci " + bci);
177 for (; bp != null; bp = bp.getNext()) {
178 System.err.println("Breakpoint at bci " + bp.getBCI() + ", bytecode " +
179 bp.getOrigBytecode());
180 }
181 Assert.that(false, "Should not reach here");
182 return -1; // not reached
183 }
184
185 public byte getBytecodeByteArg(int bci) {
186 return getConstMethod().getBytecodeByteArg(bci);
187 }
188
189 /** Fetches a 16-bit big-endian ("Java ordered") value from the
190 bytecode stream */
191 public short getBytecodeShortArg(int bci) {
192 return getConstMethod().getBytecodeShortArg(bci);
193 }
194
195 /** Fetches a 16-bit native ordered value from the
196 bytecode stream */
197 public short getNativeShortArg(int bci) {
198 return getConstMethod().getNativeShortArg(bci);
199 }
200
201 /** Fetches a 32-bit big-endian ("Java ordered") value from the
202 bytecode stream */
203 public int getBytecodeIntArg(int bci) {
204 return getConstMethod().getBytecodeIntArg(bci);
205 }
206
207 /** Fetches a 32-bit native ordered value from the
208 bytecode stream */
209 public int getNativeIntArg(int bci) {
210 return getConstMethod().getNativeIntArg(bci);
211 }
212
213 public byte[] getByteCode() {
214 return getConstMethod().getByteCode();
215 }
216
217 /*
218 public Address getCode() { return codeField.getValue(this); }
219 public Address getInterpreterEntry() { return interpreterEntryField.getValue(this); }
220 public Address getFromCompiledCodeEntryPoint() { return fromCompiledCodeEntryPointField.getValue(this); }
221 */
222 // Accessors
223 public Symbol getName() { return getConstants().getSymbolAt(getNameIndex()); }
224 public Symbol getSignature() { return getConstants().getSymbolAt(getSignatureIndex()); }
225 public Symbol getGenericSignature() {
226 long index = getGenericSignatureIndex();
227 return (index != 0L) ? getConstants().getSymbolAt(index) : null;
228 }
229
230 // Method holder (the Klass holding this method)
231 public InstanceKlass getMethodHolder() { return getConstants().getPoolHolder(); }
232
233 // Access flags
234 public boolean isPublic() { return getAccessFlagsObj().isPublic(); }
235 public boolean isPrivate() { return getAccessFlagsObj().isPrivate(); }
236 public boolean isProtected() { return getAccessFlagsObj().isProtected(); }
237 public boolean isPackagePrivate() { AccessFlags af = getAccessFlagsObj();
238 return (!af.isPublic() && !af.isPrivate() && !af.isProtected()); }
239 public boolean isStatic() { return getAccessFlagsObj().isStatic(); }
240 public boolean isFinal() { return getAccessFlagsObj().isFinal(); }
241 public boolean isSynchronized() { return getAccessFlagsObj().isSynchronized(); }
242 public boolean isBridge() { return getAccessFlagsObj().isBridge(); }
243 public boolean isVarArgs() { return getAccessFlagsObj().isVarArgs(); }
244 public boolean isNative() { return getAccessFlagsObj().isNative(); }
245 public boolean isAbstract() { return getAccessFlagsObj().isAbstract(); }
246 public boolean isStrict() { return getAccessFlagsObj().isStrict(); }
247 public boolean isSynthetic() { return getAccessFlagsObj().isSynthetic(); }
248
249 public boolean isConstructor() {
250 return (!isStatic()) && getName().equals(objectInitializerName());
251 }
252
253 public boolean isStaticInitializer() {
254 return isStatic() && getName().equals(classInitializerName());
255 }
256
257 public OopMapCacheEntry getMaskFor(int bci) {
258 OopMapCacheEntry entry = new OopMapCacheEntry();
259 entry.fill(this, bci);
260 return entry;
261 }
262
263 public long getSize() {
264 return type.getSize() + (isNative() ? 2: 0);
265 }
266
267 public void printValueOn(PrintStream tty) {
268 tty.print("Method " + getMethodHolder().getName().asString() + "." +
269 getName().asString() + getSignature().asString() + "@" + getAddress());
270 }
271
272 public void iterateFields(MetadataVisitor visitor) {
273 visitor.doCInt(accessFlags, true);
274 }
275
276 public boolean hasLineNumberTable() {
277 return getConstMethod().hasLineNumberTable();
278 }
279
280 public int getLineNumberFromBCI(int bci) {
281 return getConstMethod().getLineNumberFromBCI(bci);
282 }
283
284 public LineNumberTableElement[] getLineNumberTable() {
285 return getConstMethod().getLineNumberTable();
286 }
287
288 public boolean hasLocalVariableTable() {
289 return getConstMethod().hasLocalVariableTable();
290 }
291
292 /** Should only be called if table is present */
293 public LocalVariableTableElement[] getLocalVariableTable() {
294 return getConstMethod().getLocalVariableTable();
295 }
296
297 public Symbol getLocalVariableName(int bci, int slot) {
298 if (! hasLocalVariableTable()) {
299 return null;
300 }
301
302 LocalVariableTableElement[] locals = getLocalVariableTable();
303 for (int l = 0; l < locals.length; l++) {
304 LocalVariableTableElement local = locals[l];
305 if ((bci >= local.getStartBCI()) &&
306 (bci < (local.getStartBCI() + local.getLength())) &&
307 slot == local.getSlot()) {
308 return getConstants().getSymbolAt(local.getNameCPIndex());
309 }
310 }
311
312 return null;
313 }
314
315 public boolean hasExceptionTable() {
316 return getConstMethod().hasExceptionTable();
317 }
318
319 public ExceptionTableElement[] getExceptionTable() {
320 return getConstMethod().getExceptionTable();
321 }
322
323 public boolean hasCheckedExceptions() {
324 return getConstMethod().hasCheckedExceptions();
325 }
326
327 /** Should only be called if table is present */
328 public CheckedExceptionElement[] getCheckedExceptions() {
329 return getConstMethod().getCheckedExceptions();
330 }
331
332 /** Returns name and signature in external form for debugging
333 purposes */
334 public String externalNameAndSignature() {
335 final StringBuffer buf = new StringBuffer();
336 buf.append(getMethodHolder().getName().asString());
337 buf.append(".");
338 buf.append(getName().asString());
339 buf.append("(");
340 new SignatureConverter(getSignature(), buf).iterateParameters();
341 buf.append(")");
342 return buf.toString().replace('/', '.');
343 }
344
345 public int interpreterThrowoutCount() {
346 return getMethodCounters().interpreterThrowoutCount();
347 }
348
349 public long interpreterInvocationCount() {
350 return getInvocationCount();
351 }
352
353 public String nameAsAscii() {
354 return getMethodHolder().getName().asString() + " " +
355 OopUtilities.escapeString(getName().asString()) + " " +
356 getSignature().asString();
357 }
358
359 public U1Array getAnnotations() {
360 return getConstMethod().getMethodAnnotations();
361 }
362
363 public U1Array getParameterAnnotations() {
364 return getConstMethod().getParameterAnnotations();
365 }
366
367 public U1Array getTypeAnnotations() {
368 return getConstMethod().getTypeAnnotations();
369 }
370
371 public U1Array getAnnotationDefault() {
372 return getConstMethod().getDefaultAnnotations();
373 }
374 }