Example usage for java.lang.reflect Field isAnnotationPresent

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

Introduction

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

Prototype

@Override
public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) 

Source Link

Usage

From source file:org.apdplat.platform.action.ExtJSSimpleAction.java

private void renderDataForExport(List<String> data, T obj) {
    //?//from  w w w  .  j av  a2 s.c om
    List<Field> fields = ReflectionUtils.getDeclaredFields(obj);

    for (Field field : fields) {
        if (field.isAnnotationPresent(ModelAttr.class)) {
            //??*_id
            Map<String, String> temp = new HashMap<>();
            addFieldValue(obj, field, temp);
            //??
            temp.remove(field.getName() + "_id");
            data.addAll(temp.values());
        }
    }
}

From source file:com.quangphuong.crawler.dbutil.DBWrapper.java

public List<Object> getEntities(Class<?> entity) {
    String sql = selectAll.concat(entity.getSimpleName());
    Connection con = DBHandler.openConnection();
    List<Object> listResult = new ArrayList();
    try {//from   w  ww .ja  va 2 s .com
        Statement statement = con.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);

        Field[] attributes = entity.getDeclaredFields();

        while (resultSet.next()) {
            List<Object> listFields = new ArrayList();
            List<Class<?>> listFieldTypes = new ArrayList();
            for (Field attribute : attributes) {
                attribute.setAccessible(true);
                if (!attribute.isAnnotationPresent(Ignore.class)) {
                    Object obj = resultSet.getObject(attribute.getName());
                    listFields.add(obj);
                    listFieldTypes.add(attribute.getType());
                }
            }
            Object result = entity.getConstructor((Class<?>[]) listFieldTypes.toArray(new Class[0]))
                    .newInstance(listFields.toArray());
            listResult.add(result);
        }
    } catch (Exception ex) {
        Logger.getLogger(DBWrapper.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    return listResult;
}

From source file:org.apdplat.platform.action.ExtJSSimpleAction.java

protected void renderForExport(List<List<String>> result) {
    List<String> data = new ArrayList<>();
    //?// www  .j  av  a2s . co m
    List<Field> fields = ReflectionUtils.getDeclaredFields(model);
    for (Field field : fields) {
        if (field.isAnnotationPresent(ModelAttr.class)) {
            ModelAttr attr = field.getAnnotation(ModelAttr.class);
            String fieldAttr = attr.value();
            data.add(fieldAttr);
        }
    }
    result.add(data);
    for (T obj : page.getModels()) {
        data = new ArrayList<>();
        renderDataForExport(data, obj);
        result.add(data);
    }
}

From source file:com.consol.citrus.admin.converter.AbstractObjectConverter.java

/**
 * Adds new endpoint property.//  www  .j  a v a2 s  . c o  m
 * @param fieldName
 * @param displayName
 * @param definition
 * @param defaultValue
 * @param required
 */
protected Property property(String fieldName, String displayName, S definition, String defaultValue,
        boolean required) {
    Field field = ReflectionUtils.findField(definition.getClass(), fieldName);

    if (field != null) {
        Method getter = ReflectionUtils.findMethod(definition.getClass(), getMethodName(fieldName));

        String value = defaultValue;
        if (getter != null) {
            Object getterResult = ReflectionUtils.invokeMethod(getter, definition);
            if (getterResult != null) {
                value = getterResult.toString();
            }
        }

        if (value != null) {
            if (field.isAnnotationPresent(XmlAttribute.class)) {
                return new Property(field.getAnnotation(XmlAttribute.class).name(), fieldName, displayName,
                        resolvePropertyExpression(value), required);
            } else {
                return new Property(fieldName, fieldName, displayName, resolvePropertyExpression(value),
                        required);
            }
        } else {
            return new Property(fieldName, fieldName, displayName, null, required);
        }
    } else {
        log.warn(String.format("Unknown field '%s' on source type '%s'", fieldName, definition.getClass()));
        return null;
    }
}

From source file:org.apdplat.platform.action.ExtJSSimpleAction.java

protected void objectReference(T model) {
    Field[] fields = model.getClass().getDeclaredFields();//?
    for (Field field : fields) {// ??
        if (field.isAnnotationPresent(ManyToOne.class) || field.isAnnotationPresent(OneToOne.class)) {
            LOG.debug(model.getMetaData() + " ManyToOne  OneToOne" + field.getName());
            Model value = (Model) ReflectionUtils.getFieldValue(model, field);
            if (value == null) {
                LOG.debug(model.getMetaData() + " " + field.getName() + "?");
                continue;
            }//from ww w .j  a  va 2 s. c o m
            int id = value.getId();
            LOG.debug("id: " + id);
            value = getService().retrieve(value.getClass(), id);
            ReflectionUtils.setFieldValue(model, field, value);
        }
    }
}

From source file:com.quangphuong.crawler.dbutil.DBWrapper.java

public List<Object> searchFullText(Object entity, String searchVal) {
    String sql = "";
    Connection con = null;//from   w w w.  j a  va 2s .co m
    if (this.isDisconnect) {
        con = DBHandler.openConnection();
    }
    List<Object> result = new ArrayList<Object>();
    try {
        Statement statement;
        if (this.isDisconnect) {
            statement = con.createStatement();
        } else {
            statement = connection.createStatement();
        }
        Field[] attributes = entity.getClass().getDeclaredFields();

        int count = 0;
        String fullTextFields = "";
        String searchClause = "";
        for (Field attribute : attributes) {
            attribute.setAccessible(true);
            if (!attribute.isAnnotationPresent(AutoIncrement.class)
                    && attribute.isAnnotationPresent(FullTextIndex.class)) {
                if (count == 0) {
                    fullTextFields = fullTextFields.concat(attribute.getName());
                } else {
                    fullTextFields = fullTextFields.concat("," + attribute.getName());
                }
                count++;
            }
        }
        sql = fulltextClause.replace("@", fullTextFields);
        if (lowerBound != 0 || upperBound != 0) {
            searchClause = searchRangeByBoolClause.replace("@1",
                    searchVal.replace("*", "").replace(" ", "* ").concat("*"));
            searchClause = searchClause.replace("@L1", String.valueOf(lowerBound)).replace("@L2",
                    String.valueOf(upperBound));
        } else {
            searchClause = searchByBoolClause.replace("@1",
                    searchVal.replace("*", "").replace(" ", "* ").concat("*"));
        }
        searchClause = searchClause.replace("@2", entity.getClass().getSimpleName());
        sql = sql.concat(searchClause);
        System.out.println(sql);
        ResultSet resultSet = statement.executeQuery(sql);
        while (resultSet.next()) {
            List<Object> listFields = new ArrayList();
            List<Class<?>> listFieldTypes = new ArrayList();
            for (Field attribute : attributes) {
                Object obj = resultSet.getObject(attribute.getName());
                listFields.add(obj);
                listFieldTypes.add(attribute.getType());
            }
            Object obj = entity.getClass().getConstructor((Class<?>[]) listFieldTypes.toArray(new Class[0]))
                    .newInstance(listFields.toArray());
            result.add(obj);
        }
    } catch (Exception ex) {
        Logger.getLogger(DBWrapper.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            if (this.isDisconnect && con != null) {
                con.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    return result;
}

From source file:com.quangphuong.crawler.dbutil.DBWrapper.java

public List<Object> suggest(Object entity, String searchVal) {
    String sql = "";
    Connection con = null;/*  w w w . ja va 2  s.  c  om*/
    if (this.isDisconnect) {
        con = DBHandler.openConnection();
    }
    List<Object> result = new ArrayList<Object>();
    try {
        Statement statement;
        if (this.isDisconnect) {
            statement = con.createStatement();
        } else {
            statement = connection.createStatement();
        }
        Field[] attributes = entity.getClass().getDeclaredFields();

        int count = 0;
        String fullTextFields = "";
        String searchClause = "";
        for (Field attribute : attributes) {
            attribute.setAccessible(true);
            if (!attribute.isAnnotationPresent(AutoIncrement.class)
                    && attribute.isAnnotationPresent(FullTextIndex.class)) {
                if (count == 0) {
                    fullTextFields = fullTextFields.concat(attribute.getName());
                } else {
                    fullTextFields = fullTextFields.concat("," + attribute.getName());
                }
                count++;
            }
        }
        sql = fulltextClause.replace("@", fullTextFields);
        if (lowerBound != 0 || upperBound != 0) {
            searchClause = searchRangeByBoolClause.replace("@1",
                    searchVal.replace("*", "").replace(" ", "* ").concat("*"));
            searchClause = searchClause.replace("@L1", String.valueOf(lowerBound)).replace("@L2",
                    String.valueOf(upperBound));
        } else {
            searchClause = suggestClause.replace("@1",
                    searchVal.replace("*", "").replace(" ", "* ").concat("*"));
        }
        searchClause = searchClause.replace("@2", entity.getClass().getSimpleName());
        sql = sql.concat(searchClause);
        System.out.println(sql);
        ResultSet resultSet = statement.executeQuery(sql);
        while (resultSet.next()) {
            List<Object> listFields = new ArrayList();
            List<Class<?>> listFieldTypes = new ArrayList();
            for (Field attribute : attributes) {
                Object obj = resultSet.getObject(attribute.getName());
                listFields.add(obj);
                listFieldTypes.add(attribute.getType());
            }
            Object obj = entity.getClass().getConstructor((Class<?>[]) listFieldTypes.toArray(new Class[0]))
                    .newInstance(listFields.toArray());
            result.add(obj);
        }
    } catch (Exception ex) {
        Logger.getLogger(DBWrapper.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            if (this.isDisconnect && con != null) {
                con.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    return result;
}

From source file:com.impetus.kundera.validation.rules.RelationAttributeRule.java

/**
 * @param relationField/*ww w. j  a  v  a 2s . co m*/
 * @param annotate
 * @return
 * @throws RuleValidationException
 */
private Boolean validateOneToMany(Field relationField, Annotation annotate) throws RuleValidationException {

    OneToMany ann = (OneToMany) annotate;
    Class<?> targetEntity = PropertyAccessorHelper.getGenericClass(relationField);

    // now, check annotations
    if (null != ann.targetEntity() && !ann.targetEntity().getSimpleName().equals("void")) {
        targetEntity = ann.targetEntity();
    }

    boolean isJoinedByTable = relationField.isAnnotationPresent(JoinTable.class);

    if (isJoinedByTable) {
        throw new UnsupportedOperationException("@JoinTable not supported for one to many association");
    }

    boolean isJoinedByColumn = relationField.isAnnotationPresent(JoinTable.class);

    return true;
}

From source file:com.epam.ta.reportportal.ws.validation.JaskonRequiredPropertiesValidator.java

@Override
public void validate(Object object, Errors errors) {
    for (Field field : collectFields(object.getClass())) {
        if (AnnotationUtils.isAnnotationDeclaredLocally(JsonInclude.class, field.getType())) {
            try {
                Object innerObject = Accessible.on(object).field(field).getValue();
                if (null != innerObject) {
                    errors.pushNestedPath(field.getName());
                    validate(innerObject, errors);
                }//from w  w w.j  av a 2  s. co  m
            } catch (Exception e) {
                LOGGER.error("JaskonRequiredPropertiesValidator error: " + e.getMessage(), e);
                // do nothing
            }

        }
        if (field.isAnnotationPresent(JsonProperty.class)
                && field.getAnnotation(JsonProperty.class).required()) {
            String errorCode = new StringBuilder("NotNull.").append(field.getName()).toString();
            ValidationUtils.rejectIfEmpty(errors, field.getName(), errorCode, new Object[] { errorCode });
        }
    }
    if (errors.getNestedPath() != null && errors.getNestedPath().length() != 0) {
        errors.popNestedPath();
    }
}

From source file:com.m4rc310.cb.builders.ComponentBuilder.java

public Field getField(String ref) {
    for (Field field : fields) {
        if (field.isAnnotationPresent(Acomponent.class)) {
            Acomponent ac = field.getDeclaredAnnotation(Acomponent.class);
            if (ac.ref().equalsIgnoreCase(ref)) {
                return field;
            }//  ww w  .j  a v  a2 s  . co m
        }
    }

    throw new UnsupportedOperationException("Field not found!");
}