AnnotatableEntity.java 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. package org.mozilla.gecko.annotationProcessors.classloader;
  5. import org.mozilla.gecko.annotationProcessors.AnnotationInfo;
  6. import java.lang.reflect.Constructor;
  7. import java.lang.reflect.Field;
  8. import java.lang.reflect.Member;
  9. import java.lang.reflect.Method;
  10. import java.lang.reflect.Modifier;
  11. /**
  12. * Union type to hold either a method, field, or ctor. Allows us to iterate "The generatable stuff", despite
  13. * the fact that such things can be of either flavour.
  14. */
  15. public class AnnotatableEntity {
  16. public enum ENTITY_TYPE {METHOD, NATIVE, FIELD, CONSTRUCTOR}
  17. private final Member mMember;
  18. public final ENTITY_TYPE mEntityType;
  19. public final AnnotationInfo mAnnotationInfo;
  20. public AnnotatableEntity(Member aObject, AnnotationInfo aAnnotationInfo) {
  21. mMember = aObject;
  22. mAnnotationInfo = aAnnotationInfo;
  23. if (aObject instanceof Method) {
  24. if (Modifier.isNative(aObject.getModifiers())) {
  25. mEntityType = ENTITY_TYPE.NATIVE;
  26. } else {
  27. mEntityType = ENTITY_TYPE.METHOD;
  28. }
  29. } else if (aObject instanceof Field) {
  30. mEntityType = ENTITY_TYPE.FIELD;
  31. } else {
  32. mEntityType = ENTITY_TYPE.CONSTRUCTOR;
  33. }
  34. }
  35. public Method getMethod() {
  36. if (mEntityType != ENTITY_TYPE.METHOD && mEntityType != ENTITY_TYPE.NATIVE) {
  37. throw new UnsupportedOperationException("Attempt to cast to unsupported member type.");
  38. }
  39. return (Method) mMember;
  40. }
  41. public Field getField() {
  42. if (mEntityType != ENTITY_TYPE.FIELD) {
  43. throw new UnsupportedOperationException("Attempt to cast to unsupported member type.");
  44. }
  45. return (Field) mMember;
  46. }
  47. public Constructor<?> getConstructor() {
  48. if (mEntityType != ENTITY_TYPE.CONSTRUCTOR) {
  49. throw new UnsupportedOperationException("Attempt to cast to unsupported member type.");
  50. }
  51. return (Constructor<?>) mMember;
  52. }
  53. }