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:net.eledge.android.toolkit.db.internal.TableBuilder.java

public List<String> createTableUpdates(Class<?> clazz) {
    List<String> updates = new ArrayList<>();
    final List<String> names = getExistingFields(clazz);
    for (Field field : clazz.getFields()) {
        if (field.isAnnotationPresent(Column.class)) {
            String name = SQLBuilder.getFieldName(field);
            if (!names.contains(name)) {
                StringBuilder sb = new StringBuilder("ALTER TABLE ");
                sb.append(SQLBuilder.getTableName(clazz));
                sb.append(" ADD COLUMN ");
                sb.append(createFieldDef(clazz, field));
                sb.append(";");
                updates.add(sb.toString());
            }//  ww  w  . j av a 2s  . c  om
        }
    }
    return updates;
}

From source file:ofc4j.OFC.java

private void doAlias(Class<?> c) {
    if (c.isAnnotationPresent(Alias.class)) {
        converter.alias(c.getAnnotation(Alias.class).value(), c);
    }/*from w w w .  j a  va 2  s  . c om*/
    for (Field f : c.getDeclaredFields()) {
        if (f.isAnnotationPresent(Alias.class)) {
            converter.aliasField(f.getAnnotation(Alias.class).value(), c, f.getName());
        }
    }
}

From source file:com.blstream.hooks.springframework.mongodb.event.CascadingMongoEventListener.java

public void onBeforeConvert(final Object source) {
    ReflectionUtils.doWithFields(source.getClass(), new ReflectionUtils.FieldCallback() {

        @Override//  w  w  w.j a v a 2s  .  c om
        public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
            ReflectionUtils.makeAccessible(field);

            if (field.isAnnotationPresent(DBRef.class) && field.isAnnotationPresent(DBRefCascade.class)) {
                final Object fieldValue = field.get(source);

                DbRefFieldCallback callback = new DbRefFieldCallback();

                ReflectionUtils.doWithFields(fieldValue.getClass(), callback);

                if (!callback.isIdFound()) {
                    throw new MappingException("Cannot perform cascade save on child object without id set");
                }

                mongoOperations.save(fieldValue);
            }
        }
    });
}

From source file:com.crosstreelabs.cognitio.common.CustomCommander.java

@Override
public void usage(StringBuilder out, String indent) {
    Map<String, ParameterDescription> parameters = new HashMap<>();
    for (ParameterDescription pd : getParameters()) {
        parameters.put(pd.getParameterized().getName(), pd);
    }/*  w ww  .j a v a 2s  . c om*/

    //
    // First line of the usage
    //
    String programName = "<main class>";
    out.append(indent).append("Usage: ").append(programName).append(" [options]");
    if (!getCommands().isEmpty()) {
        out.append(indent).append(" [command] [command options]");
    }
    out.append("\n");

    //
    // Display all the names and descriptions
    //
    for (Object obj : getObjects()) {
        int i = 0;
        for (Field field : obj.getClass().getDeclaredFields()) {
            if (!field.isAnnotationPresent(Parameter.class)) {
                continue;
            }

            if (i == 0) {
                String name = obj.getClass().getSimpleName();
                if (name.toLowerCase().endsWith("configuration")) {
                    name = name.substring(0, name.length() - 13);
                }
                out.append(indent).append("\n  " + name + " Options:\n");
            }

            ParameterDescription pd = parameters.get(field.getName());
            WrappedParameter parameter = pd.getParameter();

            StringBuilder description = new StringBuilder(pd.getNames()).append(" ")
                    .append(parameter.required() ? "(REQUIRED)" : "").append("\n");
            wrapDescription(description, pd.getDescription());

            Object def = pd.getDefault();
            if (pd.isDynamicParameter()) {
                description.append("\n            ").append(
                        "Syntax: " + parameter.names()[0] + "key" + parameter.getAssignment() + "value");
            }
            if (def != null) {
                String displayedDef = Strings.isStringEmpty(def.toString()) ? "<empty string>" : def.toString();
                description.append("\n            ")
                        .append("Default: " + (parameter.password() ? "********" : displayedDef));
            }
            description.append("\n");
            out.append(description);
            i++;
        }
    }
}

From source file:net.eledge.android.toolkit.db.abstracts.Dao.java

private ContentValues mapToContentValues(E entity) {
    ContentValues values = new ContentValues();
    try {//from  w  ww  .  jav  a2s .  c  o m
        for (Field field : clazz.getFields()) {
            if (field.isAnnotationPresent(Column.class)) {
                final FieldType fieldType = FieldType.getType(field.getType());
                final String key = SQLBuilder.getFieldName(field);
                final String value = fieldType.toString(entity, field);
                values.put(key, value);
            }
        }
    } catch (IllegalArgumentException | IllegalAccessException e) {
        Log.e(this.getClass().getName(), e.getMessage(), e);
    }
    return values;
}

From source file:com.avanza.ymer.YmerConverterTestBase.java

public void testFailsIfSpaceClassIsMissingSpringIdAnnotation() throws Exception {
    Object spaceObject = testCase.spaceObject;
    Field[] fields = spaceObject.getClass().getDeclaredFields();
    boolean hasAnnotation = false;
    for (Field field : fields) {
        if (field.isAnnotationPresent(Id.class)) {
            hasAnnotation = true;/*w w  w .j  ava2 s .  c  o m*/
        }
    }
    assertTrue("@Id annotation is missing on class " + spaceObject.getClass().getCanonicalName(),
            hasAnnotation);
}

From source file:com.github.wiselenium.testng.WiseTest.java

private void injectPageFieldIntoTest(Field field, WiseTest testInstance) {
    if (!field.isAnnotationPresent(Page.class))
        return;/*  ww  w.ja  v a2  s . co  m*/

    field.setAccessible(true);
    Object page = testInstance.initElements(field.getType());
    try {
        field.set(testInstance, page);
    } catch (Exception e) {
        throw new PageInjectionException(field.getType(), this.getClass(), e);
    }
}

From source file:nl.knaw.dans.common.lang.search.bean.GenericSearchBeanConverter.java

@SuppressWarnings("unchecked")
public IndexDocument toIndexDocument(Object searchBean)
        throws SearchBeanConverterException, SearchBeanException {
    Class sbClass = searchBean.getClass();
    if (!sbClass.isAnnotationPresent(SearchBean.class))
        throw new ObjectIsNotASearchBeanException(sbClass.toString());

    SimpleIndexDocument indexDocument = new SimpleIndexDocument(SearchBeanUtil.getDefaultIndex(sbClass));

    for (java.lang.reflect.Field classField : ClassUtil.getAllFields(sbClass).values()) {
        if (classField.isAnnotationPresent(SearchField.class)) {
            SearchField sbField = classField.getAnnotation(SearchField.class);

            String fieldName = sbField.name();
            boolean isRequired = sbField.required();
            Class<? extends SearchFieldConverter<?>> converter = sbField.converter();
            String propName = classField.getName();
            String getMethodName = "get" + StringUtils.capitalize(propName);
            addFieldToDocument(searchBean, indexDocument, getMethodName, fieldName, isRequired, converter);

            if (classField.isAnnotationPresent(CopyField.class)) {
                for (Annotation annot : classField.getAnnotations()) {
                    if (annot instanceof CopyField) {
                        CopyField sbCopyField = (CopyField) annot;
                        fieldName = sbCopyField.name();
                        isRequired = sbCopyField.required();
                        getMethodName = "get" + StringUtils.capitalize(propName)
                                + StringUtils.capitalize(sbCopyField.getterPostfix());
                        converter = sbCopyField.converter();
                        addFieldToDocument(searchBean, indexDocument, getMethodName, fieldName, isRequired,
                                converter);

                    }/*from  w ww  . j  av a2s . c o m*/
                }
            }
        }
    }

    if (indexDocument.getIndex() != null) {
        if (indexDocument.getFields().getByFieldName(indexDocument.getIndex().getPrimaryKey()) == null)
            throw new PrimaryKeyMissingException("Primary key not set to search bean object.");
    }

    return indexDocument;
}

From source file:com.parse.simple.SimpleParseCache.java

public synchronized Map<SimpleField, SimpleParseColumn> getColumnFields(Class<?> klass) {
    Map<SimpleField, SimpleParseColumn> columnFieldsCache = SimpleParseCache.get().columnFieldsCache.get(klass);
    if (columnFieldsCache != null)
        return columnFieldsCache;

    Map<SimpleField, SimpleParseColumn> declaredColumnFields = new LinkedHashMap<SimpleField, SimpleParseColumn>();

    Field[] fields = klass.getDeclaredFields();
    Arrays.sort(fields, new Comparator<Field>() {
        @Override/*from   w  ww. jav a2 s. com*/
        public int compare(Field field1, Field field2) {
            return field2.getName().compareTo(field1.getName());
        }
    });
    for (Field field : fields) {
        if (field.isAnnotationPresent(ParseColumn.class)) {
            declaredColumnFields.put(new SimpleField(field),
                    new SimpleParseColumn(field.getAnnotation(ParseColumn.class)));
        }
    }

    Class<?> superClass = klass.getSuperclass();
    if (superClass != null) {
        declaredColumnFields.putAll(getColumnFields(superClass));
    }

    SimpleParseCache.get().columnFieldsCache.put(klass, declaredColumnFields);
    return declaredColumnFields;
}

From source file:com.wavemaker.tools.apidocs.tools.parser.impl.ReflectionModelParser.java

protected String findFieldName(Field field) {
    String name = field.getName();
    if (field.isAnnotationPresent(JsonProperty.class)) {
        final String value = field.getAnnotation(JsonProperty.class).value();
        if (StringUtils.isNotBlank(value)) {
            name = value;//from  w  w  w.  j  av a2  s . co m
        }
    }
    return name;
}