Search in sources :

Example 46 with Annotation

use of java.lang.annotation.Annotation in project eweb4j-framework by laiweiwei.

the class ValidatorUtil method readValidator.

//	/**
//	 * 读取注解中验证器部分
//	 * 
//	 * @param actionIndex
//	 * @param validatorAnn
//	 * @param fieldAnn
//	 * @param paramAnn
//	 * @return
//	 */
//	public static List<ValidatorConfigBean> readValidator() {
//		List<ValidatorConfigBean> vList = new ArrayList<ValidatorConfigBean>();
////		String[] name = validatorAnn.value();
////		String[] clsName = validatorAnn.clazz();
//
//		for (int a = 0; a < name.length; ++a) {
//			ValidatorConfigBean v = new ValidatorConfigBean();
//			if (name != null && name.length > a)
//				v.setName(StringUtil.parsePropValue(name[a]));
//
//			if (clsName != null && clsName.length > a)
//				v.setClazz(StringUtil.parsePropValue(clsName[a]));
//
//			if (valMessAnn == null || valFieldAnn == null)
//				continue;
//
//			// 验证器数组下标
//			int[] valIndex = valMessAnn.validator();
//			// 需要验证的属性域数组下标
//			int[] fieldIndex = valMessAnn.field();
//
//			String[] valField = valFieldAnn.value();
//			String[] mess = valMessAnn.value();
//
//			List<String> fnamelist = new ArrayList<String>();
//			for (int in : fieldIndex)
//				fnamelist.add(StringUtil.parsePropValue(valField[in]));
//
//			String[] fname = fnamelist.toArray(new String[] {});
//
//			List<FieldConfigBean> fList = new ArrayList<FieldConfigBean>();
//			for (int b = 0; b < valIndex.length; ++b) {
//				if (valIndex[b] == a) {
//					FieldConfigBean f = new FieldConfigBean();
//					fList.add(f);
//					f.setName(StringUtil.parsePropValue(fname[b]));
//					f.setMessage(StringUtil.parsePropValue(mess[b]));
//
//					if (paramAnn == null || paramName == null)
//						continue;
//
//					int[] pindex = paramAnn.valMess();
//					int[] pnameIndex = paramAnn.name();
//					String[] pnames = paramName.value();
//
//					List<String> pnamelist = new ArrayList<String>();
//					for (int in : pnameIndex)
//						pnamelist.add(StringUtil.parsePropValue(pnames[in]));
//
//					String[] pname = pnamelist.toArray(new String[] {});
//					String[] pvalue = paramAnn.value();
//
//					List<ParamConfigBean> pList = new ArrayList<ParamConfigBean>();
//					for (int c = 0; c < pindex.length; ++c) {
//						if (pindex[c] == b) {
//							ParamConfigBean p = new ParamConfigBean();
//							p.setName(StringUtil.parsePropValue(pname[c]));
//							p.setValue(StringUtil.parsePropValue(pvalue[c]));
//							pList.add(p);
//						}
//					}
//
//					f.setParam(pList);
//
//				}
//			}
//
//			v.setField(fList);
//			vList.add(v);
//		}
//
//		return vList;
//	}
/**
	 * 从Action属性中读取验证器配置
	 * @param <T>
	 * @param params
	 * @param scopeName
	 * @param ru
	 * @param vList
	 * @param hasCls
	 * @return
	 */
public static <T> List<ValidatorConfigBean> readValidator(final String[] params, final String[] excepts, String scopeName, ReflectUtil ru, List<ValidatorConfigBean> vList, Set<Class<?>> hasCls) {
    if (params == null || params.length == 0)
        return null;
    if (ru == null)
        return null;
    Field[] fs = ru.getFields();
    if (fs == null)
        return null;
    if (vList == null)
        vList = new ArrayList<ValidatorConfigBean>();
    ValidatorConfigBean val = null;
    for (Field f : fs) {
        Skip iv = f.getAnnotation(Skip.class);
        if (iv != null)
            continue;
        if (ClassUtil.isPojo(f.getType()) && !UploadFile.class.isAssignableFrom(f.getType())) {
            // 解决无限递归问题
            if (hasCls == null)
                hasCls = new HashSet<Class<?>>();
            if (!hasCls.contains(f.getType())) {
                hasCls.add(f.getType());
                if (scopeName != null && scopeName.length() > 0)
                    scopeName = scopeName + "." + f.getName();
                else
                    scopeName = f.getName();
                try {
                    readValidator(params, excepts, scopeName, new ReflectUtil(f.getType()), vList, hasCls);
                    scopeName = null;
                } catch (Exception e) {
                    continue;
                }
            }
            continue;
        }
        for (Annotation ann : f.getAnnotations()) {
            ValidatorCreator valCreator = ValidatorFactory.get(ann);
            if (valCreator == null)
                continue;
            String name = f.getName();
            if (scopeName != null && scopeName.length() > 0)
                name = scopeName + "." + name;
            for (String param : params) {
                if (Arrays.asList(excepts).contains(name))
                    continue;
                boolean flag = false;
                if (!param.equals("*") && param.endsWith("*") && name.startsWith(param.replace("*", "")))
                    flag = true;
                if (!param.equals("*") && param.startsWith("*") && name.endsWith(param.replace("*", "")))
                    flag = true;
                if (name.equals(param) || param.equals("*"))
                    flag = true;
                if (flag) {
                    val = valCreator.create(name, val);
                    if (val != null)
                        vList.add(val);
                    break;
                }
            }
        }
    }
    scopeName = null;
    if (vList.size() > 0)
        return vList;
    return null;
}
Also used : Field(java.lang.reflect.Field) ReflectUtil(org.eweb4j.util.ReflectUtil) ValidatorConfigBean(org.eweb4j.mvc.config.bean.ValidatorConfigBean) ArrayList(java.util.ArrayList) Skip(org.eweb4j.mvc.validator.annotation.Skip) Annotation(java.lang.annotation.Annotation) HashSet(java.util.HashSet)

Example 47 with Annotation

use of java.lang.annotation.Annotation in project junit4 by junit-team.

the class ParameterSignatureTest method getAnnotations.

@Test
public void getAnnotations() throws SecurityException, NoSuchMethodException {
    Method method = getClass().getMethod("foo", int.class);
    List<Annotation> annotations = ParameterSignature.signatures(method).get(0).getAnnotations();
    assertThat(annotations, CoreMatchers.<TestedOn>hasItem(isA(TestedOn.class)));
}
Also used : Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) Test(org.junit.Test)

Example 48 with Annotation

use of java.lang.annotation.Annotation in project junit4 by junit-team.

the class FrameworkFieldTest method presentAnnotationIsAvailable.

@Test
public void presentAnnotationIsAvailable() throws Exception {
    Field field = ClassWithDummyField.class.getField("annotatedField");
    FrameworkField frameworkField = new FrameworkField(field);
    Annotation annotation = frameworkField.getAnnotation(Rule.class);
    assertTrue(Rule.class.isAssignableFrom(annotation.getClass()));
}
Also used : Field(java.lang.reflect.Field) Rule(org.junit.Rule) ClassRule(org.junit.ClassRule) Annotation(java.lang.annotation.Annotation) Test(org.junit.Test)

Example 49 with Annotation

use of java.lang.annotation.Annotation in project junit4 by junit-team.

the class FrameworkFieldTest method missingAnnotationIsNotAvailable.

@Test
public void missingAnnotationIsNotAvailable() throws Exception {
    Field field = ClassWithDummyField.class.getField("annotatedField");
    FrameworkField frameworkField = new FrameworkField(field);
    Annotation annotation = frameworkField.getAnnotation(ClassRule.class);
    assertThat(annotation, is(nullValue()));
}
Also used : Field(java.lang.reflect.Field) Annotation(java.lang.annotation.Annotation) Test(org.junit.Test)

Example 50 with Annotation

use of java.lang.annotation.Annotation in project junit4 by junit-team.

the class CategoryValidator method validateAnnotatedMethod.

/**
     * Adds to {@code errors} a throwable for each problem detected. Looks for
     * {@code BeforeClass}, {@code AfterClass}, {@code Before} and {@code After}
     * annotations.
     *
     * @param method the method that is being validated
     * @return A list of exceptions detected
     *
     * @since 4.12
     */
@Override
public List<Exception> validateAnnotatedMethod(FrameworkMethod method) {
    List<Exception> errors = new ArrayList<Exception>();
    Annotation[] annotations = method.getAnnotations();
    for (Annotation annotation : annotations) {
        for (Class<?> clazz : INCOMPATIBLE_ANNOTATIONS) {
            if (annotation.annotationType().isAssignableFrom(clazz)) {
                addErrorMessage(errors, clazz);
            }
        }
    }
    return unmodifiableList(errors);
}
Also used : ArrayList(java.util.ArrayList) Annotation(java.lang.annotation.Annotation)

Aggregations

Annotation (java.lang.annotation.Annotation)707 Method (java.lang.reflect.Method)171 ArrayList (java.util.ArrayList)99 Field (java.lang.reflect.Field)76 Test (org.junit.Test)66 Type (java.lang.reflect.Type)64 HashMap (java.util.HashMap)54 HashSet (java.util.HashSet)52 Map (java.util.Map)35 ParameterizedType (java.lang.reflect.ParameterizedType)34 List (java.util.List)30 Set (java.util.Set)27 InvocationTargetException (java.lang.reflect.InvocationTargetException)22 IOException (java.io.IOException)20 BindingAnnotation (com.google.inject.BindingAnnotation)17 AbstractModule (com.google.inject.AbstractModule)16 TypeElement (javax.lang.model.element.TypeElement)15 Injector (com.google.inject.Injector)14 MediaType (okhttp3.MediaType)14 AnnotatedElement (java.lang.reflect.AnnotatedElement)13