예제 #1
0
 public Object findAndInvokeMethod(Object object, String methodName, Object... params) {
   Class<?>[] argumentTypes = new Class[params.length];
   for (int i = 0; i < argumentTypes.length; i++) {
     if (params[i] != null) {
       argumentTypes[i] = params[i].getClass();
     }
   }
   Method findMethod = findMethod(object, methodName, argumentTypes);
   try {
     return findMethod.invoke(object, params);
   } catch (IllegalArgumentException e) {
     throw new RuntimeException(
         "Argumentos inválidos ao chamar método "
             + methodName
             + " na classe "
             + object.getClass().getSimpleName(),
         e);
   } catch (IllegalAccessException e) {
     throw new RuntimeException(
         "Acesso ilegal ao chamar método "
             + methodName
             + " da classe "
             + object.getClass().getSimpleName(),
         e);
   } catch (InvocationTargetException e) {
     throw new RuntimeException(
         "Método " + methodName + " lançou exeção " + e.getClass().getSimpleName(), e);
   }
 }
  @Override
  public String resolve(String variableName) {
    String value = "UNRESOLVED";
    try {
      // Try getting it from teamcity first.
      if (teamcityProperties.containsKey(variableName)) {
        value = (String) teamcityProperties.get(variableName);
      }

      // Or override it from the PayloadContent if it exists.

      value = (String) PropertyUtils.getProperty(bean, variableName);

    } catch (IllegalAccessException e) {
      Loggers.SERVER.warn(
          this.getClass().getSimpleName()
              + " :: "
              + e.getClass()
              + " thrown when trying to resolve value for "
              + variableName);
      Loggers.SERVER.debug(e);
    } catch (InvocationTargetException e) {
      Loggers.SERVER.warn(
          this.getClass().getSimpleName()
              + " :: "
              + e.getClass()
              + " thrown when trying to resolve value for "
              + variableName);
      Loggers.SERVER.debug(e);
    } catch (NoSuchMethodException e) {
      Loggers.SERVER.warn(
          this.getClass().getSimpleName()
              + " :: "
              + e.getClass()
              + " thrown when trying to resolve value for "
              + variableName);
      Loggers.SERVER.debug(e);
    }
    return value;
  }
예제 #3
0
  /**
   * Setup parameter accordnig to ZkParameter annotation.
   *
   * @param <T> type of field.
   * @param annot the annotation with parameter definition
   * @param field the field to set
   * @param clazz class of the field and parameter
   */
  protected static <T> void setupZkParameter(
      ZkParameter annot,
      String paramName,
      Class<T> clazz,
      Field field,
      Method method,
      Object instance) {
    T result = null;

    Map[] paramMaps =
        new Map[] {
          Executions.getCurrent().getArg(),
          Executions.getCurrent().getAttributes(),
          Executions.getCurrent().getParameterMap()
        };

    Map paramMap = paramMaps[0];
    for (Map map : paramMaps) {
      if (map.containsKey(paramName)) {
        paramMap = map;
        break;
      }
    }

    try {
      if (annot.required()) result = ZKHelper.getRequiredParameter(paramMap, paramName, clazz);
      else if (!paramMap.containsKey(paramName)) return; // optional parameter doesn't do anything
      else result = ZKHelper.getOptionalParameter(paramMap, paramName, clazz, null);
    } catch (IllegalArgumentException ex) {
      throw new IllegalArgumentException(
          "@ZkParameter(name='"
              + paramName
              + "', "
              + "required="
              + (annot.required() ? "true" : "false")
              + ", "
              + "createIfNull="
              + (annot.createIfNull() ? "true" : "false")
              + "): "
              + ex.getLocalizedMessage());
    }

    if (result == null && annot.createIfNull())
      try {
        result = (T) clazz.newInstance();
      } catch (InstantiationException ex) {
        throw new InstantiationError(
            "@ZkParameter(name='"
                + paramName
                + "', "
                + "required="
                + (annot.required() ? "true" : "false")
                + ", "
                + "createIfNull="
                + (annot.createIfNull() ? "true" : "false")
                + ") - Parameter is null, unable to create new object with error: "
                + ex.getLocalizedMessage());
      } catch (IllegalAccessException ex) {
        throw new InstantiationError(
            "@ZkParameter(name='"
                + paramName
                + "', "
                + "required="
                + (annot.required() ? "true" : "false")
                + ", "
                + "createIfNull="
                + (annot.createIfNull() ? "true" : "false")
                + ") - Parameter is null, unable to create new object, no public default constructor: "
                + ex.getLocalizedMessage());
      }

    if (field != null)
      try {
        field.setAccessible(true);
        field.set(instance, result);
        field.setAccessible(false);
      } catch (IllegalArgumentException ex) {
        throw new IllegalArgumentException(
            "@ZkParameter(name='"
                + paramName
                + "', "
                + "required="
                + (annot.required() ? "true" : "false")
                + ", "
                + "createIfNull="
                + (annot.createIfNull() ? "true" : "false")
                + ") - Unable to set new value of field to '"
                + result
                + "': "
                + ex.getLocalizedMessage(),
            ex);
      } catch (IllegalAccessException ex) {
        throw SystemException.Aide.wrap(ex);
      }
    if (method != null)
      try {
        method.setAccessible(true);
        method.invoke(instance, new Object[] {result});
        method.setAccessible(false);
      } catch (IllegalAccessException ex) {
        throw SystemException.Aide.wrap(ex);
      } catch (IllegalArgumentException ex) {
        throw new IllegalArgumentException(
            "@ZkParameter(name='"
                + paramName
                + "', "
                + "required="
                + (annot.required() ? "true" : "false")
                + ", "
                + "createIfNull="
                + (annot.createIfNull() ? "true" : "false")
                + ") - Unable to set new value of method to '"
                + result
                + "': "
                + ex.getClass()
                + " - "
                + ex.getLocalizedMessage(),
            ex);
      } catch (InvocationTargetException ex) {
        throw new IllegalArgumentException(
            "@ZkParameter(name='"
                + paramName
                + "', "
                + "required="
                + (annot.required() ? "true" : "false")
                + ", "
                + "createIfNull="
                + (annot.createIfNull() ? "true" : "false")
                + ") - Unable to set new value of method to '"
                + result
                + "', error in method invocation: "
                + ex.getClass()
                + " - "
                + (ex.getTargetException() == null
                    ? ex.getLocalizedMessage()
                    : ex.getTargetException().getLocalizedMessage()),
            ex);
      }
  }