001: /*
002: * Janino - An embedded Java[TM] compiler
003: *
004: * Copyright (c) 2006, Arno Unkrig
005: * All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * 2. Redistributions in binary form must reproduce the above
014: * copyright notice, this list of conditions and the following
015: * disclaimer in the documentation and/or other materials
016: * provided with the distribution.
017: * 3. The name of the author may not be used to endorse or promote
018: * products derived from this software without specific prior
019: * written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
022: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
023: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
024: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
025: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
026: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
027: * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
028: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
029: * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
030: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
031: * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
032: */
033:
034: package org.codehaus.janino;
035:
036: import java.util.*;
037:
038: /**
039: * Representation of a "method descriptor" (JVMS 4.3.3).
040: */
041: public class MethodDescriptor {
042:
043: /** The field descriptors of the method parameters. */
044: public final String[] parameterFDs;
045:
046: /** The field descriptor of the method return value. */
047: public final String returnFD;
048:
049: /** */
050: public MethodDescriptor(String[] parameterFDs, String returnFD) {
051: this .parameterFDs = parameterFDs;
052: this .returnFD = returnFD;
053: }
054:
055: /**
056: * Parse a method descriptor into parameter FDs and return FDs.
057: */
058: public MethodDescriptor(String s) {
059: if (s.charAt(0) != '(')
060: throw new RuntimeException();
061:
062: int from = 1;
063: List parameterFDs = new ArrayList(); // String
064: while (s.charAt(from) != ')') {
065: int to = from;
066: while (s.charAt(to) == '[')
067: ++to;
068: if ("BCDFIJSZ".indexOf(s.charAt(to)) != -1) {
069: ++to;
070: } else if (s.charAt(to) == 'L') {
071: for (++to; s.charAt(to) != ';'; ++to)
072: ;
073: ++to;
074: } else {
075: throw new RuntimeException();
076: }
077: parameterFDs.add(s.substring(from, to));
078: from = to;
079: }
080: this .parameterFDs = (String[]) parameterFDs
081: .toArray(new String[parameterFDs.size()]);
082: this .returnFD = s.substring(++from);
083: }
084:
085: /**
086: * Returns the "method descriptor" (JVMS 4.3.3).
087: */
088: public String toString() {
089: StringBuffer sb = new StringBuffer("(");
090: for (int i = 0; i < this .parameterFDs.length; ++i)
091: sb.append(this .parameterFDs[i]);
092: return sb.append(')').append(this .returnFD).toString();
093: }
094:
095: /**
096: * Patch an additional parameter into a given method descriptor.
097: */
098: public static String prependParameter(String md, String parameterFD) {
099: return '(' + parameterFD + md.substring(1);
100: }
101: }
|