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:adalid.core.AbstractEntity.java

private void annotateOneToOne(Field field) {
    _annotatedWithOneToOne = field.isAnnotationPresent(OneToOne.class);
    if (_annotatedWithOneToOne) {
        OneToOne annotation = field.getAnnotation(OneToOne.class);
        _navigability = annotation.navigability();
    }/*from   ww  w .  j  a  v a  2 s.  c  om*/
}

From source file:adalid.core.AbstractEntity.java

@SuppressWarnings("deprecation")
private void annotateExtension(Field field) {
    _annotatedWithExtension = field.isAnnotationPresent(Extension.class);
    //      if (_annotatedWithExtension) {
    //          Extension annotation = field.getAnnotation(Extension.class);
    //      }//www. j av a  2s .  c o  m
}

From source file:adalid.core.AbstractEntity.java

private void annotateManyToOne(Field field) {
    _annotatedWithManyToOne = field.isAnnotationPresent(ManyToOne.class);
    if (_annotatedWithManyToOne) {
        ManyToOne annotation = field.getAnnotation(ManyToOne.class);
        _navigability = annotation.navigability();
        _masterDetailView = annotation.view();
    }//from w ww.j  a  v  a2 s  .  c o m
}

From source file:adalid.core.AbstractEntity.java

private void annotateEntityReferenceSearch(Field field) {
    _annotatedWithEntityReferenceSearch = field.isAnnotationPresent(EntityReferenceSearch.class);
    if (_annotatedWithEntityReferenceSearch) {
        EntityReferenceSearch annotation = field.getAnnotation(EntityReferenceSearch.class);
        _searchType = annotation.searchType();
        _listStyle = annotation.listStyle();
        _searchDisplayMode = annotation.displayMode();
    }/*  w w w  .  ja va 2s .c om*/
}

From source file:adalid.core.AbstractEntity.java

private void annotateEntityReferenceDataGen(Field field) {
    _annotatedWithEntityReferenceDataGen = field.isAnnotationPresent(EntityReferenceDataGen.class);
    if (_annotatedWithEntityReferenceDataGen) {
        EntityReferenceDataGen annotation = field.getAnnotation(EntityReferenceDataGen.class);
        _dataGenType = annotation.type();
        _dataGenNullable = Math.min(100, Math.max(0, annotation.nullable()));
    }/*from w  w  w . j a v  a  2  s.  co  m*/
}

From source file:adalid.core.AbstractEntity.java

public void linkForeignSegmentProperty(Entity foreignSegmentProperty) {
    //      if (isParameter() || isParameterProperty()) {
    //          return;
    //      }//from   w  w  w.j  av a  2 s  .c  o  m
    String message = "failed to link foreign segment property of " + getFullName();
    if (foreignSegmentProperty == null) {
        message += "; supplied foreign property is null";
        logger.error(message);
        TLC.getProject().getParser().increaseErrorCount();
    } else {
        Field field = foreignSegmentProperty.getDeclaringField();
        boolean aye = field.isAnnotationPresent(SegmentProperty.class);
        if (aye) {
            _segmentProperty = foreignSegmentProperty;
        } else {
            message += "; " + field.getDeclaringClass().getSimpleName() + "." + field.getName()
                    + " is not a segment property";
            logger.error(message);
            TLC.getProject().getParser().increaseErrorCount();
        }
    }
}

From source file:adalid.core.AbstractEntity.java

public void linkForeignOwnerProperty(Entity foreignOwnerProperty) {
    //      if (isParameter() || isParameterProperty()) {
    //          return;
    //      }/*from   w  ww  .j a  va2s .  c  o m*/
    String message = "failed to link foreign owner property of " + getFullName();
    if (foreignOwnerProperty == null) {
        message += "; supplied foreign property is null";
        logger.error(message);
        TLC.getProject().getParser().increaseErrorCount();
    } else {
        Class<?> foreignOwnerPropertyClass = foreignOwnerProperty.getClass();
        Class<? extends Entity> userEntityClass = TLC.getProject().getUserEntityClass();
        if (userEntityClass != null && userEntityClass.isAssignableFrom(foreignOwnerPropertyClass)) {
            Field field = foreignOwnerProperty.getDeclaringField();
            boolean aye = field.isAnnotationPresent(OwnerProperty.class);
            if (aye) {
                _ownerProperty = foreignOwnerProperty;
            } else {
                message += "; " + field.getDeclaringClass().getSimpleName() + "." + field.getName()
                        + " is not an owner property";
                logger.error(message);
                TLC.getProject().getParser().increaseErrorCount();
            }
        } else {
            message += "; " + userEntityClass + " is not assignable from " + foreignOwnerPropertyClass;
            logger.error(message);
            TLC.getProject().getParser().increaseErrorCount();
        }
    }
}

From source file:adalid.core.AbstractEntity.java

void setKeyField(Field field) {
    Class<?> type = getDataType();
    Class<?> fieldType = field.getType();
    String fieldName = field.getName();
    if (field.isAnnotationPresent(UniqueKey.class)) {
        if (field.equals(getUniqueKeyField(fieldName, type))) {
            if (IntegerProperty.class.isAssignableFrom(fieldType) && _numericKeyField == null) {
                _numericKeyFieldName = fieldName;
                _numericKeyField = field;
            }//from w w  w.j a  v  a  2s.c o m
            if (StringProperty.class.isAssignableFrom(fieldType) && _characterKeyField == null) {
                _characterKeyFieldName = fieldName;
                _characterKeyField = field;
            }
        }
    }
}

From source file:com.github.rvesse.airline.model.MetadataLoader.java

/**
 * Loads injection meta-data//from ww  w. j  a va 2 s  . com
 * 
 * @param type
 *            Class
 * @param injectionMetadata
 *            Injection meta-data
 * @param fields
 *            Fields
 */
public static void loadInjectionMetadata(Class<?> type, InjectionMetadata injectionMetadata,
        List<Field> fields) {
    if (type.isInterface()) {
        return;
    }
    for (Class<?> cls = type; !Object.class.equals(cls); cls = cls.getSuperclass()) {
        for (Field field : cls.getDeclaredFields()) {
            field.setAccessible(true);
            List<Field> path = new ArrayList<>(fields);
            path.add(field);

            Inject injectAnnotation = field.getAnnotation(Inject.class);
            if (injectAnnotation != null) {
                if (field.getType().equals(GlobalMetadata.class)
                        || field.getType().equals(CommandGroupMetadata.class)
                        || field.getType().equals(CommandMetadata.class)) {
                    injectionMetadata.metadataInjections.add(new Accessor(path));
                } else {
                    loadInjectionMetadata(field.getType(), injectionMetadata, path);
                }
            }

            try {
                @SuppressWarnings("unchecked")
                Annotation aGuiceInject = field
                        .getAnnotation((Class<? extends Annotation>) Class.forName("com.google.inject.Inject"));
                if (aGuiceInject != null) {
                    if (field.getType().equals(GlobalMetadata.class)
                            || field.getType().equals(CommandGroupMetadata.class)
                            || field.getType().equals(CommandMetadata.class)) {
                        injectionMetadata.metadataInjections.add(new Accessor(path));
                    } else {
                        loadInjectionMetadata(field.getType(), injectionMetadata, path);
                    }
                }
            } catch (ClassNotFoundException e) {
                // this is ok, means Guice is not on the class path, so
                // probably not being used
                // and thus, ok that this did not work.
            } catch (ClassCastException e) {
                // ignore this too, we're doing some funky cross your
                // fingers type reflect stuff to play
                // nicely with Guice
            }

            Option optionAnnotation = field.getAnnotation(Option.class);
            DefaultOption defaultOptionAnnotation = field.getAnnotation(DefaultOption.class);
            if (optionAnnotation != null) {
                OptionType optionType = optionAnnotation.type();
                String name;
                if (!optionAnnotation.title().isEmpty()) {
                    name = optionAnnotation.title();
                } else {
                    name = field.getName();
                }

                List<String> options = AirlineUtils.arrayToList(optionAnnotation.name());
                String description = optionAnnotation.description();

                int arity = optionAnnotation.arity();
                if (arity < 0 && arity != Integer.MIN_VALUE)
                    throw new IllegalArgumentException(String.format("Invalid arity for option %s", name));

                if (optionAnnotation.arity() >= 0) {
                    arity = optionAnnotation.arity();
                } else {
                    Class<?> fieldType = field.getType();
                    if (Boolean.class.isAssignableFrom(fieldType)
                            || boolean.class.isAssignableFrom(fieldType)) {
                        arity = 0;
                    } else {
                        arity = 1;
                    }
                }

                boolean hidden = optionAnnotation.hidden();
                boolean override = optionAnnotation.override();
                boolean sealed = optionAnnotation.sealed();

                // Find and create restrictions
                Map<Class<? extends Annotation>, Set<Integer>> partials = loadPartials(field);
                List<OptionRestriction> restrictions = new ArrayList<OptionRestriction>();
                for (Class<? extends Annotation> annotationClass : RestrictionRegistry
                        .getOptionRestrictionAnnotationClasses()) {
                    Annotation annotation = field.getAnnotation(annotationClass);
                    if (annotation == null)
                        continue;
                    OptionRestriction restriction = RestrictionRegistry.getOptionRestriction(annotationClass,
                            annotation);
                    if (restriction != null) {
                        // Adjust for partial if necessary
                        if (partials.containsKey(annotationClass))
                            restriction = new PartialRestriction(partials.get(annotationClass), restriction);

                        restrictions.add(restriction);
                    }
                }

                //@formatter:off
                OptionMetadata optionMetadata = new OptionMetadata(optionType, options, name, description,
                        arity, hidden, override, sealed, restrictions, path);
                //@formatter:on
                switch (optionType) {
                case GLOBAL:
                    if (defaultOptionAnnotation != null)
                        throw new IllegalArgumentException(String.format(
                                "Field %s which defines a global option cannot be annotated with @DefaultOption as this may only be applied to command options",
                                field));
                    injectionMetadata.globalOptions.add(optionMetadata);
                    break;
                case GROUP:
                    if (defaultOptionAnnotation != null)
                        throw new IllegalArgumentException(String.format(
                                "Field %s which defines a global option cannot be annotated with @DefaultOption as this may only be applied to command options",
                                field));
                    injectionMetadata.groupOptions.add(optionMetadata);
                    break;
                case COMMAND:
                    // Do we also have a @DefaultOption annotation

                    if (defaultOptionAnnotation != null) {
                        // Can't have both @DefaultOption and @Arguments
                        if (injectionMetadata.arguments.size() > 0)
                            throw new IllegalArgumentException(String.format(
                                    "Field %s cannot be annotated with @DefaultOption because there are fields with @Arguments annotations present",
                                    field));
                        // Can't have more than one @DefaultOption
                        if (injectionMetadata.defaultOption != null)
                            throw new IllegalArgumentException(String.format(
                                    "Command type %s has more than one field with @DefaultOption declared upon it",
                                    type));
                        // Arity of associated @Option must be 1
                        if (optionMetadata.getArity() != 1)
                            throw new IllegalArgumentException(String.format(
                                    "Field %s annotated with @DefaultOption must also have an @Option annotation with an arity of 1",
                                    field));
                        injectionMetadata.defaultOption = optionMetadata;
                    }
                    injectionMetadata.commandOptions.add(optionMetadata);
                    break;
                }
            }

            if (optionAnnotation == null && defaultOptionAnnotation != null) {
                // Can't have @DefaultOption on a field without also @Option
                throw new IllegalArgumentException(String.format(
                        "Field %s annotated with @DefaultOption must also have an @Option annotation", field));
            }

            Arguments argumentsAnnotation = field.getAnnotation(Arguments.class);
            if (field.isAnnotationPresent(Arguments.class)) {
                // Can't have both @DefaultOption and @Arguments
                if (injectionMetadata.defaultOption != null)
                    throw new IllegalArgumentException(String.format(
                            "Field %s cannot be annotated with @Arguments because there is a field with @DefaultOption present",
                            field));

                List<String> titles = new ArrayList<>();

                if (!(argumentsAnnotation.title().length == 1 && argumentsAnnotation.title()[0].equals(""))) {
                    titles.addAll(AirlineUtils.arrayToList(argumentsAnnotation.title()));
                } else {
                    titles.add(field.getName());
                }

                String description = argumentsAnnotation.description();

                Map<Class<? extends Annotation>, Set<Integer>> partials = loadPartials(field);
                List<ArgumentsRestriction> restrictions = new ArrayList<>();
                for (Class<? extends Annotation> annotationClass : RestrictionRegistry
                        .getArgumentsRestrictionAnnotationClasses()) {
                    Annotation annotation = field.getAnnotation(annotationClass);
                    if (annotation == null)
                        continue;
                    ArgumentsRestriction restriction = RestrictionRegistry
                            .getArgumentsRestriction(annotationClass, annotation);
                    if (restriction != null) {
                        // Adjust for partial if necessary
                        if (partials.containsKey(annotationClass))
                            restriction = new PartialRestriction(partials.get(annotationClass), restriction);

                        restrictions.add(restriction);
                    }
                }

                //@formatter:off
                injectionMetadata.arguments.add(new ArgumentsMetadata(titles, description, restrictions, path));
                //@formatter:on
            }
        }
    }
}