Java Class.isAnnotationPresent Examples

Java Class.isAnnotationPresent - 30 examples found. These are the top rated real world Java examples of Class.isAnnotationPresent extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
  protected void configure() {
    final Set<Class<?>> resources = new HashSet<>();
    final Map<Class<?>, Class<?>> resourcesWithContainers = new HashMap<>();
    injector
        .getAllBindings()
        .entrySet()
        .forEach(
            entry -> {
              final Key<?> key = entry.getKey();
              final Class<?> classObject = key.getTypeLiteral().getRawType();
              final Class<?> superClassObject = classObject.getSuperclass();
              if (classObject.isAnnotationPresent(Path.class)) {
                log.info("Loaded Resource " + classObject);
                resources.add(classObject);
              } else if (!Object.class.equals(classObject)
                  && !classObject.isInterface()
                  && superClassObject.isAnnotationPresent(Path.class)) {
                log.info("Loaded Resource " + superClassObject + " with Container " + classObject);
                resourcesWithContainers.put(superClassObject, classObject);
              }
            });

    for (final Class<?> resource : resources) {
      final Object container = injector.getInstance(resource);
      bind((Class) resource).toInstance(container);
      log.info("Binding Resource " + resource + " to Container " + container);
    }

    for (final Map.Entry<Class<?>, Class<?>> entry : resourcesWithContainers.entrySet()) {
      bindResourceToLocalContainer(entry.getKey(), entry.getValue());
      log.info("Binding Resource " + entry.getKey() + " to Container " + entry.getValue());
    }
  }
  protected static <T> void parseEDISegmentOrSegmentGroup(
      EDIMessage ediMessage,
      T object,
      Iterator<Field> fieldIterator,
      Queue<String> lookAhead,
      SegmentIterator segmentIterator)
      throws EDIMessageException, IllegalAccessException, InvocationTargetException,
          InstantiationException, ClassNotFoundException, ConversionException {
    if (!fieldIterator.hasNext()) {
      throw new EDIMessageException("No more fields to read.");
    }

    if (!segmentIterator.hasNext() && lookAhead.size() == 0) {
      return;
    }

    // get the queued object first...
    String line = lookAhead.size() > 0 ? lookAhead.remove() : segmentIterator.next();

    // match up the field with the line...
    FieldMatch fm = advanceToMatch(ediMessage, fieldIterator, line);

    //

    if (fm != null) {
      Class<?> fieldType = getEDISegmentOrGroupType(fm.getField());
      if (fieldType.isAnnotationPresent(EDISegment.class)) {
        processSegment(ediMessage, object, lookAhead, segmentIterator, fm);
      } else if (fieldType.isAnnotationPresent(EDISegmentGroup.class)) {
        processSegmentGroup(ediMessage, object, lookAhead, segmentIterator, fm);
      }
    } else {
      lookAhead.add(line);
    }
  }
Example #3
0
 @SuppressWarnings("unchecked")
 @Override
 public T readFrom(
     Class<T> type,
     Type genericType,
     Annotation[] annotations,
     MediaType mediaType,
     MultivaluedMap<String, String> httpHeaders,
     InputStream entityStream)
     throws IOException, WebApplicationException {
   if (type.isAnnotationPresent(DTO.class)) {
     return DtoFactory.getInstance().createDtoFromJson(entityStream, type);
   } else if (type.isAssignableFrom(List.class) && genericType instanceof ParameterizedType) {
     ParameterizedType parameterizedType = (ParameterizedType) genericType;
     Type elementType = parameterizedType.getActualTypeArguments()[0];
     if (elementType instanceof Class) {
       Class elementClass = (Class) elementType;
       if (elementClass.isAnnotationPresent(DTO.class)) {
         return (T) DtoFactory.getInstance().createListDtoFromJson(entityStream, elementClass);
       }
     }
   }
   return (T)
       delegate.readFrom(type, genericType, annotations, mediaType, httpHeaders, entityStream);
 }
Example #4
0
    @Override
    public List<T> handle(ResultSet rs) throws SQLException {
      List<T> list = new LinkedList<>();
      while (rs.next()) {
        try {
          T t = this.clz.newInstance();
          BeanInfo beanInfo = Introspector.getBeanInfo(this.clz, Object.class);
          PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();

          Map<String, Object> map = new HashMap<String, Object>();
          for (PropertyDescriptor pd : descriptors) {
            String propertyName = pd.getName();
            Class<?> propertyType = pd.getPropertyType();
            if (propertyType.isAnnotationPresent(Entry.class)) {
              propertyName = propertyName + "_id";
            }
            Object value = rs.getObject(propertyName);
            if (propertyType.isAnnotationPresent(Entry.class)) {
              value = getObject(value, propertyType);
            }
            map.put(pd.getName(), value);
          }
          BeanUtils.copyProperties(t, map);
          list.add(t);

        } catch (InstantiationException
            | IllegalAccessException
            | IntrospectionException
            | InvocationTargetException e) {
          e.printStackTrace();
        }
      }
      return list;
    }
 private void addControllerServlet(Class<?> klass)
     throws InstantiationException, IllegalAccessException {
   assert klass != null;
   ControllerServlet servlet = null;
   Controller controller;
   if (Controller.class.isAssignableFrom(klass)) {
     controller = controllerFactory.createController(klass.asSubclass(Controller.class));
   } else {
     SimpleControllerWrapper.checkValidSimpleControllerClass(klass);
     SimpleControllerWrapper simpleController =
         SimpleControllerWrapper.createInstance(controllerFactory.createController(klass));
     configure(simpleController);
     controller = simpleController;
   }
   URL url = klass.getAnnotation(URL.class);
   servlet = new ControllerServlet(controller);
   AllowedHttpMethods httpMethods = klass.getAnnotation(AllowedHttpMethods.class);
   if (httpMethods != null) {
     servlet.setValidHttpMethods(httpMethods.value());
   }
   ControllerServlet.AfterControllerAction action = null;
   if (klass.isAnnotationPresent(ForwardTo.class)) {
     action = new ControllerServlet.ForwardAction(klass.getAnnotation(ForwardTo.class).value());
   } else if (klass.isAnnotationPresent(RedirectTo.class)) {
     action = new ControllerServlet.ForwardAction(klass.getAnnotation(RedirectTo.class).value());
   }
   servlet.setAfterControllerAction(action);
   ServletRegistration.Dynamic dynamic = context.addServlet(klass.getSimpleName(), servlet);
   dynamic.addMapping(url.value());
 }
  protected static boolean matchesSegment(Field field, String segmentTag) {
    Class<?> clz = field.getType();

    if (Collection.class.isAssignableFrom(clz)) {
      clz = getCollectionType(field);
    }

    if (clz.isAnnotationPresent(EDISegment.class)) {
      EDISegment es = clz.getAnnotation(EDISegment.class);
      if (StringUtils.equals(segmentTag, es.tag())) {
        return true;
      }
    } else if (clz.isAnnotationPresent(EDISegmentGroup.class)) {
      EDISegmentGroup esg = clz.getAnnotation(EDISegmentGroup.class);

      String ediSegmentGroupTag = esg.header();

      if (StringUtils.isNotBlank(ediSegmentGroupTag)) {
        if (StringUtils.equals(segmentTag, ediSegmentGroupTag)) {
          return true;
        }
      } else {
        // get the segement group's first field, and recurse.
        if (clz.getDeclaredFields().length > 0) {
          return matchesSegment(clz.getDeclaredFields()[0], segmentTag);
        }
      }
    }

    return false;
  }