1 /*
   2  * Copyright (c) 1998, 2017, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.jdi;
  27 
  28 import java.util.List;
  29 
  30 /**
  31  * A mirror of a class in the target VM. A ClassType is a refinement
  32  * of {@link ReferenceType} that applies to true classes in the JLS
  33  * sense of the definition (not an interface, not an array type). Any
  34  * {@link ObjectReference} that mirrors an instance of such a class
  35  * will have a ClassType as its type.
  36  *
  37  * @see ObjectReference
  38  *
  39  * @author Robert Field
  40  * @author Gordon Hirsch
  41  * @author James McIlree
  42  * @since  1.3
  43  */
  44 public interface ClassType extends ReferenceType {
  45     /**
  46      * Gets the superclass of this class.
  47      *
  48      * @return a {@link ClassType} that mirrors the superclass
  49      * of this class in the target VM. If no such class exists,
  50      * returns null
  51      */
  52     ClassType superclass();
  53 
  54     /**
  55      * Gets the interfaces directly implemented by this class.
  56      * Only the interfaces that are declared with the "implements"
  57      * keyword in this class are included.
  58      *
  59      * @return a List of {@link InterfaceType} objects each mirroring
  60      * a direct interface this ClassType in the target VM.
  61      * If none exist, returns a zero length List.
  62      * @throws ClassNotPreparedException if this class not yet been
  63      * prepared.
  64      */
  65     List<InterfaceType> interfaces();
  66 
  67     /**
  68      * Gets the interfaces directly and indirectly implemented
  69      * by this class. Interfaces returned by {@link ClassType#interfaces}
  70      * are returned as well all superinterfaces.
  71      *
  72      * @return a List of {@link InterfaceType} objects each mirroring
  73      * an interface of this ClassType in the target VM.
  74      * If none exist, returns a zero length List.
  75      * @throws ClassNotPreparedException if this class not yet been
  76      * prepared.
  77      */
  78     List<InterfaceType> allInterfaces();
  79 
  80     /**
  81      * Gets the currently loaded, direct subclasses of this class.
  82      * No ordering of this list is guaranteed.
  83      *
  84      * @return a List of {@link ClassType} objects each mirroring a loaded
  85      * subclass of this class in the target VM. If no such classes
  86      * exist, this method returns a zero-length list.
  87      */
  88     List<ClassType> subclasses();
  89 
  90     /**
  91      * Determine if this class was declared as an enum.
  92      * @return <code>true</code> if this class was declared as an enum; false
  93      * otherwise.
  94      */
  95     boolean isEnum();
  96 
  97     /**
  98      * Assigns a value to a static field.
  99      * The {@link Field} must be valid for this ClassType; that is,
 100      * it must be from the mirrored object's class or a superclass of that class.
 101      * The field must not be final.
 102      * <p>
 103      * Object values must be assignment compatible with the field type
 104      * (This implies that the field type must be loaded through the
 105      * enclosing class' class loader). Primitive values must be
 106      * either assignment compatible with the field type or must be
 107      * convertible to the field type without loss of information.
 108      * See JLS section 5.2 for more information on assignment
 109      * compatibility.
 110      *
 111      * @param field the field to set.
 112      * @param value the value to be assigned.
 113      * @throws java.lang.IllegalArgumentException if the field is
 114      * not static, the field is final, or the field does not exist
 115      * in this class.
 116      * @throws ClassNotLoadedException if the field type has not yet been loaded
 117      * through the appropriate class loader.
 118      * @throws InvalidTypeException if the value's type does not match
 119      * the field's declared type.
 120      * @throws VMCannotBeModifiedException if the VirtualMachine is read-only - see {@link VirtualMachine#canBeModified()}.
 121      */
 122     void setValue(Field field, Value value)
 123         throws InvalidTypeException, ClassNotLoadedException;
 124 
 125     /** Perform method invocation with only the invoking thread resumed */
 126     static final int INVOKE_SINGLE_THREADED = 0x1;
 127 
 128     /**
 129      * Invokes the specified static {@link Method} in the
 130      * target VM. The
 131      * specified method can be defined in this class,
 132      * or in a superclass.
 133      * The method must be a static method
 134      * but not a static initializer.
 135      * Use {@link ClassType#newInstance} to create a new object and
 136      * run its constructor.
 137      * <p>
 138      * The method invocation will occur in the specified thread.
 139      * Method invocation can occur only if the specified thread
 140      * has been suspended by an event which occurred in that thread.
 141      * Method invocation is not supported
 142      * when the target VM has been suspended through
 143      * {@link VirtualMachine#suspend} or when the specified thread
 144      * is suspended through {@link ThreadReference#suspend}.
 145      * <p>
 146      * The specified method is invoked with the arguments in the specified
 147      * argument list.  The method invocation is synchronous; this method
 148      * does not return until the invoked method returns in the target VM.
 149      * If the invoked method throws an exception, this method will throw
 150      * an {@link InvocationException} which contains a mirror to the exception
 151      * object thrown.
 152      * <p>
 153      * Object arguments must be assignment compatible with the argument type
 154      * (This implies that the argument type must be loaded through the
 155      * enclosing class' class loader). Primitive arguments must be
 156      * either assignment compatible with the argument type or must be
 157      * convertible to the argument type without loss of information.
 158      * If the method being called accepts a variable number of arguments,
 159      * then the last argument type is an array of some component type.
 160      * The argument in the matching position can be omitted, or can be null,
 161      * an array of the same component type, or an argument of the
 162      * component type followed by any number of other arguments of the same
 163      * type. If the argument is omitted, then a 0 length array of the
 164      * component type is passed.  The component type can be a primitive type.
 165      * Autoboxing is not supported.
 166      *
 167      * See Section 5.2 of
 168      * <cite>The Java&trade; Language Specification</cite>
 169      * for more information on assignment compatibility.
 170      * <p>
 171      * By default, all threads in the target VM are resumed while
 172      * the method is being invoked if they were previously
 173      * suspended by an event or by {@link VirtualMachine#suspend} or
 174      * {@link ThreadReference#suspend}. This is done to prevent the deadlocks
 175      * that will occur if any of the threads own monitors
 176      * that will be needed by the invoked method.
 177      * Note, however, that this implicit resume acts exactly like
 178      * {@link ThreadReference#resume}, so if the thread's suspend
 179      * count is greater than 1, it will remain in a suspended state
 180      * during the invocation and thus a deadlock could still occur.
 181      * By default, when the invocation completes,
 182      * all threads in the target VM are suspended, regardless their state
 183      * before the invocation.
 184      * It is possible that
 185      * breakpoints or other events might occur during the invocation.
 186      * This can cause deadlocks as described above. It can also cause a deadlock
 187      * if invokeMethod is called from the client's event handler thread.  In this
 188      * case, this thread will be waiting for the invokeMethod to complete and
 189      * won't read the EventSet that comes in for the new event.  If this
 190      * new EventSet is SUSPEND_ALL, then a deadlock will occur because no
 191      * one will resume the EventSet.  To avoid this, all EventRequests should
 192      * be disabled before doing the invokeMethod, or the invokeMethod should
 193      * not be done from the client's event handler thread.
 194      * <p>
 195      * The resumption of other threads during the invocation can be prevented
 196      * by specifying the {@link #INVOKE_SINGLE_THREADED}
 197      * bit flag in the <code>options</code> argument; however,
 198      * there is no protection against or recovery from the deadlocks
 199      * described above, so this option should be used with great caution.
 200      * Only the specified thread will be resumed (as described for all
 201      * threads above). Upon completion of a single threaded invoke, the invoking thread
 202      * will be suspended once again. Note that any threads started during
 203      * the single threaded invocation will not be suspended when the
 204      * invocation completes.
 205      * <p>
 206      * If the target VM is disconnected during the invoke (for example, through
 207      * {@link VirtualMachine#dispose}) the method invocation continues.
 208      *
 209      * @param thread the thread in which to invoke.
 210      * @param method the {@link Method} to invoke.
 211      * @param arguments the list of {@link Value} arguments bound to the
 212      * invoked method. Values from the list are assigned to arguments
 213      * in the order they appear in the method signature.
 214      * @param options the integer bit flag options.
 215      * @return a {@link Value} mirror of the invoked method's return value.
 216      * @throws java.lang.IllegalArgumentException if the method is not
 217      * a member of this class or a superclass, if the size of the argument list
 218      * does not match the number of declared arguments for the method, or
 219      * if the method is an initializer, constructor or static intializer.
 220      * @throws ClassNotLoadedException if any argument type has not yet been loaded
 221      * through the appropriate class loader.
 222      * @throws IncompatibleThreadStateException if the specified thread has not
 223      * been suspended by an event.
 224      * @throws InvocationException if the method invocation resulted in
 225      * an exception in the target VM.
 226      * @throws InvalidTypeException If the arguments do not meet this requirement --
 227      *         Object arguments must be assignment compatible with the argument
 228      *         type.  This implies that the argument type must be
 229      *         loaded through the enclosing class' class loader.
 230      *         Primitive arguments must be either assignment compatible with the
 231      *         argument type or must be convertible to the argument type without loss
 232      *         of information. See JLS section 5.2 for more information on assignment
 233      *         compatibility.
 234      * @throws VMCannotBeModifiedException if the VirtualMachine is read-only - see {@link VirtualMachine#canBeModified()}.
 235      */
 236     Value invokeMethod(ThreadReference thread, Method method,
 237                        List<? extends Value> arguments, int options)
 238                                    throws InvalidTypeException,
 239                                           ClassNotLoadedException,
 240                                           IncompatibleThreadStateException,
 241                                           InvocationException;
 242 
 243     /**
 244      * Constructs a new instance of this type, using
 245      * the given constructor {@link Method} in the
 246      * target VM. The
 247      * specified constructor must be defined in this class.
 248      * <p>
 249      * Instance creation will occur in the specified thread.
 250      * Instance creation can occur only if the specified thread
 251      * has been suspended by an event which occurred in that thread.
 252      * Instance creation is not supported
 253      * when the target VM has been suspended through
 254      * {@link VirtualMachine#suspend} or when the specified thread
 255      * is suspended through {@link ThreadReference#suspend}.
 256      * <p>
 257      * The specified constructor is invoked with the arguments in the specified
 258      * argument list.  The invocation is synchronous; this method
 259      * does not return until the constructor returns in the target VM.
 260      * If the invoked method throws an
 261      * exception, this method will throw an {@link InvocationException}
 262      * which contains a mirror to the exception object thrown.
 263      * <p>
 264      * Object arguments must be assignment compatible with the argument type
 265      * (This implies that the argument type must be loaded through the
 266      * enclosing class' class loader). Primitive arguments must be
 267      * either assignment compatible with the argument type or must be
 268      * convertible to the argument type without loss of information.
 269      * If the method being called accepts a variable number of arguments,
 270      * then the last argument type is an array of some component type.
 271      * The argument in the matching position can be omitted, or can be null,
 272      * an array of the same component type, or an argument of the
 273      * component type, followed by any number of other arguments of the same
 274      * type. If the argument is omitted, then a 0 length array of the
 275      * component type is passed.  The component type can be a primitive type.
 276      * Autoboxing is not supported.
 277      *
 278      * See section 5.2 of
 279      * <cite>The Java&trade; Language Specification</cite>
 280      * for more information on assignment compatibility.
 281      * <p>
 282      * By default, all threads in the target VM are resumed while
 283      * the method is being invoked if they were previously
 284      * suspended by an event or by {@link VirtualMachine#suspend} or
 285      * {@link ThreadReference#suspend}. This is done to prevent the deadlocks
 286      * that will occur if any of the threads own monitors
 287      * that will be needed by the invoked method. It is possible that
 288      * breakpoints or other events might occur during the invocation.
 289      * Note, however, that this implicit resume acts exactly like
 290      * {@link ThreadReference#resume}, so if the thread's suspend
 291      * count is greater than 1, it will remain in a suspended state
 292      * during the invocation. By default, when the invocation completes,
 293      * all threads in the target VM are suspended, regardless their state
 294      * before the invocation.
 295      * <p>
 296      * The resumption of other threads during the invocation can be prevented
 297      * by specifying the {@link #INVOKE_SINGLE_THREADED}
 298      * bit flag in the <code>options</code> argument; however,
 299      * there is no protection against or recovery from the deadlocks
 300      * described above, so this option should be used with great caution.
 301      * Only the specified thread will be resumed (as described for all
 302      * threads above). Upon completion of a single threaded invoke, the invoking thread
 303      * will be suspended once again. Note that any threads started during
 304      * the single threaded invocation will not be suspended when the
 305      * invocation completes.
 306      * <p>
 307      * If the target VM is disconnected during the invoke (for example, through
 308      * {@link VirtualMachine#dispose}) the method invocation continues.
 309      *
 310      * @param thread the thread in which to invoke.
 311      * @param method the constructor {@link Method} to invoke.
 312      * @param arguments the list of {@link Value} arguments bound to the
 313      * invoked constructor. Values from the list are assigned to arguments
 314      * in the order they appear in the constructor signature.
 315      * @param options the integer bit flag options.
 316      * @return an {@link ObjectReference} mirror of the newly created
 317      * object.
 318      * @throws java.lang.IllegalArgumentException if the method is not
 319      * a member of this class, if the size of the argument list
 320      * does not match the number of declared arguments for the constructor,
 321      * or if the method is not a constructor.
 322      * @throws ClassNotLoadedException if any argument type has not yet been loaded
 323      * through the appropriate class loader.
 324      * @throws IncompatibleThreadStateException if the specified thread has not
 325      * been suspended by an event.
 326      * @throws InvocationException if the method invocation resulted in
 327      * an exception in the target VM.
 328      * @throws InvalidTypeException If the arguments do not meet this requirement --
 329      *         Object arguments must be assignment compatible with the argument
 330      *         type.  This implies that the argument type must be
 331      *         loaded through the enclosing class' class loader.
 332      *         Primitive arguments must be either assignment compatible with the
 333      *         argument type or must be convertible to the argument type without loss
 334      *         of information. See JLS section 5.2 for more information on assignment
 335      *         compatibility.
 336      * @throws VMCannotBeModifiedException if the VirtualMachine is read-only
 337      * - see {@link VirtualMachine#canBeModified()}.
 338      */
 339     ObjectReference newInstance(ThreadReference thread, Method method,
 340                                 List<? extends Value> arguments, int options)
 341                                    throws InvalidTypeException,
 342                                           ClassNotLoadedException,
 343                                           IncompatibleThreadStateException,
 344                                           InvocationException;
 345 
 346     /**
 347      * Returns a the single non-abstract {@link Method} visible from
 348      * this class that has the given name and signature.
 349      * See {@link ReferenceType#methodsByName(java.lang.String, java.lang.String)}
 350      * for information on signature format.
 351      * <p>
 352      * The returned method (if non-null) is a component of
 353      * {@link ClassType}.
 354      *
 355      * @see ReferenceType#visibleMethods
 356      * @see ReferenceType#methodsByName(java.lang.String name)
 357      * @see ReferenceType#methodsByName(java.lang.String name, java.lang.String signature)
 358      * @param name the name of the method to find.
 359      * @param signature the signature of the method to find
 360      * @return the {@link Method} that matches the given
 361      * name and signature or <code>null</code> if there is no match.
 362      * @throws ClassNotPreparedException if methods are not yet available
 363      * because the class has not yet been prepared.
 364      */
 365     Method concreteMethodByName(String name, String signature);
 366 }