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;
1769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
1869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.bytecode.*;
1969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.compiler.Javac;
2069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.compiler.SymbolTable;
2169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.compiler.CompileError;
2269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.compiler.ast.ASTree;
2369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.compiler.ast.IntConst;
2469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.compiler.ast.DoubleConst;
2569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.compiler.ast.StringL;
2669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
2769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal/**
2869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * An instance of CtField represents a field.
2969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *
3069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @see CtClass#getDeclaredFields()
3169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */
3269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalpublic class CtField extends CtMember {
3369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    static final String javaLangString = "java.lang.String";
3469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
3569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    protected FieldInfo fieldInfo;
3669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
3769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
3869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Creates a <code>CtField</code> object.
3969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * The created field must be added to a class
4069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * with <code>CtClass.addField()</code>.
4169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * An initial value of the field is specified
4269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * by a <code>CtField.Initializer</code> object.
4369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
4469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * <p>If getter and setter methods are needed,
4569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * call <code>CtNewMethod.getter()</code> and
4669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * <code>CtNewMethod.setter()</code>.
4769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
4869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param type              field type
4969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param name              field name
5069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param declaring         the class to which the field will be added.
5169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
5269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @see CtClass#addField(CtField)
5369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @see CtNewMethod#getter(String,CtField)
5469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @see CtNewMethod#setter(String,CtField)
5569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @see CtField.Initializer
5669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
5769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public CtField(CtClass type, String name, CtClass declaring)
5869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        throws CannotCompileException
5969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    {
6069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        this(Descriptor.of(type), name, declaring);
6169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
6269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
6369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
6469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Creates a copy of the given field.
6569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * The created field must be added to a class
6669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * with <code>CtClass.addField()</code>.
6769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * An initial value of the field is specified
6869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * by a <code>CtField.Initializer</code> object.
6969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
7069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * <p>If getter and setter methods are needed,
7169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * call <code>CtNewMethod.getter()</code> and
7269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * <code>CtNewMethod.setter()</code>.
7369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
7469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param src               the original field
7569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param declaring         the class to which the field will be added.
7669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @see CtNewMethod#getter(String,CtField)
7769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @see CtNewMethod#setter(String,CtField)
7869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @see CtField.Initializer
7969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
8069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public CtField(CtField src, CtClass declaring)
8169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        throws CannotCompileException
8269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    {
8369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        this(src.fieldInfo.getDescriptor(), src.fieldInfo.getName(),
8469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal             declaring);
8569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        java.util.ListIterator iterator
8669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            = src.fieldInfo.getAttributes().listIterator();
8769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        FieldInfo fi = fieldInfo;
8869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        fi.setAccessFlags(src.fieldInfo.getAccessFlags());
8969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        ConstPool cp = fi.getConstPool();
9069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        while (iterator.hasNext()) {
9169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            AttributeInfo ainfo = (AttributeInfo)iterator.next();
9269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            fi.addAttribute(ainfo.copy(cp, null));
9369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
9469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
9569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
9669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    private CtField(String typeDesc, String name, CtClass clazz)
9769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        throws CannotCompileException
9869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    {
9969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        super(clazz);
10069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        ClassFile cf = clazz.getClassFile2();
10169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (cf == null)
10269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            throw new CannotCompileException("bad declaring class: "
10369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                                             + clazz.getName());
10469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
10569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        fieldInfo = new FieldInfo(cf.getConstPool(), name, typeDesc);
10669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
10769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
10869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    CtField(FieldInfo fi, CtClass clazz) {
10969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        super(clazz);
11069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        fieldInfo = fi;
11169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
11269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
11369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
11469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns a String representation of the object.
11569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
11669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public String toString() {
11769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return getDeclaringClass().getName() + "." + getName()
11869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal               + ":" + fieldInfo.getDescriptor();
11969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
12069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
12169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    protected void extendToString(StringBuffer buffer) {
12269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        buffer.append(' ');
12369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        buffer.append(getName());
12469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        buffer.append(' ');
12569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        buffer.append(fieldInfo.getDescriptor());
12669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
12769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
12869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /* Javac.CtFieldWithInit overrides.
12969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
13069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    protected ASTree getInitAST() { return null; }
13169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
13269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /* Called by CtClassType.addField().
13369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
13469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    Initializer getInit() {
13569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        ASTree tree = getInitAST();
13669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (tree == null)
13769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return null;
13869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        else
13969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return Initializer.byExpr(tree);
14069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
14169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
14269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
14369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Compiles the given source code and creates a field.
14469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Examples of the source code are:
14569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
14669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * <ul><pre>
14769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * "public String name;"
14869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * "public int k = 3;"</pre></ul>
14969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
15069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * <p>Note that the source code ends with <code>';'</code>
15169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * (semicolon).
15269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
15369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param src               the source text.
15469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param declaring    the class to which the created field is added.
15569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
15669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static CtField make(String src, CtClass declaring)
15769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        throws CannotCompileException
15869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    {
15969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        Javac compiler = new Javac(declaring);
16069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        try {
16169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            CtMember obj = compiler.compile(src);
16269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            if (obj instanceof CtField)
16369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                return (CtField)obj; // an instance of Javac.CtFieldWithInit
16469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
16569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        catch (CompileError e) {
16669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            throw new CannotCompileException(e);
16769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
16869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
16969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        throw new CannotCompileException("not a field");
17069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
17169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
17269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
17369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns the FieldInfo representing the field in the class file.
17469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
17569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public FieldInfo getFieldInfo() {
17669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        declaringClass.checkModify();
17769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return fieldInfo;
17869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
17969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
18069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
18169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns the FieldInfo representing the field in the class
18269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * file (read only).
18369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Normal applications do not need calling this method.  Use
18469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * <code>getFieldInfo()</code>.
18569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
18669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * <p>The <code>FieldInfo</code> object obtained by this method
18769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * is read only.  Changes to this object might not be reflected
18869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * on a class file generated by <code>toBytecode()</code>,
18969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * <code>toClass()</code>, etc in <code>CtClass</code>.
19069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
19169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * <p>This method is available even if the <code>CtClass</code>
19269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * containing this field is frozen.  However, if the class is
19369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * frozen, the <code>FieldInfo</code> might be also pruned.
19469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
19569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @see #getFieldInfo()
19669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @see CtClass#isFrozen()
19769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @see CtClass#prune()
19869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
19969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public FieldInfo getFieldInfo2() { return fieldInfo; }
20069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
20169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
20269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns the class declaring the field.
20369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
20469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public CtClass getDeclaringClass() {
20569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        // this is redundant but for javadoc.
20669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return super.getDeclaringClass();
20769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
20869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
20969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
21069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns the name of the field.
21169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
21269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public String getName() {
21369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return fieldInfo.getName();
21469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
21569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
21669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
21769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Changes the name of the field.
21869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
21969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public void setName(String newName) {
22069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        declaringClass.checkModify();
22169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        fieldInfo.setName(newName);
22269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
22369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
22469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
22569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns the encoded modifiers of the field.
22669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
22769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @see Modifier
22869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
22969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public int getModifiers() {
23069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return AccessFlag.toModifier(fieldInfo.getAccessFlags());
23169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
23269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
23369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
23469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Sets the encoded modifiers of the field.
23569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
23669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @see Modifier
23769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
23869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public void setModifiers(int mod) {
23969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        declaringClass.checkModify();
24069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        fieldInfo.setAccessFlags(AccessFlag.of(mod));
24169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
24269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
24369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
24469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns true if the class has the specified annotation class.
24569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
24669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param clz the annotation class.
24769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @return <code>true</code> if the annotation is found, otherwise <code>false</code>.
24869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @since 3.11
24969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
25069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public boolean hasAnnotation(Class clz) {
25169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        FieldInfo fi = getFieldInfo2();
25269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        AnnotationsAttribute ainfo = (AnnotationsAttribute)
25369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    fi.getAttribute(AnnotationsAttribute.invisibleTag);
25469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        AnnotationsAttribute ainfo2 = (AnnotationsAttribute)
25569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    fi.getAttribute(AnnotationsAttribute.visibleTag);
25669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return CtClassType.hasAnnotationType(clz, getDeclaringClass().getClassPool(),
25769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                                             ainfo, ainfo2);
25869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
25969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
26069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
26169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns the annotation if the class has the specified annotation class.
26269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * For example, if an annotation <code>@Author</code> is associated
26369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * with this field, an <code>Author</code> object is returned.
26469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * The member values can be obtained by calling methods on
26569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * the <code>Author</code> object.
26669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
26769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param clz the annotation class.
26869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @return the annotation if found, otherwise <code>null</code>.
26969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @since 3.11
27069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
27169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public Object getAnnotation(Class clz) throws ClassNotFoundException {
27269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        FieldInfo fi = getFieldInfo2();
27369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        AnnotationsAttribute ainfo = (AnnotationsAttribute)
27469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    fi.getAttribute(AnnotationsAttribute.invisibleTag);
27569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        AnnotationsAttribute ainfo2 = (AnnotationsAttribute)
27669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    fi.getAttribute(AnnotationsAttribute.visibleTag);
27769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return CtClassType.getAnnotationType(clz, getDeclaringClass().getClassPool(),
27869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                                             ainfo, ainfo2);
27969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
28069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
28169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
28269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns the annotations associated with this field.
28369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
28469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @return an array of annotation-type objects.
28569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @see #getAvailableAnnotations()
28669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @since 3.1
28769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
28869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public Object[] getAnnotations() throws ClassNotFoundException {
28969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return getAnnotations(false);
29069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
29169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
29269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
29369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns the annotations associated with this field.
29469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * If any annotations are not on the classpath, they are not included
29569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * in the returned array.
29669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
29769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @return an array of annotation-type objects.
29869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @see #getAnnotations()
29969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @since 3.3
30069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
30169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public Object[] getAvailableAnnotations(){
30269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        try {
30369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return getAnnotations(true);
30469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
30569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        catch (ClassNotFoundException e) {
30669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal           throw new RuntimeException("Unexpected exception", e);
30769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
30869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
30969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
31069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    private Object[] getAnnotations(boolean ignoreNotFound) throws ClassNotFoundException {
31169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        FieldInfo fi = getFieldInfo2();
31269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        AnnotationsAttribute ainfo = (AnnotationsAttribute)
31369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    fi.getAttribute(AnnotationsAttribute.invisibleTag);
31469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        AnnotationsAttribute ainfo2 = (AnnotationsAttribute)
31569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    fi.getAttribute(AnnotationsAttribute.visibleTag);
31669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return CtClassType.toAnnotationType(ignoreNotFound, getDeclaringClass().getClassPool(),
31769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                                            ainfo, ainfo2);
31869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
31969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
32069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
32169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns the character string representing the type of the field.
32269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * The field signature is represented by a character string
32369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * called a field descriptor, which is defined in the JVM specification.
32469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * If two fields have the same type,
32569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * <code>getSignature()</code> returns the same string.
32669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
32769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * <p>Note that the returned string is not the type signature
32869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * contained in the <code>SignatureAttirbute</code>.  It is
32969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * a descriptor.  To obtain a type signature, call the following
33069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * methods:
33169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
33269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * <ul><pre>getFieldInfo().getAttribute(SignatureAttribute.tag)
33369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * </pre></ul>
33469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
33569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @see javassist.bytecode.Descriptor
33669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @see javassist.bytecode.SignatureAttribute
33769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
33869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public String getSignature() {
33969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return fieldInfo.getDescriptor();
34069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
34169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
34269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
34369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns the type of the field.
34469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
34569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public CtClass getType() throws NotFoundException {
34669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return Descriptor.toCtClass(fieldInfo.getDescriptor(),
34769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                                    declaringClass.getClassPool());
34869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
34969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
35069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
35169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Sets the type of the field.
35269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
35369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public void setType(CtClass clazz) {
35469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        declaringClass.checkModify();
35569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        fieldInfo.setDescriptor(Descriptor.of(clazz));
35669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
35769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
35869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
35969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns the value of this field if it is a constant field.
36069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * This method works only if the field type is a primitive type
36169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * or <code>String</code> type.  Otherwise, it returns <code>null</code>.
36269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * A constant field is <code>static</code> and <code>final</code>.
36369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
36469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @return  a <code>Integer</code>, <code>Long</code>, <code>Float</code>,
36569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *          <code>Double</code>, <code>Boolean</code>,
36669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *          or <code>String</code> object
36769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *          representing the constant value.
36869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *          <code>null</code> if it is not a constant field
36969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *          or if the field type is not a primitive type
37069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *          or <code>String</code>.
37169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
37269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public Object getConstantValue() {
37369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        // When this method is modified,
37469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        // see also getConstantFieldValue() in TypeChecker.
37569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
37669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int index = fieldInfo.getConstantValue();
37769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (index == 0)
37869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return null;
37969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
38069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        ConstPool cp = fieldInfo.getConstPool();
38169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        switch (cp.getTag(index)) {
38269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            case ConstPool.CONST_Long :
38369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                return new Long(cp.getLongInfo(index));
38469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            case ConstPool.CONST_Float :
38569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                return new Float(cp.getFloatInfo(index));
38669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            case ConstPool.CONST_Double :
38769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                return new Double(cp.getDoubleInfo(index));
38869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            case ConstPool.CONST_Integer :
38969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                int value = cp.getIntegerInfo(index);
39069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                // "Z" means boolean type.
39169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                if ("Z".equals(fieldInfo.getDescriptor()))
39269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    return new Boolean(value != 0);
39369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                else
39469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    return new Integer(value);
39569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            case ConstPool.CONST_String :
39669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                return cp.getStringInfo(index);
39769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            default :
39869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                throw new RuntimeException("bad tag: " + cp.getTag(index)
39969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                                           + " at " + index);
40069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
40169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
40269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
40369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
40469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Obtains an attribute with the given name.
40569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * If that attribute is not found in the class file, this
40669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * method returns null.
40769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
40869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * <p>Note that an attribute is a data block specified by
40969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * the class file format.
41069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * See {@link javassist.bytecode.AttributeInfo}.
41169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
41269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param name              attribute name
41369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
41469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public byte[] getAttribute(String name) {
41569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        AttributeInfo ai = fieldInfo.getAttribute(name);
41669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (ai == null)
41769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return null;
41869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        else
41969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return ai.get();
42069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
42169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
42269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
42369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Adds an attribute. The attribute is saved in the class file.
42469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
42569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * <p>Note that an attribute is a data block specified by
42669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * the class file format.
42769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * See {@link javassist.bytecode.AttributeInfo}.
42869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
42969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param name      attribute name
43069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param data      attribute value
43169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
43269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public void setAttribute(String name, byte[] data) {
43369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        declaringClass.checkModify();
43469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        fieldInfo.addAttribute(new AttributeInfo(fieldInfo.getConstPool(),
43569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                                                 name, data));
43669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
43769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
43869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    // inner classes
43969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
44069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
44169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Instances of this class specify how to initialize a field.
44269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * <code>Initializer</code> is passed to
44369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * <code>CtClass.addField()</code> with a <code>CtField</code>.
44469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
44569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * <p>This class cannot be instantiated with the <code>new</code> operator.
44669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Factory methods such as <code>byParameter()</code> and
44769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * <code>byNew</code>
44869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * must be used for the instantiation.  They create a new instance with
44969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * the given parameters and return it.
45069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
45169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @see CtClass#addField(CtField,CtField.Initializer)
45269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
45369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static abstract class Initializer {
45469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        /**
45569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * Makes an initializer that assigns a constant integer value.
45669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * The field must be integer, short, char, or byte type.
45769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         */
45869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        public static Initializer constant(int i) {
45969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return new IntInitializer(i);
46069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
46169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
46269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        /**
46369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * Makes an initializer that assigns a constant boolean value.
46469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * The field must be boolean type.
46569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         */
46669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        public static Initializer constant(boolean b) {
46769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return new IntInitializer(b ? 1 : 0);
46869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
46969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
47069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        /**
47169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * Makes an initializer that assigns a constant long value.
47269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * The field must be long type.
47369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         */
47469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        public static Initializer constant(long l) {
47569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return new LongInitializer(l);
47669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
47769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
47869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        /**
47969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * Makes an initializer that assigns a constant float value.
48069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * The field must be float type.
48169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         */
48269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        public static Initializer constant(float l) {
48369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return new FloatInitializer(l);
48469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
48569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
48669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        /**
48769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * Makes an initializer that assigns a constant double value.
48869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * The field must be double type.
48969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         */
49069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        public static Initializer constant(double d) {
49169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return new DoubleInitializer(d);
49269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
49369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
49469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        /**
49569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * Makes an initializer that assigns a constant string value.
49669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * The field must be <code>java.lang.String</code> type.
49769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         */
49869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        public static Initializer constant(String s) {
49969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return new StringInitializer(s);
50069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
50169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
50269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        /**
50369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * Makes an initializer using a constructor parameter.
50469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
50569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * <p>The initial value is the
50669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * N-th parameter given to the constructor of the object including
50769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * the field.  If the constructor takes less than N parameters,
50869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * the field is not initialized.
50969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * If the field is static, it is never initialized.
51069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
51169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * @param nth           the n-th (&gt;= 0) parameter is used as
51269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *                      the initial value.
51369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *                      If nth is 0, then the first parameter is
51469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *                      used.
51569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         */
51669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        public static Initializer byParameter(int nth) {
51769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            ParamInitializer i = new ParamInitializer();
51869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            i.nthParam = nth;
51969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return i;
52069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
52169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
52269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        /**
52369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * Makes an initializer creating a new object.
52469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
52569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * <p>This initializer creates a new object and uses it as the initial
52669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * value of the field.  The constructor of the created object receives
52769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * the parameter:
52869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
52969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * <ul><code>Object obj</code> - the object including the field.<br>
53069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * </ul>
53169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
53269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * <p>If the initialized field is static, then the constructor does
53369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * not receive any parameters.
53469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
53569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * @param objectType    the class instantiated for the initial value.
53669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         */
53769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        public static Initializer byNew(CtClass objectType) {
53869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            NewInitializer i = new NewInitializer();
53969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            i.objectType = objectType;
54069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            i.stringParams = null;
54169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            i.withConstructorParams = false;
54269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return i;
54369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
54469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
54569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        /**
54669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * Makes an initializer creating a new object.
54769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
54869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * <p>This initializer creates a new object and uses it as the initial
54969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * value of the field.  The constructor of the created object receives
55069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * the parameters:
55169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
55269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * <ul><code>Object obj</code> - the object including the field.<br>
55369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *     <code>String[] strs</code> - the character strings specified
55469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *                              by <code>stringParams</code><br>
55569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * </ul>
55669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
55769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * <p>If the initialized field is static, then the constructor
55869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * receives only <code>strs</code>.
55969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
56069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * @param objectType    the class instantiated for the initial value.
56169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * @param stringParams  the array of strings passed to the
56269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *                      constructor.
56369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         */
56469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        public static Initializer byNew(CtClass objectType,
56569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                                             String[] stringParams) {
56669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            NewInitializer i = new NewInitializer();
56769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            i.objectType = objectType;
56869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            i.stringParams = stringParams;
56969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            i.withConstructorParams = false;
57069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return i;
57169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
57269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
57369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        /**
57469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * Makes an initializer creating a new object.
57569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
57669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * <p>This initializer creates a new object and uses it as the initial
57769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * value of the field.  The constructor of the created object receives
57869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * the parameters:
57969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
58069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * <ul><code>Object obj</code> - the object including the field.<br>
58169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *     <code>Object[] args</code> - the parameters passed to the
58269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *                      constructor of the object including the
58369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *                      filed.
58469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * </ul>
58569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
58669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * <p>If the initialized field is static, then the constructor does
58769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * not receive any parameters.
58869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
58969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * @param objectType    the class instantiated for the initial value.
59069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
59169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * @see javassist.CtField.Initializer#byNewArray(CtClass,int)
59269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * @see javassist.CtField.Initializer#byNewArray(CtClass,int[])
59369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         */
59469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        public static Initializer byNewWithParams(CtClass objectType) {
59569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            NewInitializer i = new NewInitializer();
59669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            i.objectType = objectType;
59769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            i.stringParams = null;
59869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            i.withConstructorParams = true;
59969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return i;
60069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
60169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
60269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        /**
60369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * Makes an initializer creating a new object.
60469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
60569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * <p>This initializer creates a new object and uses it as the initial
60669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * value of the field.  The constructor of the created object receives
60769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * the parameters:
60869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
60969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * <ul><code>Object obj</code> - the object including the field.<br>
61069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *     <code>String[] strs</code> - the character strings specified
61169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *                              by <code>stringParams</code><br>
61269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *     <code>Object[] args</code> - the parameters passed to the
61369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *                      constructor of the object including the
61469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *                      filed.
61569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * </ul>
61669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
61769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * <p>If the initialized field is static, then the constructor receives
61869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * only <code>strs</code>.
61969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
62069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * @param objectType    the class instantiated for the initial value.
62169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * @param stringParams  the array of strings passed to the
62269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *                              constructor.
62369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         */
62469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        public static Initializer byNewWithParams(CtClass objectType,
62569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                                               String[] stringParams) {
62669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            NewInitializer i = new NewInitializer();
62769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            i.objectType = objectType;
62869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            i.stringParams = stringParams;
62969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            i.withConstructorParams = true;
63069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return i;
63169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
63269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
63369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        /**
63469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * Makes an initializer calling a static method.
63569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
63669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * <p>This initializer calls a static method and uses the returned
63769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * value as the initial value of the field.
63869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * The called method receives the parameters:
63969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
64069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * <ul><code>Object obj</code> - the object including the field.<br>
64169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * </ul>
64269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
64369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * <p>If the initialized field is static, then the method does
64469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * not receive any parameters.
64569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
64669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * <p>The type of the returned value must be the same as the field
64769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * type.
64869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
64969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * @param methodClass   the class that the static method is
65069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *                              declared in.
65169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * @param methodName    the name of the satic method.
65269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         */
65369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        public static Initializer byCall(CtClass methodClass,
65469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                                              String methodName) {
65569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            MethodInitializer i = new MethodInitializer();
65669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            i.objectType = methodClass;
65769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            i.methodName = methodName;
65869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            i.stringParams = null;
65969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            i.withConstructorParams = false;
66069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return i;
66169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
66269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
66369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        /**
66469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * Makes an initializer calling a static method.
66569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
66669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * <p>This initializer calls a static method and uses the returned
66769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * value as the initial value of the field.  The called method
66869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * receives the parameters:
66969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
67069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * <ul><code>Object obj</code> - the object including the field.<br>
67169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *     <code>String[] strs</code> - the character strings specified
67269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *                              by <code>stringParams</code><br>
67369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * </ul>
67469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
67569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * <p>If the initialized field is static, then the method
67669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * receive only <code>strs</code>.
67769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
67869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * <p>The type of the returned value must be the same as the field
67969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * type.
68069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
68169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * @param methodClass   the class that the static method is
68269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *                              declared in.
68369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * @param methodName    the name of the satic method.
68469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * @param stringParams  the array of strings passed to the
68569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *                              static method.
68669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         */
68769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        public static Initializer byCall(CtClass methodClass,
68869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                                              String methodName,
68969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                                              String[] stringParams) {
69069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            MethodInitializer i = new MethodInitializer();
69169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            i.objectType = methodClass;
69269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            i.methodName = methodName;
69369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            i.stringParams = stringParams;
69469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            i.withConstructorParams = false;
69569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return i;
69669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
69769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
69869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        /**
69969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * Makes an initializer calling a static method.
70069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
70169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * <p>This initializer calls a static method and uses the returned
70269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * value as the initial value of the field.  The called method
70369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * receives the parameters:
70469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
70569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * <ul><code>Object obj</code> - the object including the field.<br>
70669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *     <code>Object[] args</code> - the parameters passed to the
70769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *                      constructor of the object including the
70869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *                      filed.
70969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * </ul>
71069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
71169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * <p>If the initialized field is static, then the method does
71269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * not receive any parameters.
71369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
71469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * <p>The type of the returned value must be the same as the field
71569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * type.
71669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
71769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * @param methodClass   the class that the static method is
71869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *                              declared in.
71969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * @param methodName    the name of the satic method.
72069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         */
72169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        public static Initializer byCallWithParams(CtClass methodClass,
72269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                                                        String methodName) {
72369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            MethodInitializer i = new MethodInitializer();
72469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            i.objectType = methodClass;
72569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            i.methodName = methodName;
72669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            i.stringParams = null;
72769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            i.withConstructorParams = true;
72869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return i;
72969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
73069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
73169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        /**
73269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * Makes an initializer calling a static method.
73369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
73469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * <p>This initializer calls a static method and uses the returned
73569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * value as the initial value of the field.  The called method
73669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * receives the parameters:
73769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
73869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * <ul><code>Object obj</code> - the object including the field.<br>
73969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *     <code>String[] strs</code> - the character strings specified
74069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *                              by <code>stringParams</code><br>
74169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *     <code>Object[] args</code> - the parameters passed to the
74269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *                      constructor of the object including the
74369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *                      filed.
74469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * </ul>
74569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
74669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * <p>If the initialized field is static, then the method
74769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * receive only <code>strs</code>.
74869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
74969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * <p>The type of the returned value must be the same as the field
75069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * type.
75169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
75269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * @param methodClass   the class that the static method is
75369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *                              declared in.
75469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * @param methodName    the name of the satic method.
75569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * @param stringParams  the array of strings passed to the
75669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *                              static method.
75769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         */
75869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        public static Initializer byCallWithParams(CtClass methodClass,
75969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                                String methodName, String[] stringParams) {
76069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            MethodInitializer i = new MethodInitializer();
76169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            i.objectType = methodClass;
76269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            i.methodName = methodName;
76369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            i.stringParams = stringParams;
76469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            i.withConstructorParams = true;
76569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return i;
76669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
76769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
76869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        /**
76969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * Makes an initializer creating a new array.
77069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
77169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * @param type  the type of the array.
77269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * @param size  the size of the array.
77369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * @throws NotFoundException    if the type of the array components
77469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *                              is not found.
77569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         */
77669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        public static Initializer byNewArray(CtClass type, int size)
77769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            throws NotFoundException
77869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        {
77969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return new ArrayInitializer(type.getComponentType(), size);
78069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
78169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
78269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        /**
78369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * Makes an initializer creating a new multi-dimensional array.
78469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
78569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * @param type  the type of the array.
78669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * @param sizes an <code>int</code> array of the size in every
78769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *                      dimension.
78869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *                      The first element is the size in the first
78969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *                      dimension.  The second is in the second, etc.
79069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         */
79169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        public static Initializer byNewArray(CtClass type, int[] sizes) {
79269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return new MultiArrayInitializer(type, sizes);
79369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
79469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
79569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        /**
79669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * Makes an initializer.
79769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
79869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * @param source        initializer expression.
79969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         */
80069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        public static Initializer byExpr(String source) {
80169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return new CodeInitializer(source);
80269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
80369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
80469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        static Initializer byExpr(ASTree source) {
80569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return new PtreeInitializer(source);
80669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
80769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
80869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        // Check whether this initializer is valid for the field type.
80969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        // If it is invaild, this method throws an exception.
81069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        void check(String desc) throws CannotCompileException {}
81169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
81269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        // produce codes for initialization
81369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        abstract int compile(CtClass type, String name, Bytecode code,
81469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                             CtClass[] parameters, Javac drv)
81569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            throws CannotCompileException;
81669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
81769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        // produce codes for initialization
81869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        abstract int compileIfStatic(CtClass type, String name,
81969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                Bytecode code, Javac drv) throws CannotCompileException;
82069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
82169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        // returns the index of CONSTANT_Integer_info etc
82269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        // if the value is constant.  Otherwise, 0.
82369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int getConstantValue(ConstPool cp, CtClass type) { return 0; }
82469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
82569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
82669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    static abstract class CodeInitializer0 extends Initializer {
82769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        abstract void compileExpr(Javac drv) throws CompileError;
82869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
82969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int compile(CtClass type, String name, Bytecode code,
83069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    CtClass[] parameters, Javac drv)
83169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            throws CannotCompileException
83269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        {
83369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            try {
83469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                code.addAload(0);
83569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                compileExpr(drv);
83669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
83769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                return code.getMaxStack();
83869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            }
83969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            catch (CompileError e) {
84069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                throw new CannotCompileException(e);
84169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            }
84269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
84369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
84469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int compileIfStatic(CtClass type, String name, Bytecode code,
84569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                            Javac drv) throws CannotCompileException
84669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        {
84769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            try {
84869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                compileExpr(drv);
84969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
85069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                return code.getMaxStack();
85169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            }
85269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            catch (CompileError e) {
85369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                throw new CannotCompileException(e);
85469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            }
85569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
85669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
85769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int getConstantValue2(ConstPool cp, CtClass type, ASTree tree) {
85869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            if (type.isPrimitive()) {
85969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                if (tree instanceof IntConst) {
86069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    long value = ((IntConst)tree).get();
86169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    if (type == CtClass.doubleType)
86269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                        return cp.addDoubleInfo((double)value);
86369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    else if (type == CtClass.floatType)
86469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                        return cp.addFloatInfo((float)value);
86569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    else if (type == CtClass.longType)
86669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                        return cp.addLongInfo(value);
86769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    else  if (type != CtClass.voidType)
86869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                        return cp.addIntegerInfo((int)value);
86969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                }
87069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                else if (tree instanceof DoubleConst) {
87169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    double value = ((DoubleConst)tree).get();
87269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    if (type == CtClass.floatType)
87369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                        return cp.addFloatInfo((float)value);
87469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    else if (type == CtClass.doubleType)
87569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                        return cp.addDoubleInfo(value);
87669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                }
87769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            }
87869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            else if (tree instanceof StringL
87969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                     && type.getName().equals(javaLangString))
88069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                return cp.addStringInfo(((StringL)tree).get());
88169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
88269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return 0;
88369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
88469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
88569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
88669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    static class CodeInitializer extends CodeInitializer0 {
88769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        private String expression;
88869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
88969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        CodeInitializer(String expr) { expression = expr; }
89069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
89169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        void compileExpr(Javac drv) throws CompileError {
89269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            drv.compileExpr(expression);
89369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
89469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
89569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int getConstantValue(ConstPool cp, CtClass type) {
89669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            try {
89769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                ASTree t = Javac.parseExpr(expression, new SymbolTable());
89869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                return getConstantValue2(cp, type, t);
89969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            }
90069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            catch (CompileError e) {
90169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                return 0;
90269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            }
90369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
90469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
90569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
90669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    static class PtreeInitializer extends CodeInitializer0 {
90769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        private ASTree expression;
90869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
90969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        PtreeInitializer(ASTree expr) { expression = expr; }
91069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
91169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        void compileExpr(Javac drv) throws CompileError {
91269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            drv.compileExpr(expression);
91369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
91469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
91569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int getConstantValue(ConstPool cp, CtClass type) {
91669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return getConstantValue2(cp, type, expression);
91769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
91869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
91969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
92069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
92169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * A field initialized with a parameter passed to the constructor
92269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * of the class containing that field.
92369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
92469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    static class ParamInitializer extends Initializer {
92569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int nthParam;
92669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
92769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        ParamInitializer() {}
92869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
92969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int compile(CtClass type, String name, Bytecode code,
93069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    CtClass[] parameters, Javac drv)
93169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            throws CannotCompileException
93269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        {
93369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            if (parameters != null && nthParam < parameters.length) {
93469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                code.addAload(0);
93569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                int nth = nthParamToLocal(nthParam, parameters, false);
93669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                int s = code.addLoad(nth, type) + 1;
93769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
93869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                return s;       // stack size
93969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            }
94069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            else
94169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                return 0;       // do not initialize
94269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
94369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
94469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        /**
94569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * Computes the index of the local variable that the n-th parameter
94669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * is assigned to.
94769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         *
94869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * @param nth           n-th parameter
94969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * @param params                list of parameter types
95069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * @param isStatic              true if the method is static.
95169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         */
95269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        static int nthParamToLocal(int nth, CtClass[] params,
95369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                                   boolean isStatic) {
95469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            CtClass longType = CtClass.longType;
95569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            CtClass doubleType = CtClass.doubleType;
95669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            int k;
95769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            if (isStatic)
95869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                k = 0;
95969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            else
96069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                k = 1;  // 0 is THIS.
96169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
96269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            for (int i = 0; i < nth; ++i) {
96369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                CtClass type = params[i];
96469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                if (type == longType || type == doubleType)
96569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    k += 2;
96669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                else
96769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    ++k;
96869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            }
96969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
97069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return k;
97169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
97269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
97369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int compileIfStatic(CtClass type, String name, Bytecode code,
97469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                            Javac drv) throws CannotCompileException
97569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        {
97669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return 0;
97769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
97869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
97969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
98069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
98169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * A field initialized with an object created by the new operator.
98269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
98369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    static class NewInitializer extends Initializer {
98469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        CtClass objectType;
98569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        String[] stringParams;
98669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        boolean withConstructorParams;
98769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
98869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        NewInitializer() {}
98969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
99069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        /**
99169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * Produces codes in which a new object is created and assigned to
99269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * the field as the initial value.
99369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         */
99469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int compile(CtClass type, String name, Bytecode code,
99569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    CtClass[] parameters, Javac drv)
99669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            throws CannotCompileException
99769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        {
99869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            int stacksize;
99969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
100069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addAload(0);
100169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addNew(objectType);
100269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.add(Bytecode.DUP);
100369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addAload(0);
100469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
100569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            if (stringParams == null)
100669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                stacksize = 4;
100769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            else
100869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                stacksize = compileStringParameter(code) + 4;
100969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
101069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            if (withConstructorParams)
101169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                stacksize += CtNewWrappedMethod.compileParameterList(code,
101269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                                                            parameters, 1);
101369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
101469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addInvokespecial(objectType, "<init>", getDescriptor());
101569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
101669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return stacksize;
101769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
101869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
101969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        private String getDescriptor() {
102069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            final String desc3
102169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        = "(Ljava/lang/Object;[Ljava/lang/String;[Ljava/lang/Object;)V";
102269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
102369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            if (stringParams == null)
102469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                if (withConstructorParams)
102569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    return "(Ljava/lang/Object;[Ljava/lang/Object;)V";
102669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                else
102769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    return "(Ljava/lang/Object;)V";
102869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            else
102969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                if (withConstructorParams)
103069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    return desc3;
103169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                else
103269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    return "(Ljava/lang/Object;[Ljava/lang/String;)V";
103369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
103469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
103569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        /**
103669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * Produces codes for a static field.
103769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         */
103869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int compileIfStatic(CtClass type, String name, Bytecode code,
103969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                            Javac drv) throws CannotCompileException
104069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        {
104169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            String desc;
104269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
104369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addNew(objectType);
104469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.add(Bytecode.DUP);
104569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
104669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            int stacksize = 2;
104769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            if (stringParams == null)
104869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                desc = "()V";
104969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            else {
105069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                desc = "([Ljava/lang/String;)V";
105169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                stacksize += compileStringParameter(code);
105269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            }
105369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
105469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addInvokespecial(objectType, "<init>", desc);
105569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
105669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return stacksize;
105769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
105869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
105969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        protected final int compileStringParameter(Bytecode code)
106069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            throws CannotCompileException
106169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        {
106269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            int nparam = stringParams.length;
106369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addIconst(nparam);
106469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addAnewarray(javaLangString);
106569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            for (int j = 0; j < nparam; ++j) {
106669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                code.add(Bytecode.DUP);         // dup
106769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                code.addIconst(j);                      // iconst_<j>
106869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                code.addLdc(stringParams[j]);   // ldc ...
106969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                code.add(Bytecode.AASTORE);             // aastore
107069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            }
107169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
107269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return 4;
107369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
107469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
107569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
107669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
107769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
107869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * A field initialized with the result of a static method call.
107969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
108069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    static class MethodInitializer extends NewInitializer {
108169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        String methodName;
108269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        // the method class is specified by objectType.
108369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
108469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        MethodInitializer() {}
108569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
108669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        /**
108769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * Produces codes in which a new object is created and assigned to
108869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * the field as the initial value.
108969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         */
109069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int compile(CtClass type, String name, Bytecode code,
109169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    CtClass[] parameters, Javac drv)
109269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            throws CannotCompileException
109369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        {
109469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            int stacksize;
109569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
109669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addAload(0);
109769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addAload(0);
109869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
109969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            if (stringParams == null)
110069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                stacksize = 2;
110169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            else
110269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                stacksize = compileStringParameter(code) + 2;
110369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
110469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            if (withConstructorParams)
110569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                stacksize += CtNewWrappedMethod.compileParameterList(code,
110669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                                                            parameters, 1);
110769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
110869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            String typeDesc = Descriptor.of(type);
110969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            String mDesc = getDescriptor() + typeDesc;
111069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addInvokestatic(objectType, methodName, mDesc);
111169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addPutfield(Bytecode.THIS, name, typeDesc);
111269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return stacksize;
111369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
111469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
111569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        private String getDescriptor() {
111669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            final String desc3
111769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                = "(Ljava/lang/Object;[Ljava/lang/String;[Ljava/lang/Object;)";
111869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
111969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            if (stringParams == null)
112069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                if (withConstructorParams)
112169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    return "(Ljava/lang/Object;[Ljava/lang/Object;)";
112269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                else
112369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    return "(Ljava/lang/Object;)";
112469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            else
112569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                if (withConstructorParams)
112669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    return desc3;
112769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                else
112869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    return "(Ljava/lang/Object;[Ljava/lang/String;)";
112969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
113069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
113169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        /**
113269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         * Produces codes for a static field.
113369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal         */
113469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int compileIfStatic(CtClass type, String name, Bytecode code,
113569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                            Javac drv) throws CannotCompileException
113669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        {
113769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            String desc;
113869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
113969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            int stacksize = 1;
114069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            if (stringParams == null)
114169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                desc = "()";
114269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            else {
114369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                desc = "([Ljava/lang/String;)";
114469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                stacksize += compileStringParameter(code);
114569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            }
114669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
114769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            String typeDesc = Descriptor.of(type);
114869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addInvokestatic(objectType, methodName, desc + typeDesc);
114969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addPutstatic(Bytecode.THIS, name, typeDesc);
115069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return stacksize;
115169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
115269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
115369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
115469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    static class IntInitializer extends Initializer {
115569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int value;
115669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
115769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        IntInitializer(int v) { value = v; }
115869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
115969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        void check(String desc) throws CannotCompileException {
116069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            char c = desc.charAt(0);
116169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            if (c != 'I' && c != 'S' && c != 'B' && c != 'C' && c != 'Z')
116269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                throw new CannotCompileException("type mismatch");
116369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
116469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
116569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int compile(CtClass type, String name, Bytecode code,
116669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    CtClass[] parameters, Javac drv)
116769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            throws CannotCompileException
116869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        {
116969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addAload(0);
117069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addIconst(value);
117169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
117269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return 2;   // stack size
117369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
117469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
117569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int compileIfStatic(CtClass type, String name, Bytecode code,
117669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                            Javac drv) throws CannotCompileException
117769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        {
117869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addIconst(value);
117969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
118069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return 1;   // stack size
118169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
118269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
118369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int getConstantValue(ConstPool cp, CtClass type) {
118469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return cp.addIntegerInfo(value);
118569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
118669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
118769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
118869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    static class LongInitializer extends Initializer {
118969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        long value;
119069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
119169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        LongInitializer(long v) { value = v; }
119269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
119369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        void check(String desc) throws CannotCompileException {
119469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            if (!desc.equals("J"))
119569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                throw new CannotCompileException("type mismatch");
119669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
119769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
119869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int compile(CtClass type, String name, Bytecode code,
119969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    CtClass[] parameters, Javac drv)
120069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            throws CannotCompileException
120169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        {
120269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addAload(0);
120369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addLdc2w(value);
120469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
120569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return 3;   // stack size
120669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
120769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
120869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int compileIfStatic(CtClass type, String name, Bytecode code,
120969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                            Javac drv) throws CannotCompileException
121069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        {
121169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addLdc2w(value);
121269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
121369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return 2;   // stack size
121469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
121569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
121669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int getConstantValue(ConstPool cp, CtClass type) {
121769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            if (type == CtClass.longType)
121869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                return cp.addLongInfo(value);
121969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            else
122069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                return 0;
122169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
122269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
122369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
122469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    static class FloatInitializer extends Initializer {
122569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        float value;
122669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
122769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        FloatInitializer(float v) { value = v; }
122869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
122969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        void check(String desc) throws CannotCompileException {
123069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            if (!desc.equals("F"))
123169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                throw new CannotCompileException("type mismatch");
123269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
123369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
123469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int compile(CtClass type, String name, Bytecode code,
123569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    CtClass[] parameters, Javac drv)
123669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            throws CannotCompileException
123769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        {
123869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addAload(0);
123969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addFconst(value);
124069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
124169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return 3;   // stack size
124269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
124369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
124469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int compileIfStatic(CtClass type, String name, Bytecode code,
124569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                            Javac drv) throws CannotCompileException
124669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        {
124769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addFconst(value);
124869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
124969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return 2;   // stack size
125069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
125169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
125269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int getConstantValue(ConstPool cp, CtClass type) {
125369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            if (type == CtClass.floatType)
125469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                return cp.addFloatInfo(value);
125569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            else
125669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                return 0;
125769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
125869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
125969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
126069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    static class DoubleInitializer extends Initializer {
126169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        double value;
126269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
126369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        DoubleInitializer(double v) { value = v; }
126469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
126569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        void check(String desc) throws CannotCompileException {
126669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            if (!desc.equals("D"))
126769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                throw new CannotCompileException("type mismatch");
126869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
126969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
127069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int compile(CtClass type, String name, Bytecode code,
127169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    CtClass[] parameters, Javac drv)
127269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            throws CannotCompileException
127369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        {
127469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addAload(0);
127569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addLdc2w(value);
127669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
127769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return 3;   // stack size
127869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
127969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
128069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int compileIfStatic(CtClass type, String name, Bytecode code,
128169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                            Javac drv) throws CannotCompileException
128269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        {
128369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addLdc2w(value);
128469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
128569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return 2;   // stack size
128669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
128769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
128869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int getConstantValue(ConstPool cp, CtClass type) {
128969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            if (type == CtClass.doubleType)
129069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                return cp.addDoubleInfo(value);
129169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            else
129269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                return 0;
129369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
129469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
129569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
129669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    static class StringInitializer extends Initializer {
129769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        String value;
129869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
129969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        StringInitializer(String v) { value = v; }
130069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
130169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int compile(CtClass type, String name, Bytecode code,
130269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    CtClass[] parameters, Javac drv)
130369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            throws CannotCompileException
130469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        {
130569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addAload(0);
130669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addLdc(value);
130769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
130869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return 2;   // stack size
130969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
131069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
131169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int compileIfStatic(CtClass type, String name, Bytecode code,
131269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                            Javac drv) throws CannotCompileException
131369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        {
131469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addLdc(value);
131569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
131669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return 1;   // stack size
131769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
131869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
131969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int getConstantValue(ConstPool cp, CtClass type) {
132069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            if (type.getName().equals(javaLangString))
132169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                return cp.addStringInfo(value);
132269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            else
132369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                return 0;
132469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
132569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
132669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
132769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    static class ArrayInitializer extends Initializer {
132869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        CtClass type;
132969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int size;
133069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
133169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        ArrayInitializer(CtClass t, int s) { type = t; size = s; }
133269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
133369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        private void addNewarray(Bytecode code) {
133469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            if (type.isPrimitive())
133569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                code.addNewarray(((CtPrimitiveType)type).getArrayType(),
133669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                                 size);
133769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            else
133869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                code.addAnewarray(type, size);
133969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
134069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
134169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int compile(CtClass type, String name, Bytecode code,
134269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    CtClass[] parameters, Javac drv)
134369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            throws CannotCompileException
134469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        {
134569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addAload(0);
134669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            addNewarray(code);
134769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
134869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return 2;   // stack size
134969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
135069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
135169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int compileIfStatic(CtClass type, String name, Bytecode code,
135269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                            Javac drv) throws CannotCompileException
135369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        {
135469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            addNewarray(code);
135569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
135669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return 1;   // stack size
135769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
135869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
135969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
136069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    static class MultiArrayInitializer extends Initializer {
136169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        CtClass type;
136269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int[] dim;
136369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
136469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        MultiArrayInitializer(CtClass t, int[] d) { type = t; dim = d; }
136569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
136669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        void check(String desc) throws CannotCompileException {
136769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            if (desc.charAt(0) != '[')
136869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                throw new CannotCompileException("type mismatch");
136969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
137069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
137169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int compile(CtClass type, String name, Bytecode code,
137269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    CtClass[] parameters, Javac drv)
137369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            throws CannotCompileException
137469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        {
137569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addAload(0);
137669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            int s = code.addMultiNewarray(type, dim);
137769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
137869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return s + 1;       // stack size
137969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
138069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
138169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int compileIfStatic(CtClass type, String name, Bytecode code,
138269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                            Javac drv) throws CannotCompileException
138369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        {
138469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            int s = code.addMultiNewarray(type, dim);
138569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
138669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return s;   // stack size
138769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
138869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
138969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal}
1390