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

📄 classfile.java

📁 Javassist是一个开源的分析、编辑和创建Java字节码的类库。是由东京技术学院的数学和计算机科学系的 Shigeru Chiba 所创建的。它已加入了开放源代码JBoss 应用服务器项目,通过使
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        ArrayList list = methods;        int n = list.size();        for (int i = 0; i < n; ++i) {            MethodInfo minfo = (MethodInfo)list.get(i);            String desc = minfo.getDescriptor();            minfo.setDescriptor(Descriptor.rename(desc, classnames));        }        list = fields;        n = list.size();        for (int i = 0; i < n; ++i) {            FieldInfo finfo = (FieldInfo)list.get(i);            String desc = finfo.getDescriptor();            finfo.setDescriptor(Descriptor.rename(desc, classnames));        }    }    /**     * Returns the names of the interfaces implemented by the class.     * The returned array is read only.     */    public String[] getInterfaces() {        if (cachedInterfaces != null)            return cachedInterfaces;        String[] rtn = null;        if (interfaces == null)            rtn = new String[0];        else {            int n = interfaces.length;            String[] list = new String[n];            for (int i = 0; i < n; ++i)                list[i] = constPool.getClassInfo(interfaces[i]);            rtn = list;        }        cachedInterfaces = rtn;        return rtn;    }    /**     * Sets the interfaces.     *      * @param nameList     *            the names of the interfaces.     */    public void setInterfaces(String[] nameList) {        cachedInterfaces = null;        if (nameList != null) {            int n = nameList.length;            interfaces = new int[n];            for (int i = 0; i < n; ++i)                interfaces[i] = constPool.addClassInfo(nameList[i]);        }    }    /**     * Appends an interface to the interfaces implemented by the class.     */    public void addInterface(String name) {        cachedInterfaces = null;        int info = constPool.addClassInfo(name);        if (interfaces == null) {            interfaces = new int[1];            interfaces[0] = info;        }        else {            int n = interfaces.length;            int[] newarray = new int[n + 1];            System.arraycopy(interfaces, 0, newarray, 0, n);            newarray[n] = info;            interfaces = newarray;        }    }    /**     * Returns all the fields declared in the class.     *      * @return a list of <code>FieldInfo</code>.     * @see FieldInfo     */    public List getFields() {        return fields;    }    /**     * Appends a field to the class.     *     * @throws DuplicateMemberException         when the field is already included.     */    public void addField(FieldInfo finfo) throws DuplicateMemberException {        testExistingField(finfo.getName(), finfo.getDescriptor());        fields.add(finfo);    }    private void addField0(FieldInfo finfo) {        fields.add(finfo);    }    private void testExistingField(String name, String descriptor)            throws DuplicateMemberException {        ListIterator it = fields.listIterator(0);        while (it.hasNext()) {            FieldInfo minfo = (FieldInfo)it.next();            if (minfo.getName().equals(name))                throw new DuplicateMemberException("duplicate field: " + name);        }    }    /**     * Returns all the methods declared in the class.     *      * @return a list of <code>MethodInfo</code>.     * @see MethodInfo     */    public List getMethods() {        return methods;    }    /**     * Returns the method with the specified name. If there are multiple methods     * with that name, this method returns one of them.     *      * @return null if no such a method is found.     */    public MethodInfo getMethod(String name) {        ArrayList list = methods;        int n = list.size();        for (int i = 0; i < n; ++i) {            MethodInfo minfo = (MethodInfo)list.get(i);            if (minfo.getName().equals(name))                return minfo;        }        return null;    }    /**     * Returns a static initializer (class initializer), or null if it does not     * exist.     */    public MethodInfo getStaticInitializer() {        return getMethod(MethodInfo.nameClinit);    }    /**     * Appends a method to the class.     *     * @throws DuplicateMemberException         when the method is already included.     */    public void addMethod(MethodInfo minfo) throws DuplicateMemberException {        testExistingMethod(minfo);        methods.add(minfo);    }    private void addMethod0(MethodInfo minfo) {        methods.add(minfo);    }    private void testExistingMethod(MethodInfo newMinfo)        throws DuplicateMemberException    {        String name = newMinfo.getName();        String descriptor = newMinfo.getDescriptor();        ListIterator it = methods.listIterator(0);        while (it.hasNext()) {            MethodInfo minfo = (MethodInfo)it.next();            if (minfo.getName().equals(name)                    && notBridgeMethod(minfo) && notBridgeMethod(newMinfo)                    && Descriptor.eqParamTypes(minfo.getDescriptor(),                                               descriptor))                throw new DuplicateMemberException("duplicate method: " + name                                                 + " in " + this.getName());        }    }    /* For a bridge method, see Sec. 15.12.4.5 of JLS 3rd Ed.     */    private boolean notBridgeMethod(MethodInfo minfo) {        return (minfo.getAccessFlags() & AccessFlag.BRIDGE) == 0;    }    /**     * Returns all the attributes.  The returned <code>List</code> object     * is shared with this object.  If you add a new attribute to the list,     * the attribute is also added to the classs file represented by this     * object.  If you remove an attribute from the list, it is also removed     * from the class file.     *      * @return a list of <code>AttributeInfo</code> objects.     * @see AttributeInfo     */    public List getAttributes() {        return attributes;    }    /**     * Returns the attribute with the specified name.  If there are multiple     * attributes with that name, this method returns either of them.   It     * returns null if the specified attributed is not found.     *      * @param name          attribute name     * @see #getAttributes()     */    public AttributeInfo getAttribute(String name) {        LinkedList list = attributes;        int n = list.size();        for (int i = 0; i < n; ++i) {            AttributeInfo ai = (AttributeInfo)list.get(i);            if (ai.getName().equals(name))                return ai;        }        return null;    }    /**     * Appends an attribute. If there is already an attribute with the same     * name, the new one substitutes for it.     *     * @see #getAttributes()     */    public void addAttribute(AttributeInfo info) {        AttributeInfo.remove(attributes, info.getName());        attributes.add(info);    }    /**     * Returns the source file containing this class.     *      * @return null if this information is not available.     */    public String getSourceFile() {        SourceFileAttribute sf            = (SourceFileAttribute)getAttribute(SourceFileAttribute.tag);        if (sf == null)            return null;        else            return sf.getFileName();    }    private void read(DataInputStream in) throws IOException {        int i, n;        int magic = in.readInt();        if (magic != 0xCAFEBABE)            throw new IOException("non class file");        minor = in.readUnsignedShort();        major = in.readUnsignedShort();        constPool = new ConstPool(in);        accessFlags = in.readUnsignedShort();        thisClass = in.readUnsignedShort();        constPool.setThisClassInfo(thisClass);        superClass = in.readUnsignedShort();        n = in.readUnsignedShort();        if (n == 0)            interfaces = null;        else {            interfaces = new int[n];            for (i = 0; i < n; ++i)                interfaces[i] = in.readUnsignedShort();        }        ConstPool cp = constPool;        n = in.readUnsignedShort();        fields = new ArrayList();        for (i = 0; i < n; ++i)            addField0(new FieldInfo(cp, in));        n = in.readUnsignedShort();        methods = new ArrayList();        for (i = 0; i < n; ++i)            addMethod0(new MethodInfo(cp, in));        attributes = new LinkedList();        n = in.readUnsignedShort();        for (i = 0; i < n; ++i)            addAttribute(AttributeInfo.read(cp, in));        thisclassname = constPool.getClassInfo(thisClass);    }    /**     * Writes a class file represened by this object into an output stream.     */    public void write(DataOutputStream out) throws IOException {        int i, n;        out.writeInt(0xCAFEBABE); // magic        out.writeShort(minor); // minor version        out.writeShort(major); // major version        constPool.write(out); // constant pool        out.writeShort(accessFlags);        out.writeShort(thisClass);        out.writeShort(superClass);        if (interfaces == null)            n = 0;        else            n = interfaces.length;        out.writeShort(n);        for (i = 0; i < n; ++i)            out.writeShort(interfaces[i]);        ArrayList list = fields;        n = list.size();        out.writeShort(n);        for (i = 0; i < n; ++i) {            FieldInfo finfo = (FieldInfo)list.get(i);            finfo.write(out);        }        list = methods;        n = list.size();        out.writeShort(n);        for (i = 0; i < n; ++i) {            MethodInfo minfo = (MethodInfo)list.get(i);            minfo.write(out);        }        out.writeShort(attributes.size());        AttributeInfo.writeAll(attributes, out);    }    /**     * Get the Major version.     *      * @return the major version     */    public int getMajorVersion() {        return major;    }    /**     * Set the major version.     *      * @param major     *            the major version     */    public void setMajorVersion(int major) {        this.major = major;    }    /**     * Get the minor version.     *      * @return the minor version     */    public int getMinorVersion() {        return minor;    }    /**     * Set the minor version.     *      * @param minor     *            the minor version     */    public void setMinorVersion(int minor) {        this.minor = minor;    }    /**     * Sets the major and minor version to Java 5.     *     * If the major version is older than 49, Java 5     * extensions such as annotations are ignored     * by the JVM.     */    public void setVersionToJava5() {        this.major = 49;        this.minor = 0;    }}

⌨️ 快捷键说明

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