Example usage for java.lang.reflect Array getByte

List of usage examples for java.lang.reflect Array getByte

Introduction

In this page you can find the example usage for java.lang.reflect Array getByte.

Prototype

public static native byte getByte(Object array, int index)
        throws IllegalArgumentException, ArrayIndexOutOfBoundsException;

Source Link

Document

Returns the value of the indexed component in the specified array object, as a byte .

Usage

From source file:Main.java

public static void main(String args[]) {
    byte[] array = new byte[] { 1, 2, 3 };

    byte value = Array.getByte(array, 1);

    System.out.println(value);/* w w w.  j a va  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 ww.  j a va  2  s .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.apache.hawq.pxf.service.BridgeOutputBuilder.java

/**
 * Fills one GPDBWritable field./*from  ww  w.  ja  v a  2  s .  c om*/
 *
 * @param oneField field
 * @param colIdx column index
 * @throws BadRecordException if field type is not supported or doesn't
 *             match the schema
 */
void fillOneGPDBWritableField(OneField oneField, int colIdx) throws BadRecordException {
    int type = oneField.type;
    Object val = oneField.val;
    GPDBWritable gpdbOutput = (GPDBWritable) output;
    try {
        switch (DataType.get(type)) {
        case INTEGER:
            gpdbOutput.setInt(colIdx, (Integer) val);
            break;
        case FLOAT8:
            gpdbOutput.setDouble(colIdx, (Double) val);
            break;
        case REAL:
            gpdbOutput.setFloat(colIdx, (Float) val);
            break;
        case BIGINT:
            gpdbOutput.setLong(colIdx, (Long) val);
            break;
        case SMALLINT:
            gpdbOutput.setShort(colIdx, (Short) val);
            break;
        case BOOLEAN:
            gpdbOutput.setBoolean(colIdx, (Boolean) val);
            break;
        case BYTEA:
            byte[] bts = null;
            if (val != null) {
                int length = Array.getLength(val);
                bts = new byte[length];
                for (int j = 0; j < length; j++) {
                    bts[j] = Array.getByte(val, j);
                }
            }
            gpdbOutput.setBytes(colIdx, bts);
            break;
        case VARCHAR:
        case BPCHAR:
        case CHAR:
        case TEXT:
        case NUMERIC:
        case TIMESTAMP:
        case DATE:
            gpdbOutput.setString(colIdx, ObjectUtils.toString(val, null));
            break;
        default:
            String valClassName = (val != null) ? val.getClass().getSimpleName() : null;
            throw new UnsupportedOperationException(valClassName + " is not supported for HAWQ conversion");
        }
    } catch (TypeMismatchException e) {
        throw new BadRecordException(e);
    }
}

From source file:org.jgentleframework.core.interceptor.AnnotationAroundAdviceMethodInterceptor.java

/**
 * Hash code.//from  w w  w  .java2  s.co 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;
}