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:me.xiaopan.android.gohttp.requestobject.RequestParser.java

/**
 * ??/*from   w w w  .  ja va  2 s  .  c  om*/
 * @param context 
 * @param requestClass class??class??????
 * @param requestParams ??null????????
 * @return ?
 */
@SuppressWarnings("unchecked")
public static RequestParams parseRequestParams(Context context, Class<? extends Request> requestClass,
        RequestParams requestParams) {
    if (requestParams == null) {
        requestParams = new RequestParams();
    }

    String requestParamName;
    String requestParamValue;
    Object requestParamValueObject = null;
    for (Field field : getFields(requestClass, true, true, true)) {
        // ???????
        if (!field.isAnnotationPresent(Param.class) || !Modifier.isStatic(field.getModifiers())) {
            continue;
        }

        // ????
        requestParamName = parseParamAnnotation(context, field);
        if (requestParamName == null) {
            requestParamName = field.getName();
        }

        // ?ValueValue?
        if (field.isAnnotationPresent(Value.class)) {
            String value = parseValueAnnotation(context, field);
            if (value != null) {
                requestParams.put(requestParamName, value);
                continue;
            }
        }

        try {
            field.setAccessible(true);
            requestParamValueObject = field.get(null);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // ?null?
        if (requestParamValueObject == null) {
            continue;
        }

        // ?Map???
        if (Map.class.isAssignableFrom(field.getType())) {
            for (java.util.Map.Entry<Object, Object> entry : ((Map<Object, Object>) requestParamValueObject)
                    .entrySet()) {
                if (entry.getKey() != null && entry.getValue() != null) {
                    String key = entry.getKey().toString();
                    String value = entry.getValue().toString();
                    if (key != null && !"".equals(key) && value != null && !"".equals(value)) {
                        requestParams.put(key, value);
                    }
                }
            }
            continue;
        }

        // ?File?
        if (File.class.isAssignableFrom(field.getType())) {
            try {
                requestParams.put(requestParamName, (File) requestParamValueObject);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            continue;
        }

        // ?ArrayListArrayList?
        if (ArrayList.class.isAssignableFrom(field.getType())) {
            requestParams.put(requestParamName, (ArrayList<String>) requestParamValueObject);
            continue;
        }

        // ?boolean
        if (Boolean.class.isAssignableFrom(field.getType())) {
            if ((Boolean) requestParamValueObject) {
                requestParamValue = parseTrueAnnotation(context, field);
                if (requestParamValue == null) {
                    requestParamValue = DEFAULT_VALUE_TRUE;
                }
            } else {
                requestParamValue = parseFalseAnnotation(context, field);
                if (requestParamValue == null) {
                    requestParamValue = DEFAULT_VALUE_FALSE;
                }
            }
            requestParams.put(requestParamName, requestParamValue);
            continue;
        }

        // ?
        if (Enum.class.isAssignableFrom(field.getType())) {
            Enum<?> enumObject = (Enum<?>) requestParamValueObject;
            requestParamValue = parseValueAnnotationFromEnum(context, enumObject);
            if (requestParamValue == null) {
                requestParamValue = enumObject.name();
            }
            requestParams.put(requestParamName, requestParamValue);
            continue;
        }

        // ???
        requestParamValue = requestParamValueObject.toString();
        if (requestParamValue != null && !"".equals(requestParamValue)) {
            requestParams.put(requestParamName, requestParamValue);
        }
    }

    return requestParams;
}

From source file:com.cassius.spring.assembly.test.common.process.InjectIntoProcessor.java

/**
 * Need process before./*from  w ww.ja  v  a 2  s  . co  m*/
 *
 * @param instance the instance
 * @param field the field
 * @return the boolean
 */
@Override
protected boolean needProcessBefore(Object instance, Field field) {
    return field.isAnnotationPresent(InjectInto.class);
}

From source file:com.cassius.spring.assembly.test.common.process.InjectIntoProcessor.java

/**
 * Need process after./* www  .ja v  a2  s  .  co m*/
 *
 * @param instance the instance
 * @param field the field
 * @return the boolean
 */
@Override
protected boolean needProcessAfter(Object instance, Field field) {
    return field.isAnnotationPresent(InjectInto.class);
}

From source file:com.jfinal.plugin.spring.IocInterceptor.java

private void injectField(Controller controller, Field field) {
    Object bean = null;/*  w w  w  .  ja v a2 s  .  c o  m*/
    if (field.isAnnotationPresent(Inject.BY_NAME.class))
        bean = ctx.getBean(field.getName());
    else if (field.isAnnotationPresent(Inject.IGNORE.class))
        return;
    else
        bean = ctx.getBean(field.getType());

    try {
        field.setAccessible(true);
        field.set(controller, bean);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:io.coala.guice.config.ConfigTypeListener.java

@Override
public <T> void hear(final TypeLiteral<T> type, final TypeEncounter<T> encounter) {
    for (Field field : type.getRawType().getDeclaredFields()) {
        if (field.isAnnotationPresent(InjectConfig.class)) {
            encounter.register(new ConfigMembersInjector<T>(field, configuration));
        }/*from  ww w  . j  a v  a2 s .  c  o  m*/
    }
}

From source file:com.epam.ta.reportportal.database.ActivityDocumentHandler.java

private String getDbRepresentation(Field field) {
    return field.isAnnotationPresent(org.springframework.data.mongodb.core.mapping.Field.class)
            ? field.getAnnotation(org.springframework.data.mongodb.core.mapping.Field.class).value()
            : field.getName();/*from   w  w  w  .j  a v  a 2 s. com*/
}

From source file:pl.bristleback.server.bristle.conf.resolver.message.ObjectSenderInjector.java

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) {
    Field[] fields = bean.getClass().getDeclaredFields();
    for (Field field : fields) {
        if (field.isAnnotationPresent(ObjectSender.class)) {
            ConditionObjectSender objectSender = resolveSender(field);
            injectProperty(bean, field, objectSender);
        }/*from w  ww.  j a va2s  .  co m*/
    }
    return bean;
}

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

protected final void populateIdColumn(EntityMetadata metadata, Class<?> clazz, Field f) {
    if (f.isAnnotationPresent(Column.class)) {
        Column c = f.getAnnotation(Column.class);
        if (!c.name().isEmpty()) {
            metadata.setIdColumn(metadata.new Column(c.name(), f));
        }//from  w  w  w  .j  a v a  2s. c o  m
    }
}

From source file:cn.easy.plugin.spring.IocInterceptor.java

public void intercept(ActionInvocation ai) {
    Controller controller = ai.getController();
    Field[] fields = controller.getClass().getDeclaredFields();
    for (Field field : fields) {
        Object bean = null;/*www.j  a v a2s. c o m*/
        if (field.isAnnotationPresent(Inject.BY_NAME.class))
            bean = ctx.getBean(field.getName());
        else if (field.isAnnotationPresent(Inject.BY_TYPE.class))
            bean = ctx.getBean(field.getType());
        else
            continue;

        try {
            if (bean != null) {
                field.setAccessible(true);
                field.set(controller, bean);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    ai.invoke();
}

From source file:com.buy.plugin.spring.IocInterceptor.java

public void intercept(Invocation ai) {
    Controller controller = ai.getController();
    Field[] fields = controller.getClass().getDeclaredFields();
    for (Field field : fields) {
        Object bean = null;//w  w  w .j  a  v  a 2s .co  m
        if (field.isAnnotationPresent(Inject.BY_NAME.class))
            bean = ctx.getBean(field.getName());
        else if (field.isAnnotationPresent(Inject.BY_TYPE.class))
            bean = ctx.getBean(field.getType());
        else
            continue;

        try {
            if (bean != null) {
                field.setAccessible(true);
                field.set(controller, bean);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    ai.invoke();
}