1 /*
   2  * Copyright (c) 1998, 2013, 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 {@link InvalidTypeException} if any argument in the
 221      * argument list is not assignable to the corresponding method argument
 222      * type.
 223      * @throws ClassNotLoadedException if any argument type has not yet been loaded
 224      * through the appropriate class loader.
 225      * @throws IncompatibleThreadStateException if the specified thread has not
 226      * been suspended by an event.
 227      * @throws InvocationException if the method invocation resulted in
 228      * an exception in the target VM.
 229      * @throws InvalidTypeException If the arguments do not meet this requirement --
 230      *         Object arguments must be assignment compatible with the argument
 231      *         type.  This implies that the argument type must be
 232      *         loaded through the enclosing class' class loader.
 233      *         Primitive arguments must be either assignment compatible with the
 234      *         argument type or must be convertible to the argument type without loss
 235      *         of information. See JLS section 5.2 for more information on assignment
 236      *         compatibility.
 237      * @throws VMCannotBeModifiedException if the VirtualMachine is read-only - see {@link VirtualMachine#canBeModified()}.
 238      */
 239     Value invokeMethod(ThreadReference thread, Method method,
 240                        List<? extends Value> arguments, int options)
 241                                    throws InvalidTypeException,
 242                                           ClassNotLoadedException,
 243                                           IncompatibleThreadStateException,
 244                                           InvocationException;
 245 
 246     /**
 247      * Constructs a new instance of this type, using
 248      * the given constructor {@link Method} in the
 249      * target VM. The
 250      * specified constructor must be defined in this class.
 251      * <p>
 252      * Instance creation will occur in the specified thread.
 253      * Instance creation can occur only if the specified thread
 254      * has been suspended by an event which occurred in that thread.
 255      * Instance creation is not supported
 256      * when the target VM has been suspended through
 257      * {@link VirtualMachine#suspend} or when the specified thread
 258      * is suspended through {@link ThreadReference#suspend}.
 259      * <p>
 260      * The specified constructor is invoked with the arguments in the specified
 261      * argument list.  The invocation is synchronous; this method
 262      * does not return until the constructor returns in the target VM.
 263      * If the invoked method throws an
 264      * exception, this method will throw an {@link InvocationException}
 265      * which contains a mirror to the exception object thrown.
 266      * <p>
 267      * Object arguments must be assignment compatible with the argument type
 268      * (This implies that the argument type must be loaded through the
 269      * enclosing class' class loader). Primitive arguments must be
 270      * either assignment compatible with the argument type or must be
 271      * convertible to the argument type without loss of information.
 272      * If the method being called accepts a variable number of arguments,
 273      * then the last argument type is an array of some component type.
 274      * The argument in the matching position can be omitted, or can be null,
 275      * an array of the same component type, or an argument of the
 276      * component type, followed by any number of other arguments of the same
 277      * type. If the argument is omitted, then a 0 length array of the
 278      * component type is passed.  The component type can be a primitive type.
 279      * Autoboxing is not supported.
 280      *
 281      * See section 5.2 of
 282      * <cite>The Java&trade; Language Specification</cite>
 283      * for more information on assignment compatibility.
 284      * <p>
 285      * By default, all threads in the target VM are resumed while
 286      * the method is being invoked if they were previously
 287      * suspended by an event or by {@link VirtualMachine#suspend} or
 288      * {@link ThreadReference#suspend}. This is done to prevent the deadlocks
 289      * that will occur if any of the threads own monitors
 290      * that will be needed by the invoked method. It is possible that
 291      * breakpoints or other events might occur during the invocation.
 292      * Note, however, that this implicit resume acts exactly like
 293      * {@link ThreadReference#resume}, so if the thread's suspend
 294      * count is greater than 1, it will remain in a suspended state
 295      * during the invocation. By default, when the invocation completes,
 296      * all threads in the target VM are suspended, regardless their state
 297      * before the invocation.
 298      * <p>
 299      * The resumption of other threads during the invocation can be prevented
 300      * by specifying the {@link #INVOKE_SINGLE_THREADED}
 301      * bit flag in the <code>options</code> argument; however,
 302      * there is no protection against or recovery from the deadlocks
 303      * described above, so this option should be used with great caution.
 304      * Only the specified thread will be resumed (as described for all
 305      * threads above). Upon completion of a single threaded invoke, the invoking thread
 306      * will be suspended once again. Note that any threads started during
 307      * the single threaded invocation will not be suspended when the
 308      * invocation completes.
 309      * <p>
 310      * If the target VM is disconnected during the invoke (for example, through
 311      * {@link VirtualMachine#dispose}) the method invocation continues.
 312      *
 313      * @param thread the thread in which to invoke.
 314      * @param method the constructor {@link Method} to invoke.
 315      * @param arguments the list of {@link Value} arguments bound to the
 316      * invoked constructor. Values from the list are assigned to arguments
 317      * in the order they appear in the constructor signature.
 318      * @param options the integer bit flag options.
 319      * @return an {@link ObjectReference} mirror of the newly created
 320      * object.
 321      * @throws java.lang.IllegalArgumentException if the method is not
 322      * a member of this class, if the size of the argument list
 323      * does not match the number of declared arguments for the constructor,
 324      * or if the method is not a constructor.
 325      * @throws {@link InvalidTypeException} if any argument in the
 326      * argument list is not assignable to the corresponding method argument
 327      * type.
 328      * @throws ClassNotLoadedException if any argument type has not yet been loaded
 329      * through the appropriate class loader.
 330      * @throws IncompatibleThreadStateException if the specified thread has not
 331      * been suspended by an event.
 332      * @throws InvocationException if the method invocation resulted in
 333      * an exception in the target VM.
 334      * @throws InvalidTypeException If the arguments do not meet this requirement --
 335      *         Object arguments must be assignment compatible with the argument
 336      *         type.  This implies that the argument type must be
 337      *         loaded through the enclosing class' class loader.
 338      *         Primitive arguments must be either assignment compatible with the
 339      *         argument type or must be convertible to the argument type without loss
 340      *         of information. See JLS section 5.2 for more information on assignment
 341      *         compatibility.
 342      * @throws VMCannotBeModifiedException if the VirtualMachine is read-only
 343      * - see {@link VirtualMachine#canBeModified()}.
 344      */
 345     ObjectReference newInstance(ThreadReference thread, Method method,
 346                                 List<? extends Value> arguments, int options)
 347                                    throws InvalidTypeException,
 348                                           ClassNotLoadedException,
 349                                           IncompatibleThreadStateException,
 350                                           InvocationException;
 351 
 352     /**
 353      * Returns a the single non-abstract {@link Method} visible from
 354      * this class that has the given name and signature.
 355      * See {@link ReferenceType#methodsByName(java.lang.String, java.lang.String)}
 356      * for information on signature format.
 357      * <p>
 358      * The returned method (if non-null) is a component of
 359      * {@link ClassType}.
 360      *
 361      * @see ReferenceType#visibleMethods
 362      * @see ReferenceType#methodsByName(java.lang.String name)
 363      * @see ReferenceType#methodsByName(java.lang.String name, java.lang.String signature)
 364      * @param name the name of the method to find.
 365      * @param signature the signature of the method to find
 366      * @return the {@link Method} that matches the given
 367      * name and signature or <code>null</code> if there is no match.
 368      * @throws ClassNotPreparedException if methods are not yet available
 369      * because the class has not yet been prepared.
 370      */
 371     Method concreteMethodByName(String name, String signature);
 372 }