001/*
002 * Copyright (c) 2011, 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.hotspot;
024
025import static java.util.FormattableFlags.*;
026import java.util.*;
027
028import jdk.internal.jvmci.meta.*;
029
030public abstract class HotSpotMethod implements JavaMethod, Formattable /* , JavaMethodContex */{
031
032    public static String applyFormattingFlagsAndWidth(String s, int flags, int width) {
033        if (flags == 0 && width < 0) {
034            return s;
035        }
036        StringBuilder sb = new StringBuilder(s);
037
038        // apply width and justification
039        int len = sb.length();
040        if (len < width) {
041            for (int i = 0; i < width - len; i++) {
042                if ((flags & LEFT_JUSTIFY) == LEFT_JUSTIFY) {
043                    sb.append(' ');
044                } else {
045                    sb.insert(0, ' ');
046                }
047            }
048        }
049
050        String res = sb.toString();
051        if ((flags & UPPERCASE) == UPPERCASE) {
052            res = res.toUpperCase();
053        }
054        return res;
055    }
056
057    protected String name;
058
059    /**
060     * Controls whether {@link #toString()} includes the qualified or simple name of the class in
061     * which the method is declared.
062     */
063    public static final boolean FULLY_QUALIFIED_METHOD_NAME = false;
064
065    protected HotSpotMethod(String name) {
066        this.name = name;
067    }
068
069    @Override
070    public final String getName() {
071        return name;
072    }
073
074    @Override
075    public final String toString() {
076        char h = FULLY_QUALIFIED_METHOD_NAME ? 'H' : 'h';
077        String suffix = this instanceof ResolvedJavaMethod ? "" : ", unresolved";
078        String fmt = String.format("HotSpotMethod<%%%c.%%n(%%p)%s>", h, suffix);
079        return format(fmt);
080    }
081
082    public void formatTo(Formatter formatter, int flags, int width, int precision) {
083        String base = (flags & ALTERNATE) == ALTERNATE ? getName() : toString();
084        formatter.format(applyFormattingFlagsAndWidth(base, flags & ~ALTERNATE, width));
085    }
086
087    public JavaMethod asJavaMethod() {
088        return this;
089    }
090}