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.zols.datastore.validator.TV4.java

private String addIdField(Class type, Map<String, Object> jsonSchemaAsMap) throws JsonProcessingException {
    jsonSchemaAsMap.put("id", type.getName());
    for (Field field : type.getDeclaredFields()) {
        if (field.isAnnotationPresent(Id.class)) {
            jsonSchemaAsMap.put(ID_FIELD, field.getName());
        }// w w  w .  j  a  va2 s. c o m
    }
    return mapper.writeValueAsString(jsonSchemaAsMap);
}

From source file:com.impetus.kundera.metadata.validator.EntityValidatorImpl.java

/**
 * Checks the validity of a class for Cassandra entity.
 * // w ww  . j  a va2s .c o m
 * @param clazz
 *            validates this class
 * 
 * @return returns 'true' if valid
 */
@Override
// TODO: reduce Cyclomatic complexity
public final void validate(final Class<?> clazz) {

    if (classes.contains(clazz)) {
        return;
    }

    log.debug("Validating " + clazz.getName());

    // Is Entity?
    if (!clazz.isAnnotationPresent(Entity.class)) {
        throw new InvalidEntityDefinitionException(clazz.getName() + " is not annotated with @Entity");
    }

    // must have a default no-argument constructor
    try {
        clazz.getConstructor();
    } catch (NoSuchMethodException nsme) {
        throw new InvalidEntityDefinitionException(
                clazz.getName() + " must have a default no-argument constructor.");
    }

    // Must be annotated with @Table
    if (!clazz.isAnnotationPresent(Table.class)) {
        throw new InvalidEntityDefinitionException(clazz.getName() + " must be annotated with @Table");
    }

    // Check for @Key and ensure that there is just 1 @Key field of String
    // type.
    List<Field> keys = new ArrayList<Field>();
    for (Field field : clazz.getDeclaredFields()) {
        if (field.isAnnotationPresent(Id.class)) {
            keys.add(field);
        }
    }

    if (keys.size() == 0) {
        throw new InvalidEntityDefinitionException(clazz.getName() + " must have an @Id field.");
    } else if (keys.size() > 1) {
        throw new InvalidEntityDefinitionException(clazz.getName() + " can only have 1 @Id field.");
    }

    // if (!keys.get(0).getType().equals(String.class))
    // {
    // throw new PersistenceException(clazz.getName() +
    // " @Id must be of String type.");
    // }

    // save in cache

    classes.add(clazz);
}

From source file:fr.itinerennes.bundler.cli.GtfsItinerennesBundler.java

private void loadArguments(final Properties p) {
    for (final Field f : getClass().getDeclaredFields()) {
        if (f.isAnnotationPresent(Option.class) || f.isAnnotationPresent(Argument.class)) {
            try {
                final String name = String.format("program.args.%s", f.getName());
                final String value = String.valueOf(f.get(this));
                p.setProperty(name, value);
                LOGGER.info("Adding property {}={} to application context", name, value);
            } catch (final Exception e) {
                throw new RuntimeException("unable to retrieve field values", e);
            }/*from w  w  w.j  av  a2s  .  c  om*/
        }
    }
}

From source file:com.CodeSeance.JSeance.CodeGenXML.XMLElements.Node.java

public void loadAttributes(Context context) {
    ContextManager contextManager = context.getManager();
    for (Field field : this.getClass().getDeclaredFields()) {
        if (field.isAnnotationPresent(XMLAttribute.class)) {
            Class type = field.getType();

            String attributeName = field.getAnnotation(XMLAttribute.class).attributeName();
            if ("".equals(attributeName) || attributeName == null) {
                attributeName = field.getName();
            }/* w  w  w  . j av a  2  s  . co  m*/

            String stringValue = element.getAttribute(attributeName);
            try {
                field.set(this, replaceJSAndConvert(contextManager, stringValue, type));
            } catch (IllegalAccessException ex) {
                // This is due to a programming error, member field should be public
                assert false : ex;
            }
        } else if (field.isAnnotationPresent(XMLTextContent.class)) {
            Class type = field.getType();
            try {
                field.set(this, replaceJSAndConvert(contextManager, element.getTextContent(), type));
            } catch (IllegalAccessException ex) {
                // This is due to a programming error, member field should be public
                assert false : ex;
            }
        }
    }
}

From source file:com.linkedin.pinot.tools.admin.command.AbstractBaseCommand.java

public void printUsage() {
    System.out.println("Usage: " + this.getName());

    for (Field f : this.getClass().getDeclaredFields()) {
        if (f.isAnnotationPresent(Option.class)) {
            Option option = f.getAnnotation(Option.class);

            System.out.println(String.format("\t%-25s %-30s: %s (required=%s)", option.name(), option.metaVar(),
                    option.usage(), option.required()));
        }// w  w  w .  j a va 2 s .co  m
    }
}

From source file:com.impetus.kundera.metadata.processor.SuperColumnFamilyProcessor.java

@Override
public final void process(Class<?> clazz, EntityMetadata metadata) {

    if (!clazz.isAnnotationPresent(SuperColumnFamily.class)) {
        return;//from   w  ww. ja  v a2  s  .c  o m
    }

    LOG.debug("Processing @Entity " + clazz.getName() + " for SuperColumnFamily.");

    metadata.setType(EntityMetadata.Type.SUPER_COLUMN_FAMILY);

    // check for SuperColumnFamily annotation.
    SuperColumnFamily scf = clazz.getAnnotation(SuperColumnFamily.class);

    // set columnFamily
    metadata.setColumnFamilyName(scf.family());

    // set keyspace
    String keyspace = scf.keyspace().length() != 0 ? scf.keyspace() : em.getKeyspace();
    metadata.setKeyspaceName(keyspace);

    // scan for fields
    for (Field f : clazz.getDeclaredFields()) {

        // if @Id
        if (f.isAnnotationPresent(Id.class)) {
            LOG.debug(f.getName() + " => Id");
            metadata.setIdProperty(f);
            populateIdAccessorMethods(metadata, clazz, f);
        }
        // if @SuperColumn
        else if (f.isAnnotationPresent(SuperColumn.class)) {
            SuperColumn sc = f.getAnnotation(SuperColumn.class);
            String superColumnName = sc.column();

            String columnName = getValidJPAColumnName(clazz, f);
            if (null == columnName) {
                continue;
            }
            LOG.debug(f.getName() + " => Column:" + columnName + ", SuperColumn:" + superColumnName);

            EntityMetadata.SuperColumn superColumn = metadata.getSuperColumn(superColumnName);
            if (null == superColumn) {
                superColumn = metadata.new SuperColumn(superColumnName);
            }
            superColumn.addColumn(columnName, f);
            metadata.addSuperColumn(superColumnName, superColumn);

        }
    }
}

From source file:net.eledge.android.toolkit.db.internal.TableBuilder.java

private void collectFieldUpdatesAnnotations(SparseArray<List<String>> versionUpdates, Class<?> clazz) {
    for (Field field : clazz.getFields()) {
        if (field.isAnnotationPresent(ModelUpdate.class)) {
            addUpdateIfNeeded(versionUpdates, field.getAnnotation(ModelUpdate.class));
        } else if (field.isAnnotationPresent(ModelUpdates.class)) {
            ModelUpdates updates = field.getAnnotation(ModelUpdates.class);
            for (ModelUpdate update : updates.value()) {
                addUpdateIfNeeded(versionUpdates, update);
            }// w w w  . j  av a2  s  .c om
        }
    }
}

From source file:com.tlabs.android.evanova.mvp.PresenterLifeCycle.java

private void addAnnotatedPresenter(Object source) {
    for (Field field : source.getClass().getDeclaredFields()) {
        if (field.isAnnotationPresent(Presenter.class)) {
            if (Modifier.isPrivate(field.getModifiers())) {
                throw new NotAccessibleException("presenter on " + field.getName() + " canot be private");
            } else {
                try {
                    field.setAccessible(true);
                    ViewPresenter presenter = (ViewPresenter) field.get(source);
                    registerPresenter(presenter);
                    field.setAccessible(false);
                } catch (IllegalAccessException e) {
                    NotAccessibleException exception = new NotAccessibleException(field.getName(), e);
                    throw exception;
                }/* w  w  w .  j  a  v a  2  s .  c o  m*/
            }
        }
    }
}

From source file:org.vaadin.spring.i18n.Translator.java

private void analyzeFields(Class<?> clazz) {
    for (Field f : clazz.getDeclaredFields()) {
        if (f.isAnnotationPresent(TranslatedProperty.class)) {
            translatedFields.put(f.getAnnotation(TranslatedProperty.class), f);
        } else if (f.isAnnotationPresent(TranslatedProperties.class)) {
            for (TranslatedProperty annotation : f.getAnnotation(TranslatedProperties.class).value()) {
                translatedFields.put(annotation, f);
            }//www.j  a  v  a2  s. co m
        }
    }
}

From source file:net.eledge.android.toolkit.db.internal.TableBuilder.java

public String create(Class<?> clazz) {
    StringBuilder sb = new StringBuilder("CREATE TABLE ");
    sb.append(SQLBuilder.getTableName(clazz));
    sb.append(" (");
    boolean first = true;
    for (Field field : clazz.getFields()) {
        if (field.isAnnotationPresent(Column.class)) {
            if (!first) {
                sb.append(", ");
            }//from   w  w  w .  j  ava 2 s  .  c  o  m
            sb.append(createFieldDef(clazz, field));
            first = false;
        }
    }
    sb.append(")");
    return sb.toString();
}