List of usage examples for java.lang.reflect Array getBoolean
public static native boolean getBoolean(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
From source file:Main.java
public static void main(String args[]) { boolean[] array = new boolean[] { true, true, true }; boolean value = Array.getBoolean(array, 1); System.out.println(value);/*from w w w . j ava 2 s .co m*/ }
From source file:alice.tuprolog.lib.OOLibrary.java
/** * Sets the value of the field 'i' with 'what' * @param obj_id/*from w w w . ja v a 2s . com*/ * @param i * @param what * @return * @throws JavaException */ public boolean java_array_get_primitive_3(PTerm obj_id, PTerm i, PTerm what) throws JavaException { Struct objId = (Struct) obj_id.getTerm(); Number index = (Number) i.getTerm(); what = what.getTerm(); Object obj = null; if (!index.isInteger()) { throw new JavaException(new IllegalArgumentException(index.toString())); } try { Class<?> cl = null; String objName = alice.util.Tools.removeApices(objId.toString()); obj = currentObjects.get(objName); if (obj != null) { cl = obj.getClass(); } else { throw new JavaException(new IllegalArgumentException(objId.toString())); } if (!cl.isArray()) { throw new JavaException(new IllegalArgumentException(objId.toString())); } String name = cl.toString(); switch (name) { case "class [I": { PTerm value = new Int(Array.getInt(obj, index.intValue())); if (unify(what, value)) return true; else throw new JavaException(new IllegalArgumentException(what.toString())); } case "class [D": { PTerm value = new alice.tuprolog.Double(Array.getDouble(obj, index.intValue())); if (unify(what, value)) return true; else throw new JavaException(new IllegalArgumentException(what.toString())); } case "class [F": { PTerm value = new alice.tuprolog.Float(Array.getFloat(obj, index.intValue())); if (unify(what, value)) return true; else throw new JavaException(new IllegalArgumentException(what.toString())); } case "class [L": { PTerm value = new alice.tuprolog.Long(Array.getLong(obj, index.intValue())); if (unify(what, value)) return true; else throw new JavaException(new IllegalArgumentException(what.toString())); } case "class [C": { PTerm value = new Struct("" + Array.getChar(obj, index.intValue())); if (unify(what, value)) return true; else throw new JavaException(new IllegalArgumentException(what.toString())); } case "class [Z": boolean b = Array.getBoolean(obj, index.intValue()); if (b) { if (unify(what, PTerm.TRUE)) return true; else throw new JavaException(new IllegalArgumentException(what.toString())); } else { if (unify(what, PTerm.FALSE)) return true; else throw new JavaException(new IllegalArgumentException(what.toString())); } case "class [B": { PTerm value = new Int(Array.getByte(obj, index.intValue())); if (unify(what, value)) return true; else throw new JavaException(new IllegalArgumentException(what.toString())); } case "class [S": { PTerm value = new Int(Array.getInt(obj, index.intValue())); if (unify(what, value)) return true; else throw new JavaException(new IllegalArgumentException(what.toString())); } default: throw new JavaException(new Exception()); } } catch (Exception ex) { // ex.printStackTrace(); throw new JavaException(ex); } }
From source file:org.freedesktop.dbus.Message.java
/** * Appends a value to the message./*from ww w.j a v a2s .co m*/ * The type of the value is read from a D-Bus signature and used to marshall * the value. * * @param sigb * A buffer of the D-Bus signature. * @param sigofs * The offset into the signature corresponding to this value. * @param data * The value to marshall. * @return The offset into the signature of the end of this value's type. */ @SuppressWarnings("unchecked") private int appendone(byte[] sigb, int sigofs, Object data) throws DBusException { try { int i = sigofs; if (log.isTraceEnabled()) { log.trace(this.bytecounter); log.trace("Appending type: " + ((char) sigb[i]) + " value: " + data); } // pad to the alignment of this type. pad(sigb[i]); switch (sigb[i]) { case ArgumentType.BYTE: appendByte(((Number) data).byteValue()); break; case ArgumentType.BOOLEAN: appendint(((Boolean) data).booleanValue() ? 1 : 0, 4); break; case ArgumentType.DOUBLE: long l = Double.doubleToLongBits(((Number) data).doubleValue()); appendint(l, 8); break; case ArgumentType.FLOAT: int rf = Float.floatToIntBits(((Number) data).floatValue()); appendint(rf, 4); break; case ArgumentType.UINT32: appendint(((Number) data).longValue(), 4); break; case ArgumentType.INT64: appendint(((Number) data).longValue(), 8); break; case ArgumentType.UINT64: if (this.big) { appendint(((UInt64) data).top(), 4); appendint(((UInt64) data).bottom(), 4); } else { appendint(((UInt64) data).bottom(), 4); appendint(((UInt64) data).top(), 4); } break; case ArgumentType.INT32: appendint(((Number) data).intValue(), 4); break; case ArgumentType.UINT16: appendint(((Number) data).intValue(), 2); break; case ArgumentType.INT16: appendint(((Number) data).shortValue(), 2); break; case ArgumentType.STRING: case ArgumentType.OBJECT_PATH: // Strings are marshalled as a UInt32 with the length, // followed by the String, followed by a null byte. String payload = data.toString(); byte[] payloadbytes = null; try { payloadbytes = payload.getBytes("UTF-8"); } catch (UnsupportedEncodingException UEe) { throw new DBusException("System does not support UTF-8 encoding", UEe); } if (log.isTraceEnabled()) { log.trace("Appending String of length " + payloadbytes.length); } appendint(payloadbytes.length, 4); appendBytes(payloadbytes); appendBytes(padding[1]); // pad(ArgumentType.STRING);? do we need this? break; case ArgumentType.SIGNATURE: // Signatures are marshalled as a byte with the length, // followed by the String, followed by a null byte. // Signatures are generally short, so preallocate the array // for the string, length and null byte. if (data instanceof Type[]) payload = Marshalling.getDBusType((Type[]) data); else payload = (String) data; byte[] pbytes = payload.getBytes(); preallocate(2 + pbytes.length); appendByte((byte) pbytes.length); appendBytes(pbytes); appendByte((byte) 0); break; case ArgumentType.ARRAY: // Arrays are given as a UInt32 for the length in bytes, // padding to the element alignment, then elements in // order. The length is the length from the end of the // initial padding to the end of the last element. if (log.isTraceEnabled()) { if (data instanceof Object[]) log.trace("Appending array: " + Arrays.deepToString((Object[]) data)); } byte[] alen = new byte[4]; appendBytes(alen); pad(sigb[++i]); long c = this.bytecounter; // optimise primatives if (data.getClass().isArray() && data.getClass().getComponentType().isPrimitive()) { byte[] primbuf; int algn = getAlignment(sigb[i]); int len = Array.getLength(data); switch (sigb[i]) { case ArgumentType.BYTE: primbuf = (byte[]) data; break; case ArgumentType.INT16: case ArgumentType.INT32: case ArgumentType.INT64: primbuf = new byte[len * algn]; for (int j = 0, k = 0; j < len; j++, k += algn) marshallint(Array.getLong(data, j), primbuf, k, algn); break; case ArgumentType.BOOLEAN: primbuf = new byte[len * algn]; for (int j = 0, k = 0; j < len; j++, k += algn) marshallint(Array.getBoolean(data, j) ? 1 : 0, primbuf, k, algn); break; case ArgumentType.DOUBLE: primbuf = new byte[len * algn]; if (data instanceof float[]) for (int j = 0, k = 0; j < len; j++, k += algn) marshallint(Double.doubleToRawLongBits(((float[]) data)[j]), primbuf, k, algn); else for (int j = 0, k = 0; j < len; j++, k += algn) marshallint(Double.doubleToRawLongBits(((double[]) data)[j]), primbuf, k, algn); break; case ArgumentType.FLOAT: primbuf = new byte[len * algn]; for (int j = 0, k = 0; j < len; j++, k += algn) marshallint(Float.floatToRawIntBits(((float[]) data)[j]), primbuf, k, algn); break; default: throw new MarshallingException("Primative array being sent as non-primative array."); } appendBytes(primbuf); } else if (data instanceof List) { Object[] contents = ((List<Object>) data).toArray(); int diff = i; ensureBuffers(contents.length * 4); for (Object o : contents) diff = appendone(sigb, i, o); i = diff; } else if (data instanceof Map) { int diff = i; ensureBuffers(((Map<Object, Object>) data).size() * 6); for (Map.Entry<Object, Object> o : ((Map<Object, Object>) data).entrySet()) diff = appendone(sigb, i, o); if (i == diff) { // advance the type parser even on 0-size arrays. Vector<Type> temp = new Vector<>(); byte[] temp2 = new byte[sigb.length - diff]; System.arraycopy(sigb, diff, temp2, 0, temp2.length); String temp3 = new String(temp2); int temp4 = Marshalling.getJavaType(temp3, temp, 1); diff += temp4; } i = diff; } else { Object[] contents = (Object[]) data; ensureBuffers(contents.length * 4); int diff = i; for (Object o : contents) diff = appendone(sigb, i, o); i = diff; } if (log.isTraceEnabled()) { log.trace("start: " + c + " end: " + this.bytecounter + " length: " + (this.bytecounter - c)); } marshallint(this.bytecounter - c, alen, 0, 4); break; case ArgumentType.STRUCT1: // Structs are aligned to 8 bytes // and simply contain each element marshalled in order Object[] contents; if (data instanceof Container) contents = ((Container) data).getParameters(); else contents = (Object[]) data; ensureBuffers(contents.length * 4); int j = 0; for (i++; sigb[i] != ArgumentType.STRUCT2; i++) i = appendone(sigb, i, contents[j++]); break; case ArgumentType.DICT_ENTRY1: // Dict entries are the same as structs. if (data instanceof Map.Entry) { i++; i = appendone(sigb, i, ((Map.Entry<Object, Object>) data).getKey()); i++; i = appendone(sigb, i, ((Map.Entry<Object, Object>) data).getValue()); i++; } else { contents = (Object[]) data; j = 0; for (i++; sigb[i] != ArgumentType.DICT_ENTRY2; i++) i = appendone(sigb, i, contents[j++]); } break; case ArgumentType.VARIANT: // Variants are marshalled as a signature // followed by the value. if (data instanceof Variant) { Variant<?> var = (Variant<?>) data; appendone(new byte[] { ArgumentType.SIGNATURE }, 0, var.getSig()); appendone((var.getSig()).getBytes(), 0, var.getValue()); } else if (data instanceof Object[]) { contents = (Object[]) data; appendone(new byte[] { ArgumentType.SIGNATURE }, 0, contents[0]); appendone(((String) contents[0]).getBytes(), 0, contents[1]); } else { String sig = Marshalling.getDBusType(data.getClass())[0]; appendone(new byte[] { ArgumentType.SIGNATURE }, 0, sig); appendone((sig).getBytes(), 0, data); } break; } return i; } catch (ClassCastException CCe) { throw new MarshallingException( String.format("Trying to marshall to unconvertable type (from %s to %s).", data.getClass().getName(), sigb[sigofs]), CCe); } }
From source file:org.jgentleframework.core.interceptor.AnnotationAroundAdviceMethodInterceptor.java
/** * Hash code.//from w w w.ja v a2 s .c o m * * @param proxy * the proxy * @return the int * @throws IllegalArgumentException * the illegal argument exception * @throws IllegalAccessException * the illegal access exception * @throws InvocationTargetException * the invocation target exception */ protected int hashCode(Object proxy) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { Method[] methods = targetClass.getDeclaredMethods(); int hashCode = 0; for (Method method : methods) { Class<?> type = method.getReturnType(); Object value = (Object) this.attributesMapping.get(method); if (value == null) value = method.getDefaultValue(); // if type is primitive type if (type.isPrimitive()) { // ////////////////////////////////// // ////////////////////////////////// hashCode += 127 * method.getName().hashCode() ^ value.hashCode(); } else if (type.isArray()) { Object array = method.invoke(proxy); Class<?> comtype = type.getComponentType(); if (comtype == byte.class || comtype == Byte.class) { byte[] valueArr = new byte[Array.getLength(array)]; for (int i = 0; i < valueArr.length; i++) { valueArr[i] = Array.getByte(array, i); } hashCode += 127 * method.getName().hashCode() ^ Arrays.hashCode(valueArr); } else if (comtype == char.class || comtype == Character.class) { char[] valueArr = new char[Array.getLength(array)]; for (int i = 0; i < valueArr.length; i++) { valueArr[i] = Array.getChar(array, i); } hashCode += 127 * method.getName().hashCode() ^ Arrays.hashCode(valueArr); } else if (comtype == double.class || comtype == Double.class) { double[] valueArr = new double[Array.getLength(array)]; for (int i = 0; i < valueArr.length; i++) { valueArr[i] = Array.getDouble(array, i); } hashCode += 127 * method.getName().hashCode() ^ Arrays.hashCode(valueArr); } else if (comtype == float.class || comtype == Float.class) { float[] valueArr = new float[Array.getLength(array)]; for (int i = 0; i < valueArr.length; i++) { valueArr[i] = Array.getFloat(array, i); } hashCode += 127 * method.getName().hashCode() ^ Arrays.hashCode(valueArr); } else if (comtype == int.class || comtype == Integer.class) { int[] valueArr = new int[Array.getLength(array)]; for (int i = 0; i < valueArr.length; i++) { valueArr[i] = Array.getInt(array, i); } hashCode += 127 * method.getName().hashCode() ^ Arrays.hashCode(valueArr); } else if (comtype == long.class || comtype == Long.class) { long[] valueArr = new long[Array.getLength(array)]; for (int i = 0; i < valueArr.length; i++) { valueArr[i] = Array.getLong(array, i); } hashCode += 127 * method.getName().hashCode() ^ Arrays.hashCode(valueArr); } else if (comtype == short.class || comtype == Short.class) { short[] valueArr = new short[Array.getLength(array)]; for (int i = 0; i < valueArr.length; i++) { valueArr[i] = Array.getShort(array, i); } hashCode += 127 * method.getName().hashCode() ^ Arrays.hashCode(valueArr); } else if (comtype == boolean.class || comtype == Boolean.class) { boolean[] valueArr = new boolean[Array.getLength(array)]; for (int i = 0; i < valueArr.length; i++) { valueArr[i] = Array.getBoolean(array, i); } hashCode += 127 * method.getName().hashCode() ^ Arrays.hashCode(valueArr); } else { Object[] valueArr = new Object[Array.getLength(array)]; for (int i = 0; i < valueArr.length; i++) { valueArr[i] = Array.get(array, i); } hashCode += 127 * method.getName().hashCode() ^ Arrays.hashCode(valueArr); } } else { // Object value = method.invoke(proxy); hashCode += 127 * method.getName().hashCode() ^ value.hashCode(); } } return hashCode; }