Пример #1
0
 /**
  * オブジェクトのフィールドにデータを設定する<br>
  * Integer、Float、Float[]、Stringにしか対応していない
  *
  * @param obj 設定対象オブジェクト
  * @param fl 設定対象フィールド
  * @param ty 設定対象フィールドの型
  * @param data 設定データ
  * @throws IllegalArgumentException
  * @throws IllegalAccessException
  */
 protected void dataSetter(Object obj, Field fl, Class ty, String data)
     throws IllegalArgumentException, IllegalAccessException {
   // Integer型のデータを設定
   if (ty.toString().equals("class java.lang.Integer")) {
     fl.set(obj, Integer.parseInt(data));
   }
   // Float型のデータを設定
   if (ty.toString().equals("class java.lang.Float")) {
     fl.set(obj, Float.parseFloat(data));
   }
   // String型のデータを設定(""の内側)
   if (ty.toString().equals("class java.lang.String")) {
     fl.set(obj, getDoubleQuoatString(data));
   }
   // Float[]型のデータを設定
   if (ty.toString().equals("class [Ljava.lang.Float;")) {
     String[] s;
     s = data.split(" ");
     Float[] f = new Float[s.length];
     for (int i = 0; i < s.length; i++) {
       f[i] = Float.parseFloat(s[i]);
     }
     fl.set(obj, f);
   }
 }
 /**
  * Delegates to {@link Field#set(Object,Object)} hiding IllegalAccessException.
  *
  * @param f A field.
  * @param obj An object.
  * @param val A value.
  * @throws IOException If failed.
  */
 static void set(Field f, Object obj, Object val) throws IOException {
   try {
     f.set(obj, val);
   } catch (IllegalAccessException e) {
     throw new IOException("Failed to set field value: " + f, e);
   }
 }
Пример #3
0
  /**
   * Copy from the copy method in StructUtil. Did not want to drag that code in. maybe this actually
   * should go to struct.
   *
   * @param from
   * @param to
   * @param excludes
   * @return
   * @throws Exception
   */
  public static <T extends struct> T xcopy(struct from, T to, String... excludes) throws Exception {
    Arrays.sort(excludes);
    for (Field f : from.fields()) {
      if (Arrays.binarySearch(excludes, f.getName()) >= 0) continue;

      Object o = f.get(from);
      if (o == null) continue;

      Field tof = to.getField(f.getName());
      if (tof != null)
        try {
          tof.set(to, Converter.cnv(tof.getGenericType(), o));
        } catch (Exception e) {
          System.out.println(
              "Failed to convert "
                  + f.getName()
                  + " from "
                  + from.getClass()
                  + " to "
                  + to.getClass()
                  + " value "
                  + o
                  + " exception "
                  + e);
        }
    }

    return to;
  }
Пример #4
0
 private static void netxsurgery() throws Exception {
   /* Force off NetX codebase classloading. */
   Class<?> nxc;
   try {
     nxc = Class.forName("net.sourceforge.jnlp.runtime.JNLPClassLoader");
   } catch (ClassNotFoundException e1) {
     try {
       nxc = Class.forName("netx.jnlp.runtime.JNLPClassLoader");
     } catch (ClassNotFoundException e2) {
       throw (new Exception("No known NetX on classpath"));
     }
   }
   ClassLoader cl = MainFrame.class.getClassLoader();
   if (!nxc.isInstance(cl)) {
     throw (new Exception("Not running from a NetX classloader"));
   }
   Field cblf, lf;
   try {
     cblf = nxc.getDeclaredField("codeBaseLoader");
     lf = nxc.getDeclaredField("loaders");
   } catch (NoSuchFieldException e) {
     throw (new Exception("JNLPClassLoader does not conform to its known structure"));
   }
   cblf.setAccessible(true);
   lf.setAccessible(true);
   Set<Object> loaders = new HashSet<Object>();
   Stack<Object> open = new Stack<Object>();
   open.push(cl);
   while (!open.empty()) {
     Object cur = open.pop();
     if (loaders.contains(cur)) continue;
     loaders.add(cur);
     Object curl;
     try {
       curl = lf.get(cur);
     } catch (IllegalAccessException e) {
       throw (new Exception("Reflection accessibility not available even though set"));
     }
     for (int i = 0; i < Array.getLength(curl); i++) {
       Object other = Array.get(curl, i);
       if (nxc.isInstance(other)) open.push(other);
     }
   }
   for (Object cur : loaders) {
     try {
       cblf.set(cur, null);
     } catch (IllegalAccessException e) {
       throw (new Exception("Reflection accessibility not available even though set"));
     }
   }
 }
Пример #5
0
 private static void writeField(Object o, String field, Object value) throws Exception {
   Field f = o.getClass().getDeclaredField(field);
   f.setAccessible(true);
   f.set(o, value);
 }