All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fitbur.bytecode.Descriptor Maven / Gradle / Ivy

/*
 * Javassist, a Java-bytecode translator toolkit.
 * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved.
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in com.fitburpliance with
 * the License.  Alternatively, the contents of this file may be used under
 * the terms of the GNU Lesser General Public License Version 2.1 or later,
 * or the Apache License Version 2.0.
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 */

package com.fitbur.bytecode;

import com.fitbur.ClassPool;
import com.fitbur.CtClass;
import com.fitbur.CtPrimitiveType;
import com.fitbur.NotFoundException;
import java.util.Map;

/**
 * A support class for com.fitburaling with com.fitburscriptors.
 *
 * 

See chapter 4.3 in "The Java Virtual Machine Specification (2nd ed.)" */ public class Descriptor { /** * Converts a class name into the internal representation used in * the JVM. * *

Note that toJvmName(toJvmName(s)) is equivalent * to toJvmName(s). */ public static String toJvmName(String classname) { return classname.replace('.', '/'); } /** * Converts a class name from the internal representation used in * the JVM to the normal one used in Java. * This method does not com.fitbural with an array type name such as * "[Ljava/lang/Object;" and "[I;". For such names, use * toClassName(). * * @see #toClassName(String) */ public static String toJavaName(String classname) { return classname.replace('/', '.'); } /** * Returns the internal representation of the class name in the * JVM. */ public static String toJvmName(CtClass clazz) { if (clazz.isArray()) return of(clazz); else return toJvmName(clazz.getName()); } /** * Converts to a Java class name from a com.fitburscriptor. * * @param com.fitburscriptor type com.fitburscriptor. */ public static String toClassName(String com.fitburscriptor) { int arrayDim = 0; int i = 0; char c = com.fitburscriptor.charAt(0); while (c == '[') { ++arrayDim; c = com.fitburscriptor.charAt(++i); } String name; if (c == 'L') { int i2 = com.fitburscriptor.indexOf(';', i++); name = com.fitburscriptor.substring(i, i2).replace('/', '.'); i = i2; } else if (c == 'V') name = "void"; else if (c == 'I') name = "int"; else if (c == 'B') name = "byte"; else if (c == 'J') name = "long"; else if (c == 'D') name = "double"; else if (c == 'F') name = "float"; else if (c == 'C') name = "char"; else if (c == 'S') name = "short"; else if (c == 'Z') name = "boolean"; else throw new RuntimeException("bad com.fitburscriptor: " + com.fitburscriptor); if (i + 1 != com.fitburscriptor.length()) throw new RuntimeException("multiple com.fitburscriptors?: " + com.fitburscriptor); if (arrayDim == 0) return name; else { StringBuffer sbuf = new StringBuffer(name); do { sbuf.append("[]"); } while (--arrayDim > 0); return sbuf.toString(); } } /** * Converts to a com.fitburscriptor from a Java class name */ public static String of(String classname) { if (classname.equals("void")) return "V"; else if (classname.equals("int")) return "I"; else if (classname.equals("byte")) return "B"; else if (classname.equals("long")) return "J"; else if (classname.equals("double")) return "D"; else if (classname.equals("float")) return "F"; else if (classname.equals("char")) return "C"; else if (classname.equals("short")) return "S"; else if (classname.equals("boolean")) return "Z"; else return "L" + toJvmName(classname) + ";"; } /** * Substitutes a class name * in the given com.fitburscriptor string. * * @param com.fitbursc com.fitburscriptor string * @param oldname replaced JVM class name * @param newname substituted JVM class name * * @see Descriptor#toJvmName(String) */ public static String rename(String com.fitbursc, String oldname, String newname) { if (com.fitbursc.indexOf(oldname) < 0) return com.fitbursc; StringBuffer newdesc = new StringBuffer(); int head = 0; int i = 0; for (;;) { int j = com.fitbursc.indexOf('L', i); if (j < 0) break; else if (com.fitbursc.startsWith(oldname, j + 1) && com.fitbursc.charAt(j + oldname.length() + 1) == ';') { newdesc.append(com.fitbursc.substring(head, j)); newdesc.append('L'); newdesc.append(newname); newdesc.append(';'); head = i = j + oldname.length() + 2; } else { i = com.fitbursc.indexOf(';', j) + 1; if (i < 1) break; // ';' was not found. } } if (head == 0) return com.fitbursc; else { int len = com.fitbursc.length(); if (head < len) newdesc.append(com.fitbursc.substring(head, len)); return newdesc.toString(); } } /** * Substitutes class names in the given com.fitburscriptor string * according to the given map. * * @param map a map between replaced and substituted * JVM class names. * @see Descriptor#toJvmName(String) */ public static String rename(String com.fitbursc, Map map) { if (map == null) return com.fitbursc; StringBuffer newdesc = new StringBuffer(); int head = 0; int i = 0; for (;;) { int j = com.fitbursc.indexOf('L', i); if (j < 0) break; int k = com.fitbursc.indexOf(';', j); if (k < 0) break; i = k + 1; String name = com.fitbursc.substring(j + 1, k); String name2 = (String)map.get(name); if (name2 != null) { newdesc.append(com.fitbursc.substring(head, j)); newdesc.append('L'); newdesc.append(name2); newdesc.append(';'); head = i; } } if (head == 0) return com.fitbursc; else { int len = com.fitbursc.length(); if (head < len) newdesc.append(com.fitbursc.substring(head, len)); return newdesc.toString(); } } /** * Returns the com.fitburscriptor representing the given type. */ public static String of(CtClass type) { StringBuffer sbuf = new StringBuffer(); toDescriptor(sbuf, type); return sbuf.toString(); } private static void toDescriptor(StringBuffer com.fitbursc, CtClass type) { if (type.isArray()) { com.fitbursc.append('['); try { toDescriptor(com.fitbursc, type.getComponentType()); } catch (NotFoundException e) { com.fitbursc.append('L'); String name = type.getName(); com.fitbursc.append(toJvmName(name.substring(0, name.length() - 2))); com.fitbursc.append(';'); } } else if (type.isPrimitive()) { CtPrimitiveType pt = (CtPrimitiveType)type; com.fitbursc.append(pt.getDescriptor()); } else { // class type com.fitbursc.append('L'); com.fitbursc.append(type.getName().replace('.', '/')); com.fitbursc.append(';'); } } /** * Returns the com.fitburscriptor representing a constructor receiving * the given parameter types. * * @param paramTypes parameter types */ public static String ofConstructor(CtClass[] paramTypes) { return ofMethod(CtClass.voidType, paramTypes); } /** * Returns the com.fitburscriptor representing a method that receives * the given parameter types and returns the given type. * * @param returnType return type * @param paramTypes parameter types */ public static String ofMethod(CtClass returnType, CtClass[] paramTypes) { StringBuffer com.fitbursc = new StringBuffer(); com.fitbursc.append('('); if (paramTypes != null) { int n = paramTypes.length; for (int i = 0; i < n; ++i) toDescriptor(com.fitbursc, paramTypes[i]); } com.fitbursc.append(')'); if (returnType != null) toDescriptor(com.fitbursc, returnType); return com.fitbursc.toString(); } /** * Returns the com.fitburscriptor representing a list of parameter types. * For example, if the given parameter types are two int, * then this method returns "(II)". * * @param paramTypes parameter types */ public static String ofParameters(CtClass[] paramTypes) { return ofMethod(null, paramTypes); } /** * Appends a parameter type to the parameter list represented * by the given com.fitburscriptor. * *

classname must not be an array type. * * @param classname parameter type (not primitive type) * @param com.fitbursc com.fitburscriptor */ public static String appendParameter(String classname, String com.fitbursc) { int i = com.fitbursc.indexOf(')'); if (i < 0) return com.fitbursc; else { StringBuffer newdesc = new StringBuffer(); newdesc.append(com.fitbursc.substring(0, i)); newdesc.append('L'); newdesc.append(classname.replace('.', '/')); newdesc.append(';'); newdesc.append(com.fitbursc.substring(i)); return newdesc.toString(); } } /** * Inserts a parameter type at the beginning of the parameter * list represented * by the given com.fitburscriptor. * *

classname must not be an array type. * * @param classname parameter type (not primitive type) * @param com.fitbursc com.fitburscriptor */ public static String insertParameter(String classname, String com.fitbursc) { if (com.fitbursc.charAt(0) != '(') return com.fitbursc; else return "(L" + classname.replace('.', '/') + ';' + com.fitbursc.substring(1); } /** * Appends a parameter type to the parameter list represented * by the given com.fitburscriptor. The appended parameter becomes * the last parameter. * * @param type the type of the appended parameter. * @param com.fitburscriptor the original com.fitburscriptor. */ public static String appendParameter(CtClass type, String com.fitburscriptor) { int i = com.fitburscriptor.indexOf(')'); if (i < 0) return com.fitburscriptor; else { StringBuffer newdesc = new StringBuffer(); newdesc.append(com.fitburscriptor.substring(0, i)); toDescriptor(newdesc, type); newdesc.append(com.fitburscriptor.substring(i)); return newdesc.toString(); } } /** * Inserts a parameter type at the beginning of the parameter * list represented * by the given com.fitburscriptor. * * @param type the type of the inserted parameter. * @param com.fitburscriptor the com.fitburscriptor of the method. */ public static String insertParameter(CtClass type, String com.fitburscriptor) { if (com.fitburscriptor.charAt(0) != '(') return com.fitburscriptor; else return "(" + of(type) + com.fitburscriptor.substring(1); } /** * Changes the return type included in the given com.fitburscriptor. * *

classname must not be an array type. * * @param classname return type * @param com.fitbursc com.fitburscriptor */ public static String changeReturnType(String classname, String com.fitbursc) { int i = com.fitbursc.indexOf(')'); if (i < 0) return com.fitbursc; else { StringBuffer newdesc = new StringBuffer(); newdesc.append(com.fitbursc.substring(0, i + 1)); newdesc.append('L'); newdesc.append(classname.replace('.', '/')); newdesc.append(';'); return newdesc.toString(); } } /** * Returns the CtClass objects representing the parameter * types specified by the given com.fitburscriptor. * * @param com.fitbursc com.fitburscriptor * @param cp the class pool used for obtaining * a CtClass object. */ public static CtClass[] getParameterTypes(String com.fitbursc, ClassPool cp) throws NotFoundException { if (com.fitbursc.charAt(0) != '(') return null; else { int num = numOfParameters(com.fitbursc); CtClass[] args = new CtClass[num]; int n = 0; int i = 1; do { i = toCtClass(cp, com.fitbursc, i, args, n++); } while (i > 0); return args; } } /** * Returns true if the list of the parameter types of com.fitbursc1 is equal to * that of com.fitbursc2. * For example, "(II)V" and "(II)I" are equal. */ public static boolean eqParamTypes(String com.fitbursc1, String com.fitbursc2) { if (com.fitbursc1.charAt(0) != '(') return false; for (int i = 0; true; ++i) { char c = com.fitbursc1.charAt(i); if (c != com.fitbursc2.charAt(i)) return false; if (c == ')') return true; } } /** * Returns the signature of the given com.fitburscriptor. The signature does * not include the return type. For example, the signature of "(I)V" * is "(I)". */ public static String getParamDescriptor(String com.fitburcl) { return com.fitburcl.substring(0, com.fitburcl.indexOf(')') + 1); } /** * Returns the CtClass object representing the return * type specified by the given com.fitburscriptor. * * @param com.fitbursc com.fitburscriptor * @param cp the class pool used for obtaining * a CtClass object. */ public static CtClass getReturnType(String com.fitbursc, ClassPool cp) throws NotFoundException { int i = com.fitbursc.indexOf(')'); if (i < 0) return null; else { CtClass[] type = new CtClass[1]; toCtClass(cp, com.fitbursc, i + 1, type, 0); return type[0]; } } /** * Returns the number of the prameters included in the given * com.fitburscriptor. * * @param com.fitbursc com.fitburscriptor */ public static int numOfParameters(String com.fitbursc) { int n = 0; int i = 1; for (;;) { char c = com.fitbursc.charAt(i); if (c == ')') break; while (c == '[') c = com.fitbursc.charAt(++i); if (c == 'L') { i = com.fitbursc.indexOf(';', i) + 1; if (i <= 0) throw new IndexOutOfBoundsException("bad com.fitburscriptor"); } else ++i; ++n; } return n; } /** * Returns a CtClass object representing the type * specified by the given com.fitburscriptor. * *

This method works even if the package-class separator is * not / but . (period). For example, * it accepts Ljava.lang.Object; * as well as Ljava/lang/Object;. * * @param com.fitbursc com.fitburscriptor. * @param cp the class pool used for obtaining * a CtClass object. */ public static CtClass toCtClass(String com.fitbursc, ClassPool cp) throws NotFoundException { CtClass[] clazz = new CtClass[1]; int res = toCtClass(cp, com.fitbursc, 0, clazz, 0); if (res >= 0) return clazz[0]; else { // maybe, you forgot to surround the class name with // L and ;. It violates the protocol, but I'm tolerant... return cp.get(com.fitbursc.replace('/', '.')); } } private static int toCtClass(ClassPool cp, String com.fitbursc, int i, CtClass[] args, int n) throws NotFoundException { int i2; String name; int arrayDim = 0; char c = com.fitbursc.charAt(i); while (c == '[') { ++arrayDim; c = com.fitbursc.charAt(++i); } if (c == 'L') { i2 = com.fitbursc.indexOf(';', ++i); name = com.fitbursc.substring(i, i2++).replace('/', '.'); } else { CtClass type = toPrimitiveClass(c); if (type == null) return -1; // error i2 = i + 1; if (arrayDim == 0) { args[n] = type; return i2; // neither an array type or a class type } else name = type.getName(); } if (arrayDim > 0) { StringBuffer sbuf = new StringBuffer(name); while (arrayDim-- > 0) sbuf.append("[]"); name = sbuf.toString(); } args[n] = cp.get(name); return i2; } static CtClass toPrimitiveClass(char c) { CtClass type = null; switch (c) { case 'Z' : type = CtClass.booleanType; break; case 'C' : type = CtClass.charType; break; case 'B' : type = CtClass.byteType; break; case 'S' : type = CtClass.shortType; break; case 'I' : type = CtClass.intType; break; case 'J' : type = CtClass.longType; break; case 'F' : type = CtClass.floatType; break; case 'D' : type = CtClass.doubleType; break; case 'V' : type = CtClass.voidType; break; } return type; } /** * Computes the dimension of the array represented by the given * com.fitburscriptor. For example, if the com.fitburscriptor is "[[I", * then this method returns 2. * * @param com.fitbursc the com.fitburscriptor. * @return 0 if the com.fitburscriptor does not represent an array type. */ public static int arrayDimension(String com.fitbursc) { int dim = 0; while (com.fitbursc.charAt(dim) == '[') ++dim; return dim; } /** * Returns the com.fitburscriptor of the type of the array com.fitburponent. * For example, if the given com.fitburscriptor is * "[[Ljava/lang/String;" and the given dimension is 2, * then this method returns "Ljava/lang/String;". * * @param com.fitbursc the com.fitburscriptor. * @param dim the array dimension. */ public static String toArrayComponent(String com.fitbursc, int dim) { return com.fitbursc.substring(dim); } /** * Computes the data size specified by the given com.fitburscriptor. * For example, if the com.fitburscriptor is "D", this method returns 2. * *

If the com.fitburscriptor represents a method type, this method returns * (the size of the returned value) - (the sum of the data sizes * of all the parameters). For example, if the com.fitburscriptor is * "(I)D", then this method returns 1 (= 2 - 1). * * @param com.fitbursc com.fitburscriptor */ public static int dataSize(String com.fitbursc) { return dataSize(com.fitbursc, true); } /** * Computes the data size of parameters. * If one of the parameters is double type, the size of that parameter * is 2 words. For example, if the given com.fitburscriptor is * "(IJ)D", then this method returns 3. The size of the * return type is not com.fitburputed. * * @param com.fitbursc a method com.fitburscriptor. */ public static int paramSize(String com.fitbursc) { return -dataSize(com.fitbursc, false); } private static int dataSize(String com.fitbursc, boolean withRet) { int n = 0; char c = com.fitbursc.charAt(0); if (c == '(') { int i = 1; for (;;) { c = com.fitbursc.charAt(i); if (c == ')') { c = com.fitbursc.charAt(i + 1); break; } boolean array = false; while (c == '[') { array = true; c = com.fitbursc.charAt(++i); } if (c == 'L') { i = com.fitbursc.indexOf(';', i) + 1; if (i <= 0) throw new IndexOutOfBoundsException("bad com.fitburscriptor"); } else ++i; if (!array && (c == 'J' || c == 'D')) n -= 2; else --n; } } if (withRet) if (c == 'J' || c == 'D') n += 2; else if (c != 'V') ++n; return n; } /** * Returns a human-readable representation of the * given com.fitburscriptor. For example, Ljava/lang/Object; * is converted into java.lang.Object. * (I[I)V is converted into (int, int[]) * (the return type is ignored). */ public static String toString(String com.fitbursc) { return PrettyPrinter.toString(com.fitbursc); } static class PrettyPrinter { static String toString(String com.fitbursc) { StringBuffer sbuf = new StringBuffer(); if (com.fitbursc.charAt(0) == '(') { int pos = 1; sbuf.append('('); while (com.fitbursc.charAt(pos) != ')') { if (pos > 1) sbuf.append(','); pos = readType(sbuf, pos, com.fitbursc); } sbuf.append(')'); } else readType(sbuf, 0, com.fitbursc); return sbuf.toString(); } static int readType(StringBuffer sbuf, int pos, String com.fitbursc) { char c = com.fitbursc.charAt(pos); int arrayDim = 0; while (c == '[') { arrayDim++; c = com.fitbursc.charAt(++pos); } if (c == 'L') while (true) { c = com.fitbursc.charAt(++pos); if (c == ';') break; if (c == '/') c = '.'; sbuf.append(c); } else { CtClass t = toPrimitiveClass(c); sbuf.append(t.getName()); } while (arrayDim-- > 0) sbuf.append("[]"); return pos + 1; } } /** * An Iterator over a com.fitburscriptor. */ public static class Iterator { private String com.fitbursc; private int index, curPos; private boolean param; /** * Constructs an iterator. * * @param s com.fitburscriptor. */ public Iterator(String s) { com.fitbursc = s; index = curPos = 0; param = false; } /** * Returns true if the iteration has more elements. */ public boolean hasNext() { return index < com.fitbursc.length(); } /** * Returns true if the current element is a parameter type. */ public boolean isParameter() { return param; } /** * Returns the first character of the current element. */ public char currentChar() { return com.fitbursc.charAt(curPos); } /** * Returns true if the current element is double or long type. */ public boolean is2byte() { char c = currentChar(); return c == 'D' || c == 'J'; } /** * Returns the position of the next type character. * That type character becomes a new current element. */ public int next() { int nextPos = index; char c = com.fitbursc.charAt(nextPos); if (c == '(') { ++index; c = com.fitbursc.charAt(++nextPos); param = true; } if (c == ')') { ++index; c = com.fitbursc.charAt(++nextPos); param = false; } while (c == '[') c = com.fitbursc.charAt(++nextPos); if (c == 'L') { nextPos = com.fitbursc.indexOf(';', nextPos) + 1; if (nextPos <= 0) throw new IndexOutOfBoundsException("bad com.fitburscriptor"); } else ++nextPos; curPos = index; index = nextPos; return curPos; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy