Java Class.getAnnotations Examples

Java Class.getAnnotations - 30 examples found. These are the top rated real world Java examples of Class.getAnnotations extracted from open source projects. You can rate examples to help us improve the quality of examples.
 private void assertAnnotations(Class<?> type) {
   assertThat(
       describe(type).getDeclaredAnnotations(),
       hasItems(
           new AnnotationList.ForLoadedAnnotation(type.getDeclaredAnnotations())
               .toArray(new AnnotationDescription[type.getDeclaredAnnotations().length])));
   assertThat(
       describe(type).getDeclaredAnnotations().size(), is(type.getDeclaredAnnotations().length));
   assertThat(
       describe(type).getInheritedAnnotations(),
       hasItems(
           new AnnotationList.ForLoadedAnnotation(type.getAnnotations())
               .toArray(new AnnotationDescription[type.getAnnotations().length])));
   assertThat(describe(type).getInheritedAnnotations().size(), is(type.getAnnotations().length));
 }
Example #2
0
  /**
   * Checks for {@link InstanceResolverAnnotation} and creates an instance resolver from it if any.
   * Otherwise null.
   */
  public static <T> InstanceResolver<T> createFromInstanceResolverAnnotation(
      @NotNull Class<T> clazz) {
    for (Annotation a : clazz.getAnnotations()) {
      InstanceResolverAnnotation ira =
          a.annotationType().getAnnotation(InstanceResolverAnnotation.class);
      if (ira == null) continue;
      Class<? extends InstanceResolver> ir = ira.value();
      try {
        return ir.getConstructor(Class.class).newInstance(clazz);
      } catch (InstantiationException e) {
        throw new WebServiceException(
            ServerMessages.FAILED_TO_INSTANTIATE_INSTANCE_RESOLVER(
                ir.getName(), a.annotationType(), clazz.getName()));
      } catch (IllegalAccessException e) {
        throw new WebServiceException(
            ServerMessages.FAILED_TO_INSTANTIATE_INSTANCE_RESOLVER(
                ir.getName(), a.annotationType(), clazz.getName()));
      } catch (InvocationTargetException e) {
        throw new WebServiceException(
            ServerMessages.FAILED_TO_INSTANTIATE_INSTANCE_RESOLVER(
                ir.getName(), a.annotationType(), clazz.getName()));
      } catch (NoSuchMethodException e) {
        throw new WebServiceException(
            ServerMessages.FAILED_TO_INSTANTIATE_INSTANCE_RESOLVER(
                ir.getName(), a.annotationType(), clazz.getName()));
      }
    }

    return null;
  }
  /**
   * @param uri The uri to identify the component with.
   * @param type The type to create the metadata for
   * @param factory A reflection library to provide class construction and field get/set
   *     functionality
   * @param copyStrategies A copy strategy library
   * @throws NoSuchMethodException If the component has no default constructor
   */
  public ComponentMetadata(
      SimpleUri uri, Class<T> type, ReflectFactory factory, CopyStrategyLibrary copyStrategies)
      throws NoSuchMethodException {
    super(uri, type, factory, copyStrategies, Predicates.<Field>alwaysTrue());
    replicated = type.getAnnotation(Replicate.class) != null;
    blockLifecycleEventsRequired = type.getAnnotation(RequiresBlockLifecycleEvents.class) != null;
    ForceBlockActive forceBlockActiveAnnotation = type.getAnnotation(ForceBlockActive.class);
    if (forceBlockActiveAnnotation != null) {
      forceBlockActive = true;
      retainUnalteredOnBlockChange = forceBlockActiveAnnotation.retainUnalteredOnBlockChange();
    }

    for (ComponentFieldMetadata<T, ?> field : getFields()) {
      if (field.isReplicated()) {
        replicated = true;
        if (field.getReplicationInfo().value().isReplicateFromOwner()) {
          replicatedFromOwner = true;
        }
      }
      if (field.isOwnedReference()) {
        referenceOwner = true;
      }
    }

    annotations = Lists.newArrayList(type.getAnnotations());
  }
  /**
   * Goes through the class hierarchy and find the authorization annotations attached to a certain
   * method.
   *
   * @param clazz class to be scanned
   * @param authorizationActionMap the map to be populated
   */
  private void findAuthorizationActions(
      Class<?> clazz, Map<String, String> authorizationActionMap) {
    if (clazz == null || clazz == Object.class) {
      return;
    }
    String classAuthorizationActionsAllowed =
        getAuthorizationActions(clazz.getAnnotations(), AUTHORIZATION_ANNOTATION_CLASS_NAME);
    for (Method m : clazz.getMethods()) {
      if (SKIP_METHODS.contains(m.getName())) {
        continue;
      }
      String methodAuthorizationActionsAllowed =
          getAuthorizationActions(m.getAnnotations(), AUTHORIZATION_ANNOTATION_CLASS_NAME);
      String authorizationActions =
          methodAuthorizationActionsAllowed != null
              ? methodAuthorizationActionsAllowed
              : classAuthorizationActionsAllowed;
      if (authorizationActions != null) {
        authorizationActionMap.put(m.getName(), authorizationActions);
      }
    }
    if (!authorizationActionMap.isEmpty()) {
      return;
    }

    findAuthorizationActions(clazz.getSuperclass(), authorizationActionMap);

    if (!authorizationActionMap.isEmpty()) {
      return;
    }

    for (Class<?> interfaceCls : clazz.getInterfaces()) {
      findAuthorizationActions(interfaceCls, authorizationActionMap);
    }
  }
  private void introspectInjectClass(
      AnnotatedType<X> type, ArrayList<ConfigProgram> injectProgramList) {
    InjectManager cdiManager = getBeanManager();

    for (Annotation ann : type.getAnnotations()) {
      Class<? extends Annotation> annType = ann.annotationType();

      InjectionPointHandler handler = cdiManager.getInjectionPointHandler(annType);

      if (handler != null) {
        injectProgramList.add(new ClassHandlerProgram(ann, handler));
      }
    }

    // ioc/123i
    for (Class<?> parentClass = type.getJavaClass().getSuperclass();
        parentClass != null;
        parentClass = parentClass.getSuperclass()) {
      for (Annotation ann : parentClass.getAnnotations()) {
        Class<? extends Annotation> annType = ann.annotationType();

        InjectionPointHandler handler = cdiManager.getInjectionPointHandler(annType);

        if (handler != null) {
          injectProgramList.add(new ClassHandlerProgram(ann, handler));
        }
      }
    }
  }
  private final void validateTwoAnnotations(final PackageInfoModifier.Modification mod)
      throws Exception {
    assertNotNull(mod);
    final byte[] newClass = mod.toByteArray();
    assertNotNull(newClass);
    assertTrue(newClass.length > 0);

    final Class<?> c = new ClassDefiner().define(mod.getPackageName(), newClass);
    assertNotNull(c);

    final Annotation[] annotations = c.getAnnotations();
    assertNotNull(annotations);
    assertTrue(annotations.length == 2);

    Annotation a = annotations[0];
    assertNotNull(a);
    if (a instanceof Deprecated) {
      a = annotations[1];
      assertNotNull(a);
    }
    assertTrue(a instanceof XmlJavaTypeAdapters);
    final XmlJavaTypeAdapters adaptersAnnotation = (XmlJavaTypeAdapters) a;
    final XmlJavaTypeAdapter[] adapters = adaptersAnnotation.value();
    assertNotNull(adapters);
    assertEquals(1, adapters.length);
    final XmlJavaTypeAdapter adapter = adapters[0];
    assertNotNull(adapter);
    assertEquals(Person.class, adapter.type());
    assertEquals(AnyTypeAdapter.class, adapter.value());
  }