Example usage for java.lang.reflect Field getAnnotation

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

Introduction

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

Prototype

public <T extends Annotation> T getAnnotation(Class<T> annotationClass) 

Source Link

Usage

From source file:org.cybercat.automation.annotations.AnnotationBuilder.java

public static final <T extends IFeature> void processCCPageObject(T entity)
        throws AutomationFrameworkException {
    for (Field field : entity.getClass().getDeclaredFields()) {
        field.setAccessible(true);//w ww  . j  av  a 2s .  com
        if (field.getAnnotation(CCPageObject.class) != null) {
            createPageObjectField(entity, field);
        } else if (field.getAnnotation(CCProperty.class) != null) {
            processPropertyField(entity, field);
        }
    }
}

From source file:net.ostis.sc.memory.SCKeynodesBase.java

private static boolean checkKeynodesNumberPatternURI(SCSession session, Class<?> klass, Field field)
        throws IllegalArgumentException, IllegalAccessException, SecurityException, NoSuchFieldException {
    KeynodesNumberPatternURI patternURI = field.getAnnotation(KeynodesNumberPatternURI.class);

    if (patternURI != null) {
        String[] comp = URIUtils.splitByIdtf(patternURI.patternURI());

        SCSegment segment = session.openSegment(comp[0]);

        List<SCAddr> keynodes = new LinkedList<SCAddr>();
        for (int i = patternURI.startIndex(); i <= patternURI.endIndex(); ++i) {
            String keynodeName = MessageFormat.format(comp[1], i);

            SCAddr keynode = session.findByIdtf(keynodeName, segment);
            Validate.notNull(keynode, keynodeName);

            keynodes.add(keynode);//from ww w  . java  2 s .  c om

            String fieldName = MessageFormat.format(patternURI.patternName(), i);
            Field keynodeField = klass.getField(fieldName);
            keynodeField.set(null, keynode);

            if (log.isDebugEnabled())
                log.debug(comp[0] + "/" + keynodeName + " --> " + keynodeField.getName());
        }

        field.set(null, (SCAddr[]) keynodes.toArray(new SCAddr[keynodes.size()]));

        if (log.isDebugEnabled())
            log.debug(patternURI.patternURI() + " --> " + field.getName());

        return true;
    } else {
        return false;
    }
}

From source file:be.fedict.eid.applet.service.AppletServiceServlet.java

public static void injectInitParams(ServletConfig config, MessageHandler<?> messageHandler)
        throws ServletException, IllegalArgumentException, IllegalAccessException {
    Class<?> messageHandlerClass = messageHandler.getClass();
    Field[] fields = messageHandlerClass.getDeclaredFields();
    for (Field field : fields) {
        InitParam initParamAnnotation = field.getAnnotation(InitParam.class);
        if (null == initParamAnnotation) {
            continue;
        }//from  w  w w .j a  v a 2  s . c o m
        String initParamName = initParamAnnotation.value();
        Class<?> fieldType = field.getType();
        field.setAccessible(true);
        if (ServiceLocator.class.equals(fieldType)) {
            /*
             * We always inject a service locator.
             */
            ServiceLocator<Object> fieldValue = new ServiceLocator<Object>(initParamName, config);
            field.set(messageHandler, fieldValue);
            continue;
        }
        String initParamValue = config.getInitParameter(initParamName);
        if (initParamAnnotation.required() && null == initParamValue) {
            throw new ServletException("missing required init-param: " + initParamName + " for message handler:"
                    + messageHandlerClass.getName());
        }
        if (null == initParamValue) {
            continue;
        }
        if (Boolean.TYPE.equals(fieldType)) {
            LOG.debug("injecting boolean: " + initParamValue);
            Boolean fieldValue = Boolean.parseBoolean(initParamValue);
            field.set(messageHandler, fieldValue);
            continue;
        }
        if (String.class.equals(fieldType)) {
            field.set(messageHandler, initParamValue);
            continue;
        }
        if (InetAddress.class.equals(fieldType)) {
            InetAddress inetAddress;
            try {
                inetAddress = InetAddress.getByName(initParamValue);
            } catch (UnknownHostException e) {
                throw new ServletException("unknown host: " + initParamValue);
            }
            field.set(messageHandler, inetAddress);
            continue;
        }
        if (Long.class.equals(fieldType)) {
            Long fieldValue = Long.parseLong(initParamValue);
            field.set(messageHandler, fieldValue);
            continue;
        }
        throw new ServletException("unsupported init-param field type: " + fieldType.getName());
    }
}

From source file:com.eviware.x.form.support.ADialogBuilder.java

private static void buildForm(XFormDialogBuilder builder, String name, Class<?> formClass,
        MessageSupport messages) {// w w  w .j  a v  a 2  s. c  o  m
    XForm form = builder.createForm(name);
    for (Field formField : formClass.getFields()) {
        AField formFieldAnnotation = formField.getAnnotation(AField.class);
        if (formFieldAnnotation != null) {
            try {
                addFormField(form, formField, formFieldAnnotation, messages);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:com.google.code.simplestuff.bean.BusinessObjectContext.java

/**
 * Returns an array of all fields used by this object from it's class and
 * all super classes.//from   w w  w.  j a v a  2  s.  c om
 * 
 * @author Andrew Phillips (anph)
 * @param objectClass the class
 * @param fields the current field list
 * @return an array of fields
 * 
 */
private static Collection<Field> getAnnotatedFields(Class<? extends Object> objectClass) {
    final List<Field> businessFields = new ArrayList<Field>();

    ReflectionUtils.doWithFields(objectClass, new FieldCallback() {

        // simply add each found field to the list
        public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
            businessFields.add(field);
        }

    }, new FieldFilter() {

        // match fields with the "@BusinessField" annotation
        public boolean matches(Field field) {
            return (field.getAnnotation(BusinessField.class) != null);
        }

    });

    return businessFields;
}

From source file:com.eviware.x.form.support.ADialogBuilder.java

public static XFormDialog buildWizard(Class<? extends Object> tabbedFormClass) {
    AForm formAnnotation = tabbedFormClass.getAnnotation(AForm.class);
    if (formAnnotation == null) {
        throw new RuntimeException("formClass is not annotated correctly..");
    }/*from www  . j a  va2 s.com*/

    MessageSupport messages = MessageSupport.getMessages(tabbedFormClass);
    XFormDialogBuilder builder = XFormFactory.createDialogBuilder(formAnnotation.name());

    for (Field field : tabbedFormClass.getFields()) {
        APage pageAnnotation = field.getAnnotation(APage.class);
        if (pageAnnotation != null) {
            buildForm(builder, pageAnnotation.name(), field.getType(), messages);
        }
    }

    XFormDialog dialog = builder.buildWizard(formAnnotation.description(),
            UISupport.createImageIcon(formAnnotation.icon()), formAnnotation.helpUrl());

    return dialog;
}

From source file:com.cloudbees.jenkins.support.impl.LoadStats.java

/**
 * The fields that a {@link LoadStatistics} has change as you move from pre-1.607 to post 1.607, so better to
 * just look and see what there is rather than hard-code.
 *
 * @return the fields that correspond to {@link MultiStageTimeSeries}
 *///from  ww w. j ava 2s  .c  om
private static List<Field> findFields() {
    List<Field> result = new ArrayList<Field>();
    for (Field f : LoadStatistics.class.getFields()) {
        if (Modifier.isPublic(f.getModifiers()) && MultiStageTimeSeries.class.isAssignableFrom(f.getType())
                && f.getAnnotation(Deprecated.class) == null) {
            result.add(f);
        }
    }
    return result;
}

From source file:com.github.reinert.jjschema.HyperSchemaGeneratorV4.java

private static <T> ObjectNode transformJsonToHyperSchema(Class<T> type, ObjectNode jsonSchema) {
    ObjectNode properties = (ObjectNode) jsonSchema.get("properties");
    Iterator<String> namesIterator = properties.fieldNames();

    while (namesIterator.hasNext()) {
        String prop = namesIterator.next();
        try {/*  w w  w .ja v a2 s . co m*/
            Field field = type.getDeclaredField(prop);
            com.github.reinert.jjschema.Media media = field
                    .getAnnotation(com.github.reinert.jjschema.Media.class);
            if (media != null) {
                ObjectNode hyperProp = (ObjectNode) properties.get(prop);
                hyperProp.put(MEDIA_TYPE, media.type());
                hyperProp.put(BINARY_ENCODING, media.binaryEncoding());
                if (hyperProp.get("type").isArray()) {
                    TextNode typeString = new TextNode("string");
                    ((ArrayNode) hyperProp.get("type")).set(0, typeString);
                } else {
                    hyperProp.put("type", "string");
                }
                properties.put(prop, hyperProp);
            }
        } catch (NoSuchFieldException e) {
            //e.printStackTrace();
        } catch (SecurityException e) {
            //e.printStackTrace();
        }
    }
    return jsonSchema;
}

From source file:com.alfresco.orm.ORMUtil.java

/**
 * @param alfrescoContent//w  w w.j a va 2s.  co  m
 * @param field
 * @return
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 */
private static List<AlfrescoContent> getAsscoiationObject(final AlfrescoContent alfrescoContent,
        final Field field) throws IllegalAccessException, InvocationTargetException {
    List<AlfrescoContent> retVal = new ArrayList<AlfrescoContent>();
    AlfrescoAssociation alfrescoAssociation = field.getAnnotation(AlfrescoAssociation.class);
    Method method = ReflectionUtil.getMethod(alfrescoContent.getClass(), field.getName());
    if (alfrescoAssociation.many()) {
        Collection<? extends AlfrescoContent> temp = (Collection<? extends AlfrescoContent>) method
                .invoke(alfrescoContent);
        if (null != temp) {
            retVal.addAll(temp);
        }
    } else {
        AlfrescoContent temp = (AlfrescoContent) method.invoke(alfrescoContent);
        if (null != temp) {
            retVal.add(temp);
        }
    }
    return retVal;
}

From source file:com.searchbox.core.ref.ReflectionUtils.java

public static void inspectAndSaveAttribute(Class<?> searchElement, Collection<AttributeEntity> attributes) {
    if (searchElement != null) {
        for (Field field : searchElement.getDeclaredFields()) {
            if (field.isAnnotationPresent(SearchAttribute.class)) {
                AttributeEntity attrDef = new AttributeEntity().setName(field.getName())
                        .setType(field.getType());
                String value = field.getAnnotation(SearchAttribute.class).value();
                if (value != null && !value.isEmpty()) {
                    try {
                        Object ovalue = BeanUtils.instantiateClass(field.getType().getConstructor(String.class),
                                value);/*from   ww  w  .  jav  a 2  s.co  m*/
                        attrDef.setValue(ovalue);
                    } catch (BeanInstantiationException | NoSuchMethodException | SecurityException e) {
                        LOGGER.trace("Could not construct default value (not much of a problem)", e);
                    }
                }
                attributes.add(attrDef);
            }
        }
        inspectAndSaveAttribute(searchElement.getSuperclass(), attributes);
    } else {
        return;
    }
}