1 /* 2 * Copyright (c) 2009, 2012, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package jdk.vm.ci.meta; 24 25 import java.util.*; 26 27 /** 28 * Represents a reference to a Java method, either resolved or unresolved. Methods, like fields and 29 * types, are resolved through {@link ConstantPool constant pools}. 30 */ 31 public interface JavaMethod extends TrustedInterface { 32 33 /** 34 * Returns the name of this method. 35 */ 36 String getName(); 37 38 /** 39 * Returns the {@link JavaType} object representing the class or interface that declares this 40 * method. 41 */ 42 JavaType getDeclaringClass(); 43 44 /** 45 * Returns the signature of this method. 46 */ 47 Signature getSignature(); 48 49 /** 50 * Gets a string for this method formatted according to a given format specification. A format 51 * specification is composed of characters that are to be copied verbatim to the result and 52 * specifiers that denote an attribute of this method that is to be copied to the result. A 53 * specifier is a single character preceded by a '%' character. The accepted specifiers and the 54 * method attributes they denote are described below: 55 * 56 * <pre> 57 * Specifier | Description | Example(s) 58 * ----------+------------------------------------------------------------------------------------------ 59 * 'R' | Qualified return type | "int" "java.lang.String" 60 * 'r' | Unqualified return type | "int" "String" 61 * 'H' | Qualified holder | "java.util.Map.Entry" 62 * 'h' | Unqualified holder | "Entry" 63 * 'n' | Method name | "add" 64 * 'P' | Qualified parameter types, separated by ', ' | "int, java.lang.String" 65 * 'p' | Unqualified parameter types, separated by ', ' | "int, String" 66 * 'f' | Indicator if method is unresolved, static or virtual | "unresolved" "static" "virtual" 67 * '%' | A '%' character | "%" 68 * </pre> 69 * 70 * @param format a format specification 71 * @return the result of formatting this method according to {@code format} 72 * @throws IllegalFormatException if an illegal specifier is encountered in {@code format} 73 */ 74 @SuppressWarnings("fallthrough") 75 default String format(String format) throws IllegalFormatException { 76 StringBuilder sb = new StringBuilder(); 77 int index = 0; 78 Signature sig = null; 79 while (index < format.length()) { 80 char ch = format.charAt(index++); 81 if (ch == '%') { 82 if (index >= format.length()) { 83 throw new UnknownFormatConversionException("An unquoted '%' character cannot terminate a method format specification"); 84 } 85 char specifier = format.charAt(index++); 86 switch (specifier) { 87 case 'R': 88 case 'r': { 89 if (sig == null) { 90 sig = getSignature(); 91 } 92 sb.append(sig.getReturnType(null).toJavaName(specifier == 'R')); 93 break; 94 } 95 case 'H': 96 case 'h': { 97 sb.append(getDeclaringClass().toJavaName(specifier == 'H')); 98 break; 99 } 100 case 'n': { 101 sb.append(getName()); 102 break; 103 } 104 case 'P': 105 case 'p': { 106 if (sig == null) { 107 sig = getSignature(); 108 } 109 for (int i = 0; i < sig.getParameterCount(false); i++) { 110 if (i != 0) { 111 sb.append(", "); 112 } 113 sb.append(sig.getParameterType(i, null).toJavaName(specifier == 'P')); 114 } 115 break; 116 } 117 case 'f': { 118 sb.append(!(this instanceof ResolvedJavaMethod) ? "unresolved" : ((ResolvedJavaMethod) this).isStatic() ? "static" : "virtual"); 119 break; 120 } 121 case '%': { 122 sb.append('%'); 123 break; 124 } 125 default: { 126 throw new UnknownFormatConversionException(String.valueOf(specifier)); 127 } 128 } 129 } else { 130 sb.append(ch); 131 } 132 } 133 return sb.toString(); 134 } 135 }