Package alt.jiapi.reflect

Source Code of alt.jiapi.reflect.ClassBuilder

package alt.jiapi.reflect;

import java.util.Iterator;
import java.util.List;
import java.util.LinkedList;

import alt.jiapi.file.ClassFile;
import alt.jiapi.file.ConstantPool;
import alt.jiapi.file.Field;
import alt.jiapi.file.Method;
import alt.jiapi.file.SyntheticAttribute;
import alt.jiapi.file.CodeAttribute;

/**
* Class ClassBuilder.
*
* @author Mika Riekkinen
*/
class ClassBuilder {
    private ClassFile clazz;

    ClassBuilder(ClassFile clazz) {
        this.clazz = clazz;
    }

    JiapiField addField(short modifiers, String type, String name) throws FieldExistsException {
        ConstantPool cp = clazz.getConstantPool();

        // Check, that the field don't exist
        List fields = clazz.getFields();
        Iterator i = fields.iterator();
        while(i.hasNext()) {
            Field field = (Field)i.next();
            if (field.getName().equals(name)) {
                throw new FieldExistsException(new JiapiField(field));
            }
        }

        // Add Synthetic attribute for the field
        List attrs = new LinkedList();
        attrs.add(new SyntheticAttribute(cp));

        // Create field, and add it to ClassFile
        Field f = new Field(cp, modifiers, name,
                            TypeHelper.typeToDescriptor(type), attrs);
        fields.add(f);

        return new JiapiField(f);
    }


    JiapiMethod addMethod(short modifiers, String name, Signature signature) throws MethodExistsException {
        ConstantPool cp = clazz.getConstantPool();


        // Add Synthetic attribute for the field
        List attrs = new LinkedList();
        attrs.add(new SyntheticAttribute(cp));

        short maxStack = 0;
        int maxLocals = signature.getParameters().length + 1;

        attrs.add(new CodeAttribute(cp, maxStack, (short)maxLocals));

        // Create field, and add it to ClassFile
        Method m = new Method(cp, modifiers, name, signature.getDescriptor(),
                              attrs);

        clazz.getMethods().add(m);

        return new JiapiMethod(m);
    }


    ClassFile getClassFile() {
        return clazz;
    }
}
TOP

Related Classes of alt.jiapi.reflect.ClassBuilder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.