Example usage for java.lang.reflect Array getInt

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

Introduction

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

Prototype

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

Source Link

Document

Returns the value of the indexed component in the specified array object, as an int .

Usage

From source file:Main.java

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

    int value = Array.getInt(array, 1);

    System.out.println(value);/*from  www. ja  v a2 s. c o  m*/

}

From source file:MainClass.java

public static void main(String args[]) {
    Object array = Array.newInstance(int.class, 3);

    int length = Array.getLength(array);
    Random generator = new Random(System.currentTimeMillis());
    for (int i = 0; i < length; i++) {
        int random = generator.nextInt();
        Array.setInt(array, i, random);
    }//from w  w  w .  ja va 2s. co  m

    for (int i = 0; i < length; i++) {
        int value = Array.getInt(array, i);
        System.out.println("Position: " + i + ", value: " + value);
    }
}

From source file:ArrayTrouble.java

public static void main(String... args) {
    Object o = Array.newInstance(int.class, 0);
    int[] i = (int[]) o;
    int[] j = new int[0];
    out.format("i.length = %d, j.length = %d, args.length = %d%n", i.length, j.length, args.length);
    Array.getInt(o, 0); // ArrayIndexOutOfBoundsException
}

From source file:MainClass.java

private static void displayArray(Object array) {
    int length = Array.getLength(array);
    for (int i = 0; i < length; i++) {
        int value = Array.getInt(array, i);
        System.out.println("Position: " + i + ", value: " + value);
    }/*from  www  .j  av a  2  s .  c  o m*/
}

From source file:alice.tuprolog.lib.OOLibrary.java

/**
 * Sets the value of the field 'i' with 'what'
 * @param obj_id/*from  ww w.  j  a  va2  s  .  c om*/
 * @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.jgentleframework.core.interceptor.AnnotationAroundAdviceMethodInterceptor.java

/**
 * Hash code./*from  ww w  .j  av  a  2 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;
}