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:io.mandrel.OtherTest.java

public void inspect(int level, Type clazz, String name) {

    if (level > 6)
        return;//w w w. j a v a  2s . c  o m

    if (clazz instanceof Class && !((Class<?>) clazz).isEnum() && ((Class<?>) clazz).getPackage() != null
            && ((Class<?>) clazz).getPackage().getName().startsWith("io.mandrel")) {
        int newLevel = level + 1;
        Field[] fields = ((Class<?>) clazz).getDeclaredFields();

        for (Field field : fields) {
            Class<?> fieldType = field.getType();
            String text;
            if (!field.isAnnotationPresent(JsonIgnore.class) && !Modifier.isStatic(field.getModifiers())) {
                if (List.class.equals(fieldType) || Map.class.equals(fieldType)) {
                    Type type = field.getGenericType();
                    if (type instanceof ParameterizedType) {
                        ParameterizedType pType = (ParameterizedType) type;
                        Type paramType = pType.getActualTypeArguments()[pType.getActualTypeArguments().length
                                - 1];

                        text = field.getName() + "(container of " + paramType + ")";
                        System.err.println(StringUtils.leftPad(text, text.length() + newLevel * 5, "\t-   "));
                        inspect(newLevel, paramType, "");
                    }
                } else {
                    text = field.getName()
                            + (field.getType().isPrimitive() || field.getType().equals(String.class)
                                    || field.getType().equals(LocalDateTime.class)
                                            ? " (" + field.getType().getName() + ")"
                                            : "");
                    System.err.println(StringUtils.leftPad(text, text.length() + newLevel * 5, "\t-   "));
                    inspect(newLevel, fieldType, field.getName());
                }

                // JsonSubTypes subtype =
                // fieldType.getAnnotation(JsonSubTypes.class);
                // if (subtype != null) {
                // int subLevel = level + 2;
                // text = "subtypes:";
                // System.err.println(StringUtils.leftPad(text,
                // text.length() + subLevel * 5, "\t-   "));
                // for (JsonSubTypes.Type type : subtype.value()) {
                // text = "subtype:" + type.name();
                // System.err.println(StringUtils.leftPad(text,
                // text.length() + (subLevel + 1) * 5, "\t-   "));
                // inspect((subLevel + 1), type.value(), type.name());
                // }
                // }
            }
        }

        JsonSubTypes subtype = ((Class<?>) clazz).getAnnotation(JsonSubTypes.class);
        if (subtype != null) {
            int subLevel = level + 1;
            String text = "subtypes:";
            System.err.println(StringUtils.leftPad(text, text.length() + subLevel * 5, "\t-   "));
            for (JsonSubTypes.Type type : subtype.value()) {
                text = "subtype:" + type.name();
                System.err.println(StringUtils.leftPad(text, text.length() + (subLevel + 1) * 5, "\t-   "));
                inspect((subLevel + 1), type.value(), type.name());
            }
        }
    }
}

From source file:com.urbanmania.spring.beans.factory.config.annotations.PropertyAnnotationAndPlaceholderConfigurer.java

private void processAnnotatedProperties(Properties properties, String name, MutablePropertyValues mpv,
        Class<?> clazz) {/*from w w  w.j  a  v  a2 s . com*/
    // TODO support proxies
    if (clazz != null && clazz.getPackage() != null) {
        if (basePackage != null && !clazz.getPackage().getName().startsWith(basePackage)) {
            return;
        }

        log.info("Configuring properties for bean=" + name + "[" + clazz + "]");

        for (PropertyDescriptor property : BeanUtils.getPropertyDescriptors(clazz)) {
            if (log.isLoggable(Level.FINE))
                log.fine("examining property=[" + clazz.getName() + "." + property.getName() + "]");
            Method setter = property.getWriteMethod();
            Method getter = property.getReadMethod();
            Property annotation = null;
            if (setter != null && setter.isAnnotationPresent(Property.class)) {
                annotation = setter.getAnnotation(Property.class);
            } else if (setter != null && getter != null && getter.isAnnotationPresent(Property.class)) {
                annotation = getter.getAnnotation(Property.class);
            } else if (setter == null && getter != null && getter.isAnnotationPresent(Property.class)) {
                throwBeanConfigurationException(clazz, property.getName());
            }
            if (annotation != null) {
                setProperty(properties, name, mpv, clazz, property, annotation);
            }
        }

        for (Field field : clazz.getDeclaredFields()) {
            if (log.isLoggable(Level.FINE))
                log.fine("examining field=[" + clazz.getName() + "." + field.getName() + "]");
            if (field.isAnnotationPresent(Property.class)) {
                Property annotation = field.getAnnotation(Property.class);
                PropertyDescriptor property = BeanUtils.getPropertyDescriptor(clazz, field.getName());

                if (property == null || property.getWriteMethod() == null) {
                    throwBeanConfigurationException(clazz, field.getName());
                }

                setProperty(properties, name, mpv, clazz, property, annotation);
            }
        }
    }
}

From source file:org.zht.framework.service.impl.BaseServiceImpl.java

private String getParentPropertyName(M m) {
    Field[] fields = m.getClass().getDeclaredFields();
    if (fields == null || fields.length == 0) {
        return null;
    }//  w w w  . j  av a  2  s .  c om
    for (Field f : fields) {
        if (f.isAnnotationPresent(TreeParentFied.class)) {
            return f.getName();
        }
    }
    return null;
}

From source file:org.zht.framework.service.impl.BaseServiceImpl.java

private void generateCurrentTimeStamp(M m) {
    Field[] fields = m.getClass().getDeclaredFields();
    if (fields == null || fields.length == 0) {
        return;/*from   ww  w.jav  a  2 s. c o  m*/
    }
    for (Field f : fields) {
        if (f.isAnnotationPresent(CurrentTimeStamp.class)) {
            MethodAccess access = MethodAccess.get(m.getClass());
            access.invoke(m, "set" + ZStrUtil.toUpCaseFirst(f.getName()), new Date(System.currentTimeMillis()));
        }
    }
}

From source file:at.alladin.rmbt.shared.hstoreparser.HstoreParser.java

/**
 * /* www  . ja v  a  2  s. c  o  m*/
 * @param clazz
 * @throws HstoreParseException 
 */
private void initFields(Class<?> clazz) throws HstoreParseException {
    if (clazz.getSuperclass() != null) {
        initFields(clazz.getSuperclass());
    }

    for (Field f : clazz.getDeclaredFields()) {
        if (f.isAnnotationPresent(HstoreKey.class)) {
            //annotation: HstoreKey was found
            String hstoreKey = ((HstoreKey) f.getAnnotation(HstoreKey.class)).value();
            //check for duplicates:
            if (!fieldsWithKeys.containsKey(hstoreKey)) {
                fieldsWithKeys.put(hstoreKey, f);
            } else {
                throw new HstoreParseException(
                        HstoreParseException.HSTORE_OBJECT_KEY_ALREADY_IN_USE + hstoreKey);
            }
        }

        if (f.isAnnotationPresent(HstoreCollection.class)) {
            //annotation: HstoreCollection was found
            Class<?> hstoreClazz = ((HstoreCollection) f.getAnnotation(HstoreCollection.class)).value();
            //insert class into hstore
            if (!hstore.getParserMap().containsKey(hstoreClazz)) {
                hstore.addClass(hstoreClazz);
            }
        }
    }
}

From source file:com.madrobot.di.wizard.json.JSONDeserializer.java

private Field getField(final Class<?> userClass, final String jsonKey) throws NoSuchFieldException {

    Field targetField = null;/*  w  ww .j av a2  s  .com*/
    for (Field field : userClass.getDeclaredFields()) {
        if (field.getName().equals(jsonKey)) {
            targetField = field;
            break;
        } else if (field.isAnnotationPresent(SerializedName.class)) {
            SerializedName serializedNameObj = field.getAnnotation(SerializedName.class);
            if (serializedNameObj.value().equals(jsonKey)) {
                targetField = field;
                break;
            }
        }
    }

    if (targetField == null)
        throw new NoSuchFieldException("NoSuchFieldException : " + jsonKey);

    return targetField;
}

From source file:org.apache.nifi.web.security.spring.LoginIdentityProviderFactoryBean.java

private void performFieldInjection(final LoginIdentityProvider instance, final Class loginIdentityProviderClass)
        throws IllegalArgumentException, IllegalAccessException {
    for (final Field field : loginIdentityProviderClass.getDeclaredFields()) {
        if (field.isAnnotationPresent(LoginIdentityProviderContext.class)) {
            // make the method accessible
            final boolean isAccessible = field.isAccessible();
            field.setAccessible(true);/*from ww  w  .j ava  2 s .  co m*/

            try {
                // get the type
                final Class<?> fieldType = field.getType();

                // only consider this field if it isn't set yet
                if (field.get(instance) == null) {
                    // look for well known types
                    if (NiFiProperties.class.isAssignableFrom(fieldType)) {
                        // nifi properties injection
                        field.set(instance, properties);
                    }
                }

            } finally {
                field.setAccessible(isAccessible);
            }
        }
    }

    final Class parentClass = loginIdentityProviderClass.getSuperclass();
    if (parentClass != null && LoginIdentityProvider.class.isAssignableFrom(parentClass)) {
        performFieldInjection(instance, parentClass);
    }
}

From source file:com.adf.bean.AbsBean.java

public List<String> dbColumns() {
    Field[] fields = getClass().getDeclaredFields();

    List<String> cols = new ArrayList<String>();
    for (Field field : fields) {
        if (field.isAnnotationPresent(AdfDbColumn.class)) {
            cols.add(field.getName());// w w  w.  j  a  v a2s.c o m
        }
    }
    return cols;
}

From source file:com.adf.bean.AbsBean.java

public List<String> jsonColumns() {
    Field[] fields = getClass().getDeclaredFields();

    List<String> cols = new ArrayList<String>();
    for (Field field : fields) {
        if (field.isAnnotationPresent(AdfJsonColumn.class)) {
            cols.add(field.getName());//  ww  w . java  2s.c  o  m
        }
    }
    return cols;
}