|
Java example source code file (method.hpp)
The method.hpp Java example source code
/*
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#ifndef SHARE_VM_OOPS_METHODOOP_HPP
#define SHARE_VM_OOPS_METHODOOP_HPP
#include "classfile/vmSymbols.hpp"
#include "code/compressedStream.hpp"
#include "compiler/oopMap.hpp"
#include "interpreter/invocationCounter.hpp"
#include "oops/annotations.hpp"
#include "oops/constantPool.hpp"
#include "oops/methodCounters.hpp"
#include "oops/instanceKlass.hpp"
#include "oops/oop.hpp"
#include "oops/typeArrayOop.hpp"
#include "utilities/accessFlags.hpp"
#include "utilities/growableArray.hpp"
// A Method* represents a Java method.
//
// Memory layout (each line represents a word). Note that most applications load thousands of methods,
// so keeping the size of this structure small has a big impact on footprint.
//
// We put all oops and method_size first for better gc cache locality.
//
// The actual bytecodes are inlined after the end of the Method struct.
//
// There are bits in the access_flags telling whether inlined tables are present.
// Note that accessing the line number and local variable tables is not performance critical at all.
// Accessing the checked exceptions table is used by reflection, so we put that last to make access
// to it fast.
//
// The line number table is compressed and inlined following the byte codes. It is found as the first
// byte following the byte codes. The checked exceptions table and the local variable table are inlined
// after the line number table, and indexed from the end of the method. We do not compress the checked
// exceptions table since the average length is less than 2, and do not bother to compress the local
// variable table either since it is mostly absent.
//
// Note that native_function and signature_handler has to be at fixed offsets (required by the interpreter)
//
// |------------------------------------------------------|
// | header |
// | klass |
// |------------------------------------------------------|
// | ConstMethod* (oop) |
// |------------------------------------------------------|
// | methodData (oop) |
// | methodCounters |
// |------------------------------------------------------|
// | access_flags |
// | vtable_index |
// |------------------------------------------------------|
// | result_index (C++ interpreter only) |
// |------------------------------------------------------|
// | method_size | intrinsic_id| flags |
// |------------------------------------------------------|
// | code (pointer) |
// | i2i (pointer) |
// | adapter (pointer) |
// | from_compiled_entry (pointer) |
// | from_interpreted_entry (pointer) |
// |------------------------------------------------------|
// | native_function (present only if native) |
// | signature_handler (present only if native) |
// |------------------------------------------------------|
class CheckedExceptionElement;
class LocalVariableTableElement;
class AdapterHandlerEntry;
class MethodData;
class MethodCounters;
class ConstMethod;
class InlineTableSizes;
class KlassSizeStats;
class Method : public Metadata {
friend class VMStructs;
private:
ConstMethod* _constMethod; // Method read-only data.
MethodData* _method_data;
MethodCounters* _method_counters;
AccessFlags _access_flags; // Access flags
int _vtable_index; // vtable index of this method (see VtableIndexFlag)
// note: can have vtables with >2**16 elements (because of inheritance)
#ifdef CC_INTERP
int _result_index; // C++ interpreter needs for converting results to/from stack
#endif
u2 _method_size; // size of this object
u1 _intrinsic_id; // vmSymbols::intrinsic_id (0 == _none)
u1 _jfr_towrite : 1, // Flags
_caller_sensitive : 1,
_force_inline : 1,
_hidden : 1,
_dont_inline : 1,
: 3;
#ifndef PRODUCT
int _compiled_invocation_count; // Number of nmethod invocations so far (for perf. debugging)
#endif
// Entry point for calling both from and to the interpreter.
address _i2i_entry; // All-args-on-stack calling convention
// Adapter blob (i2c/c2i) for this Method*. Set once when method is linked.
AdapterHandlerEntry* _adapter;
// Entry point for calling from compiled code, to compiled code if it exists
// or else the interpreter.
volatile address _from_compiled_entry; // Cache of: _code ? _code->entry_point() : _adapter->c2i_entry()
// The entry point for calling both from and to compiled code is
// "_code->entry_point()". Because of tiered compilation and de-opt, this
// field can come and go. It can transition from NULL to not-null at any
// time (whenever a compile completes). It can transition from not-null to
// NULL only at safepoints (because of a de-opt).
nmethod* volatile _code; // Points to the corresponding piece of native code
volatile address _from_interpreted_entry; // Cache of _code ? _adapter->i2c_entry() : _i2i_entry
// Constructor
Method(ConstMethod* xconst, AccessFlags access_flags, int size);
public:
static Method* allocate(ClassLoaderData* loader_data,
int byte_code_size,
AccessFlags access_flags,
InlineTableSizes* sizes,
ConstMethod::MethodType method_type,
TRAPS);
// CDS and vtbl checking can create an empty Method to get vtbl pointer.
Method(){}
// The Method vtable is restored by this call when the Method is in the
// shared archive. See patch_klass_vtables() in metaspaceShared.cpp for
// all the gory details. SA, dtrace and pstack helpers distinguish metadata
// by their vtable.
void restore_vtable() { guarantee(is_method(), "vtable restored by this call"); }
bool is_method() const volatile { return true; }
// accessors for instance variables
ConstMethod* constMethod() const { return _constMethod; }
void set_constMethod(ConstMethod* xconst) { _constMethod = xconst; }
static address make_adapters(methodHandle mh, TRAPS);
volatile address from_compiled_entry() const { return (address)OrderAccess::load_ptr_acquire(&_from_compiled_entry); }
volatile address from_interpreted_entry() const{ return (address)OrderAccess::load_ptr_acquire(&_from_interpreted_entry); }
// access flag
AccessFlags access_flags() const { return _access_flags; }
void set_access_flags(AccessFlags flags) { _access_flags = flags; }
// name
Symbol* name() const { return constants()->symbol_at(name_index()); }
int name_index() const { return constMethod()->name_index(); }
void set_name_index(int index) { constMethod()->set_name_index(index); }
// signature
Symbol* signature() const { return constants()->symbol_at(signature_index()); }
int signature_index() const { return constMethod()->signature_index(); }
void set_signature_index(int index) { constMethod()->set_signature_index(index); }
// generics support
Symbol* generic_signature() const { int idx = generic_signature_index(); return ((idx != 0) ? constants()->symbol_at(idx) : (Symbol*)NULL); }
int generic_signature_index() const { return constMethod()->generic_signature_index(); }
void set_generic_signature_index(int index) { constMethod()->set_generic_signature_index(index); }
// annotations support
AnnotationArray* annotations() const {
return constMethod()->method_annotations();
}
AnnotationArray* parameter_annotations() const {
return constMethod()->parameter_annotations();
}
AnnotationArray* annotation_default() const {
return constMethod()->default_annotations();
}
AnnotationArray* type_annotations() const {
return constMethod()->type_annotations();
}
#ifdef CC_INTERP
void set_result_index(BasicType type);
int result_index() { return _result_index; }
#endif
// Helper routine: get klass name + "." + method name + signature as
// C string, for the purpose of providing more useful NoSuchMethodErrors
// and fatal error handling. The string is allocated in resource
// area if a buffer is not provided by the caller.
char* name_and_sig_as_C_string() const;
char* name_and_sig_as_C_string(char* buf, int size) const;
// Static routine in the situations we don't have a Method*
static char* name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature);
static char* name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature, char* buf, int size);
Bytecodes::Code java_code_at(int bci) const {
return Bytecodes::java_code_at(this, bcp_from(bci));
}
Bytecodes::Code code_at(int bci) const {
return Bytecodes::code_at(this, bcp_from(bci));
}
// JVMTI breakpoints
Bytecodes::Code orig_bytecode_at(int bci) const;
void set_orig_bytecode_at(int bci, Bytecodes::Code code);
void set_breakpoint(int bci);
void clear_breakpoint(int bci);
void clear_all_breakpoints();
// Tracking number of breakpoints, for fullspeed debugging.
// Only mutated by VM thread.
u2 number_of_breakpoints() const {
if (method_counters() == NULL) {
return 0;
} else {
return method_counters()->number_of_breakpoints();
}
}
void incr_number_of_breakpoints(TRAPS) {
MethodCounters* mcs = get_method_counters(CHECK);
if (mcs != NULL) {
mcs->incr_number_of_breakpoints();
}
}
void decr_number_of_breakpoints(TRAPS) {
MethodCounters* mcs = get_method_counters(CHECK);
if (mcs != NULL) {
mcs->decr_number_of_breakpoints();
}
}
// Initialization only
void clear_number_of_breakpoints() {
if (method_counters() != NULL) {
method_counters()->clear_number_of_breakpoints();
}
}
// index into InstanceKlass methods() array
// note: also used by jfr
u2 method_idnum() const { return constMethod()->method_idnum(); }
void set_method_idnum(u2 idnum) { constMethod()->set_method_idnum(idnum); }
// code size
int code_size() const { return constMethod()->code_size(); }
// method size
int method_size() const { return _method_size; }
void set_method_size(int size) {
assert(0 <= size && size < (1 << 16), "invalid method size");
_method_size = size;
}
// constant pool for Klass* holding this method
ConstantPool* constants() const { return constMethod()->constants(); }
void set_constants(ConstantPool* c) { constMethod()->set_constants(c); }
// max stack
// return original max stack size for method verification
int verifier_max_stack() const { return constMethod()->max_stack(); }
int max_stack() const { return constMethod()->max_stack() + extra_stack_entries(); }
void set_max_stack(int size) { constMethod()->set_max_stack(size); }
// max locals
int max_locals() const { return constMethod()->max_locals(); }
void set_max_locals(int size) { constMethod()->set_max_locals(size); }
int highest_comp_level() const;
void set_highest_comp_level(int level);
int highest_osr_comp_level() const;
void set_highest_osr_comp_level(int level);
// Count of times method was exited via exception while interpreting
void interpreter_throwout_increment(TRAPS) {
MethodCounters* mcs = get_method_counters(CHECK);
if (mcs != NULL) {
mcs->interpreter_throwout_increment();
}
}
int interpreter_throwout_count() const {
if (method_counters() == NULL) {
return 0;
} else {
return method_counters()->interpreter_throwout_count();
}
}
// size of parameters
int size_of_parameters() const { return constMethod()->size_of_parameters(); }
void set_size_of_parameters(int size) { constMethod()->set_size_of_parameters(size); }
bool has_stackmap_table() const {
return constMethod()->has_stackmap_table();
}
Array<u1>* stackmap_data() const {
return constMethod()->stackmap_data();
}
void set_stackmap_data(Array<u1>* sd) {
constMethod()->set_stackmap_data(sd);
}
// exception handler table
bool has_exception_handler() const
{ return constMethod()->has_exception_handler(); }
int exception_table_length() const
{ return constMethod()->exception_table_length(); }
ExceptionTableElement* exception_table_start() const
{ return constMethod()->exception_table_start(); }
// Finds the first entry point bci of an exception handler for an
// exception of klass ex_klass thrown at throw_bci. A value of NULL
// for ex_klass indicates that the exception klass is not known; in
// this case it matches any constraint class. Returns -1 if the
// exception cannot be handled in this method. The handler
// constraint classes are loaded if necessary. Note that this may
// throw an exception if loading of the constraint classes causes
// an IllegalAccessError (bugid 4307310) or an OutOfMemoryError.
// If an exception is thrown, returns the bci of the
// exception handler which caused the exception to be thrown, which
// is needed for proper retries. See, for example,
// InterpreterRuntime::exception_handler_for_exception.
static int fast_exception_handler_bci_for(methodHandle mh, KlassHandle ex_klass, int throw_bci, TRAPS);
// method data access
MethodData* method_data() const {
return _method_data;
}
void set_method_data(MethodData* data) {
_method_data = data;
}
MethodCounters* method_counters() const {
return _method_counters;
}
void set_method_counters(MethodCounters* counters) {
_method_counters = counters;
}
#ifdef TIERED
// We are reusing interpreter_invocation_count as a holder for the previous event count!
// We can do that since interpreter_invocation_count is not used in tiered.
int prev_event_count() const {
if (method_counters() == NULL) {
return 0;
} else {
return method_counters()->interpreter_invocation_count();
}
}
void set_prev_event_count(int count, TRAPS) {
MethodCounters* mcs = get_method_counters(CHECK);
if (mcs != NULL) {
mcs->set_interpreter_invocation_count(count);
}
}
jlong prev_time() const {
return method_counters() == NULL ? 0 : method_counters()->prev_time();
}
void set_prev_time(jlong time, TRAPS) {
MethodCounters* mcs = get_method_counters(CHECK);
if (mcs != NULL) {
mcs->set_prev_time(time);
}
}
float rate() const {
return method_counters() == NULL ? 0 : method_counters()->rate();
}
void set_rate(float rate, TRAPS) {
MethodCounters* mcs = get_method_counters(CHECK);
if (mcs != NULL) {
mcs->set_rate(rate);
}
}
#endif
int invocation_count();
int backedge_count();
bool was_executed_more_than(int n);
bool was_never_executed() { return !was_executed_more_than(0); }
static void build_interpreter_method_data(methodHandle method, TRAPS);
static MethodCounters* build_method_counters(Method* m, TRAPS);
int interpreter_invocation_count() {
if (TieredCompilation) return invocation_count();
else return (method_counters() == NULL) ? 0 :
method_counters()->interpreter_invocation_count();
}
int increment_interpreter_invocation_count(TRAPS) {
if (TieredCompilation) ShouldNotReachHere();
MethodCounters* mcs = get_method_counters(CHECK_0);
return (mcs == NULL) ? 0 : mcs->increment_interpreter_invocation_count();
}
#ifndef PRODUCT
int compiled_invocation_count() const { return _compiled_invocation_count; }
void set_compiled_invocation_count(int count) { _compiled_invocation_count = count; }
#endif // not PRODUCT
// Clear (non-shared space) pointers which could not be relevant
// if this (shared) method were mapped into another JVM.
void remove_unshareable_info();
// nmethod/verified compiler entry
address verified_code_entry();
bool check_code() const; // Not inline to avoid circular ref
nmethod* volatile code() const { assert( check_code(), "" ); return (nmethod *)OrderAccess::load_ptr_acquire(&_code); }
void clear_code(); // Clear out any compiled code
static void set_code(methodHandle mh, nmethod* code);
void set_adapter_entry(AdapterHandlerEntry* adapter) { _adapter = adapter; }
address get_i2c_entry();
address get_c2i_entry();
address get_c2i_unverified_entry();
AdapterHandlerEntry* adapter() { return _adapter; }
// setup entry points
void link_method(methodHandle method, TRAPS);
// clear entry points. Used by sharing code
void unlink_method();
// vtable index
enum VtableIndexFlag {
// Valid vtable indexes are non-negative (>= 0).
// These few negative values are used as sentinels.
itable_index_max = -10, // first itable index, growing downward
pending_itable_index = -9, // itable index will be assigned
invalid_vtable_index = -4, // distinct from any valid vtable index
garbage_vtable_index = -3, // not yet linked; no vtable layout yet
nonvirtual_vtable_index = -2 // there is no need for vtable dispatch
// 6330203 Note: Do not use -1, which was overloaded with many meanings.
};
DEBUG_ONLY(bool valid_vtable_index() const { return _vtable_index >= nonvirtual_vtable_index; })
bool has_vtable_index() const { return _vtable_index >= 0; }
int vtable_index() const { return _vtable_index; }
void set_vtable_index(int index) { _vtable_index = index; }
DEBUG_ONLY(bool valid_itable_index() const { return _vtable_index <= pending_itable_index; })
bool has_itable_index() const { return _vtable_index <= itable_index_max; }
int itable_index() const { assert(valid_itable_index(), "");
return itable_index_max - _vtable_index; }
void set_itable_index(int index) { _vtable_index = itable_index_max - index; assert(valid_itable_index(), ""); }
// interpreter entry
address interpreter_entry() const { return _i2i_entry; }
// Only used when first initialize so we can set _i2i_entry and _from_interpreted_entry
void set_interpreter_entry(address entry) { _i2i_entry = entry; _from_interpreted_entry = entry; }
// native function (used for native methods only)
enum {
native_bind_event_is_interesting = true
};
address native_function() const { return *(native_function_addr()); }
address critical_native_function();
// Must specify a real function (not NULL).
// Use clear_native_function() to unregister.
void set_native_function(address function, bool post_event_flag);
bool has_native_function() const;
void clear_native_function();
// signature handler (used for native methods only)
address signature_handler() const { return *(signature_handler_addr()); }
void set_signature_handler(address handler);
// Interpreter oopmap support
void mask_for(int bci, InterpreterOopMap* mask);
#ifndef PRODUCT
// operations on invocation counter
void print_invocation_count();
#endif
// byte codes
void set_code(address code) { return constMethod()->set_code(code); }
address code_base() const { return constMethod()->code_base(); }
bool contains(address bcp) const { return constMethod()->contains(bcp); }
// prints byte codes
void print_codes() const { print_codes_on(tty); }
void print_codes_on(outputStream* st) const PRODUCT_RETURN;
void print_codes_on(int from, int to, outputStream* st) const PRODUCT_RETURN;
// method parameters
bool has_method_parameters() const
{ return constMethod()->has_method_parameters(); }
int method_parameters_length() const
{ return constMethod()->method_parameters_length(); }
MethodParametersElement* method_parameters_start() const
{ return constMethod()->method_parameters_start(); }
// checked exceptions
int checked_exceptions_length() const
{ return constMethod()->checked_exceptions_length(); }
CheckedExceptionElement* checked_exceptions_start() const
{ return constMethod()->checked_exceptions_start(); }
// localvariable table
bool has_localvariable_table() const
{ return constMethod()->has_localvariable_table(); }
int localvariable_table_length() const
{ return constMethod()->localvariable_table_length(); }
LocalVariableTableElement* localvariable_table_start() const
{ return constMethod()->localvariable_table_start(); }
bool has_linenumber_table() const
{ return constMethod()->has_linenumber_table(); }
u_char* compressed_linenumber_table() const
{ return constMethod()->compressed_linenumber_table(); }
// method holder (the Klass* holding this method)
InstanceKlass* method_holder() const { return constants()->pool_holder(); }
void compute_size_of_parameters(Thread *thread); // word size of parameters (receiver if any + arguments)
Symbol* klass_name() const; // returns the name of the method holder
BasicType result_type() const; // type of the method result
int result_type_index() const; // type index of the method result
bool is_returning_oop() const { BasicType r = result_type(); return (r == T_OBJECT || r == T_ARRAY); }
bool is_returning_fp() const { BasicType r = result_type(); return (r == T_FLOAT || r == T_DOUBLE); }
// Checked exceptions thrown by this method (resolved to mirrors)
objArrayHandle resolved_checked_exceptions(TRAPS) { return resolved_checked_exceptions_impl(this, THREAD); }
// Access flags
bool is_public() const { return access_flags().is_public(); }
bool is_private() const { return access_flags().is_private(); }
bool is_protected() const { return access_flags().is_protected(); }
bool is_package_private() const { return !is_public() && !is_private() && !is_protected(); }
bool is_static() const { return access_flags().is_static(); }
bool is_final() const { return access_flags().is_final(); }
bool is_synchronized() const { return access_flags().is_synchronized();}
bool is_native() const { return access_flags().is_native(); }
bool is_abstract() const { return access_flags().is_abstract(); }
bool is_strict() const { return access_flags().is_strict(); }
bool is_synthetic() const { return access_flags().is_synthetic(); }
// returns true if contains only return operation
bool is_empty_method() const;
// returns true if this is a vanilla constructor
bool is_vanilla_constructor() const;
// checks method and its method holder
bool is_final_method() const;
bool is_final_method(AccessFlags class_access_flags) const;
bool is_default_method() const;
// true if method needs no dynamic dispatch (final and/or no vtable entry)
bool can_be_statically_bound() const;
bool can_be_statically_bound(AccessFlags class_access_flags) const;
// returns true if the method has any backward branches.
bool has_loops() {
return access_flags().loops_flag_init() ? access_flags().has_loops() : compute_has_loops_flag();
};
bool compute_has_loops_flag();
bool has_jsrs() {
return access_flags().has_jsrs();
};
void set_has_jsrs() {
_access_flags.set_has_jsrs();
}
// returns true if the method has any monitors.
bool has_monitors() const { return is_synchronized() || access_flags().has_monitor_bytecodes(); }
bool has_monitor_bytecodes() const { return access_flags().has_monitor_bytecodes(); }
void set_has_monitor_bytecodes() { _access_flags.set_has_monitor_bytecodes(); }
// monitor matching. This returns a conservative estimate of whether the monitorenter/monitorexit bytecodes
// propererly nest in the method. It might return false, even though they actually nest properly, since the info.
// has not been computed yet.
bool guaranteed_monitor_matching() const { return access_flags().is_monitor_matching(); }
void set_guaranteed_monitor_matching() { _access_flags.set_monitor_matching(); }
// returns true if the method is an accessor function (setter/getter).
bool is_accessor() const;
// returns true if the method is an initializer (<init> or
Other Java examples (source code examples)Here is a short list of links related to this Java method.hpp source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.