⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 classitem.java

📁 对xml很好的java处理引擎,编译中绑定xml
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*Copyright (c) 2003-2005, Dennis M. SosnoskiAll rights reserved.Redistribution and use in source and binary forms, with or without modification,are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this   list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice,   this list of conditions and the following disclaimer in the documentation   and/or other materials provided with the distribution. * Neither the name of JiBX nor the names of its contributors may be used   to endorse or promote products derived from this software without specific   prior written permission.THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" ANDANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIEDWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AREDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FORANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ONANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THISSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/package org.jibx.binding.classes;import java.util.HashMap;import org.apache.bcel.Constants;import org.apache.bcel.classfile.Attribute;import org.apache.bcel.classfile.ExceptionTable;import org.apache.bcel.classfile.FieldOrMethod;import org.apache.bcel.classfile.LocalVariableTable;import org.apache.bcel.classfile.Method;import org.apache.bcel.classfile.Utility;import org.apache.bcel.generic.ArrayType;import org.apache.bcel.generic.ObjectType;import org.apache.bcel.generic.Type;import org.jibx.runtime.JiBXException;/** * Wrapper for field or method information. Provides the information needed * for access to either existing or added methods in existing classes. * * @author Dennis M. Sosnoski * @version 1.0 */public class ClassItem{    /** Empty array of strings. */    private static final String[] EMPTY_STRING_ARRAY = new String[0];        /** Tag value for signature attribute. */    private static final byte SIGNATURE_ATTRIBUTE_TAG = 10;        /** Map for primitive type signature variants. */    private static HashMap s_primitiveMap = new HashMap();        /** Map from type name to BCEL type. */    private static HashMap s_typeMap = new HashMap();        /** Map from method signature to array of argument types. */    private static HashMap s_signatureParamsMap = new HashMap();        /** Map from method signature to return type. */    private static HashMap s_signatureTypeMap = new HashMap();        static {        s_primitiveMap.put("boolean", new String[] { "Z", "I" });        s_primitiveMap.put("byte", new String[] { "B", "S", "I" });        s_primitiveMap.put("char", new String[] { "C", "I" });        s_primitiveMap.put("double", new String[] { "D" });        s_primitiveMap.put("float", new String[] { "F" });        s_primitiveMap.put("int", new String[] { "I" });        s_primitiveMap.put("long", new String[] { "J" });        s_primitiveMap.put("short", new String[] { "S", "I" });        s_primitiveMap.put("void", new String[] { "V" });        s_typeMap.put("boolean", Type.BOOLEAN);        s_typeMap.put("byte", Type.BYTE);        s_typeMap.put("char", Type.CHAR);        s_typeMap.put("double", Type.DOUBLE);        s_typeMap.put("float", Type.FLOAT);        s_typeMap.put("int", Type.INT);        s_typeMap.put("long", Type.LONG);        s_typeMap.put("short", Type.SHORT);        s_typeMap.put("void", Type.VOID);    }        /** Owning class information. */    private ClassFile m_classFile;        /** Item name. */    private String m_name;        /** Encoded signature. */    private String m_signature;        /** Fully qualified class name of item type. */    private String m_typeName;        /** Argument types for method. */    private String[] m_argTypes;        /** Wrapped existing item. */    private FieldOrMethod m_item;        /**     * Constructor. Builds a wrapper for an item based on an existing field or     * method.     *     * @param name field or method name     * @param cf owning class information     * @param item field or method information     */         public ClassItem(String name, ClassFile cf, FieldOrMethod item) {        m_classFile = cf;        m_name = name;        m_item = item;        m_signature = item.getSignature();        if (item instanceof Method) {            m_typeName = getTypeFromSignature(m_signature);            m_argTypes = getParametersFromSignature(m_signature);        } else {            m_typeName = Utility.signatureToString(m_signature, false);        }    }        /**     * Get owning class information.     *     * @return owning class information     */         public ClassFile getClassFile() {        return m_classFile;    }        /**     * Get item name.     *     * @return item name     */         public String getName() {        return m_name;    }        /**     * Get item type as fully qualified class name.     *     * @return item type name     */         public String getTypeName() {        return m_typeName;    }        /**     * Get number of arguments for method.     *     * @return argument count for method, or zero if not a method     */         public int getArgumentCount() {        if (m_item instanceof Method) {            return m_argTypes.length;        } else {            return 0;        }    }        /**     * Get argument type as fully qualified class name.     *     * @param index argument number     * @return argument type name     */         public String getArgumentType(int index) {        if (m_item instanceof Method) {            return m_argTypes[index];        } else {            throw new IllegalArgumentException("Internal error: not a method");        }    }        /**     * Get method parameter name.     *     * @return parameter name     */         public String getParameterName(int index) {        if (m_item instanceof Method) {            int position = index;            if (!isStatic()) {                position++;            }            LocalVariableTable vtab = ((Method)m_item).getLocalVariableTable();            if (vtab == null) {                return null;            } else {                return vtab.getLocalVariable(position).getName();            }        } else {            throw new IllegalArgumentException("Internal error: not a method");        }    }        /**     * Get argument types as array of fully qualified class names.     *     * @return array of argument types     */         public String[] getArgumentTypes() {        if (m_item instanceof Method) {            return m_argTypes;        } else {            return null;        }    }        /**     * Get access flags.     *     * @return flags for access type of field or method     */         public int getAccessFlags() {        return m_item.getAccessFlags();    }        /**     * Set access flags.     *     * @param flags access flags for field or method     */         public void setAccessFlags(int flags) {        m_item.setAccessFlags(flags);        m_classFile.setModified();    }    /**     * Make accessible item. Check if this field or method is accessible from     * another class, and if not decreases the access restrictions to make it     * accessible.     *     * @param src class file for required access     * @throws JiBXException if cannot be accessed     */    public void makeAccessible(ClassFile src) throws JiBXException {                // no need to change if already public access        int access = getAccessFlags();        if ((access & Constants.ACC_PUBLIC) == 0) {                        // check for same package as most restrictive case            ClassFile dest = getClassFile();            if (dest.getPackage().equals(src.getPackage())) {                if ((access & Constants.ACC_PRIVATE) != 0) {                    access = access - Constants.ACC_PRIVATE;                }            } else  {                                // check if access is from a subclass of this method class                ClassFile ancestor = src;                while ((ancestor = ancestor.getSuperFile()) != null) {                    if (ancestor == dest) {                        break;                    }                }                                // handle access adjustments based on subclass status                if (ancestor == null) {                    int clear = Constants.ACC_PRIVATE |                        Constants.ACC_PROTECTED;                    access = (access & ~clear) | Constants.ACC_PUBLIC;                } else if ((access & Constants.ACC_PROTECTED) == 0) {                    access = (access & ~Constants.ACC_PRIVATE) |                        Constants.ACC_PROTECTED;                }            }                        // set new access flags            if (access != getAccessFlags()) {                if (dest.isModifiable()) {                    setAccessFlags(access);                } else {                    throw new JiBXException                        ("Unable to change access permissions for " +                        getName() + " in class " + src.getName());                }            }        }    }        /**     * Check if item is a static.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -