📄 type.java
字号:
/*** * ASM: a very small and fast Java bytecode manipulation framework * Copyright (C) 2000 INRIA, France Telecom * Copyright (C) 2002 France Telecom * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Contact: Eric.Bruneton@rd.francetelecom.com * * Author: Eric Bruneton */package org.objectweb.asm;import java.lang.reflect.Method;/** * A Java type. This class can be used to make it easier to manipulate type * and method descriptors. */public class Type { /** * The sort of the <tt>void</tt> type. See {@link #getSort getSort}. */ public final static int VOID = 0; /** * The sort of the <tt>boolean</tt> type. See {@link #getSort getSort}. */ public final static int BOOLEAN = 1; /** * The sort of the <tt>char</tt> type. See {@link #getSort getSort}. */ public final static int CHAR = 2; /** * The sort of the <tt>byte</tt> type. See {@link #getSort getSort}. */ public final static int BYTE = 3; /** * The sort of the <tt>short</tt> type. See {@link #getSort getSort}. */ public final static int SHORT = 4; /** * The sort of the <tt>int</tt> type. See {@link #getSort getSort}. */ public final static int INT = 5; /** * The sort of the <tt>float</tt> type. See {@link #getSort getSort}. */ public final static int FLOAT = 6; /** * The sort of the <tt>long</tt> type. See {@link #getSort getSort}. */ public final static int LONG = 7; /** * The sort of the <tt>double</tt> type. See {@link #getSort getSort}. */ public final static int DOUBLE = 8; /** * The sort of array reference types. See {@link #getSort getSort}. */ public final static int ARRAY = 9; /** * The sort of object reference type. See {@link #getSort getSort}. */ public final static int OBJECT = 10; /** * The <tt>void</tt> type. */ public final static Type VOID_TYPE = new Type(VOID); /** * The <tt>boolean</tt> type. */ public final static Type BOOLEAN_TYPE = new Type(BOOLEAN); /** * The <tt>char</tt> type. */ public final static Type CHAR_TYPE = new Type(CHAR); /** * The <tt>byte</tt> type. */ public final static Type BYTE_TYPE = new Type(BYTE); /** * The <tt>short</tt> type. */ public final static Type SHORT_TYPE = new Type(SHORT); /** * The <tt>int</tt> type. */ public final static Type INT_TYPE = new Type(INT); /** * The <tt>float</tt> type. */ public final static Type FLOAT_TYPE = new Type(FLOAT); /** * The <tt>long</tt> type. */ public final static Type LONG_TYPE = new Type(LONG); /** * The <tt>double</tt> type. */ public final static Type DOUBLE_TYPE = new Type(DOUBLE); // -------------------------------------------------------------------------- // Fields // -------------------------------------------------------------------------- /** * The sort of this Java type. */ private final int sort; /** * A buffer containing the descriptor of this Java type. * This field is only used for reference types. */ private char[] buf; /** * The offset of the descriptor of this Java type in {@link #buf buf}. * This field is only used for reference types. */ private int off; /** * The length of the descriptor of this Java type. */ private int len; // -------------------------------------------------------------------------- // Constructors // -------------------------------------------------------------------------- /** * Constructs a primitive type. * * @param sort the sort of the primitive type to be constructed. */ private Type (final int sort) { this.sort = sort; this.len = 1; } /** * Constructs a reference type. * * @param sort the sort of the reference type to be constructed. * @param buf a buffer containing the descriptor of the previous type. * @param off the offset of this descriptor in the previous buffer. * @param len the length of this descriptor. */ private Type ( final int sort, final char[] buf, final int off, final int len) { this.sort = sort; this.buf = buf; this.off = off; this.len = len; } /** * Returns the Java type corresponding to the given type descriptor. * * @param typeDescriptor a type descriptor. * @return the Java type corresponding to the given type descriptor. */ public static Type getType (final String typeDescriptor) { return getType(typeDescriptor.toCharArray(), 0); } /** * Returns the Java type corresponding to the given class. * * @param c a class. * @return the Java type corresponding to the given class. */ public static Type getType (final Class c) { if (c.isPrimitive()) { if (c == Integer.TYPE) { return INT_TYPE; } else if (c == Void.TYPE) { return VOID_TYPE; } else if (c == Boolean.TYPE) { return BOOLEAN_TYPE; } else if (c == Byte.TYPE) { return BYTE_TYPE; } else if (c == Character.TYPE) { return CHAR_TYPE; } else if (c == Short.TYPE) { return SHORT_TYPE; } else if (c == Double.TYPE) { return DOUBLE_TYPE; } else if (c == Float.TYPE) { return FLOAT_TYPE; } else /*if (c == Long.TYPE)*/ { return LONG_TYPE; } } else { return getType(getDescriptor(c)); } } /** * Returns the Java types corresponding to the argument types of the given * method descriptor. * * @param methodDescriptor a method descriptor. * @return the Java types corresponding to the argument types of the given * method descriptor. */ public static Type[] getArgumentTypes (final String methodDescriptor) { char[] buf = methodDescriptor.toCharArray(); int off = 1; int size = 0; while (true) { char car = buf[off++]; if (car == ')') { break; } else if (car == 'L') { while (buf[off++] != ';') { } ++size; } else if (car != '[') { ++size; } } Type[] args = new Type[size]; off = 1; size = 0; while (buf[off] != ')') { args[size] = getType(buf, off); off += args[size].len; size += 1; } return args; } /** * Returns the Java types corresponding to the argument types of the given * method. * * @param method a method. * @return the Java types corresponding to the argument types of the given * method. */ public static Type[] getArgumentTypes (final Method method) { Class[] classes = method.getParameterTypes(); Type[] types = new Type[classes.length]; for (int i = classes.length - 1; i >= 0; --i) { types[i] = getType(classes[i]); } return types; } /** * Returns the Java type corresponding to the return type of the given * method descriptor. * * @param methodDescriptor a method descriptor. * @return the Java type corresponding to the return type of the given * method descriptor. */ public static Type getReturnType (final String methodDescriptor) { char[] buf = methodDescriptor.toCharArray(); return getType(buf, methodDescriptor.indexOf(')') + 1); } /** * Returns the Java type corresponding to the return type of the given * method. * * @param method a method. * @return the Java type corresponding to the return type of the given * method. */ public static Type getReturnType (final Method method) { return getType(method.getReturnType()); } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -