001/*
002 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package jdk.internal.jvmci.meta;
024
025import java.util.*;
026
027/**
028 * Represents a reference to a Java method, either resolved or unresolved. Methods, like fields and
029 * types, are resolved through {@link ConstantPool constant pools}.
030 */
031public interface JavaMethod extends TrustedInterface {
032
033    /**
034     * Returns the name of this method.
035     */
036    String getName();
037
038    /**
039     * Returns the {@link JavaType} object representing the class or interface that declares this
040     * method.
041     */
042    JavaType getDeclaringClass();
043
044    /**
045     * Returns the signature of this method.
046     */
047    Signature getSignature();
048
049    /**
050     * Gets a string for this method formatted according to a given format specification. A format
051     * specification is composed of characters that are to be copied verbatim to the result and
052     * specifiers that denote an attribute of this method that is to be copied to the result. A
053     * specifier is a single character preceded by a '%' character. The accepted specifiers and the
054     * method attributes they denote are described below:
055     *
056     * <pre>
057     *     Specifier | Description                                          | Example(s)
058     *     ----------+------------------------------------------------------------------------------------------
059     *     'R'       | Qualified return type                                | "int" "java.lang.String"
060     *     'r'       | Unqualified return type                              | "int" "String"
061     *     'H'       | Qualified holder                                     | "java.util.Map.Entry"
062     *     'h'       | Unqualified holder                                   | "Entry"
063     *     'n'       | Method name                                          | "add"
064     *     'P'       | Qualified parameter types, separated by ', '         | "int, java.lang.String"
065     *     'p'       | Unqualified parameter types, separated by ', '       | "int, String"
066     *     'f'       | Indicator if method is unresolved, static or virtual | "unresolved" "static" "virtual"
067     *     '%'       | A '%' character                                      | "%"
068     * </pre>
069     *
070     * @param format a format specification
071     * @return the result of formatting this method according to {@code format}
072     * @throws IllegalFormatException if an illegal specifier is encountered in {@code format}
073     */
074    default String format(String format) throws IllegalFormatException {
075        StringBuilder sb = new StringBuilder();
076        int index = 0;
077        Signature sig = null;
078        while (index < format.length()) {
079            char ch = format.charAt(index++);
080            if (ch == '%') {
081                if (index >= format.length()) {
082                    throw new UnknownFormatConversionException("An unquoted '%' character cannot terminate a method format specification");
083                }
084                char specifier = format.charAt(index++);
085                boolean qualified = false;
086                switch (specifier) {
087                    case 'R':
088                        qualified = true;
089                        // fall through
090                    case 'r': {
091                        if (sig == null) {
092                            sig = getSignature();
093                        }
094                        sb.append(sig.getReturnType(null).toJavaName(qualified));
095                        break;
096                    }
097                    case 'H':
098                        qualified = true;
099                        // fall through
100                    case 'h': {
101                        sb.append(getDeclaringClass().toJavaName(qualified));
102                        break;
103                    }
104                    case 'n': {
105                        sb.append(getName());
106                        break;
107                    }
108                    case 'P':
109                        qualified = true;
110                        // fall through
111                    case 'p': {
112                        if (sig == null) {
113                            sig = getSignature();
114                        }
115                        for (int i = 0; i < sig.getParameterCount(false); i++) {
116                            if (i != 0) {
117                                sb.append(", ");
118                            }
119                            sb.append(sig.getParameterType(i, null).toJavaName(qualified));
120                        }
121                        break;
122                    }
123                    case 'f': {
124                        sb.append(!(this instanceof ResolvedJavaMethod) ? "unresolved" : ((ResolvedJavaMethod) this).isStatic() ? "static" : "virtual");
125                        break;
126                    }
127                    case '%': {
128                        sb.append('%');
129                        break;
130                    }
131                    default: {
132                        throw new UnknownFormatConversionException(String.valueOf(specifier));
133                    }
134                }
135            } else {
136                sb.append(ch);
137            }
138        }
139        return sb.toString();
140    }
141}