|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 package org.mozilla.gecko.annotationProcessors.classloader; |
|
6 |
|
7 import org.mozilla.gecko.annotationProcessors.AnnotationInfo; |
|
8 |
|
9 import java.lang.reflect.Constructor; |
|
10 import java.lang.reflect.Field; |
|
11 import java.lang.reflect.Member; |
|
12 import java.lang.reflect.Method; |
|
13 |
|
14 /** |
|
15 * Union type to hold either a method, field, or ctor. Allows us to iterate "The generatable stuff", despite |
|
16 * the fact that such things can be of either flavour. |
|
17 */ |
|
18 public class AnnotatableEntity { |
|
19 public enum ENTITY_TYPE {METHOD, FIELD, CONSTRUCTOR} |
|
20 |
|
21 private final Member mMember; |
|
22 public final ENTITY_TYPE mEntityType; |
|
23 |
|
24 public final AnnotationInfo mAnnotationInfo; |
|
25 |
|
26 public AnnotatableEntity(Member aObject, AnnotationInfo aAnnotationInfo) { |
|
27 mMember = aObject; |
|
28 mAnnotationInfo = aAnnotationInfo; |
|
29 |
|
30 if (aObject instanceof Method) { |
|
31 mEntityType = ENTITY_TYPE.METHOD; |
|
32 } else if (aObject instanceof Field) { |
|
33 mEntityType = ENTITY_TYPE.FIELD; |
|
34 } else { |
|
35 mEntityType = ENTITY_TYPE.CONSTRUCTOR; |
|
36 } |
|
37 } |
|
38 |
|
39 public Method getMethod() { |
|
40 if (mEntityType != ENTITY_TYPE.METHOD) { |
|
41 throw new UnsupportedOperationException("Attempt to cast to unsupported member type."); |
|
42 } |
|
43 return (Method) mMember; |
|
44 } |
|
45 public Field getField() { |
|
46 if (mEntityType != ENTITY_TYPE.FIELD) { |
|
47 throw new UnsupportedOperationException("Attempt to cast to unsupported member type."); |
|
48 } |
|
49 return (Field) mMember; |
|
50 } |
|
51 public Constructor getConstructor() { |
|
52 if (mEntityType != ENTITY_TYPE.CONSTRUCTOR) { |
|
53 throw new UnsupportedOperationException("Attempt to cast to unsupported member type."); |
|
54 } |
|
55 return (Constructor) mMember; |
|
56 } |
|
57 } |