HotSpotMethod.java revision 12651:6ef01bd40ce2
11573Srgrimes/* 21573Srgrimes * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved. 31573Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 41573Srgrimes * 51573Srgrimes * This code is free software; you can redistribute it and/or modify it 61573Srgrimes * under the terms of the GNU General Public License version 2 only, as 71573Srgrimes * published by the Free Software Foundation. 81573Srgrimes * 91573Srgrimes * This code is distributed in the hope that it will be useful, but WITHOUT 101573Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 111573Srgrimes * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 121573Srgrimes * version 2 for more details (a copy is included in the LICENSE file that 131573Srgrimes * accompanied this code). 141573Srgrimes * 151573Srgrimes * You should have received a copy of the GNU General Public License version 161573Srgrimes * 2 along with this work; if not, write to the Free Software Foundation, 171573Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 181573Srgrimes * 191573Srgrimes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 201573Srgrimes * or visit www.oracle.com if you need additional information or have any 211573Srgrimes * questions. 221573Srgrimes */ 231573Srgrimespackage jdk.vm.ci.hotspot; 241573Srgrimes 251573Srgrimesimport static java.util.FormattableFlags.ALTERNATE; 261573Srgrimesimport static java.util.FormattableFlags.LEFT_JUSTIFY; 271573Srgrimesimport static java.util.FormattableFlags.UPPERCASE; 281573Srgrimes 2950476Speterimport java.util.Formattable; 301573Srgrimesimport java.util.Formatter; 31196820Sdes 321573Srgrimesimport jdk.vm.ci.meta.JavaMethod; 331573Srgrimesimport jdk.vm.ci.meta.ResolvedJavaMethod; 341573Srgrimes 351573Srgrimesabstract class HotSpotMethod implements JavaMethod, Formattable { 361573Srgrimes 3759460Sphantom public static String applyFormattingFlagsAndWidth(String s, int flags, int width) { 3859460Sphantom if (flags == 0 && width < 0) { 391573Srgrimes return s; 4084306Sru } 411573Srgrimes StringBuilder sb = new StringBuilder(s); 4224872Sbde 43232935Stheraven // apply width and justification 44248803Sjilles int len = sb.length(); 451573Srgrimes if (len < width) { 461573Srgrimes for (int i = 0; i < width - len; i++) { 471573Srgrimes if ((flags & LEFT_JUSTIFY) == LEFT_JUSTIFY) { 481573Srgrimes sb.append(' '); 4987110Sache } else { 50207943Suqs sb.insert(0, ' '); 51207943Suqs } 5287110Sache } 5387110Sache } 54199180Sroam 5587110Sache String res = sb.toString(); 56199180Sroam if ((flags & UPPERCASE) == UPPERCASE) { 57148087Stjr res = res.toUpperCase(); 58152551Sru } 5959149Sache return res; 6059149Sache } 61232935Stheraven 62233648Seadler /** 63232935Stheraven * Controls whether {@link #toString()} includes the qualified or simple name of the class in 64232935Stheraven * which the method is declared. 65232935Stheraven */ 66232935Stheraven public static final boolean FULLY_QUALIFIED_METHOD_NAME = false; 67140505Sru 68140505Sru @Override 69140505Sru public final String toString() { 70140505Sru char h = FULLY_QUALIFIED_METHOD_NAME ? 'H' : 'h'; 71140505Sru String suffix = this instanceof ResolvedJavaMethod ? "" : ", unresolved"; 72104562Stjr String fmt = String.format("HotSpotMethod<%%%c.%%n(%%p)%s>", h, suffix); 73132859Stjr return format(fmt); 74132859Stjr } 75132859Stjr 76104562Stjr public void formatTo(Formatter formatter, int flags, int width, int precision) { 77134103Stjr String base = (flags & ALTERNATE) == ALTERNATE ? getName() : toString(); 78134103Stjr formatter.format(applyFormattingFlagsAndWidth(base, flags & ~ALTERNATE, width)); 79132859Stjr } 80104562Stjr 81134103Stjr public JavaMethod asJavaMethod() { 821573Srgrimes return this; 831573Srgrimes } 84104562Stjr} 85232935Stheraven