示例#1
0
 {
   for (Method m : this.getClass().getDeclaredMethods()) {
     if (m.getName().matches("m[0-9]+(_check)?")) {
       assert (Modifier.isStatic(m.getModifiers())) : m;
       tests.put(m.getName(), m);
     }
   }
 }
示例#2
0
  void addBooleanComponent(String name, boolean currentValue) {
    configGridLayout.setRows(configGridLayout.getRows() + 1);
    addConfigLabel(name);

    JCheckBox checkBox = new JCheckBox();
    checkBox.setSelected(currentValue);

    componentByName.put(name, checkBox);
    configPanel.add(checkBox);
  }
示例#3
0
  void addTextBox(String name, String currentValue) {
    configGridLayout.setRows(configGridLayout.getRows() + 1);
    addConfigLabel(name);

    JTextField textField = new JTextField();
    textField.setText(currentValue);

    componentByName.put(name, textField);
    configPanel.add(textField);
  }
示例#4
0
 void revertConfig() {
   try {
     debug("reverting config panel");
     Config config = playerObjects.getConfig();
     for (Field field : config.getClass().getDeclaredFields()) {
       Annotation excludeAnnotation = field.getAnnotation(ReflectionHelper.Exclude.class);
       if (excludeAnnotation == null) { // so, this field is not excluded
         debug("field " + field.getName());
         Class<?> fieldType = field.getType();
         Method getMethod = getGetMethod(config.getClass(), field.getType(), field.getName());
         if (getMethod != null) {
           debug(" ... found accessor method");
           Object value = getMethod.invoke(config);
           String fieldname = field.getName();
           Component component = componentByName.get(fieldname);
           if (component != null) {
             debug(" ... found component");
             if (fieldType == String.class) {
               ((JTextField) component).setText((String) value);
             }
             if (fieldType == boolean.class || fieldType == Boolean.class) {
               ((JCheckBox) component).setSelected((Boolean) value);
             }
             if (fieldType == float.class || fieldType == Float.class) {
               ((JTextField) component).setText("" + value);
             }
             if (fieldType == int.class || fieldType == Integer.class) {
               ((JTextField) component).setText("" + value);
             }
           }
         } else {
           playerObjects
               .getLogFile()
               .WriteLine("No get accessor method for config field " + field.getName());
         }
       }
     }
   } catch (Exception e) {
     playerObjects.getLogFile().WriteLine(Formatting.exceptionToStackTrace(e));
   }
 }
示例#5
0
  void doTest(String name) throws Exception {
    Method m = tests.get(name);
    Method m_check = tests.get(name + "_check");
    Class[] paramTypes = m.getParameterTypes();
    Object[] params = new Object[paramTypes.length];
    Class retType = m.getReturnType();
    boolean isIntArray =
        (retType.isPrimitive() && !retType.equals(Void.TYPE))
            || (retType.equals(Void.TYPE) && paramTypes[0].getComponentType().isPrimitive())
            || (retType.isArray() && retType.getComponentType().isPrimitive());

    Args args = m.getAnnotation(Args.class);

    Object src = null;
    switch (args.src()) {
      case SMALL:
        {
          if (isIntArray) {
            src = small_int_src;
          } else {
            src = small_a_src;
          }
          break;
        }
      case LARGE:
        {
          if (isIntArray) {
            src = large_int_src;
          } else {
            src = large_a_src;
          }
          break;
        }
      case ZERO:
        {
          if (isIntArray) {
            src = zero_int_src;
          } else {
            src = zero_a_src;
          }
          break;
        }
    }

    for (int i = 0; i < 20000; i++) {
      boolean failure = false;

      int p = 0;

      if (params.length > 0) {
        if (isIntArray) {
          params[0] = ((int[]) src).clone();
        } else {
          params[0] = ((A[]) src).clone();
        }
        p++;
      }

      if (params.length > 1) {
        switch (args.dst()) {
          case NEW:
            {
              if (isIntArray) {
                params[1] = new int[((int[]) params[0]).length];
              } else {
                params[1] = new A[((A[]) params[0]).length];
              }
              p++;
              break;
            }
          case SRC:
            {
              params[1] = params[0];
              p++;
              break;
            }
          case NONE:
            break;
        }
      }

      for (int j = 0; j < args.extra_args().length; j++) {
        params[p + j] = args.extra_args()[j];
      }

      Object res = m.invoke(null, params);

      if (retType.isPrimitive() && !retType.equals(Void.TYPE)) {
        int s = (int) res;
        int sum = 0;
        int[] int_res = (int[]) src;
        for (int j = 0; j < int_res.length; j++) {
          sum += int_res[j];
        }
        failure = (s != sum);
        if (failure) {
          System.out.println("Test " + name + " failed: result = " + s + " != " + sum);
        }
      } else {
        Object dest = null;
        if (!retType.equals(Void.TYPE)) {
          dest = res;
        } else {
          dest = params[1];
        }

        if (m_check != null) {
          failure = (boolean) m_check.invoke(null, new Object[] {src, dest});
        } else {
          if (isIntArray) {
            int[] int_res = (int[]) src;
            int[] int_dest = (int[]) dest;
            for (int j = 0; j < int_res.length; j++) {
              if (int_res[j] != int_dest[j]) {
                System.out.println(
                    "Test "
                        + name
                        + " failed for "
                        + j
                        + " src["
                        + j
                        + "]="
                        + int_res[j]
                        + ", dest["
                        + j
                        + "]="
                        + int_dest[j]);
                failure = true;
              }
            }
          } else {
            Object[] object_res = (Object[]) src;
            Object[] object_dest = (Object[]) dest;
            for (int j = 0; j < object_res.length; j++) {
              if (object_res[j] != object_dest[j]) {
                System.out.println(
                    "Test "
                        + name
                        + " failed for "
                        + j
                        + " src["
                        + j
                        + "]="
                        + object_res[j]
                        + ", dest["
                        + j
                        + "]="
                        + object_dest[j]);
                failure = true;
              }
            }
          }
        }
      }

      if (failure) {
        success = false;
        break;
      }
    }
  }
示例#6
0
 void applyConfig() {
   debug("applying config from panel");
   Config config = playerObjects.getConfig();
   for (Field field : config.getClass().getDeclaredFields()) {
     Annotation excludeAnnotation = field.getAnnotation(ReflectionHelper.Exclude.class);
     if (excludeAnnotation == null) { // so, this field is not excluded
       debug("field " + field.getName());
       Class<?> fieldType = field.getType();
       Method setMethod = getSetMethod(config.getClass(), field.getType(), field.getName());
       if (setMethod != null) {
         debug(" ... found accessor method");
         String fieldname = field.getName();
         Component component = componentByName.get(fieldname);
         if (component != null) {
           debug(" ... found component");
           Object value = null;
           if (fieldType == String.class) {
             value = ((JTextField) component).getText();
           }
           if (fieldType == boolean.class || fieldType == Boolean.class) {
             value = ((JCheckBox) component).isSelected();
           }
           if (fieldType == float.class || fieldType == Float.class) {
             String stringvalue = (String) ((JTextField) component).getText();
             try {
               value = Float.parseFloat(stringvalue);
             } catch (Exception e) {
             }
           }
           if (fieldType == int.class || fieldType == Integer.class) {
             String stringvalue = (String) ((JTextField) component).getText();
             try {
               value = Integer.parseInt(stringvalue);
             } catch (Exception e) {
             }
           }
           if (value != null) {
             try {
               setMethod.invoke(config, value);
             } catch (Exception e) {
               playerObjects.getLogFile().WriteLine(Formatting.exceptionToStackTrace(e));
             }
           }
         }
         if (fieldType == boolean.class || fieldType == Boolean.class) {
           // addBooleanComponent( field.getName(), (Boolean)value );
         }
       } else {
         playerObjects
             .getLogFile()
             .WriteLine("No get accessor method for config field " + field.getName());
       }
     }
   }
   revertConfig(); // in case some parses and stuff didn't work, so
   // user can see what is actually being read.
   playerObjects
       .getMainUI()
       .showInfo(
           "Config updated.  Note that most changes require an AI restart.  You can click on 'reloadAI' in 'Actions' tab to do so.");
   playerObjects.getConfig().configUpdated();
 }