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:fr.eoit.db.AbstractDTOPopulator.java

public List<FieldDefinition> getFieldDefinition(Class<?> beanClass) {
    if (beanClassCache.containsKey(beanClass)) {
        return beanClassCache.get(beanClass);
    } else if (beanClass.getAnnotation(DTO.class) != null) {
        List<FieldDefinition> fieldDefinitions = new ArrayList<FieldDefinition>();
        for (Field field : beanClass.getDeclaredFields()) {
            DTOField fieldAnnotation = field.getAnnotation(DTOField.class);
            if (fieldAnnotation != null) {
                FieldDefinition fieldDefinition = new FieldDefinition(fieldAnnotation);
                if (fieldAnnotation.name().equals("")) {
                    fieldDefinition.columnName = field.getName();
                }/*from   w w w  . ja  v a 2  s .  c  o  m*/
                fieldDefinition.fieldName = field.getName();
                fieldDefinitions.add(fieldDefinition);
            }
        }

        fieldDefinitions.addAll(getFieldDefinition(beanClass.getSuperclass()));
        beanClassCache.put(beanClass, fieldDefinitions);
        return fieldDefinitions;
    } else {
        return new ArrayList<FieldDefinition>();
    }
}

From source file:main.okapi.cf.annotations.AnnotationsInfo.java

public JSONObject getInfo() throws ClassNotFoundException, IOException, JSONException {
    JSONObject obj = new JSONObject();
    ArrayList<JSONObject> cl = new ArrayList<JSONObject>();
    Iterable<Class> classes = getClasses(this.topPackage);
    for (Class c : classes) {
        JSONObject forClass = new JSONObject();
        if (c.getAnnotations().length > 0) {

            ArrayList<JSONObject> parameters = new ArrayList<JSONObject>();
            for (Field field : c.getDeclaredFields()) {
                if (field.isAnnotationPresent(HyperParameter.class)) {
                    HyperParameter hp = field.getAnnotation(HyperParameter.class);
                    JSONObject parJSON = new JSONObject();
                    parJSON.put("parameterName", hp.parameterName());
                    parJSON.put("defaultValue", hp.defaultValue());
                    parJSON.put("minimumValue", hp.minimumValue());
                    parJSON.put("maximumValue", hp.maximumValue());
                    parameters.add(parJSON);
                }//  w ww .j  ava 2  s.  c om
            }
            JSONObject method = new JSONObject();
            method.put("hyperParameters", parameters);
            method.put("class", c.getCanonicalName());
            cl.add(method);
        }
    }
    obj.put("methods", cl);
    return obj;
}

From source file:management.limbr.ui.entity.EntityEditorPresenter.java

private void commitFieldValue(Field field) {
    Password passwordAnnotation = field.getAnnotation(Password.class);
    if (passwordAnnotation != null) {
        String value = getView().getFieldValue(String.class, field.getName());
        if (!DEFAULT_VALUE.equals(value)) {
            try {
                Field saltField = entity.getClass().getDeclaredField(passwordAnnotation.saltWith());

                Object saltValue = getView().getFieldValue(saltField.getType(), saltField.getName());

                entityUtil.callSetter(entity, field.getName(),
                        entityUtil.generatePasswordHash(saltValue.toString(), value));

            } catch (NoSuchFieldException ex) {
                LOG.warn("Cannot find field " + passwordAnnotation.saltWith() + " to hash with password field "
                        + field.getName() + " of " + entity.getClass().getName(), ex);
            }// w w  w  . j  a v  a 2s .c om
        }
    } else {
        entityUtil.callSetter(entity, field.getName(),
                getView().getFieldValue(field.getType(), field.getName()));
    }
}

From source file:net.kamhon.ieagle.vo.ORWrapper.java

private String getDbColumn(Field field) {
    if (field.isAnnotationPresent(Column.class)) {
        Column column = field.getAnnotation(Column.class);
        return column.name();
    } else {/*from w w  w .java  2 s . co m*/
        return null;
    }
}

From source file:com.microsoft.rest.serializer.FlatteningDeserializer.java

@SuppressWarnings("unchecked")
@Override/*from w  ww . jav  a 2s. c  o m*/
public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    JsonNode root = mapper.readTree(jp);
    final Class<?> tClass = this.defaultDeserializer.handledType();
    for (Field field : tClass.getDeclaredFields()) {
        JsonNode node = root;
        JsonProperty property = field.getAnnotation(JsonProperty.class);
        if (property != null) {
            String value = property.value();
            if (value.matches(".+[^\\\\]\\..+")) {
                String[] values = value.split("((?<!\\\\))\\.");
                for (String val : values) {
                    val = val.replace("\\.", ".");
                    node = node.get(val);
                    if (node == null) {
                        break;
                    }
                }
                ((ObjectNode) root).put(value, node);
            }
        }
    }
    JsonParser parser = new JsonFactory().createParser(root.toString());
    parser.nextToken();
    return defaultDeserializer.deserialize(parser, ctxt);
}

From source file:com.threewks.thundr.search.gae.meta.SearchMetadata.java

private String determineName(Field field) {
    SearchIndex annotation = field.getAnnotation(SearchIndex.class);
    return annotation == null || StringUtils.isBlank(annotation.value()) ? field.getName() : annotation.value();
}

From source file:atunit.spring.SpringContainer.java

protected void fillInMissingFieldBeans(Class<?> testClass, GenericApplicationContext ctx) throws Exception {
    for (Field field : testClass.getDeclaredFields()) {
        Bean beanAnno = field.getAnnotation(Bean.class);
        if (beanAnno == null)
            continue;
        String name = beanAnno.value();
        if (!name.equals("") && !ctx.containsBean(name)) {
            ctx.registerBeanDefinition(name, defineAutowireBean(field.getType()));
        } else if (ctx.getBeansOfType(field.getType()).isEmpty()) {
            BeanDefinitionReaderUtils.registerWithGeneratedName(defineAutowireBean(field.getType()), ctx);
        }/* w w w.j av a2 s  .  c  o m*/
    }
}

From source file:org.oncoblocks.centromere.web.controller.ModelResourceAssembler.java

/**
 * Inspects the target {@link Model} class for {@link ForeignKey} annotations, and creates links
 *   based upon the inferred relationship and field names.
 * //from   www.j  a v a 2s  .  c  o m
 * @param t
 * @return
 */
private List<Link> addForeignKeyLinks(T t) {
    List<Link> links = new ArrayList<>();
    for (Field field : model.getDeclaredFields()) {
        ForeignKey fk = field.getAnnotation(ForeignKey.class);
        if (fk == null)
            continue;
        if (fk.model() == null)
            throw new RuntimeException(String.format(
                    "ForeignKey annotation for " + "class %s does not contain any class reference!",
                    model.getName()));
        Class<?> fkCLass = fk.model();
        String relName = fk.rel().equals("") ? field.getName() : fk.rel();
        String fieldName = fk.field().equals("") ? field.getName() : fk.field();
        if (!Model.class.isAssignableFrom(fkCLass))
            continue;
        Link link = null;
        try {
            field.setAccessible(true);
            if (fk.relationship().equals(ForeignKey.Relationship.MANY_TO_ONE)
                    && (!field.getType().isArray() && !Collection.class.isAssignableFrom(field.getType()))) {
                link = entityLinks.linkToSingleResource(fkCLass, field.get(t)).withRel(relName);
            } else if (fk.relationship().equals(ForeignKey.Relationship.ONE_TO_MANY)
                    && (field.getType().isArray() || Collection.class.isAssignableFrom(field.getType()))) {
                Map<String, Object> map = new HashMap<>();
                map.put(fieldName, field.get(t));
                link = entityLinks.linkToCollectionResource(fkCLass).expand(map).withRel(relName);
            } else if (fk.relationship().equals(ForeignKey.Relationship.MANY_TO_MANY)
                    && (field.getType().isArray() || Collection.class.isAssignableFrom(field.getType()))) {
                Map<String, Object> map = new HashMap<>();
                map.put(fieldName, field.get(t));
                link = entityLinks.linkToCollectionResource(fkCLass).expand(map).withRel(relName);
            } else {
                throw new RuntimeException(
                        String.format("Unable to determine correct link format for " + "field %s of class %s",
                                field.getName(), model.getName()));
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        if (link != null)
            links.add(link);
    }
    return links;
}

From source file:de.hsos.ecs.richwps.wpsmonitor.boundary.cli.command.annotation.CommandAnnotationProcessor.java

public void injectOptions(final Command cmd, final String[] args) throws CommandAnnotationProcessorException {
    try {/*from ww  w.  j  ava  2  s .c o m*/
        final Options opt = cmd.getOptions();
        final CommandLine cmdLine = parser.parse(opt, args);

        final List<Field> fields = getFields(cmd.getClass());

        for (final Field f : fields) {
            final CommandOption annotation = f.getAnnotation(CommandOption.class);

            if (annotation != null) {
                final String optionValue = cmdLine.getOptionValue(annotation.shortOptionName());

                if (annotation.required() && optionValue == null) {
                    throw new CommandAnnotationProcessorException(
                            "Option " + annotation.longOptionName() + " is required.");
                }

                Object toInject = null;

                if (f.getType() == Boolean.class) {
                    toInject = cmdLine.hasOption(annotation.shortOptionName());
                } else if (f.getType() != String.class) {
                    if (optionValue != null && !optionValue.isEmpty()) {
                        StringConverter converter = converterMap.get(f.getType());

                        if (converter == null) {
                            throw new CommandAnnotationProcessorException(
                                    "Can't convert the given string to " + f.getType().toString());
                        }

                        toInject = converter.convert(optionValue);
                    }
                } else {
                    toInject = optionValue;
                }

                f.setAccessible(true);
                f.set(cmd, toInject);
            }
        }
    } catch (ParseException | ConverterException | IllegalArgumentException | IllegalAccessException ex) {
        throw new CommandAnnotationProcessorException("Parse Exception.", ex);
    }
}

From source file:com.trigonic.utils.spring.cmdline.CommandLineMetaData.java

private void populateOptionFields(Class<?> beanClass) {
    Class<?> superClass = beanClass.getSuperclass();
    if (!superClass.equals(Object.class)) {
        populateOptionFields(superClass);
    }/*ww  w . j a v  a2s.c om*/
    for (Field field : beanClass.getDeclaredFields()) {
        Option option = field.getAnnotation(Option.class);
        if (option != null) {
            checkWriteableProperty(option, beanClass, field);
            options.put(option, new OptionFieldHandler(option, field));
        }
    }
}