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:es.caib.sgtsic.utils.ejb.AbstractService.java

private List<Field> getMappedFields() {

    List<Field> lBound = new ArrayList<>();
    for (Field f : entityClass.getDeclaredFields()) {
        if (f.getAnnotation(OneToMany.class) != null || f.getAnnotation(ManyToMany.class) != null) {
            lBound.add(f);/*from   w ww .ja v  a2s .c  o  m*/
            continue;
        }
        if (f.getAnnotation(OneToOne.class) != null && !"".equals(f.getAnnotation(OneToOne.class).mappedBy())) {
            lBound.add(f);
        }
    }
    return lBound;
}

From source file:cn.xdf.thinkutils.db2.util.sql.UpdateSqlBuilder.java

/**
 * Where?/*from  w w  w .j  av  a2s. c  om*/
 * 
 * @param entity
 * @return
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 * @throws DBException
 */
public ArrayListEx buildWhere(Object entity)
        throws IllegalArgumentException, IllegalAccessException, DBException {
    Class<?> clazz = entity.getClass();
    ArrayListEx whereArrayList = new ArrayListEx();
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        field.setAccessible(true);
        if (!DBUtils.isTransient(field)) {
            if (DBUtils.isBaseDateType(field)) {
                Annotation annotation = field.getAnnotation(PrimaryKey.class);
                if (annotation != null) {
                    String columnName = DBUtils.getColumnByField(field);
                    whereArrayList.add(
                            (columnName != null && !columnName.equals("")) ? columnName : field.getName(),
                            field.get(entity).toString());
                }

            }
        }
    }
    if (whereArrayList.isEmpty()) {
        throw new DBException("?Where??");
    }
    return whereArrayList;
}

From source file:com.CodeSeance.JSeance2.CodeGenXML.TemplateElements.Node.java

public void loadAttributes(Context context) {
    StringBuilder missingParameters = new StringBuilder();
    try {/*from   ww  w  . j  a va  2s.c  o  m*/
        Object[] params = evaluateParams(context, arguments);
        int paramNumber = 0;
        for (Field field : this.getClass().getDeclaredFields()) {
            if (field.isAnnotationPresent(TagParameter.class)) {
                TagParameter annotation = field.getAnnotation(TagParameter.class);

                if (paramNumber < params.length) {
                    Object paramValue = params[paramNumber++];
                    setFieldValue(field, paramValue == null ? annotation.defaultValue() : paramValue);
                } else if (!annotation.required()) {
                    setFieldValue(field, annotation.defaultValue());
                } else {
                    missingParameters
                            .append(missingParameters.length() > 0 ? ", " + field.getName() : field.getName());
                }
            }

        }
    } catch (Exception ex) {
        throw new RuntimeException(ExecutionError.INVALID_TAG_ARGUMENTS.getMessage(arguments,
                position.getLine(), position.getCol(), ex.getMessage()));
    }

    if (missingParameters.length() > 0) {
        throw new RuntimeException(ExecutionError.MISSING_TAG_ARGUMENTS.getMessage(missingParameters,
                position.getLine(), position.getCol()));
    }
}

From source file:com.changhong.util.db.util.sql.UpdateSqlBuilder.java

/**
 * Where?//ww w  .  j a  v a 2s.  co m
 * 
 * @param entity
 * @return
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 * @throws CHDBException
 */
public CHArrayList buildWhere(Object entity)
        throws IllegalArgumentException, IllegalAccessException, CHDBException {
    Class<?> clazz = entity.getClass();
    CHArrayList whereArrayList = new CHArrayList();
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        field.setAccessible(true);
        if (!DBUtils.isTransient(field)) {
            if (DBUtils.isBaseDateType(field)) {
                Annotation annotation = field.getAnnotation(CHPrimaryKey.class);
                if (annotation != null) {
                    String columnName = DBUtils.getColumnByField(field);
                    whereArrayList.add(
                            (columnName != null && !columnName.equals("")) ? columnName : field.getName(),
                            field.get(entity).toString());
                }

            }
        }
    }
    if (whereArrayList.isEmpty()) {
        throw new CHDBException("?Where??");
    }
    return whereArrayList;
}

From source file:acmi.l2.clientmod.xdat.Controller.java

private static void buildTree(IOEntity entity, Field listField, TreeView<Object> elements, String nameFilter) {
    elements.setRoot(null);//  ww  w .ja v  a 2s  . co m

    if (entity == null)
        return;

    try {
        List<IOEntity> list = (List<IOEntity>) listField.get(entity);
        if (!listField.isAnnotationPresent(Type.class)) {
            log.log(Level.WARNING, String.format("XDAT.%s: @Type not defined", listField.getName()));
            Dialogs.show(Alert.AlertType.ERROR, "ReflectiveOperationException", null,
                    String.format("XDAT.%s: @Type not defined", listField.getName()));
        } else {
            Class<? extends IOEntity> type = listField.getAnnotation(Type.class).value()
                    .asSubclass(IOEntity.class);
            TreeItem<Object> rootItem = new TreeItem<>(new ListHolder(entity, list, listField.getName(), type));

            elements.setRoot(rootItem);

            rootItem.getChildren().addAll(list.stream().map(Controller::createTreeItem)
                    .filter(treeItem -> checkTreeNode(treeItem, nameFilter)).collect(Collectors.toList()));
        }
    } catch (IllegalAccessException e) {
        log.log(Level.WARNING, String.format("%s.%s is not accessible",
                listField.getDeclaringClass().getSimpleName(), listField.getName()), e);
        Dialogs.show(Alert.AlertType.ERROR, "ReflectiveOperationException", null,
                listField.getDeclaringClass().getSimpleName() + "." + listField.getName()
                        + " is not accessible");
    }
}

From source file:com.alading.library.util.db.util.sql.TAUpdateSqlBuilder.java

/**
 * Where?/*  w ww . j  ava 2  s .c o m*/
 * 
 * @param entity
 * @return
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 * @throws TADBException
 */
public TAArrayList buildWhere(Object entity)
        throws IllegalArgumentException, IllegalAccessException, TADBException {
    Class<?> clazz = entity.getClass();
    TAArrayList whereArrayList = new TAArrayList();
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        field.setAccessible(true);
        if (!TADBUtils.isTransient(field)) {
            if (TADBUtils.isBaseDateType(field)) {
                Annotation annotation = field.getAnnotation(TAPrimaryKey.class);
                if (annotation != null) {
                    String columnName = TADBUtils.getColumnByField(field);
                    whereArrayList.add(
                            (columnName != null && !columnName.equals("")) ? columnName : field.getName(),
                            field.get(entity).toString());
                }

            }
        }
    }
    if (whereArrayList.isEmpty()) {
        throw new TADBException("?Where??");
    }
    return whereArrayList;
}

From source file:com.witness.utils.db.util.sql.TAUpdateSqlBuilder.java

/**
 * Where?// www . j  a v  a2 s  . c o  m
 *
 * @param entity
 * @return
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 * @throws TADBException
 */
public TAArrayList buildWhere(Object entity)
        throws IllegalArgumentException, IllegalAccessException, TADBException {
    Class<?> clazz = entity.getClass();
    TAArrayList whereArrayList = new TAArrayList();
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        field.setAccessible(true);
        if (!TADBUtils.isTransient(field)) {
            if (TADBUtils.isBaseDateType(field)) {
                Annotation annotation = field.getAnnotation(DBPrimaryKey.class);
                if (annotation != null) {
                    String columnName = TADBUtils.getColumnByField(field);
                    whereArrayList.add(
                            (columnName != null && !columnName.equals("")) ? columnName : field.getName(),
                            field.get(entity).toString());
                }

            }
        }
    }
    if (whereArrayList.isEmpty()) {
        throw new TADBException("?Where??");
    }
    return whereArrayList;
}

From source file:ea.compoment.db.util.sql.UpdateSqlBuilder.java

/**
 * Where?/*from  w  w  w .j a  v  a 2s . co  m*/
 * 
 * @param entity
 * @return
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 * @throws DBException
 */
public NVArrayList buildWhere(Object entity)
        throws IllegalArgumentException, IllegalAccessException, DBException {
    Class<?> clazz = entity.getClass();
    NVArrayList whereArrayList = new NVArrayList();
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        field.setAccessible(true);
        if (!DBAnnoUtils.isTransient(field)) {
            if (DBAnnoUtils.isBaseDateType(field)) {
                Annotation annotation = field.getAnnotation(PrimaryKey.class);
                if (annotation != null) {
                    String columnName = DBAnnoUtils.getColumnByField(field);
                    whereArrayList.add(
                            (columnName != null && !columnName.equals("")) ? columnName : field.getName(),
                            field.get(entity).toString());
                }

            }
        }
    }
    if (whereArrayList.isEmpty()) {
        throw new DBException("?Where??");
    }
    return whereArrayList;
}

From source file:fr.exanpe.tapestry.tldgen.taglib.builder.StructureBuilder.java

/**
 * Build the &lt;attribute&gt; tag corresponding to a field
 * //  ww  w.  j ava  2s  . com
 * @param field the field
 * @return the {@link Attribute} object
 */
private Attribute buildAttributeFromField(Field field) {
    log.debug("Processing Attribute : " + field.getName());

    Attribute attribute = new Attribute();
    attribute.setName(StringUtils.isNotEmpty(field.getAnnotation(Parameter.class).name())
            ? field.getAnnotation(Parameter.class).name()
            : field.getName());

    attribute.setDeferredValue(field.getType().getName());
    attribute.setRequired(field.getAnnotation(Parameter.class).required());
    attribute.setParameterDescription(buildParameterDescription(field.getAnnotation(Parameter.class)));

    return attribute;
}

From source file:fr.juanwolf.mysqlbinlogreplicator.component.DomainClassAnalyzer.java

@PostConstruct
public void postConstruct() throws BeansException, NoSuchMethodException, IllegalAccessException,
        InvocationTargetException, InstantiationException {
    mappingTablesExpected = new ArrayList<>();
    nestedTables = new ArrayList<>();
    Reflections reflections = new Reflections(scanMapping);
    Set<Class<?>> types = reflections.getTypesAnnotatedWith(MysqlMapping.class);
    final Iterator<Class<?>> iterator = types.iterator();
    while (iterator.hasNext()) {
        Class classDomain = iterator.next();
        MysqlMapping mysqlMapping = (MysqlMapping) classDomain.getAnnotation(MysqlMapping.class);
        DomainClass domainClass = new DomainClass();
        domainClass.setDomainClass(classDomain);
        mappingTablesExpected.add(mysqlMapping.table());
        CrudRepository crudRepository = (CrudRepository) applicationContext.getBean(mysqlMapping.repository());
        domainClass.setCrudRepository(crudRepository);
        domainClass.setTable(mysqlMapping.table());
        Map<String, SQLRequester> nestedClassesMap = new HashMap<>();
        for (Field field : classDomain.getDeclaredFields()) {
            NestedMapping nestedMapping = field.getAnnotation(NestedMapping.class);
            if (nestedMapping != null) {
                Class sqlRequesterClass = nestedMapping.sqlAssociaton().getRequesterClass();
                Constructor sqlRequesterConstructor = sqlRequesterClass.getConstructor();
                SQLRequester sqlRequester = (SQLRequester) sqlRequesterConstructor.newInstance();
                sqlRequester.setDatabaseName(databaseName);
                sqlRequester.setEntryTableName(mysqlMapping.table());
                sqlRequester.setExitTableName(nestedMapping.table());
                sqlRequester.setForeignKey(nestedMapping.foreignKey());
                sqlRequester.setPrimaryKeyForeignEntity(nestedMapping.primaryKey());
                sqlRequester.setEntryType(classDomain);
                sqlRequester.setAssociatedField(field);
                Class foreignType = field.getType();
                if (field.getGenericType() instanceof ParameterizedType) {
                    ParameterizedType genericType = (ParameterizedType) field.getGenericType();
                    Class properType = (Class) genericType.getActualTypeArguments()[0];
                    foreignType = properType;
                }//  w  w w . j a v a 2  s .c o  m
                sqlRequester.setForeignType(foreignType);
                sqlRequester.setJdbcTemplate(jdbcTemplate);
                NestedRowMapper currentClassNestedRowMapper = new NestedRowMapper(classDomain, this,
                        mysqlMapping.table());
                NestedRowMapper foreignClassNestedRowMapper = new NestedRowMapper(foreignType, this,
                        mysqlMapping.table());
                sqlRequester.setRowMapper(currentClassNestedRowMapper);
                sqlRequester.setForeignRowMapper(foreignClassNestedRowMapper);
                nestedClassesMap.put(field.getName(), sqlRequester);
                nestedTables.add(nestedMapping.table());
                nestedDomainClassMap.put(nestedMapping.table(), domainClass);
            }
        }
        domainClass.setSqlRequesters(nestedClassesMap);
        domainClassMap.put(domainClass.getTable(), domainClass);
    }
    if (environment.getProperty("date.output") != null) {
        binlogOutputDateFormatter = new SimpleDateFormat(environment.getProperty("date.output"));
    }
}