summaryrefslogtreecommitdiff
path: root/src/msil/ch/epfl/lamp/compiler/msil/CustomAttributeProvider.java
blob: 0e58c181147c3c918351693aca30071698c60db2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
 * 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;
}