Path: blob/master/src/jdk.jdi/share/classes/com/sun/jdi/ObjectReference.java
41159 views
/*1* Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.jdi;2627import java.util.List;28import java.util.Map;2930import com.sun.jdi.event.EventQueue;31import com.sun.jdi.event.VMDisconnectEvent;3233/**34* An object that currently exists in the target VM. An ObjectReference35* mirrors only the object itself and is not specific to any36* {@link Field} or {@link LocalVariable} to which it is currently37* assigned. An ObjectReference can have 0 or more references from38* field(s) and/or variable(s).39* <p>40* Any method on <code>ObjectReference</code> which directly or indirectly41* takes <code>ObjectReference</code> as a parameter may throw42* {@link VMDisconnectedException} if the target VM is disconnected and the43* {@link VMDisconnectEvent} has been or is available to be read from the44* {@link EventQueue}.45* <p>46* Any method on <code>ObjectReference</code> which directly or indirectly47* takes <code>ObjectReference</code> as a parameter may throw48* {@link VMOutOfMemoryException} if the target VM has run out of memory.49* <p>50* Any method on <code>ObjectReference</code> or which directly or indirectly51* takes <code>ObjectReference</code> as parameter may throw52* {@link ObjectCollectedException} if the mirrored object has been53* garbage collected.54*55* @author Robert Field56* @author Gordon Hirsch57* @author James McIlree58* @since 1.359*/60public interface ObjectReference extends Value {6162/**63* Gets the {@link ReferenceType} that mirrors the type64* of this object. The type may be a subclass or implementor of the65* declared type of any field or variable which currently holds it.66* For example, right after the following statement.67* <p>68* <code>Object obj = new String("Hello, world!");</code>69* <p>70* The ReferenceType of obj will mirror java.lang.String and not71* java.lang.Object.72* <p>73* The type of an object never changes, so this method will74* always return the same ReferenceType over the lifetime of the75* mirrored object.76* <p>77* The returned ReferenceType will be a {@link ClassType} or78* {@link ArrayType} and never an {@link InterfaceType}.79*80* @return the {@link ReferenceType} for this object.81*/82ReferenceType referenceType();8384/**85* Gets the value of a given instance or static field in this object.86* The Field must be valid for this ObjectReference;87* that is, it must be from88* the mirrored object's class or a superclass of that class.89*90* @param sig the field containing the requested value91* @return the {@link Value} of the instance field.92* @throws java.lang.IllegalArgumentException if the field is not valid for93* this object's class.94*/95Value getValue(Field sig);9697/**98* Gets the value of multiple instance and/or static fields in this object.99* The Fields must be valid for this ObjectReference;100* that is, they must be from101* the mirrored object's class or a superclass of that class.102*103* @param fields a list of {@link Field} objects containing the104* requested values.105* @return a Map of the requested {@link Field} objects with106* their {@link Value}.107* @throws java.lang.IllegalArgumentException if any field is not valid for108* this object's class.109*/110Map<Field,Value> getValues(List<? extends Field> fields);111112/**113* Sets the value of a given instance or static field in this object.114* The {@link Field} must be valid for this ObjectReference; that is,115* it must be from the mirrored object's class or a superclass of that class.116* If static, the field must not be final.117* <p>118* Object values must be assignment compatible with the field type119* (This implies that the field type must be loaded through the120* enclosing class's class loader). Primitive values must be121* either assignment compatible with the field type or must be122* convertible to the field type without loss of information.123* See section 5.2 of124* <cite>The Java Language Specification</cite>125* for more information on assignment126* compatibility.127*128* @param field the field containing the requested value129* @param value the new value to assign130* @throws java.lang.IllegalArgumentException if the field is not valid for131* this object's class.132* @throws InvalidTypeException if the value's type does not match133* the field's type.134* @throws ClassNotLoadedException if 'value' is not null, and the field135* type has not yet been loaded through the appropriate class loader.136* @throws VMCannotBeModifiedException if the VirtualMachine is read-only - see {@link VirtualMachine#canBeModified()}.137*/138void setValue(Field field, Value value)139throws InvalidTypeException, ClassNotLoadedException;140141/** Perform method invocation with only the invoking thread resumed */142static final int INVOKE_SINGLE_THREADED = 0x1;143/** Perform non-virtual method invocation */144static final int INVOKE_NONVIRTUAL = 0x2;145146/**147* Invokes the specified {@link Method} on this object in the148* target VM. The149* specified method can be defined in this object's class,150* in a superclass of this object's class, or in an interface151* implemented by this object. The method may be a static method152* or an instance method, but not a static initializer or constructor.153* Use {@link ClassType#newInstance} to create a new object and154* run its constructor.155* <p>156* The method invocation will occur in the specified thread.157* Method invocation can occur only if the specified thread158* has been suspended by an event which occurred in that thread.159* Method invocation is not supported160* when the target VM has been suspended through161* {@link VirtualMachine#suspend} or when the specified thread162* is suspended through {@link ThreadReference#suspend}.163* <p>164* The specified method is invoked with the arguments in the specified165* argument list. The method invocation is synchronous; this method166* does not return until the invoked method returns in the target VM.167* If the invoked method throws an exception, this method168* will throw an {@link InvocationException} which contains169* a mirror to the exception object thrown.170* <p>171* Object arguments must be assignment compatible with the argument type172* (This implies that the argument type must be loaded through the173* enclosing class's class loader). Primitive arguments must be174* either assignment compatible with the argument type or must be175* convertible to the argument type without loss of information.176* If the method being called accepts a variable number of arguments,177* then the last argument type is an array of some component type.178* The argument in the matching position can be omitted, or can be null,179* an array of the same component type, or an argument of the180* component type followed by any number of other arguments of the same181* type. If the argument is omitted, then a 0 length array of the182* component type is passed. The component type can be a primitive type.183* Autoboxing is not supported.184*185* See section 5.2 of186* <cite>The Java Language Specification</cite>187* for more information on assignment compatibility.188* <p>189* By default, the method is invoked using dynamic lookup as190* documented in section 15.12.4.4 of191* <cite>The Java Language Specification</cite>192* in particular, overriding based on the runtime type of the object193* mirrored by this {@link ObjectReference} will occur. This194* behavior can be changed by specifying the195* {@link #INVOKE_NONVIRTUAL} bit flag in the <code>options</code>196* argument. If this flag is set, the specified method is invoked197* whether or not it is overridden for this object's runtime type.198* The method, in this case, must have an implementation, either in a class199* or an interface. This option is useful for performing method invocations200* like those done with the <code>super</code> keyword in the Java programming201* language.202* <p>203* By default, all threads in the target VM are resumed while204* the method is being invoked if they were previously205* suspended by an event or by {@link VirtualMachine#suspend} or206* {@link ThreadReference#suspend}. This is done to prevent the deadlocks207* that will occur if any of the threads own monitors208* that will be needed by the invoked method.209* Note, however, that this implicit resume acts exactly like210* {@link ThreadReference#resume}, so if the thread's suspend211* count is greater than 1, it will remain in a suspended state212* during the invocation and thus a deadlock could still occur.213* By default, when the invocation completes,214* all threads in the target VM are suspended, regardless their state215* before the invocation.216* It is possible that217* breakpoints or other events might occur during the invocation.218* This can cause deadlocks as described above. It can also cause a deadlock219* if invokeMethod is called from the client's event handler thread. In this220* case, this thread will be waiting for the invokeMethod to complete and221* won't read the EventSet that comes in for the new event. If this222* new EventSet is SUSPEND_ALL, then a deadlock will occur because no223* one will resume the EventSet. To avoid this, all EventRequests should224* be disabled before doing the invokeMethod, or the invokeMethod should225* not be done from the client's event handler thread.226* <p>227* The resumption of other threads during the invocation can be prevented228* by specifying the {@link #INVOKE_SINGLE_THREADED}229* bit flag in the <code>options</code> argument; however,230* there is no protection against or recovery from the deadlocks231* described above, so this option should be used with great caution.232* Only the specified thread will be resumed (as described for all233* threads above). Upon completion of a single threaded invoke, the invoking thread234* will be suspended once again. Note that any threads started during235* the single threaded invocation will not be suspended when the236* invocation completes.237* <p>238* If the target VM is disconnected during the invoke (for example, through239* {@link VirtualMachine#dispose}) the method invocation continues.240*241* @param thread the thread in which to invoke.242* @param method the {@link Method} to invoke.243* @param arguments the list of {@link Value} arguments bound to the244* invoked method. Values from the list are assigned to arguments245* in the order they appear in the method signature.246* @param options the integer bit flag options.247* @return a {@link Value} mirror of the invoked method's return value.248* @throws java.lang.IllegalArgumentException if the method is not249* a member of this object's class, if the size of the argument list250* does not match the number of declared arguments for the method,251* if the method is a constructor or static initializer, or252* if {@link #INVOKE_NONVIRTUAL} is specified and the method is253* abstract.254* @throws ClassNotLoadedException if any argument type has not yet been loaded255* through the appropriate class loader.256* @throws IncompatibleThreadStateException if the specified thread has not257* been suspended by an event.258* @throws InvocationException if the method invocation resulted in259* an exception in the target VM.260* @throws InvalidTypeException If the arguments do not meet this requirement --261* Object arguments must be assignment compatible with the argument262* type. This implies that the argument type must be263* loaded through the enclosing class's class loader.264* Primitive arguments must be either assignment compatible with the265* argument type or must be convertible to the argument type without loss266* of information. See JLS section 5.2 for more information on assignment267* compatibility.268* @throws VMCannotBeModifiedException if the VirtualMachine is read-only - see {@link VirtualMachine#canBeModified()}.269*/270Value invokeMethod(ThreadReference thread, Method method,271List<? extends Value> arguments, int options)272throws InvalidTypeException,273ClassNotLoadedException,274IncompatibleThreadStateException,275InvocationException;276277/**278* Prevents garbage collection for this object. By default all279* {@link ObjectReference} values returned by JDI may be collected280* at any time the target VM is running. A call to this method281* guarantees that the object will not be collected.282* {@link #enableCollection} can be used to allow collection once283* again.284* <p>285* Calls to this method are counted. Every call to this method286* requires a corresponding call to {@link #enableCollection} before287* garbage collection is re-enabled.288* <p>289* Note that while the target VM is suspended, no garbage collection290* will occur because all threads are suspended. The typical291* examination of variables, fields, and arrays during the suspension292* is safe without explicitly disabling garbage collection.293* <p>294* This method should be used sparingly, as it alters the295* pattern of garbage collection in the target VM and,296* consequently, may result in application behavior under the297* debugger that differs from its non-debugged behavior.298* @throws VMCannotBeModifiedException if the VirtualMachine is read-only299* -see {@link VirtualMachine#canBeModified()}.300*/301void disableCollection();302303/**304* Permits garbage collection for this object. By default all305* {@link ObjectReference} values returned by JDI may be collected306* at any time the target VM is running. A call to this method307* is necessary only if garbage collection was previously disabled308* with {@link #disableCollection}.309* @throws VMCannotBeModifiedException if the VirtualMachine is read-only310* -see {@link VirtualMachine#canBeModified()}.311*/312void enableCollection();313314/**315* Determines if this object has been garbage collected in the target316* VM.317*318* @return <code>true</code> if this {@link ObjectReference} has been collected;319* <code>false</code> otherwise.320* @throws VMCannotBeModifiedException if the VirtualMachine is read-only321* -see {@link VirtualMachine#canBeModified()}.322*/323boolean isCollected();324325/**326* Returns a unique identifier for this ObjectReference.327* It is guaranteed to be unique among all328* ObjectReferences from the same VM that have not yet been disposed.329* The guarantee applies as long330* as this ObjectReference has not yet been disposed.331*332* @return a long unique ID333*/334long uniqueID();335336/**337* Returns a List containing a {@link ThreadReference} for338* each thread currently waiting for this object's monitor.339* See {@link ThreadReference#currentContendedMonitor} for340* information about when a thread is considered to be waiting341* for a monitor.342* <p>343* Not all target VMs support this operation. See344* VirtualMachine#canGetMonitorInfo to determine if the345* operation is supported.346*347* @return a List of {@link ThreadReference} objects. The list348* has zero length if no threads are waiting for the monitor.349* @throws java.lang.UnsupportedOperationException if the350* target VM does not support this operation.351* @throws IncompatibleThreadStateException if any352* waiting thread is not suspended353* in the target VM354*/355List<ThreadReference> waitingThreads()356throws IncompatibleThreadStateException;357358/**359* Returns an {@link ThreadReference} for the thread, if any,360* which currently owns this object's monitor.361* See {@link ThreadReference#ownedMonitors} for a definition362* of ownership.363* <p>364* Not all target VMs support this operation. See365* VirtualMachine#canGetMonitorInfo to determine if the366* operation is supported.367*368* @return the {@link ThreadReference} which currently owns the369* monitor, or null if it is unowned.370*371* @throws java.lang.UnsupportedOperationException if the372* target VM does not support this operation.373* @throws IncompatibleThreadStateException if the owning thread is374* not suspended in the target VM375*/376ThreadReference owningThread() throws IncompatibleThreadStateException;377378/**379* Returns the number times this object's monitor has been380* entered by the current owning thread.381* See {@link ThreadReference#ownedMonitors} for a definition382* of ownership.383* <p>384* Not all target VMs support this operation. See385* VirtualMachine#canGetMonitorInfo to determine if the386* operation is supported.387*388* @see #owningThread389* @return the integer count of the number of entries.390*391* @throws java.lang.UnsupportedOperationException if the392* target VM does not support this operation.393* @throws IncompatibleThreadStateException if the owning thread is394* not suspended in the target VM395*/396int entryCount() throws IncompatibleThreadStateException;397398/**399* Returns objects that directly reference this object.400* Only objects that are reachable for the purposes of garbage collection401* are returned. Note that an object can also be referenced in other ways,402* such as from a local variable in a stack frame, or from a JNI global403* reference. Such non-object referrers are not returned by this method.404* <p>405* Not all target virtual machines support this operation.406* Use {@link VirtualMachine#canGetInstanceInfo()}407* to determine if the operation is supported.408*409* @see VirtualMachine#instanceCounts(List)410* @see ReferenceType#instances(long)411412* @param maxReferrers The maximum number of referring objects to return.413* Must be non-negative. If zero, all referring414* objects are returned.415* @return a of List of {@link ObjectReference} objects. If there are416* no objects that reference this object, a zero-length list is returned..417* @throws java.lang.UnsupportedOperationException if418* the target virtual machine does not support this419* operation - see420* {@link VirtualMachine#canGetInstanceInfo() canGetInstanceInfo()}421* @throws java.lang.IllegalArgumentException if maxReferrers is less422* than zero.423* @since 1.6424*/425List<ObjectReference> referringObjects(long maxReferrers);426427/**428* Compares the specified Object with this ObjectReference for equality.429*430* @return true if the Object is an ObjectReference, if the431* ObjectReferences belong to the same VM, and if applying the432* "==" operator on the mirrored objects in that VM evaluates to true.433*/434boolean equals(Object obj);435436/**437* Returns the hash code value for this ObjectReference.438*439* @return the integer hash code440*/441int hashCode();442}443444445