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

Scala example source code file (CustomAttributeProvider.java)

This example Scala source code file (CustomAttributeProvider.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

attribute, attribute, customattributeprovider, empty, empty, getcustomattributes, isdefined, iterator, linkedlist, linkedlist, list, object, object, type, util

The Scala CustomAttributeProvider.java source code

/*
 * System.Reflection-like API for access to .NET assemblies (DLL & EXE)
 */


package ch.epfl.lamp.compiler.msil;

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

/**
 * @author Nikolay Mihaylov
 * @version 1.0
 */
public abstract class CustomAttributeProvider implements ICustomAttributeProvider {

    //##########################################################################

    protected List/*<Attribute>*/ custAttrs;
    private static final Object[] EMPTY = new Object[0];

    //TODO: take inherit into account
    public Object[] GetCustomAttributes(boolean inherit) {
	initAttributes(null);
	return custAttrs.size() == 0 ? EMPTY
            : custAttrs.toArray(new Attribute[custAttrs.size()]);
    }

    //TODO: take inherit into account
    public Object[] GetCustomAttributes(Type attributeType, boolean inherit) {
	initAttributes(attributeType);
        List tAttrs = null;
        if (constrType == attributeType)
            tAttrs = custAttrs;
        else {
            tAttrs = new LinkedList();
            for (Iterator attrs = custAttrs.iterator(); attrs.hasNext(); ) {
                Attribute a = (Attribute) attrs.next();
                if (a.GetType() == attributeType) tAttrs.add(a);
            }
        }
	return tAttrs.size() == 0 ? EMPTY
            : tAttrs.toArray(new Attribute[tAttrs.size()]);
    }

    //TODO: take inherit into account
    public boolean IsDefined(Type attributeType, boolean inherit) {
	initAttributes(attributeType);
        if (constrType == attributeType)
            return custAttrs.size() > 0;
	Iterator attrs = custAttrs.iterator();
	while (attrs.hasNext()) {
	    if (((Attribute)attrs.next()).GetType() == attributeType)
		return true;
	}
	return false;
// 	return inherit && (DeclaringClass.BaseType != null)
// 	    && DeclaringClass.BaseType.IsDefined(inherit);
    }

    protected void addCustomAttribute(ConstructorInfo constr, byte[] value) {
        Attribute attr = new Attribute(constr, value);
        assert constrType == null || constrType == attr.GetType();
        if (custAttrs == null)
            custAttrs = new LinkedList();
	custAttrs.add(attr);
    }

    private void initAttributes(Type atype) {
	if (custAttrs != null
            && (constrType == null || constrType == atype))
	    return;
	custAttrs = new LinkedList();
        constrType = atype;
	loadCustomAttributes(atype);
    }

    protected void loadCustomAttributes(Type atype) {}

    private Type constrType;
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala CustomAttributeProvider.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.