Path: blob/master/test/hotspot/jtreg/serviceability/dcmd/compiler/MethodIdentifierParser.java
41153 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.lang.reflect.Method;24import java.util.ArrayList;2526public class MethodIdentifierParser {2728private String logString;29private String className;30private String methodName;31private String methodDescriptor;3233/**34* This is a utility class for parsing the log entries for a method. It supplies35* a few select methods for reflecting the class and method from that information.36*37* Example log entries:38* "java.util.TreeMap.successor(Ljava/util/TreeMap$Entry;)Ljava/util/TreeMap$Entry;"39*/4041public MethodIdentifierParser(String logString) {42this.logString = logString;4344int i = logString.lastIndexOf("."); // find start of method name45className = logString.substring(0, i); // classname is everything before46int i2 = logString.indexOf("("); // Signature starts with an '('47methodName = logString.substring(i+1, i2);48methodDescriptor = logString.substring(i2, logString.length());4950// Add sanity check for extracted fields51}5253public Method getMethod() throws NoSuchMethodException, SecurityException, ClassNotFoundException {54try {55return Class.forName(className).getDeclaredMethod(methodName, getParamenterDescriptorArray());56} catch (UnexpectedTokenException e) {57throw new RuntimeException("Parse failed");58}59}6061public Class<?>[] getParamenterDescriptorArray() throws ClassNotFoundException, UnexpectedTokenException {62ParameterDecriptorIterator s = new ParameterDecriptorIterator(methodDescriptor);63Class<?> paramType;64ArrayList<Class<?>> list = new ArrayList<Class<?>>();65while ((paramType = s.nextParamType()) != null) {66list.add(paramType);67}68if (list.size() > 0) {69return list.toArray(new Class<?>[list.size()]);70} else {71return null;72}73}7475class ParameterDecriptorIterator {7677// This class uses charAt() indexing for startMark and i78// That is when i points to the last char it can be retrieved with79// charAt(i). Including the last char for a subString requires80// substring(startMark, i+1);8182private String methodDescriptor;83private int startMark;8485public ParameterDecriptorIterator(String signature) {86this.methodDescriptor = signature;87this.startMark = 0;88if (signature.charAt(0) == '(') {89this.startMark = 1;90}91}9293public Class<?> nextParamType() throws UnexpectedTokenException {94int i = startMark;95while (methodDescriptor.length() > i) {96switch (methodDescriptor.charAt(i)) {97case 'C':98case 'B':99case 'I':100case 'J':101case 'Z':102case 'F':103case 'D':104case 'S':105// Primitive class case, but we may have gotten here with [ as first token106break;107case 'L':108// Internal class name suffixed by ';'109while (methodDescriptor.charAt(i) != ';') {110i++;111}112break;113case '[':114i++; // arrays -> do another pass115continue;116case ')':117return null; // end found118case 'V':119case ';':120default:121throw new UnexpectedTokenException(methodDescriptor, i);122}123break;124}125if (i == startMark) {126// Single char -> primitive class case127startMark++; // Update for next iteration128switch (methodDescriptor.charAt(i)) {129case 'C':130return char.class;131case 'B':132return byte.class;133case 'I':134return int.class;135case 'J':136return long.class;137case 'F':138return float.class;139case 'D':140return double.class;141case 'S':142return short.class;143case 'Z':144return boolean.class;145default:146throw new UnexpectedTokenException(methodDescriptor, i);147}148} else {149// Multi char case150String nextParam;151if (methodDescriptor.charAt(startMark) == 'L') {152// When reflecting a class the leading 'L' and trailing';' must be removed.153// (When reflecting an array of classes, they must remain...)154nextParam = methodDescriptor.substring(startMark+1, i);155} else {156// Any kind of array - simple case, use whole descriptor when reflecting.157nextParam = methodDescriptor.substring(startMark, i+1);158}159startMark = ++i; // Update for next iteration160try {161// The parameter descriptor uses JVM internal class identifier with '/' as162// package separator, but Class.forName expects '.'.163nextParam = nextParam.replace('/', '.');164return Class.forName(nextParam);165} catch (ClassNotFoundException e) {166System.out.println("Class not Found: " + nextParam);167return null;168}169}170}171}172173class UnexpectedTokenException extends Exception {174String descriptor;175int i;176public UnexpectedTokenException(String descriptor, int i) {177this.descriptor = descriptor;178this.i = i;179}180181@Override182public String toString() {183return "Unexpected token at: " + i + " in signature: " + descriptor;184}185186private static final long serialVersionUID = 1L;187}188189public void debugPrint() {190System.out.println("mlf in: " + logString);191System.out.println("mlf class: " + className);192System.out.println("mlf method: " + methodName);193System.out.println("mlf methodDescriptor: " + methodDescriptor);194}195}196197198