169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal/*
269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Javassist, a Java-bytecode translator toolkit.
369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Copyright (C) 1999-2007 Shigeru Chiba. All Rights Reserved.
469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *
569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * The contents of this file are subject to the Mozilla Public License Version
669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 1.1 (the "License"); you may not use this file except in compliance with
769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * the License.  Alternatively, the contents of this file may be used under
869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * the terms of the GNU Lesser General Public License Version 2.1 or later.
969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *
1069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Software distributed under the License is distributed on an "AS IS" basis,
1169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
1269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * for the specific language governing rights and limitations under the
1369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * License.
1469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */
1569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
1669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalpackage javassist.bytecode;
1769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
1869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport java.io.DataInputStream;
1969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport java.io.DataOutputStream;
2069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport java.io.IOException;
2169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport java.util.ArrayList;
2269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport java.util.List;
2369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport java.util.Map;
2469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.ClassPool;
2569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.bytecode.stackmap.MapMaker;
2669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
2769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal/**
2869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <code>method_info</code> structure.
2969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *
3069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @see javassist.CtMethod#getMethodInfo()
3169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @see javassist.CtConstructor#getMethodInfo()
3269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */
3369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalpublic class MethodInfo {
3469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    ConstPool constPool;
3569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    int accessFlags;
3669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    int name;
3769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    String cachedName;
3869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    int descriptor;
3969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    ArrayList attribute; // may be null
4069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
4169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
4269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * If this value is true, Javassist maintains a <code>StackMap</code> attribute
4369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * generated by the <code>preverify</code> tool of J2ME (CLDC).  The initial
4469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * value of this field is <code>false</code>.
4569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
4669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static boolean doPreverify = false;
4769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
4869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
4969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * The name of constructors: <code>&lt;init&gt</code>.
5069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
5169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static final String nameInit = "<init>";
5269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
5369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
5469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * The name of class initializer (static initializer):
5569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * <code>&lt;clinit&gt</code>.
5669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
5769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static final String nameClinit = "<clinit>";
5869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
5969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    private MethodInfo(ConstPool cp) {
6069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        constPool = cp;
6169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        attribute = null;
6269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
6369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
6469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
6569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Constructs a <code>method_info</code> structure. The initial value of
6669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * <code>access_flags</code> is zero.
6769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
6869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param cp
6969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *            a constant pool table
7069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param methodname
7169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *            method name
7269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param desc
7369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *            method descriptor
7469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @see Descriptor
7569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
7669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public MethodInfo(ConstPool cp, String methodname, String desc) {
7769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        this(cp);
7869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        accessFlags = 0;
7969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        name = cp.addUtf8Info(methodname);
8069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        cachedName = methodname;
8169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        descriptor = constPool.addUtf8Info(desc);
8269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
8369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
8469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    MethodInfo(ConstPool cp, DataInputStream in) throws IOException {
8569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        this(cp);
8669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        read(in);
8769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
8869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
8969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
9069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Constructs a copy of <code>method_info</code> structure. Class names
9169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * appearing in the source <code>method_info</code> are renamed according
9269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * to <code>classnameMap</code>.
9369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
9469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * <p>
9569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Note: only <code>Code</code> and <code>Exceptions</code> attributes
9669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * are copied from the source. The other attributes are ignored.
9769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
9869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param cp
9969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *            a constant pool table
10069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param methodname
10169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *            a method name
10269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param src
10369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *            a source <code>method_info</code>
10469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param classnameMap
10569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *            specifies pairs of replaced and substituted name.
10669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @see Descriptor
10769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
10869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public MethodInfo(ConstPool cp, String methodname, MethodInfo src,
10969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            Map classnameMap) throws BadBytecode {
11069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        this(cp);
11169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        read(src, methodname, classnameMap);
11269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
11369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
11469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
11569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns a string representation of the object.
11669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
11769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public String toString() {
11869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return getName() + " " + getDescriptor();
11969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
12069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
12169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
12269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Copies all constant pool items to a given new constant pool
12369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * and replaces the original items with the new ones.
12469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * This is used for garbage collecting the items of removed fields
12569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * and methods.
12669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
12769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param cp    the destination
12869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
12969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    void compact(ConstPool cp) {
13069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        name = cp.addUtf8Info(getName());
13169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        descriptor = cp.addUtf8Info(getDescriptor());
13269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        attribute = AttributeInfo.copyAll(attribute, cp);
13369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        constPool = cp;
13469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
13569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
13669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    void prune(ConstPool cp) {
13769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        ArrayList newAttributes = new ArrayList();
13869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
13969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        AttributeInfo invisibleAnnotations
14069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            = getAttribute(AnnotationsAttribute.invisibleTag);
14169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (invisibleAnnotations != null) {
14269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            invisibleAnnotations = invisibleAnnotations.copy(cp, null);
14369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            newAttributes.add(invisibleAnnotations);
14469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
14569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
14669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        AttributeInfo visibleAnnotations
14769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            = getAttribute(AnnotationsAttribute.visibleTag);
14869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (visibleAnnotations != null) {
14969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            visibleAnnotations = visibleAnnotations.copy(cp, null);
15069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            newAttributes.add(visibleAnnotations);
15169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
15269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
15369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        AttributeInfo parameterInvisibleAnnotations
15469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            = getAttribute(ParameterAnnotationsAttribute.invisibleTag);
15569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (parameterInvisibleAnnotations != null) {
15669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            parameterInvisibleAnnotations = parameterInvisibleAnnotations.copy(cp, null);
15769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            newAttributes.add(parameterInvisibleAnnotations);
15869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
15969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
16069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        AttributeInfo parameterVisibleAnnotations
16169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            = getAttribute(ParameterAnnotationsAttribute.visibleTag);
16269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (parameterVisibleAnnotations != null) {
16369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            parameterVisibleAnnotations = parameterVisibleAnnotations.copy(cp, null);
16469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            newAttributes.add(parameterVisibleAnnotations);
16569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
16669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
16769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        AnnotationDefaultAttribute defaultAttribute
16869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal             = (AnnotationDefaultAttribute) getAttribute(AnnotationDefaultAttribute.tag);
16969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (defaultAttribute != null)
17069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            newAttributes.add(defaultAttribute);
17169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
17269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        ExceptionsAttribute ea = getExceptionsAttribute();
17369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (ea != null)
17469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            newAttributes.add(ea);
17569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
17669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        AttributeInfo signature
17769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            = getAttribute(SignatureAttribute.tag);
17869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (signature != null) {
17969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            signature = signature.copy(cp, null);
18069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            newAttributes.add(signature);
18169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
18269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
18369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        attribute = newAttributes;
18469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        name = cp.addUtf8Info(getName());
18569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        descriptor = cp.addUtf8Info(getDescriptor());
18669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        constPool = cp;
18769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
18869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
18969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
19069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns a method name.
19169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
19269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public String getName() {
19369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal       if (cachedName == null)
19469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal           cachedName = constPool.getUtf8Info(name);
19569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
19669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal       return cachedName;
19769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
19869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
19969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
20069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Sets a method name.
20169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
20269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public void setName(String newName) {
20369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        name = constPool.addUtf8Info(newName);
20469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        cachedName = newName;
20569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
20669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
20769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
20869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns true if this is not a constructor or a class initializer (static
20969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * initializer).
21069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
21169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public boolean isMethod() {
21269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        String n = getName();
21369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return !n.equals(nameInit) && !n.equals(nameClinit);
21469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
21569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
21669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
21769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns a constant pool table used by this method.
21869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
21969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public ConstPool getConstPool() {
22069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return constPool;
22169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
22269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
22369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
22469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns true if this is a constructor.
22569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
22669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public boolean isConstructor() {
22769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return getName().equals(nameInit);
22869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
22969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
23069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
23169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns true if this is a class initializer (static initializer).
23269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
23369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public boolean isStaticInitializer() {
23469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return getName().equals(nameClinit);
23569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
23669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
23769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
23869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns access flags.
23969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
24069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @see AccessFlag
24169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
24269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public int getAccessFlags() {
24369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return accessFlags;
24469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
24569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
24669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
24769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Sets access flags.
24869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
24969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @see AccessFlag
25069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
25169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public void setAccessFlags(int acc) {
25269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        accessFlags = acc;
25369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
25469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
25569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
25669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns a method descriptor.
25769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
25869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @see Descriptor
25969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
26069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public String getDescriptor() {
26169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return constPool.getUtf8Info(descriptor);
26269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
26369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
26469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
26569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Sets a method descriptor.
26669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
26769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @see Descriptor
26869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
26969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public void setDescriptor(String desc) {
27069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (!desc.equals(getDescriptor()))
27169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            descriptor = constPool.addUtf8Info(desc);
27269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
27369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
27469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
27569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns all the attributes.  The returned <code>List</code> object
27669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * is shared with this object.  If you add a new attribute to the list,
27769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * the attribute is also added to the method represented by this
27869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * object.  If you remove an attribute from the list, it is also removed
27969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * from the method.
28069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
28169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @return a list of <code>AttributeInfo</code> objects.
28269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @see AttributeInfo
28369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
28469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public List getAttributes() {
28569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (attribute == null)
28669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            attribute = new ArrayList();
28769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
28869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return attribute;
28969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
29069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
29169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
29269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns the attribute with the specified name. If it is not found, this
29369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * method returns null.
29469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
29569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param name attribute name
29669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @return an <code>AttributeInfo</code> object or null.
29769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @see #getAttributes()
29869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
29969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public AttributeInfo getAttribute(String name) {
30069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return AttributeInfo.lookup(attribute, name);
30169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
30269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
30369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
30469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Appends an attribute. If there is already an attribute with the same
30569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * name, the new one substitutes for it.
30669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
30769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @see #getAttributes()
30869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
30969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public void addAttribute(AttributeInfo info) {
31069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (attribute == null)
31169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            attribute = new ArrayList();
31269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
31369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        AttributeInfo.remove(attribute, info.getName());
31469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        attribute.add(info);
31569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
31669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
31769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
31869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns an Exceptions attribute.
31969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
32069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @return an Exceptions attribute or null if it is not specified.
32169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
32269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public ExceptionsAttribute getExceptionsAttribute() {
32369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        AttributeInfo info = AttributeInfo.lookup(attribute,
32469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                ExceptionsAttribute.tag);
32569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return (ExceptionsAttribute)info;
32669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
32769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
32869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
32969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns a Code attribute.
33069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
33169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @return a Code attribute or null if it is not specified.
33269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
33369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public CodeAttribute getCodeAttribute() {
33469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        AttributeInfo info = AttributeInfo.lookup(attribute, CodeAttribute.tag);
33569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return (CodeAttribute)info;
33669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
33769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
33869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
33969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Removes an Exception attribute.
34069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
34169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public void removeExceptionsAttribute() {
34269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        AttributeInfo.remove(attribute, ExceptionsAttribute.tag);
34369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
34469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
34569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
34669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Adds an Exception attribute.
34769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
34869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * <p>
34969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * The added attribute must share the same constant pool table as this
35069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * <code>method_info</code> structure.
35169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
35269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public void setExceptionsAttribute(ExceptionsAttribute cattr) {
35369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        removeExceptionsAttribute();
35469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (attribute == null)
35569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            attribute = new ArrayList();
35669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
35769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        attribute.add(cattr);
35869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
35969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
36069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
36169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Removes a Code attribute.
36269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
36369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public void removeCodeAttribute() {
36469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        AttributeInfo.remove(attribute, CodeAttribute.tag);
36569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
36669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
36769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
36869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Adds a Code attribute.
36969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
37069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * <p>
37169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * The added attribute must share the same constant pool table as this
37269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * <code>method_info</code> structure.
37369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
37469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public void setCodeAttribute(CodeAttribute cattr) {
37569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        removeCodeAttribute();
37669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (attribute == null)
37769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            attribute = new ArrayList();
37869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
37969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        attribute.add(cattr);
38069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
38169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
38269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
38369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Rebuilds a stack map table if the class file is for Java 6
38469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * or later.  Java 5 or older Java VMs do not recognize a stack
38569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * map table.  If <code>doPreverify</code> is true, this method
38669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * also rebuilds a stack map for J2ME (CLDC).
38769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
38869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param pool          used for making type hierarchy.
38969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param cf            rebuild if this class file is for Java 6 or later.
39069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @see #rebuildStackMap(ClassPool)
39169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @see #rebuildStackMapForME(ClassPool)
39269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @since 3.6
39369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
39469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public void rebuildStackMapIf6(ClassPool pool, ClassFile cf)
39569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        throws BadBytecode
39669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    {
39769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (cf.getMajorVersion() >= ClassFile.JAVA_6)
39869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            rebuildStackMap(pool);
39969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
40069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (doPreverify)
40169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            rebuildStackMapForME(pool);
40269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
40369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
40469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
40569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Rebuilds a stack map table.  If no stack map table is included,
40669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * a new one is created.  If this <code>MethodInfo</code> does not
40769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * include a code attribute, nothing happens.
40869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
40969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param pool          used for making type hierarchy.
41069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @see StackMapTable
41169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @since 3.6
41269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
41369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public void rebuildStackMap(ClassPool pool) throws BadBytecode {
41469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        CodeAttribute ca = getCodeAttribute();
41569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (ca != null) {
41669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            StackMapTable smt = MapMaker.make(pool, this);
41769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            ca.setAttribute(smt);
41869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
41969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
42069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
42169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
42269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Rebuilds a stack map table for J2ME (CLDC).  If no stack map table is included,
42369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * a new one is created.  If this <code>MethodInfo</code> does not
42469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * include a code attribute, nothing happens.
42569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
42669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param pool          used for making type hierarchy.
42769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @see StackMapTable
42869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @since 3.12
42969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
43069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public void rebuildStackMapForME(ClassPool pool) throws BadBytecode {
43169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        CodeAttribute ca = getCodeAttribute();
43269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (ca != null) {
43369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            StackMap sm = MapMaker.make2(pool, this);
43469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            ca.setAttribute(sm);
43569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
43669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
43769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
43869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
43969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns the line number of the source line corresponding to the specified
44069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * bytecode contained in this method.
44169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
44269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param pos
44369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *            the position of the bytecode (&gt;= 0). an index into the code
44469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *            array.
44569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @return -1 if this information is not available.
44669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
44769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public int getLineNumber(int pos) {
44869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        CodeAttribute ca = getCodeAttribute();
44969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (ca == null)
45069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return -1;
45169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
45269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        LineNumberAttribute ainfo = (LineNumberAttribute)ca
45369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                .getAttribute(LineNumberAttribute.tag);
45469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (ainfo == null)
45569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return -1;
45669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
45769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return ainfo.toLineNumber(pos);
45869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
45969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
46069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
46169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Changes a super constructor called by this constructor.
46269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
46369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * <p>
46469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * This method modifies a call to <code>super()</code>, which should be
46569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * at the head of a constructor body, so that a constructor in a different
46669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * super class is called. This method does not change actual parameters.
46769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Hence the new super class must have a constructor with the same signature
46869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * as the original one.
46969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
47069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * <p>
47169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * This method should be called when the super class of the class declaring
47269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * this method is changed.
47369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
47469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * <p>
47569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * This method does not perform anything unless this <code>MethodInfo</code>
47669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * represents a constructor.
47769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
47869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param superclass
47969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *            the new super class
48069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
48169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public void setSuperclass(String superclass) throws BadBytecode {
48269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (!isConstructor())
48369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return;
48469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
48569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        CodeAttribute ca = getCodeAttribute();
48669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        byte[] code = ca.getCode();
48769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        CodeIterator iterator = ca.iterator();
48869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int pos = iterator.skipSuperConstructor();
48969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (pos >= 0) { // not this()
49069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            ConstPool cp = constPool;
49169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            int mref = ByteArray.readU16bit(code, pos + 1);
49269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            int nt = cp.getMethodrefNameAndType(mref);
49369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            int sc = cp.addClassInfo(superclass);
49469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            int mref2 = cp.addMethodrefInfo(sc, nt);
49569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            ByteArray.write16bit(mref2, code, pos + 1);
49669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
49769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
49869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
49969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    private void read(MethodInfo src, String methodname, Map classnames)
50069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            throws BadBytecode {
50169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        ConstPool destCp = constPool;
50269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        accessFlags = src.accessFlags;
50369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        name = destCp.addUtf8Info(methodname);
50469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        cachedName = methodname;
50569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        ConstPool srcCp = src.constPool;
50669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        String desc = srcCp.getUtf8Info(src.descriptor);
50769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        String desc2 = Descriptor.rename(desc, classnames);
50869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        descriptor = destCp.addUtf8Info(desc2);
50969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
51069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        attribute = new ArrayList();
51169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        ExceptionsAttribute eattr = src.getExceptionsAttribute();
51269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (eattr != null)
51369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            attribute.add(eattr.copy(destCp, classnames));
51469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
51569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        CodeAttribute cattr = src.getCodeAttribute();
51669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (cattr != null)
51769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            attribute.add(cattr.copy(destCp, classnames));
51869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
51969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
52069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    private void read(DataInputStream in) throws IOException {
52169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        accessFlags = in.readUnsignedShort();
52269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        name = in.readUnsignedShort();
52369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        descriptor = in.readUnsignedShort();
52469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int n = in.readUnsignedShort();
52569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        attribute = new ArrayList();
52669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        for (int i = 0; i < n; ++i)
52769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            attribute.add(AttributeInfo.read(constPool, in));
52869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
52969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
53069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    void write(DataOutputStream out) throws IOException {
53169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        out.writeShort(accessFlags);
53269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        out.writeShort(name);
53369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        out.writeShort(descriptor);
53469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
53569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (attribute == null)
53669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            out.writeShort(0);
53769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        else {
53869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            out.writeShort(attribute.size());
53969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            AttributeInfo.writeAll(attribute, out);
54069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
54169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
54269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal}
543