alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (JAttributeFactory.java)

This example Scala source code file (JAttributeFactory.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

class, code, constructor, constructor, illegalargumentexception, io, jattribute, jattributefactory, jattributefactory, nosuchmethodexception, object, object, reflection, runtimeexception, runtimeexception, sourcefile, util

The Scala JAttributeFactory.java source code

/* FJBG -- Fast Java Bytecode Generator
 * Copyright 2002-2011 LAMP/EPFL
 * @author  Michel Schinz
 */

package ch.epfl.lamp.fjbg;

import java.io.DataInputStream;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;

/**
 * Extensible factory to build subclasses of JAttribute based on an
 * attribute name.
 *
 * @author Michel Schinz
 * @version 1.0
 */

public class JAttributeFactory {
    protected FJBGContext context;
    protected HashMap/*<String, Constructor>*/ constructors = new HashMap();

    protected final static Class[] CONSTRUCTOR_ARGS = new Class[] {
        FJBGContext.class,
        JClass.class,
        Object.class,
        String.class,
        int.class,
        DataInputStream.class
    };

    protected final static Constructor defaultDefaultConstructor;
    static {
        try {
            defaultDefaultConstructor =
                 JOtherAttribute.class.getConstructor(CONSTRUCTOR_ARGS);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
    }

    protected final Constructor defaultConstructor;

    public JAttributeFactory(FJBGContext context,
                             Constructor defaultConstructor) {
        this.context = context;
        this.defaultConstructor = defaultConstructor;
        registerClass("BootstrapInvokeDynamic", JBootstrapInvokeDynamic.class);
        registerClass("Code", JCodeAttribute.class);
        registerClass("ConstantValue", JConstantValueAttribute.class);
        registerClass("EnclosingMethod", JEnclosingMethodAttribute.class);
        registerClass("Exceptions", JExceptionsAttribute.class);
        registerClass("InnerClasses", JInnerClassesAttribute.class);
        registerClass("LineNumberTable", JLineNumberTableAttribute.class);
        registerClass("LocalVariableTable", JLocalVariableTableAttribute.class);
        registerClass("SourceFile", JSourceFileAttribute.class);
        registerClass("StackMapTable", JStackMapTableAttribute.class);
    }

    public JAttributeFactory(FJBGContext context) {
        this(context, defaultDefaultConstructor);
    }

    public void registerClass(String attributeName,
                              Class clazz) {
        if (! JAttribute.class.isAssignableFrom(clazz))
            throw new IllegalArgumentException("Not a subclass of JAttribute: "
                                               + clazz);

        try {
            Constructor constr = clazz.getConstructor(CONSTRUCTOR_ARGS);
            constructors.put(attributeName, constr);
        } catch (NoSuchMethodException e) {
            throw new IllegalArgumentException("No appropriate constructor for "
                                               + clazz);
        }
    }

    public JAttribute newInstance(JClass clazz,
                                  Object owner,
                                  DataInputStream stream)
        throws IOException {
        String name = clazz.getConstantPool().lookupUtf8(stream.readShort());
        Integer size = new Integer(stream.readInt());
        Constructor constr = (Constructor)constructors.get(name);
        if (constr == null) constr = defaultConstructor;

        Object[] args = new Object[] { context, clazz, owner, name, size, stream };
        try {
            return (JAttribute)constr.newInstance(args);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala JAttributeFactory.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.