/** * オブジェクトのフィールドにデータを設定する<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); } }
/** * Given a map between a string and an object of unknown type, attempt to retrieve what value is * mapped to from the specified string and validate that the actual type matches the specified * class. * * @return Null if the types do not match, or the value */ private static Object validateAndGet(Map<String, Object> m, String s, Class<?> c) throws MissingMappingException, UnexpectedTypeException { Object o = m.get(s); if (o == null) throw new MissingMappingException(s); if (!c.isAssignableFrom(o.getClass())) throw new UnexpectedTypeException(s, c.toString(), o.getClass().toString()); else return o; }
static String[] c_signature(Class c, String expr) { if (c.isPrimitive()) return strs("j" + c.toString(), expr, expr); if (c == Pointer.class) return strs( "void*", "createPointerFromIO(env, " + expr + ", NULL)", "getPointerPeer(env, " + expr + ")"); // TODO callIO if (c == CLong.class) return strs("long", "BoxCLong(env, " + expr + ")", "UnBoxCLong(env, " + expr + ")"); if (c == SizeT.class) return strs("size_t", "BoxSizeT(env, " + expr + ")", "UnBoxSizeT(env, " + expr + ")"); if (c == TimeT.class) return strs("time_t", "BoxTimeT(env, " + expr + ")", "UnBoxTimeT(env, " + expr + ")"); throw new UnsupportedOperationException("Cannot compute C signature for " + c.getName()); }
public <T> T get(String path, Class<T> c) { Log.d(TAG, "get " + path); // NON-NLS File f = new File(getFullCachePath(path)); if (!f.exists()) { Log.w(TAG, "file: " + f.toString() + " not exists"); // NON-NLS return null; } T object; try { StringBuffer fileData = new StringBuffer(); BufferedReader reader = new BufferedReader(new FileReader(f)); char[] buf = new char[1024]; int numRead = 0; while ((numRead = reader.read(buf)) != -1) { String readData = String.valueOf(buf, 0, numRead); fileData.append(readData); } reader.close(); String json = fileData.toString(); Log.d(TAG, "cache: " + json); // NON-NLS object = getGson().fromJson(json, c); } catch (FileNotFoundException e) { Log.w(TAG, e.getMessage()); e.printStackTrace(); absoluteDelete(path); return null; } catch (StreamCorruptedException e) { Log.w(TAG, e.getMessage()); e.printStackTrace(); absoluteDelete(path); return null; } catch (IOException e) { Log.w(TAG, e.getMessage()); e.printStackTrace(); absoluteDelete(path); return null; } catch (JsonSyntaxException e) { Log.w(TAG, e.getMessage()); e.printStackTrace(); absoluteDelete(path); return null; } catch (IllegalArgumentException e) { Log.w(TAG, e.getMessage()); e.printStackTrace(); absoluteDelete(path); return null; } catch (RuntimeException e) { // не позволим кэшу убить нашу программу Log.w(TAG, e.getMessage()); e.printStackTrace(); absoluteDelete(path); return null; } catch (Exception e) { // не позволим кэшу убить нашу программу Log.w(TAG, e.getMessage()); e.printStackTrace(); absoluteDelete(path); return null; } if (object.getClass().toString().equals(c.toString())) return object; else return null; }
/** * Generates code to convert a wrapped value type to a primitive type. Handles unwrapping * java.lang.Boolean, and java.lang.Number types. Generates the appropriate RETURN bytecode. */ static void generateReturnResult( ClassFileWriter cfw, Class<?> retType, boolean callConvertResult) { // wrap boolean values with java.lang.Boolean, convert all other // primitive values to java.lang.Double. if (retType == Void.TYPE) { cfw.add(ByteCode.POP); cfw.add(ByteCode.RETURN); } else if (retType == Boolean.TYPE) { cfw.addInvoke( ByteCode.INVOKESTATIC, "aurora/javascript/Context", "toBoolean", "(Ljava/lang/Object;)Z"); cfw.add(ByteCode.IRETURN); } else if (retType == Character.TYPE) { // characters are represented as strings in JavaScript. // return the first character. // first convert the value to a string if possible. cfw.addInvoke( ByteCode.INVOKESTATIC, "aurora/javascript/Context", "toString", "(Ljava/lang/Object;)Ljava/lang/String;"); cfw.add(ByteCode.ICONST_0); cfw.addInvoke(ByteCode.INVOKEVIRTUAL, "java/lang/String", "charAt", "(I)C"); cfw.add(ByteCode.IRETURN); } else if (retType.isPrimitive()) { cfw.addInvoke( ByteCode.INVOKESTATIC, "aurora/javascript/Context", "toNumber", "(Ljava/lang/Object;)D"); String typeName = retType.getName(); switch (typeName.charAt(0)) { case 'b': case 's': case 'i': cfw.add(ByteCode.D2I); cfw.add(ByteCode.IRETURN); break; case 'l': cfw.add(ByteCode.D2L); cfw.add(ByteCode.LRETURN); break; case 'f': cfw.add(ByteCode.D2F); cfw.add(ByteCode.FRETURN); break; case 'd': cfw.add(ByteCode.DRETURN); break; default: throw new RuntimeException("Unexpected return type " + retType.toString()); } } else { String retTypeStr = retType.getName(); if (callConvertResult) { cfw.addLoadConstant(retTypeStr); cfw.addInvoke( ByteCode.INVOKESTATIC, "java/lang/Class", "forName", "(Ljava/lang/String;)Ljava/lang/Class;"); cfw.addInvoke( ByteCode.INVOKESTATIC, "aurora/javascript/JavaAdapter", "convertResult", "(Ljava/lang/Object;" + "Ljava/lang/Class;" + ")Ljava/lang/Object;"); } // Now cast to return type cfw.add(ByteCode.CHECKCAST, retTypeStr); cfw.add(ByteCode.ARETURN); } }