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 static or instance method in the target VM. See {@link TypeComponent} 32 * for general information about Field and Method mirrors. 33 * 34 * @see ObjectReference 35 * @see ReferenceType 36 * 37 * @author Robert Field 38 * @author Gordon Hirsch 39 * @author James McIlree 40 * @since 1.3 41 */ 42 @jdk.Exported 43 public interface Method extends TypeComponent, Locatable, Comparable<Method> { 44 45 /** 46 * Returns a text representation of the return type, 47 * as specified in the declaration of this method. 48 * <P> 49 * This type name is always available even if 50 * the type has not yet been created or loaded. 51 * 52 * @return a String containing the return type name. 53 */ returnTypeName()54 String returnTypeName(); 55 56 /** 57 * Returns the return type, 58 * as specified in the declaration of this method. 59 * <P> 60 * Note: if the return type of this method is a reference type (class, 61 * interface, or array) and it has not been created or loaded 62 * by the declaring type's class loader - that is, 63 * {@link TypeComponent#declaringType <CODE>declaringType()</CODE>} 64 * <CODE>.classLoader()</CODE>, 65 * then ClassNotLoadedException will be thrown. 66 * Also, a reference type may have been loaded but not yet prepared, 67 * in which case the type will be returned 68 * but attempts to perform some operations on the returned type 69 * (e.g. {@link ReferenceType#fields() fields()}) will throw 70 * a {@link ClassNotPreparedException}. 71 * Use {@link ReferenceType#isPrepared()} to determine if 72 * a reference type is prepared. 73 * 74 * @see Type 75 * @see Field#type() Field.type() - for usage examples 76 * @return the return {@link Type} of this method. 77 * @throws ClassNotLoadedException if the type has not yet been 78 * created or loaded 79 * through the appropriate class loader. 80 */ returnType()81 Type returnType() throws ClassNotLoadedException; 82 83 /** 84 * Returns a list containing a text representation of the type 85 * of each formal parameter of this method. 86 * <P> 87 * This list is always available even if 88 * the types have not yet been created or loaded. 89 * 90 * @return a {@link java.util.List List} of {@link String}, 91 * one List element for each parameter of this method. 92 * Each element represents the type of a formal parameter 93 * as specified at compile-time. 94 * If the formal parameter was declared with an ellipsis, then 95 * it is represented as an array of the type before the ellipsis. 96 * 97 */ argumentTypeNames()98 List<String> argumentTypeNames(); 99 100 /** 101 * Returns a list containing the type 102 * of each formal parameter of this method. 103 * <P> 104 * Note: if there is any parameter whose type 105 * is a reference type (class, interface, or array) 106 * and it has not been created or loaded 107 * by the declaring type's class loader - that is, 108 * {@link TypeComponent#declaringType <CODE>declaringType()</CODE>} 109 * <CODE>.classLoader()</CODE>, 110 * then ClassNotLoadedException will be thrown. 111 * Also, a reference type may have been loaded but not yet prepared, 112 * in which case the list will be returned 113 * but attempts to perform some operations on the type 114 * (e.g. {@link ReferenceType#fields() fields()}) will throw 115 * a {@link ClassNotPreparedException}. 116 * Use {@link ReferenceType#isPrepared()} to determine if 117 * a reference type is prepared. 118 * 119 * @see Type 120 * @return return a {@link java.util.List List} of {@link Type}, 121 * one List element for each parameter of this method. 122 * Each element represents the type of a formal parameter 123 * as specified at compile-time. 124 * If the formal parameter was declared with an ellipsis, then 125 * it is represented as an array of the type before the ellipsis. 126 * 127 * @throws ClassNotLoadedException if the type has not yet been loaded 128 * through the appropriate class loader. 129 */ argumentTypes()130 List<Type> argumentTypes() throws ClassNotLoadedException; 131 132 /** 133 * Determine if this method is abstract. 134 * 135 * @return <code>true</code> if the method is declared abstract; 136 * false otherwise. 137 */ isAbstract()138 boolean isAbstract(); 139 140 /** 141 * Determine if this method is a default method 142 * 143 * @return <code>true</code> if the method is declared default; 144 * false otherwise 145 * 146 * @since 1.8 147 */ isDefault()148 default boolean isDefault() { 149 throw new UnsupportedOperationException(); 150 } 151 152 /** 153 * Determine if this method is synchronized. 154 * 155 * @return <code>true</code> if the method is declared synchronized; 156 * false otherwise. 157 */ isSynchronized()158 boolean isSynchronized(); 159 160 /** 161 * Determine if this method is native. 162 * 163 * @return <code>true</code> if the method is declared native; 164 * false otherwise. 165 */ isNative()166 boolean isNative(); 167 168 /** 169 * Determine if this method accepts a variable number of arguments. 170 * 171 * @return <code>true</code> if the method accepts a variable number 172 * of arguments, false otherwise. 173 * 174 * @since 1.5 175 */ isVarArgs()176 boolean isVarArgs(); 177 178 /** 179 * Determine if this method is a bridge method. Bridge 180 * methods are defined in 181 * <cite>The Java™ Language Specification</cite>. 182 * 183 * @return <code>true</code> if the method is a bridge method, 184 * false otherwise. 185 * 186 * @since 1.5 187 */ isBridge()188 boolean isBridge(); 189 190 /** 191 * Determine if this method is a constructor. 192 * 193 * @return <code>true</code> if the method is a constructor; 194 * false otherwise. 195 */ isConstructor()196 boolean isConstructor(); 197 198 /** 199 * Determine if this method is a static initializer. 200 * 201 * @return <code>true</code> if the method is a static initializer; 202 * false otherwise. 203 */ isStaticInitializer()204 boolean isStaticInitializer(); 205 206 /** 207 * Determine if this method is obsolete. 208 * 209 * @return <code>true</code> if this method has been made obsolete by a 210 * {@link VirtualMachine#redefineClasses} operation. 211 * 212 * @since 1.4 213 */ isObsolete()214 boolean isObsolete(); 215 216 /** 217 * Returns a list containing a {@link Location} object for 218 * each executable source line in this method. 219 * <P> 220 * This method is equivalent to 221 * <code>allLineLocations(vm.getDefaultStratum(),null)</code> - 222 * see {@link #allLineLocations(String,String)} 223 * for more information. 224 * 225 * @return a List of all source line {@link Location} objects. 226 * 227 * @throws AbsentInformationException if there is no line 228 * number information for this (non-native, non-abstract) 229 * method. 230 */ allLineLocations()231 List<Location> allLineLocations() throws AbsentInformationException; 232 233 /** 234 * Returns a list containing a {@link Location} object for 235 * each executable source line in this method. 236 * <P> 237 * Each location maps a source line to a range of code 238 * indices. 239 * The beginning of the range can be determined through 240 * {@link Location#codeIndex}. 241 * The returned list is ordered by code index 242 * (from low to high). 243 * <P> 244 * The returned list may contain multiple locations for a 245 * particular line number, if the compiler and/or VM has 246 * mapped that line to two or more disjoint code index ranges. 247 * <P> 248 * If the method is native or abstract, an empty list is 249 * returned. 250 * <P> 251 * Returned list is for the specified <i>stratum</i> 252 * (see {@link Location} for a description of strata). 253 * 254 * @param stratum The stratum to retrieve information from 255 * or <code>null</code> for the {@link ReferenceType#defaultStratum()} 256 * 257 * @param sourceName Return locations only within this 258 * source file or <code>null</code> to return locations. 259 * 260 * @return a List of all source line {@link Location} objects. 261 * 262 * @throws AbsentInformationException if there is no line 263 * number information for this (non-native, non-abstract) 264 * method. Or if <i>sourceName</i> is non-<code>null</code> 265 * and source name information is not present. 266 * 267 * @since 1.4 268 */ allLineLocations(String stratum, String sourceName)269 List<Location> allLineLocations(String stratum, String sourceName) 270 throws AbsentInformationException; 271 272 /** 273 * Returns a List containing all {@link Location} objects 274 * that map to the given line number. 275 * <P> 276 * This method is equivalent to 277 * <code>locationsOfLine(vm.getDefaultStratum(), null, 278 * lineNumber)</code> - 279 * see {@link 280 * #locationsOfLine(java.lang.String,java.lang.String,int)} 281 * for more information. 282 * 283 * @param lineNumber the line number 284 * 285 * @return a List of {@link Location} objects that map to 286 * the given line number. 287 * 288 * @throws AbsentInformationException if there is no line 289 * number information for this method. 290 */ locationsOfLine(int lineNumber)291 List<Location> locationsOfLine(int lineNumber) throws AbsentInformationException; 292 293 /** 294 * Returns a List containing all {@link Location} objects 295 * that map to the given line number and source name. 296 * <P> 297 * Returns a list containing each {@link Location} that maps 298 * to the given line. The returned list will contain a 299 * location for each disjoint range of code indices that have 300 * been assigned to the given line by the compiler and/or 301 * VM. Each returned location corresponds to the beginning of 302 * this range. An empty list will be returned if there is no 303 * executable code at the specified line number; specifically, 304 * native and abstract methods will always return an empty 305 * list. 306 * <p> 307 * Returned list is for the specified <i>stratum</i> 308 * (see {@link Location} for a description of strata). 309 * 310 * @param stratum the stratum to use for comparing line number 311 * and source name, or null to use the default 312 * stratum 313 * @param sourceName the source name containing the 314 * line number, or null to match all 315 * source names 316 * @param lineNumber the line number 317 * 318 * @return a List of {@link Location} objects that map to 319 * the given line number. 320 * 321 * @throws AbsentInformationException if there is no line 322 * number information for this method. 323 * Or if <i>sourceName</i> is non-<code>null</code> 324 * and source name information is not present. 325 * 326 * @since 1.4 327 */ locationsOfLine(String stratum, String sourceName, int lineNumber)328 List<Location> locationsOfLine(String stratum, String sourceName, 329 int lineNumber) 330 throws AbsentInformationException; 331 332 /** 333 * Returns a {@link Location} for the given code index. 334 * 335 * @return the {@link Location} corresponding to the 336 * given code index or null if the specified code index is not a 337 * valid code index for this method (native and abstract methods 338 * will always return null). 339 */ locationOfCodeIndex(long codeIndex)340 Location locationOfCodeIndex(long codeIndex); 341 342 /** 343 * Returns a list containing each {@link LocalVariable} declared 344 * in this method. The list includes any variable declared in any 345 * scope within the method. It may contain multiple variables of the 346 * same name declared within disjoint scopes. Arguments are considered 347 * local variables and will be present in the returned list. 348 * 349 * If local variable information is not available, values of 350 * actual arguments to method invocations can be obtained 351 * by using the method {@link StackFrame#getArgumentValues()} 352 * 353 * @return the list of {@link LocalVariable} objects which mirror 354 * local variables declared in this method in the target VM. 355 * If there are no local variables, a zero-length list is returned. 356 * @throws AbsentInformationException if there is no variable 357 * information for this method. 358 * Generally, local variable information is not available for 359 * native or abstract methods (that is, their argument name 360 * information is not available), thus they will throw this exception. 361 */ variables()362 List<LocalVariable> variables() throws AbsentInformationException; 363 364 /** 365 * Returns a list containing each {@link LocalVariable} of a 366 * given name in this method. 367 * Multiple variables can be returned 368 * if the same variable name is used in disjoint 369 * scopes within the method. 370 * 371 * @return the list of {@link LocalVariable} objects of the given 372 * name. 373 * If there are no matching local variables, a zero-length list 374 * is returned. 375 * @throws AbsentInformationException if there is no variable 376 * information for this method. 377 * Generally, local variable information is not available for 378 * native or abstract methods (that is, their argument name 379 * information is not available), thus they will throw this exception. 380 */ variablesByName(String name)381 List<LocalVariable> variablesByName(String name) 382 throws AbsentInformationException; 383 384 /** 385 * Returns a list containing each {@link LocalVariable} that is 386 * declared as an argument of this method. 387 * 388 * If local variable information is not available, values of 389 * actual arguments to method invocations can be obtained 390 * by using the method {@link StackFrame#getArgumentValues()} 391 * 392 * @return the list of {@link LocalVariable} arguments. 393 * If there are no arguments, a zero-length list is returned. 394 * @throws AbsentInformationException if there is no variable 395 * information for this method. 396 * Generally, local variable information is not available for 397 * native or abstract methods (that is, their argument name 398 * information is not available), thus they will throw this exception. 399 */ arguments()400 List<LocalVariable> arguments() throws AbsentInformationException; 401 402 /** 403 * Returns an array containing the bytecodes for this method. 404 * <P> 405 * Not all target virtual machines support this operation. 406 * Use {@link VirtualMachine#canGetBytecodes()} 407 * to determine if the operation is supported. 408 * 409 * @return the array of bytecodes; abstract and native methods 410 * will return a zero-length array. 411 * @throws java.lang.UnsupportedOperationException if 412 * the target virtual machine does not support 413 * the retrieval of bytecodes. 414 */ bytecodes()415 byte[] bytecodes(); 416 417 /** 418 * Returns the {@link Location} of this method, if there 419 * is executable code associated with it. 420 * 421 * @return the {@link Location} of this mirror, or null if 422 * this is an abstract method; native methods will return a 423 * Location object whose codeIndex is -1. 424 */ location()425 Location location(); 426 427 /** 428 * Compares the specified Object with this method for equality. 429 * 430 * @return true if the Object is a method and if both 431 * mirror the same method (declared in the same class or interface, in 432 * the same VM). 433 */ equals(Object obj)434 boolean equals(Object obj); 435 436 /** 437 * Returns the hash code value for this Method. 438 * 439 * @return the integer hash code 440 */ hashCode()441 int hashCode(); 442 } 443