0N/A/*
2362N/A * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.tools.jdi;
0N/A
0N/Aimport java.util.List;
0N/Aimport java.util.ArrayList;
0N/A
0N/Apublic class JNITypeParser {
0N/A
0N/A static final char SIGNATURE_ENDCLASS = ';';
0N/A static final char SIGNATURE_FUNC = '(';
0N/A static final char SIGNATURE_ENDFUNC = ')';
0N/A
0N/A private String signature;
0N/A private List<String> typeNameList;
0N/A private List<String> signatureList;
0N/A private int currentIndex;
0N/A
0N/A JNITypeParser(String signature) {
0N/A this.signature = signature;
0N/A }
0N/A
0N/A static String typeNameToSignature(String signature) {
0N/A StringBuffer buffer = new StringBuffer();
0N/A int firstIndex = signature.indexOf('[');
0N/A int index = firstIndex;
0N/A while (index != -1) {
0N/A buffer.append('[');
0N/A index = signature.indexOf('[', index + 1);
0N/A }
0N/A
0N/A if (firstIndex != -1) {
0N/A signature = signature.substring(0, firstIndex);
0N/A }
0N/A
0N/A if (signature.equals("boolean")) {
0N/A buffer.append('Z');
0N/A } else if (signature.equals("byte")) {
0N/A buffer.append('B');
0N/A } else if (signature.equals("char")) {
0N/A buffer.append('C');
0N/A } else if (signature.equals("short")) {
0N/A buffer.append('S');
0N/A } else if (signature.equals("int")) {
0N/A buffer.append('I');
0N/A } else if (signature.equals("long")) {
0N/A buffer.append('J');
0N/A } else if (signature.equals("float")) {
0N/A buffer.append('F');
0N/A } else if (signature.equals("double")) {
0N/A buffer.append('D');
0N/A } else {
0N/A buffer.append('L');
0N/A buffer.append(signature.replace('.', '/'));
0N/A buffer.append(';');
0N/A }
0N/A
0N/A return buffer.toString();
0N/A }
0N/A
0N/A String typeName() {
28N/A return typeNameList().get(typeNameList().size()-1);
0N/A }
0N/A
0N/A List<String> argumentTypeNames() {
0N/A return typeNameList().subList(0, typeNameList().size() - 1);
0N/A }
0N/A
0N/A String signature() {
28N/A return signatureList().get(signatureList().size()-1);
0N/A }
0N/A
0N/A List<String> argumentSignatures() {
0N/A return signatureList().subList(0, signatureList().size() - 1);
0N/A }
0N/A
0N/A int dimensionCount() {
0N/A int count = 0;
0N/A String signature = signature();
0N/A while (signature.charAt(count) == '[') {
0N/A count++;
0N/A }
0N/A return count;
0N/A }
0N/A
0N/A String componentSignature(int level) {
0N/A return signature().substring(level);
0N/A }
0N/A
0N/A private synchronized List<String> signatureList() {
0N/A if (signatureList == null) {
0N/A signatureList = new ArrayList<String>(10);
0N/A String elem;
0N/A
0N/A currentIndex = 0;
0N/A
0N/A while(currentIndex < signature.length()) {
0N/A elem = nextSignature();
0N/A signatureList.add(elem);
0N/A }
0N/A if (signatureList.size() == 0) {
0N/A throw new IllegalArgumentException("Invalid JNI signature '" +
0N/A signature + "'");
0N/A }
0N/A }
0N/A return signatureList;
0N/A }
0N/A
0N/A private synchronized List<String> typeNameList() {
0N/A if (typeNameList == null) {
0N/A typeNameList = new ArrayList<String>(10);
0N/A String elem;
0N/A
0N/A currentIndex = 0;
0N/A
0N/A while(currentIndex < signature.length()) {
0N/A elem = nextTypeName();
0N/A typeNameList.add(elem);
0N/A }
0N/A if (typeNameList.size() == 0) {
0N/A throw new IllegalArgumentException("Invalid JNI signature '" +
0N/A signature + "'");
0N/A }
0N/A }
0N/A return typeNameList;
0N/A }
0N/A
0N/A private String nextSignature() {
0N/A char key = signature.charAt(currentIndex++);
0N/A
0N/A switch(key) {
0N/A case (JDWP.Tag.ARRAY):
0N/A return key + nextSignature();
0N/A
0N/A case (JDWP.Tag.OBJECT):
0N/A int endClass = signature.indexOf(SIGNATURE_ENDCLASS,
0N/A currentIndex);
0N/A String retVal = signature.substring(currentIndex - 1,
0N/A endClass + 1);
0N/A currentIndex = endClass + 1;
0N/A return retVal;
0N/A
0N/A case (JDWP.Tag.VOID):
0N/A case (JDWP.Tag.BOOLEAN):
0N/A case (JDWP.Tag.BYTE):
0N/A case (JDWP.Tag.CHAR):
0N/A case (JDWP.Tag.SHORT):
0N/A case (JDWP.Tag.INT):
0N/A case (JDWP.Tag.LONG):
0N/A case (JDWP.Tag.FLOAT):
0N/A case (JDWP.Tag.DOUBLE):
0N/A return String.valueOf(key);
0N/A
0N/A case SIGNATURE_ENDFUNC:
0N/A case SIGNATURE_FUNC:
0N/A return nextSignature();
0N/A
0N/A default:
0N/A throw new IllegalArgumentException(
0N/A "Invalid JNI signature character '" + key + "'");
0N/A
0N/A }
0N/A }
0N/A
0N/A private String nextTypeName() {
0N/A char key = signature.charAt(currentIndex++);
0N/A
0N/A switch(key) {
0N/A case (JDWP.Tag.ARRAY):
0N/A return nextTypeName() + "[]";
0N/A
0N/A case (JDWP.Tag.BYTE):
0N/A return "byte";
0N/A
0N/A case (JDWP.Tag.CHAR):
0N/A return "char";
0N/A
0N/A case (JDWP.Tag.OBJECT):
0N/A int endClass = signature.indexOf(SIGNATURE_ENDCLASS,
0N/A currentIndex);
0N/A String retVal = signature.substring(currentIndex,
0N/A endClass);
0N/A retVal = retVal.replace('/','.');
0N/A currentIndex = endClass + 1;
0N/A return retVal;
0N/A
0N/A case (JDWP.Tag.FLOAT):
0N/A return "float";
0N/A
0N/A case (JDWP.Tag.DOUBLE):
0N/A return "double";
0N/A
0N/A case (JDWP.Tag.INT):
0N/A return "int";
0N/A
0N/A case (JDWP.Tag.LONG):
0N/A return "long";
0N/A
0N/A case (JDWP.Tag.SHORT):
0N/A return "short";
0N/A
0N/A case (JDWP.Tag.VOID):
0N/A return "void";
0N/A
0N/A case (JDWP.Tag.BOOLEAN):
0N/A return "boolean";
0N/A
0N/A case SIGNATURE_ENDFUNC:
0N/A case SIGNATURE_FUNC:
0N/A return nextTypeName();
0N/A
0N/A default:
0N/A throw new IllegalArgumentException(
0N/A "Invalid JNI signature character '" + key + "'");
0N/A
0N/A }
0N/A }
0N/A}