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:com.zimbra.cs.localconfig.LocalConfigCLI.java

private void loadExtensionLC(String className) {
    try {/* ww w.j  a  va  2 s .c om*/
        Class<?> lcClass = Class.forName(className);
        Field[] fields = lcClass.getFields();
        for (Field field : fields) {
            try {
                if (field.getType() == KnownKey.class) {
                    KnownKey key = (KnownKey) field.get(null);
                    // Automatically set the key name with the variable name.
                    key.setKey(field.getName());
                    // process annotations
                    if (field.isAnnotationPresent(Supported.class))
                        key.setSupported(true);
                    if (field.isAnnotationPresent(Reloadable.class))
                        key.setReloadable(true);
                }
            } catch (Throwable never) {
                // ignore
            }
        }
    } catch (ClassNotFoundException e) {
        // ignore
    }
}

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

public Object getEntity(Class<?> entity, Object... id) {
    String sql = selectAll.concat(entity.getSimpleName());
    sql = sql.concat(whereClause);//from w w w  . j av  a2  s  .c  om
    Connection con = DBHandler.openConnection();
    Object result = null;
    try {
        Statement statement = con.createStatement();
        Field[] attributes = entity.getDeclaredFields();
        int count = 0;
        for (int i = 0; i < attributes.length; i++) {
            Field attribute = attributes[i];
            if (attribute.isAnnotationPresent(Id.class)) {
                if (i == 0) {
                    sql = sql.concat(attribute.getName() + "=" + "\'" + id[count] + "\'");
                } else {
                    sql = sql.concat(" AND " + attribute.getName() + "=" + "\'" + id[count] + "\'");
                }
                count++;
            }
        }
        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) {
                if (!attribute.isAnnotationPresent(Ignore.class)) {
                    Object obj = resultSet.getObject(attribute.getName());
                    listFields.add(obj);
                    listFieldTypes.add(obj.getClass());
                }
            }
            result = entity.getConstructor((Class<?>[]) listFieldTypes.toArray(new Class[0]))
                    .newInstance(listFields.toArray());
        }
    } catch (Exception ex) {
        Logger.getLogger(DBWrapper.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    return result;
}

From source file:org.jdbcluster.privilege.PrivilegeCheckerImpl.java

/**
 * gets domain id from annotations/*w w w.j av a2 s. c o  m*/
 * 
 * @see org.jdbcluster.domain.DomainBase there is nearly the same method
 * @param f the field instance
 * @return the domain id
 */
private String getDomainIdFromField(Field f) {
    if (!f.isAnnotationPresent(PrivilegesDomain.class)) {
        throw new ConfigurationException(
                "in @PrivilegesCluster referenced domain needs annotation @PrivilegesDomain. Not found on field: "
                        + f.getName());
    }
    DomainDependancy dd = f.getAnnotation(DomainDependancy.class);
    if (dd != null)
        return dd.domainId();
    Domain d = f.getAnnotation(Domain.class);
    if (d == null)
        throw new ConfigurationException("no annotation @Domain or @DomainDependancy found on: " + f.getName());
    return d.domainId();
}

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

public boolean insertEntity(Object entity) {
    String sql = insertClause.concat(entity.getClass().getSimpleName());
    Connection con = null;//from  w  ww .j a v  a 2s  . com
    if (this.isDisconnect) {
        con = DBHandler.openConnection();
    }
    try {
        Statement statement;
        if (this.isDisconnect) {
            statement = con.createStatement();
        } else {
            statement = connection.createStatement();
        }
        String column = "(";
        String values = "(";
        Field[] attributes = entity.getClass().getDeclaredFields();
        int count = 0;
        for (Field attribute : attributes) {
            if (!attribute.isAnnotationPresent(AutoIncrement.class)
                    && !attribute.isAnnotationPresent(Ignore.class)) {
                attribute.setAccessible(true);
                String value = "";
                if (attribute.get(entity) != null) {
                    value = attribute.get(entity).toString();
                }
                if (count == 0) {
                    column = column.concat(attribute.getName());

                    values = values.concat("\'" + value + "\'");
                } else {
                    column = column.concat("," + attribute.getName());
                    values = values.concat("," + "\'" + value + "\'");
                }
                count++;
            }
        }
        column = column.concat(")");
        values = values.concat(")");
        sql = sql.concat(column + valuesClause + values);
        System.out.println(sql);
        int result = statement.executeUpdate(sql);
        if (result > 0) {
            return true;
        }
    } 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 false;
}

From source file:io.klerch.alexa.state.model.AlexaStateModel.java

/**
 * Checks, if the given field is tagged with AlexaStateSave and whose scope is set to a scope which at least
 * is included in the given scope.//  w  w  w .java 2  s  .co  m
 * @param field the field you want to check for the AlexaStateSave-annotation
 * @param scope the scope which at least must be included in the scope of the field
 * @return True, if the field has the AlexaStateSave-annotation and given scope (or an included scope)
 */
private boolean isStateSave(final Field field, final AlexaScope scope) {
    // either field itself is tagged as state-save in given scope or whole class is statesave in the given scope
    // however, StateIgnore in given scope prevents field of being statesave
    return ((!field.isAnnotationPresent(AlexaStateIgnore.class)
            || !scope.isIn(field.getAnnotation(AlexaStateIgnore.class).Scope()))
            && ((field.isAnnotationPresent(AlexaStateSave.class)
                    && scope.includes(field.getAnnotation(AlexaStateSave.class).Scope())
                    || (this.getClass().isAnnotationPresent(AlexaStateSave.class)
                            && scope.includes(this.getClass().getAnnotation(AlexaStateSave.class).Scope())))));
}

From source file:org.mybatisorm.annotation.handler.TableHandler.java

public FieldList getNotNullFieldList(Object object) {
    FieldList fieldList = new FieldList();
    BeanWrapper bean = new BeanWrapperImpl(object);
    for (Field field : getColumnFields()) {
        Object value = bean.getPropertyValue(field.getName());
        if (value != null) {
            Column column = field.getAnnotation(Column.class);
            if (field.isAnnotationPresent(TypeHandler.class)) {
                fieldList.add(field.getName(), ColumnAnnotation.getName(field, column),
                        field.getAnnotation(TypeHandler.class));
            } else {
                fieldList.add(field.getName(), ColumnAnnotation.getName(field, column));
            }//  w  w  w.  j  ava  2 s  .com
        }
    }
    return fieldList;
}

From source file:com.madrobot.di.Converter.java

public static Object convertTo(final JSONObject jsonObject, final String fieldName, final Class<?> clz,
        final Field field) {

    Object value = null;/*from ww w. j  ava2s . c o m*/

    if (clzTypeKeyMap.containsKey(clz)) {
        try {
            final int code = clzTypeKeyMap.get(clz);
            switch (code) {
            case TYPE_STRING:
                value = jsonObject.optString(fieldName);
                break;
            case TYPE_SHORT:
                value = Short.parseShort(jsonObject.optString(fieldName, "0"));
                break;
            case TYPE_INT:
                value = jsonObject.optInt(fieldName);
                break;
            case TYPE_LONG:
                value = jsonObject.optLong(fieldName);
                break;
            case TYPE_CHAR:
                String chatValue = jsonObject.optString(fieldName);
                if (chatValue.length() > 0) {
                    value = chatValue.charAt(0);
                } else {
                    value = '\0';
                }
                break;
            case TYPE_FLOAT:
                value = Float.parseFloat(jsonObject.optString(fieldName, "0.0f"));
                break;
            case TYPE_DOUBLE:
                value = jsonObject.optDouble(fieldName);
                break;
            case TYPE_BOOLEAN:
                value = jsonObject.optString(fieldName);
                if (field.isAnnotationPresent(BooleanFormat.class)) {
                    BooleanFormat formatAnnotation = field.getAnnotation(BooleanFormat.class);
                    String trueFormat = formatAnnotation.trueFormat();
                    String falseFormat = formatAnnotation.falseFormat();
                    if (trueFormat.equals(value)) {
                        value = true;
                    } else if (falseFormat.equals(value)) {
                        value = false;
                    } else {
                        Log.e(JSONDeserializer.TAG,
                                "Expecting " + trueFormat + " / " + falseFormat + " but its " + value);
                    }
                } else {
                    value = Boolean.parseBoolean((String) value);
                }
                break;
            case TYPE_DATE:
                value = DateFormat.getDateInstance().parse(jsonObject.optString(fieldName));
                break;
            }
        } catch (NumberFormatException e) {
            Log.e(JSONDeserializer.TAG, e.getMessage());
        } catch (ParseException e) {
            Log.e(JSONDeserializer.TAG, e.getMessage());
        }
    }
    return value;
}

From source file:de.xaniox.heavyspleef.core.flag.FlagRegistry.java

private Field[] getInjectableDeclaredFieldsByFilter(Class<?> clazz, Predicate<Field> predicate) {
    Set<Field> fieldList = Sets.newHashSet();
    Class<?> currentClass = clazz;

    do {// ww  w.  j  a  va 2 s . com
        Field[] fields = currentClass.getDeclaredFields();

        for (Field field : fields) {
            if (!field.isAnnotationPresent(Inject.class)) {
                continue;
            }

            if (!predicate.apply(field)) {
                continue;
            }

            field.setAccessible(true);
            fieldList.add(field);
        }

        currentClass = currentClass.getSuperclass();
    } while (AbstractFlag.class.isAssignableFrom(currentClass));

    return fieldList.toArray(new Field[fieldList.size()]);
}

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

protected void render(Map map, T obj) {
    //?/* w  w  w . j a v  a 2  s  .c o m*/
    List<Field> fields = ReflectionUtils.getDeclaredFields(model);
    for (Field field : fields) {
        if (field.isAnnotationPresent(RenderIgnore.class)) {
            continue;
        }
        addFieldValue(obj, field, map);
    }
}

From source file:com.evolveum.midpoint.prism.marshaller.PrismBeanInspector.java

private List<String> getPropOrderUncached(Class<?> beanClass) {
    List<String> propOrder;

    // Superclass first!
    Class superclass = beanClass.getSuperclass();
    if (superclass == null || superclass.equals(Object.class)
            || superclass.getAnnotation(XmlType.class) == null) {
        propOrder = new ArrayList<>();
    } else {//from  www . j a  v  a  2 s .c  o  m
        propOrder = new ArrayList<>(getPropOrder(superclass));
    }

    XmlType xmlType = beanClass.getAnnotation(XmlType.class);
    if (xmlType == null) {
        throw new IllegalArgumentException(
                "Cannot marshall " + beanClass + " it does not have @XmlType annotation");
    }

    String[] myPropOrder = xmlType.propOrder();
    for (String myProp : myPropOrder) {
        if (StringUtils.isNotBlank(myProp)) {
            // some properties starts with underscore..we don't want to serialize them with underscore, so remove it..
            if (myProp.startsWith("_")) {
                myProp = myProp.replace("_", "");
            }
            propOrder.add(myProp);
        }
    }

    Field[] fields = beanClass.getDeclaredFields();
    for (Field field : fields) {
        if (field.isAnnotationPresent(XmlAttribute.class)) {
            propOrder.add(field.getName());
        }
    }

    Method[] methods = beanClass.getDeclaredMethods();
    for (Method method : methods) {
        if (method.isAnnotationPresent(XmlAttribute.class)) {
            propOrder.add(getPropertyNameFromGetter(method.getName()));
        }
    }

    return propOrder;
}