summaryrefslogtreecommitdiffstats
path: root/build/annotationProcessors/classloader/AnnotatableEntity.java
blob: b11a6c49a6c7cafed9c03b531cd559981d61636a (plain)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko.annotationProcessors.classloader;

import org.mozilla.gecko.annotationProcessors.AnnotationInfo;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
 * Union type to hold either a method, field, or ctor. Allows us to iterate "The generatable stuff", despite
 * the fact that such things can be of either flavour.
 */
public class AnnotatableEntity {
    public enum ENTITY_TYPE {METHOD, NATIVE, FIELD, CONSTRUCTOR}

    private final Member mMember;
    public final ENTITY_TYPE mEntityType;

    public final AnnotationInfo mAnnotationInfo;

    public AnnotatableEntity(Member aObject, AnnotationInfo aAnnotationInfo) {
        mMember = aObject;
        mAnnotationInfo = aAnnotationInfo;

        if (aObject instanceof Method) {
            if (Modifier.isNative(aObject.getModifiers())) {
                mEntityType = ENTITY_TYPE.NATIVE;
            } else {
                mEntityType = ENTITY_TYPE.METHOD;
            }
        } else if (aObject instanceof Field) {
            mEntityType = ENTITY_TYPE.FIELD;
        } else {
            mEntityType = ENTITY_TYPE.CONSTRUCTOR;
        }
    }

    public Method getMethod() {
        if (mEntityType != ENTITY_TYPE.METHOD && mEntityType != ENTITY_TYPE.NATIVE) {
            throw new UnsupportedOperationException("Attempt to cast to unsupported member type.");
        }
        return (Method) mMember;
    }
    public Field getField() {
        if (mEntityType != ENTITY_TYPE.FIELD) {
            throw new UnsupportedOperationException("Attempt to cast to unsupported member type.");
        }
        return (Field) mMember;
    }
    public Constructor<?> getConstructor() {
        if (mEntityType != ENTITY_TYPE.CONSTRUCTOR) {
            throw new UnsupportedOperationException("Attempt to cast to unsupported member type.");
        }
        return (Constructor<?>) mMember;
    }
}