コード例 #1
0
 /**
  * Populates the object with the specified parameter.
  *
  * @param object the object which should be populated.
  * @param baseDir the base directory for the population.
  * @param param the parameter
  * @param methodsByName the methods known of the object
  * @throws IllegalArgumentException when a parameter has a syntax error or when a needed method
  *     has not be found.
  * @throws InvocationTargetException when the method could not be called
  * @throws IllegalAccessException when the method could not be accessed
  */
 private static void populate(
     Object object,
     File baseDir,
     String parameterName,
     String parameterValue,
     HashMap methodsByName)
     throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
   String methodName =
       "set" + Character.toUpperCase(parameterName.charAt(0)) + parameterName.substring(1);
   Method method = (Method) methodsByName.get(methodName);
   if (method == null) {
     throw new IllegalArgumentException(
         "To use the parameter ["
             + parameterName
             + "] the class ["
             + object.getClass().getName()
             + "] needs to specify the method "
             + methodName
             + "(String), "
             + methodName
             + "(boolean) or "
             + methodName
             + "(File). ");
   }
   Class paramType = method.getParameterTypes()[0];
   if (paramType == String.class) {
     method.invoke(object, new Object[] {parameterValue});
   } else if (paramType == Boolean.TYPE) {
     Boolean argument = new Boolean(CastUtil.getBoolean(parameterValue));
     method.invoke(object, new Object[] {argument});
   } else {
     File file = new File(parameterValue);
     if (!file.isAbsolute()) {
       file = new File(baseDir.getAbsolutePath() + File.separator + parameterValue);
     }
     method.invoke(object, new Object[] {file});
   }
 }
コード例 #2
0
 /**
  * Populates the given object with the specified parameter. For the parameter-name the given
  * object needs to specify either set[param-name]( String ), set[param-name]( File ) or
  * set[param-name]( boolean ). When the parameter "message" is provided, either the method
  * setMessage( String value ), setMessage( File value ) or setMessage( boolean ) needs to be
  * defined by the given object. When the object defines both methods, it cannot be foreseen which
  * one of them will be called.
  *
  * @param object the object which should be populated.
  * @param parameterName the name of the parameter.
  * @param parameterValue the value of the parameter
  * @param baseDir the base directory for the population.
  * @throws IllegalArgumentException when a parameter has a syntax error or when a needed method
  *     has not be found.
  */
 public static final void populate(
     Object object, String parameterName, String parameterValue, File baseDir) {
   String methodName = parameterName;
   if (methodName == null) {
     throw new IllegalArgumentException("The parameter does not contain a name.");
   }
   methodName = "set" + Character.toUpperCase(methodName.charAt(0)) + methodName.substring(1);
   Class objectClass = object.getClass();
   Method method = null;
   Object argument = null;
   String value = parameterValue;
   try {
     method = objectClass.getMethod(methodName, new Class[] {String.class});
     argument = value;
   } catch (NoSuchMethodException e) {
     try {
       method = objectClass.getMethod(methodName, new Class[] {File.class});
       if (value != null) {
         File file = new File(value);
         if (!file.isAbsolute()) {
           file = new File(baseDir, value);
         }
         argument = file;
       }
     } catch (NoSuchMethodException e2) {
       try {
         method = objectClass.getMethod(methodName, new Class[] {Boolean.TYPE});
         argument = new Boolean(CastUtil.getBoolean(value));
       } catch (NoSuchMethodException e3) {
         throw new IllegalArgumentException(
             "Unable to retrieve method " + methodName + ": " + e3.toString());
       }
     }
   } catch (SecurityException e) {
     e.printStackTrace();
     throw new IllegalArgumentException(
         "Unable to retrieve method " + methodName + ": " + e.toString());
   }
   try {
     // okay, we've now have a method and an argument, so let's invoke it:
     method.invoke(object, new Object[] {argument});
   } catch (IllegalArgumentException e) {
     throw e;
   } catch (IllegalAccessException e) {
     e.printStackTrace();
     String message =
         "Unable to set the parameter ["
             + parameterName
             + "] with value ["
             + parameterValue
             + "] for class ["
             + object.getClass().getName()
             + "]: "
             + e.toString();
     throw new IllegalArgumentException(message);
   } catch (InvocationTargetException e) {
     e.printStackTrace();
     String message =
         "Unable to set the parameter ["
             + parameterName
             + "] with value ["
             + parameterValue
             + "] for class ["
             + object.getClass().getName()
             + "]: "
             + e.toString();
     throw new IllegalArgumentException(message);
   }
 }
コード例 #3
0
ファイル: Storage.java プロジェクト: TradeLine/TWT
 public int size() {
   return CastUtil.toInt(Script.code(js, ".length"));
 }
コード例 #4
0
ファイル: Storage.java プロジェクト: TradeLine/TWT
 public Optional<String> getIndexByKey(int index) {
   return Optional.ofNullable(Script.code(js, ".key(", CastUtil.toObject(index), ")"));
 }