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
106 private static String classInitializerName() {
107 if (classInitializerName == null) {
108 classInitializerName = "<clinit>";
109 }
110 return classInitializerName;
111 }
112
113
114 // Accessors for declared fields
115 public ConstMethod getConstMethod() {
116 Address addr = constMethod.getValue(getAddress());
117 return VMObjectFactory.newObject(ConstMethod.class, addr);
118 }
119 public ConstantPool getConstants() {
120 return getConstMethod().getConstants();
121 }
122 public boolean hasStackMapTable() {
123 return getConstMethod().hasStackMapTable();
124 }
125 public U1Array getStackMapData() {
126 return getConstMethod().getStackMapData();
127 }
128 public MethodCounters getMethodCounters() {
129 Address addr = methodCounters.getValue(getAddress());
130 return VMObjectFactory.newObject(MethodCounters.class, addr);
131 }
132 /** WARNING: this is in words, not useful in this system; use getObjectSize() instead */
133 public long getMaxStack() { return getConstMethod().getMaxStack(); }
134 public long getMaxLocals() { return getConstMethod().getMaxLocals(); }
135 public long getSizeOfParameters() { return getConstMethod().getSizeOfParameters(); }
136 public long getNameIndex() { return getConstMethod().getNameIndex(); }
137 public long getSignatureIndex() { return getConstMethod().getSignatureIndex(); }
138 public long getGenericSignatureIndex() { return getConstMethod().getGenericSignatureIndex(); }
139 public long getAccessFlags() { return accessFlags.getValue(this); }
140 public long getCodeSize() { return getConstMethod().getCodeSize(); }
141 public long getVtableIndex() { return vtableIndex.getValue(this); }
142 public long getInvocationCount() {
143 MethodCounters mc = getMethodCounters();
144 return mc == null ? 0 : mc.getInvocationCounter();
145 }
146 public long getBackedgeCount() {
147 MethodCounters mc = getMethodCounters();
148 return mc == null ? 0 : mc.getBackedgeCounter();
149 }
150
151 // get associated compiled native method, if available, else return null.
152 public NMethod getNativeMethod() {
153 Address addr = code.getValue(getAddress());
154 return VMObjectFactory.newObject(NMethod.class, addr);
155 }
156
157 // Convenience routine
158 public AccessFlags getAccessFlagsObj() {
159 return new AccessFlags(getAccessFlags());
160 }
161
162 /** Get a bytecode or breakpoint at the given bci */
163 public int getBytecodeOrBPAt(int bci) {
164 return getConstMethod().getBytecodeOrBPAt(bci);
165 }
166
167 /** Fetch the original non-breakpoint bytecode at the specified
168 bci. It is required that there is currently a bytecode at this
169 bci. */
170 public int getOrigBytecodeAt(int bci) {
171 BreakpointInfo bp = getMethodHolder().getBreakpoints();
172 for (; bp != null; bp = bp.getNext()) {
173 if (bp.match(this, bci)) {
174 return bp.getOrigBytecode();
175 }
176 }
177 System.err.println("Requested bci " + bci);
178 for (; bp != null; bp = bp.getNext()) {
179 System.err.println("Breakpoint at bci " + bp.getBCI() + ", bytecode " +
180 bp.getOrigBytecode());
181 }
182 Assert.that(false, "Should not reach here");
183 return -1; // not reached
184 }
185
186 public byte getBytecodeByteArg(int bci) {
187 return getConstMethod().getBytecodeByteArg(bci);
188 }
189
190 /** Fetches a 16-bit big-endian ("Java ordered") value from the
191 bytecode stream */
192 public short getBytecodeShortArg(int bci) {
193 return getConstMethod().getBytecodeShortArg(bci);
194 }
195
196 /** Fetches a 16-bit native ordered value from the
197 bytecode stream */
198 public short getNativeShortArg(int bci) {
199 return getConstMethod().getNativeShortArg(bci);
200 }
201
202 /** Fetches a 32-bit big-endian ("Java ordered") value from the
203 bytecode stream */
204 public int getBytecodeIntArg(int bci) {
205 return getConstMethod().getBytecodeIntArg(bci);
206 }
207
208 /** Fetches a 32-bit native ordered value from the
209 bytecode stream */
210 public int getNativeIntArg(int bci) {
211 return getConstMethod().getNativeIntArg(bci);
212 }
213
214 public byte[] getByteCode() {
215 return getConstMethod().getByteCode();
216 }
217
218 /*
219 public Address getCode() { return codeField.getValue(this); }
220 public Address getInterpreterEntry() { return interpreterEntryField.getValue(this); }
221 public Address getFromCompiledCodeEntryPoint() { return fromCompiledCodeEntryPointField.getValue(this); }
222 */
223 // Accessors
224 public Symbol getName() { return getConstants().getSymbolAt(getNameIndex()); }
225 public Symbol getSignature() { return getConstants().getSymbolAt(getSignatureIndex()); }
226 public Symbol getGenericSignature() {
227 long index = getGenericSignatureIndex();
228 return (index != 0L) ? getConstants().getSymbolAt(index) : null;
229 }
230
231 // Method holder (the Klass holding this method)
232 public InstanceKlass getMethodHolder() { return getConstants().getPoolHolder(); }
233
234 // Access flags
235 public boolean isPublic() { return getAccessFlagsObj().isPublic(); }
236 public boolean isPrivate() { return getAccessFlagsObj().isPrivate(); }
237 public boolean isProtected() { return getAccessFlagsObj().isProtected(); }
238 public boolean isPackagePrivate() { AccessFlags af = getAccessFlagsObj();
239 return (!af.isPublic() && !af.isPrivate() && !af.isProtected()); }
240 public boolean isStatic() { return getAccessFlagsObj().isStatic(); }
241 public boolean isFinal() { return getAccessFlagsObj().isFinal(); }
242 public boolean isSynchronized() { return getAccessFlagsObj().isSynchronized(); }
243 public boolean isBridge() { return getAccessFlagsObj().isBridge(); }
244 public boolean isVarArgs() { return getAccessFlagsObj().isVarArgs(); }
245 public boolean isNative() { return getAccessFlagsObj().isNative(); }
246 public boolean isAbstract() { return getAccessFlagsObj().isAbstract(); }
247 public boolean isStrict() { return getAccessFlagsObj().isStrict(); }
248 public boolean isSynthetic() { return getAccessFlagsObj().isSynthetic(); }
249
250 public boolean isConstructor() {
251 return (!isStatic()) && getName().equals(objectInitializerName());
252 }
253
254 public boolean isStaticInitializer() {
255 return isStatic() && getName().equals(classInitializerName());
256 }
257
258 public OopMapCacheEntry getMaskFor(int bci) {
259 OopMapCacheEntry entry = new OopMapCacheEntry();
260 entry.fill(this, bci);
261 return entry;
262 }
263
264 public long getSize() {
265 return type.getSize() + (isNative() ? 2: 0);
266 }
267
268 public void printValueOn(PrintStream tty) {
269 tty.print("Method " + getMethodHolder().getName().asString() + "." +
270 getName().asString() + getSignature().asString() + "@" + getAddress());
271 }
272
273 public void iterateFields(MetadataVisitor visitor) {
274 visitor.doCInt(accessFlags, true);
275 }
276
277 public boolean hasLineNumberTable() {
278 return getConstMethod().hasLineNumberTable();
279 }
280
281 public int getLineNumberFromBCI(int bci) {
282 return getConstMethod().getLineNumberFromBCI(bci);
283 }
284
285 public LineNumberTableElement[] getLineNumberTable() {
286 return getConstMethod().getLineNumberTable();
287 }
288
289 public boolean hasLocalVariableTable() {
290 return getConstMethod().hasLocalVariableTable();
291 }
292
293 /** Should only be called if table is present */
294 public LocalVariableTableElement[] getLocalVariableTable() {
295 return getConstMethod().getLocalVariableTable();
296 }
297
298 public Symbol getLocalVariableName(int bci, int slot) {
299 if (! hasLocalVariableTable()) {
300 return null;
301 }
302
303 LocalVariableTableElement[] locals = getLocalVariableTable();
304 for (int l = 0; l < locals.length; l++) {
305 LocalVariableTableElement local = locals[l];
306 if ((bci >= local.getStartBCI()) &&
307 (bci < (local.getStartBCI() + local.getLength())) &&
308 slot == local.getSlot()) {
309 return getConstants().getSymbolAt(local.getNameCPIndex());
310 }
311 }
312
313 return null;
314 }
315
316 public boolean hasExceptionTable() {
317 return getConstMethod().hasExceptionTable();
318 }
319
320 public ExceptionTableElement[] getExceptionTable() {
321 return getConstMethod().getExceptionTable();
322 }
323
324 public boolean hasCheckedExceptions() {
325 return getConstMethod().hasCheckedExceptions();
326 }
327
328 /** Should only be called if table is present */
329 public CheckedExceptionElement[] getCheckedExceptions() {
330 return getConstMethod().getCheckedExceptions();
331 }
332
333 /** Returns name and signature in external form for debugging
334 purposes */
335 public String externalNameAndSignature() {
336 final StringBuffer buf = new StringBuffer();
337 buf.append(getMethodHolder().getName().asString());
338 buf.append(".");
339 buf.append(getName().asString());
340 buf.append("(");
341 new SignatureConverter(getSignature(), buf).iterateParameters();
342 buf.append(")");
343 return buf.toString().replace('/', '.');
344 }
345
346 public int interpreterThrowoutCount() {
347 return getMethodCounters().interpreterThrowoutCount();
348 }
349
350 public long interpreterInvocationCount() {
351 return getInvocationCount();
352 }
353
354 public String nameAsAscii() {
355 return getMethodHolder().getName().asString() + " " +
356 OopUtilities.escapeString(getName().asString()) + " " +
357 getSignature().asString();
358 }
359
360 public U1Array getAnnotations() {
361 return getConstMethod().getMethodAnnotations();
362 }
363
364 public U1Array getParameterAnnotations() {
365 return getConstMethod().getParameterAnnotations();
366 }
367
368 public U1Array getTypeAnnotations() {
369 return getConstMethod().getTypeAnnotations();
370 }
371
372 public U1Array getAnnotationDefault() {
373 return getConstMethod().getDefaultAnnotations();
374 }
375 }