Search in sources :

Example 11 with Annotation

use of java.lang.annotation.Annotation in project jersey by jersey.

the class EntityFilteringHelper method getFilteringScopes.

/**
     * Get entity-filtering scopes from given annotations. Scopes are only derived from entity-filtering annotations.
     *
     * @param annotations list of arbitrary annotations.
     * @param filter {@code true} whether the given annotation should be reduced to only entity-filtering annotations,
     * {@code false} otherwise.
     * @return a set of entity-filtering scopes.
     */
public static Set<String> getFilteringScopes(Annotation[] annotations, final boolean filter) {
    if (annotations.length == 0) {
        return Collections.emptySet();
    }
    final Set<String> contexts = new HashSet<>(annotations.length);
    annotations = filter ? getFilteringAnnotations(annotations) : annotations;
    for (final Annotation annotation : annotations) {
        contexts.add(annotation.annotationType().getName());
    }
    return contexts;
}
Also used : Annotation(java.lang.annotation.Annotation) HashSet(java.util.HashSet)

Example 12 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 13 with Annotation

use of java.lang.annotation.Annotation in project jersey by jersey.

the class InvocableValidator method visitInvocable.

@Override
public void visitInvocable(final Invocable invocable) {
    // TODO: check invocable.
    Class resClass = invocable.getHandler().getHandlerClass();
    if (resClass != null && !checkedClasses.contains(resClass)) {
        checkedClasses.add(resClass);
        final boolean provider = Providers.isProvider(resClass);
        int counter = 0;
        for (Annotation annotation : resClass.getAnnotations()) {
            if (SCOPE_ANNOTATIONS.contains(annotation.annotationType())) {
                counter++;
            }
        }
        if (counter == 0 && provider) {
            Errors.warning(resClass, LocalizationMessages.RESOURCE_IMPLEMENTS_PROVIDER(resClass, Providers.getProviderContracts(resClass)));
        } else if (counter > 1) {
            Errors.fatal(resClass, LocalizationMessages.RESOURCE_MULTIPLE_SCOPE_ANNOTATIONS(resClass));
        }
    }
}
Also used : Annotation(java.lang.annotation.Annotation)

Example 14 with Annotation

use of java.lang.annotation.Annotation in project jersey by jersey.

the class Parameter method create.

/**
     * Create a parameter model.
     *
     * @param concreteClass   concrete resource method handler implementation class.
     * @param declaringClass  declaring class of the method the parameter belongs to or field that this parameter represents.
     * @param encodeByDefault flag indicating whether the parameter should be encoded by default or not. Note that a presence
     *                        of {@link Encoded} annotation in the list of the parameter {@code annotations} will override any
     *                        value set in the flag to {@code true}.
     * @param rawType         raw Java parameter type.
     * @param type            generic Java parameter type.
     * @param annotations     parameter annotations.
     * @return new parameter model.
     */
@SuppressWarnings("unchecked")
public static Parameter create(Class concreteClass, Class declaringClass, boolean encodeByDefault, Class<?> rawType, Type type, Annotation[] annotations) {
    if (null == annotations) {
        return null;
    }
    Annotation paramAnnotation = null;
    Parameter.Source paramSource = null;
    String paramName = null;
    boolean paramEncoded = encodeByDefault;
    String paramDefault = null;
    /**
         * Create a parameter from the list of annotations. Unknown annotated
         * parameters are also supported, and in such a cases the last
         * unrecognized annotation is taken to be that associated with the
         * parameter.
         */
    for (Annotation annotation : annotations) {
        if (ANNOTATION_HELPER_MAP.containsKey(annotation.annotationType())) {
            ParamAnnotationHelper helper = ANNOTATION_HELPER_MAP.get(annotation.annotationType());
            paramAnnotation = annotation;
            paramSource = helper.getSource();
            paramName = helper.getValueOf(annotation);
        } else if (Encoded.class == annotation.annotationType()) {
            paramEncoded = true;
        } else if (DefaultValue.class == annotation.annotationType()) {
            paramDefault = ((DefaultValue) annotation).value();
        } else {
            // Take latest unknown annotation, but don't override known annotation
            if ((paramAnnotation == null) || (paramSource == Source.UNKNOWN)) {
                paramAnnotation = annotation;
                paramSource = Source.UNKNOWN;
                paramName = getValue(annotation);
            }
        }
    }
    if (paramAnnotation == null) {
        paramSource = Parameter.Source.ENTITY;
    }
    ClassTypePair ct = ReflectionHelper.resolveGenericType(concreteClass, declaringClass, rawType, type);
    if (paramSource == Source.BEAN_PARAM) {
        return new BeanParameter(annotations, paramAnnotation, paramName, ct.rawClass(), ct.type(), paramEncoded, paramDefault);
    } else {
        return new Parameter(annotations, paramAnnotation, paramSource, paramName, ct.rawClass(), ct.type(), paramEncoded, paramDefault);
    }
}
Also used : Encoded(javax.ws.rs.Encoded) ClassTypePair(org.glassfish.jersey.internal.util.collection.ClassTypePair) Annotation(java.lang.annotation.Annotation)

Example 15 with Annotation

use of java.lang.annotation.Annotation in project jersey by jersey.

the class AnnotatedMethod method mergeParameterAnnotations.

private static Annotation[][] mergeParameterAnnotations(final Method m, final Method am) {
    final Annotation[][] methodParamAnnotations = m.getParameterAnnotations();
    final Annotation[][] annotatedMethodParamAnnotations = am.getParameterAnnotations();
    final List<List<Annotation>> methodParamAnnotationsList = new ArrayList<>();
    for (int i = 0; i < methodParamAnnotations.length; i++) {
        final List<Annotation> al = asList(methodParamAnnotations[i]);
        for (final Annotation a : annotatedMethodParamAnnotations[i]) {
            if (annotationNotInList(a.getClass(), al)) {
                al.add(a);
            }
        }
        methodParamAnnotationsList.add(al);
    }
    final Annotation[][] mergedAnnotations = new Annotation[methodParamAnnotations.length][];
    for (int i = 0; i < methodParamAnnotations.length; i++) {
        final List<Annotation> paramAnnotations = methodParamAnnotationsList.get(i);
        mergedAnnotations[i] = paramAnnotations.toArray(new Annotation[paramAnnotations.size()]);
    }
    return mergedAnnotations;
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Annotation(java.lang.annotation.Annotation)

Aggregations

Annotation (java.lang.annotation.Annotation)1784 Method (java.lang.reflect.Method)412 ArrayList (java.util.ArrayList)245 Field (java.lang.reflect.Field)172 Test (org.junit.Test)164 Type (java.lang.reflect.Type)149 HashMap (java.util.HashMap)146 HashSet (java.util.HashSet)129 Map (java.util.Map)95 List (java.util.List)92 IOException (java.io.IOException)71 Set (java.util.Set)71 ParameterizedType (java.lang.reflect.ParameterizedType)62 InvocationTargetException (java.lang.reflect.InvocationTargetException)53 Collection (java.util.Collection)52 LinkedHashMap (java.util.LinkedHashMap)36 Collectors (java.util.stream.Collectors)36 Constructor (java.lang.reflect.Constructor)33 LinkedHashSet (java.util.LinkedHashSet)32 Optional (java.util.Optional)32