public void init(Model model) {
    javaAnnotationClass = ReflectionUtil.loadClass(type);
    if (javaAnnotationClass == null) {
      logger.warn("Cannot load annotation class: {}", type);
      return;
    }

    AnnotationsManager annotationsManager = AnnotationsManager.getManager();

    Class annotationImplClass =
        annotationsManager.getAnnotationImplementationClass(javaAnnotationClass);
    if (annotationImplClass == null) {
      logger.warn("Cannot find implementation for annotation class: {}", javaAnnotationClass);
      return;
    }

    Constructor[] constructors = annotationImplClass.getConstructors();
    for (Constructor candidateConstructor : constructors) {
      Class[] parameterTypes = candidateConstructor.getParameterTypes();
      if (parameterTypes.length != values.size()) {
        continue;
      }

      try {
        Object castValues[] = new Object[parameterTypes.length];
        for (int i = 0; i < parameterTypes.length; i++) {
          Class parameterType = parameterTypes[i];
          String stringValue = values.get(i);
          Object value;
          if (parameterType.isArray()) {
            value = Util.matchStringArray(stringValue);
          } else if (parameterType.isEnum()) {
            Object[] enumValues = parameterType.getEnumConstants();
            value = stringValue;
            for (Object current : enumValues) {
              Enum enumValue = (Enum) current;
              if (enumValue.name().equals(stringValue)) {
                value = enumValue;
                break;
              }
            }
          } else {
            value = stringValue;
          }
          castValues[i] = OgnlUtils.convertValue(value, parameterType);
        }

        javaAnnotation =
            (java.lang.annotation.Annotation)
                ReflectionUtil.newInstance(candidateConstructor, castValues);
      } catch (Throwable e) {
        logger.debug("Failed to use constructor: " + candidateConstructor, e);
      }
    }

    if (javaAnnotation == null) {
      logger.warn("Cannot instanciate annotation: {}", javaAnnotationClass);
    }
  }
Exemple #2
0
 public static boolean doGuardsPass(Object actionBean, Method method, @Nullable GuardType type) {
   List<Guard> guards = getGuards(method, type);
   boolean pass = true;
   OgnlContext ognlContext = ElementsThreadLocals.getOgnlContext();
   for (Guard guard : guards) {
     Object result = OgnlUtils.getValueQuietly(guard.test(), ognlContext, actionBean);
     pass &= result instanceof Boolean && ((Boolean) result);
   }
   return pass;
 }