Example usage for java.lang.reflect Field getAnnotationsByType

List of usage examples for java.lang.reflect Field getAnnotationsByType

Introduction

In this page you can find the example usage for java.lang.reflect Field getAnnotationsByType.

Prototype

@Override
public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) 

Source Link

Usage

From source file:com.yfiton.core.Yfiton.java

private void injectParameterValues(Notifier notifiers, Parameters receivedParameters)
        throws ParameterException {
    for (Field field : AnnotationProcessor.getAllFields(notifier.getClass())) {
        // annotations available in the class or in its hierarchy
        com.yfiton.api.annotation.Parameter[] annotations = field
                .getAnnotationsByType(com.yfiton.api.annotation.Parameter.class);

        if (annotations.length == 0) {
            continue;
        }/*from   w w  w  . j a  va 2 s.c o m*/

        com.yfiton.api.annotation.Parameter annotation = annotations[0];

        String parameterName = annotation.name();
        if (parameterName.isEmpty()) {
            parameterName = field.getName();
        }

        // retrieve object representation of the parameter from already parsed annotations
        Parameter parameter = supportedParameters.get(notifier.getKey()).get(parameterName);

        if (parameter != null) {
            try {
                field.setAccessible(true);

                Object receivedValue = receivedParameters.getValue(parameter);

                if (receivedValue != null) {
                    field.set(notifier, receivedValue);
                } else if (receivedValue == null && parameter.isHidden() && parameter.isRequired()) {
                    field.set(notifier, Console.readParameterValueFromStdin(parameterName));
                } else if (receivedValue == null && parameter.isRequired()) {
                    throw new ParameterException("Missing required parameter '" + parameterName + "'");
                }
            } catch (IllegalAccessException e) {
                log.warn("Cannot inject parameter value for parameter named '{}'", parameter.getName());
            }
        }
    }
}

From source file:gr.abiss.calipso.fs.FilePersistenceService.java

public default Map<String, FilePersistencePreview> getPreviews(Field fileField) {
    Map<String, FilePersistencePreview> previews = new HashMap<String, FilePersistencePreview>();
    if (fileField.isAnnotationPresent(FilePersistencePreviews.class)) {
        FilePersistencePreview[] tmp = fileField.getAnnotation(FilePersistencePreviews.class).value();
        if (tmp != null) {
            for (int i = 0; i < tmp.length; i++) {
                FilePersistencePreview preview = tmp[i];
                previews.put(preview.maxWidth() + "x" + preview.maxHeight(), preview);
            }/*from   www .  j  a va  2  s  . c om*/
        }
    }
    if (fileField.isAnnotationPresent(FilePersistencePreview.class)) {
        FilePersistencePreview[] tmp = fileField.getAnnotationsByType(FilePersistencePreview.class);
        for (int i = 0; i < tmp.length; i++) {
            FilePersistencePreview preview = tmp[i];
            previews.put(preview.maxWidth() + "x" + preview.maxHeight(), preview);
        }
    }
    return previews;
}

From source file:com.datumbox.common.persistentstorage.factories.MongoDBStructureFactory.java

private void handleBigDataStructureField(Field field, BigDataStructureContainer learnableClass,
        MemoryConfiguration memoryConfiguration) {
    //check if the field is annotated as BigDataStructureMarker and if it is marked as Transient
    if (field.getAnnotationsByType(BigDataStructureMarker.class).length > 0
            && field.getAnnotationsByType(Transient.class).length > 0) {
        field.setAccessible(true);/*  w  w w .  j  a v  a 2  s  . c  o m*/

        //WARNING! DO NOT CHANGE THE ORDER OF IFs
        if (memoryConfiguration.getMapType().isInMemory() && Map.class.isAssignableFrom(field.getType())) {
            try {
                //try
                this //select our database
                        .getMap(field.getName(), getDefaultMapType(), getDefaultLRUsize()) //open a Map on DB with name equal to the field
                        .putAll((Map<? extends Object, ? extends Object>) field.get(learnableClass) //put inside all the contents of the field. We read the contents with reflection
                );
            } catch (IllegalArgumentException | IllegalAccessException ex) {
                throw new IllegalArgumentException(
                        "Could not store the collection in database. Try chaning the configuration to DB-backed collections.");
            }
        } else if (memoryConfiguration.getSetType().isInMemory()
                && Set.class.isAssignableFrom(field.getType())) {
            try {
                //try
                this //select our database
                        .getSet(field.getName(), getDefaultSetType()) //open a Set on DB with name equal to the field
                        .addAll((Set<? extends Object>) field.get(learnableClass) //put inside all the contents of the field. We read the contents with reflection
                );
            } catch (IllegalArgumentException | IllegalAccessException ex) {
                throw new IllegalArgumentException(
                        "Could not store the collection in database. Try chaning the configuration to DB-backed collections.");
            }
        } else if (memoryConfiguration.getQueueType().isInMemory()
                && Queue.class.isAssignableFrom(field.getType())) {
            try {
                //try
                this //select our database
                        .getQueue(field.getName(), getDefaultQueueType()) //open a Queue on DB with name equal to the field
                        .addAll((Queue<? extends Object>) field.get(learnableClass) //put inside all the contents of the field. We read the contents with reflection
                );
            } catch (IllegalArgumentException | IllegalAccessException ex) {
                throw new IllegalArgumentException(
                        "Could not store the collection in database. Try chaning the configuration to DB-backed collections.");
            }
        } else if (memoryConfiguration.getCollectionType().isInMemory()
                && Collection.class.isAssignableFrom(field.getType())) {
            try {
                //try
                this //select our database
                        .getCollection(field.getName(), getDefaultCollectionType()) //open a Collection on DB with name equal to the field
                        .addAll((Collection<? extends Object>) field.get(learnableClass) //put inside all the contents of the field. We read the contents with reflection
                );
            } catch (IllegalArgumentException | IllegalAccessException ex) {
                throw new IllegalArgumentException(
                        "Could not store the collection in database. Try chaning the configuration to DB-backed collections.");
            }
        } else {
            //no need to save it on the DB because it is not in memory or a type that can be handled
        }
    }
}

From source file:net.udidb.engine.ops.impls.help.HelpMessageProvider.java

private String getOperandDescription(Field field) {
    HelpMessage[] helpMessages = field.getAnnotationsByType(HelpMessage.class);
    String message;//from w  ww  .  j a va2s  .  c  om
    if (helpMessages.length != 0) {
        message = selectMessage(helpMessages);
    } else {
        message = null;
    }

    if (message == null) {
        Class<?> mixInClass = operandTypeHelpMixins.get(field.getType().getCanonicalName());
        if (mixInClass != null) {
            HelpMessage[] mixInMessages = mixInClass.getAnnotationsByType(HelpMessage.class);
            message = selectMessage(mixInMessages);
        }
    }

    return message;
}

From source file:ru.savvy.springjsf.system.CustomJsfInjectionProvider.java

private void injectFields(Object bean, Class<? extends Annotation> annotation)
        throws InjectionProviderException {
    // get current WebApplicationContext
    FacesContext fc = FacesContext.getCurrentInstance();
    if (fc == null) {
        throw new InjectionProviderException("Unable to get current Faces context",
                new NullPointerException("Faces context is null"));
    }//from w  w w.j av  a 2 s. c  o  m
    WebApplicationContext wac = FacesContextUtils.getWebApplicationContext(fc);
    if (wac == null) {
        throw new InjectionProviderException("Unable to get Spring WebApplicationContext",
                new NullPointerException("WebApplicationContext is null"));
    }
    Field[] fields = bean.getClass().getDeclaredFields();
    for (Field f : fields) {
        if (f.getAnnotationsByType(annotation).length > 0) {
            Class<?> typeClazz = f.getType();
            Object injection;
            try {
                injection = wac.getBean(typeClazz);
            } catch (NoSuchBeanDefinitionException e) {
                e.printStackTrace();
                throw new InjectionProviderException("Unable to inject bean of type " + typeClazz.toString(),
                        e);
            }
            if (!f.isAccessible()) {
                f.setAccessible(true);
            }
            try {
                f.set(bean, injection);
                if (logger.isDebugEnabled()) {
                    logger.debug("Injected field of type " + f.getType().getCanonicalName() + " into "
                            + bean.getClass().getCanonicalName());
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

}