Example usage for java.lang Integer TYPE

List of usage examples for java.lang Integer TYPE

Introduction

In this page you can find the example usage for java.lang Integer TYPE.

Prototype

Class TYPE

To view the source code for java.lang Integer TYPE.

Click Source Link

Document

The Class instance representing the primitive type int .

Usage

From source file:me.jaimegarza.syntax.test.java.TestJavaExpandedScanner.java

@Test
public void test03Runtime() throws ParsingException, AnalysisException, OutputException, MalformedURLException,
        ClassNotFoundException, InstantiationException, IllegalAccessException, SecurityException,
        NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
    generateLanguageFile(packedArgs);// w  w  w.j  ava  2 s  . co m

    File source = new File(tmpLanguageFile);
    File sourceDir = source.getParentFile();
    CompilationResult result = compileJavaFile(source, sourceDir);
    Assert.assertEquals(result.getErrors().length, 0, "Syntax errors found trying to execute");

    URL urls[] = new URL[1];
    urls[0] = sourceDir.toURI().toURL();
    URLClassLoader classLoader = URLClassLoader.newInstance(urls, this.getClass().getClassLoader());
    String className = FilenameUtils.getBaseName(tmpLanguageFile);
    Class<?> clazz = classLoader.loadClass(className);
    String lexicalClassName = className + "$StackElement";
    Class<?> lexicalClazz = classLoader.loadClass(lexicalClassName);
    Object parser = clazz.newInstance();
    Method setVerbose = parser.getClass().getMethod("setVerbose", boolean.class);
    Method init = parser.getClass().getMethod("init");
    Method parse = parser.getClass().getMethod("parse", Integer.TYPE, lexicalClazz);
    Method getValidTokens = parser.getClass().getMethod("getValidTokens");
    Method getTotal = parser.getClass().getMethod("getTotal");
    setVerbose.invoke(parser, true);
    init.invoke(parser);
    for (Parameter p : parameters) {
        int[] tokens = (int[]) getValidTokens.invoke(parser);
        Assert.assertTrue(arrayContains(tokens, p.token), "Token " + p.token + " ain't there");
        Object lexicalValue = lexicalClazz.newInstance();
        Method setNumber = lexicalClazz.getMethod("setNumber", Integer.TYPE);
        setNumber.invoke(lexicalValue, p.value);
        parse.invoke(parser, p.token, lexicalValue);
        Object t = getTotal.invoke(parser);
        Assert.assertEquals(((Integer) t).intValue(), p.result, "Result is not " + p.result);
    }
    Object o = getTotal.invoke(parser);
    Assert.assertTrue(o instanceof Integer);
    Integer i = (Integer) o;
    Assert.assertEquals((int) i, -17, "total does not match");
}

From source file:com.nonninz.robomodel.RoboModel.java

private void loadField(Field field, Cursor query) throws DatabaseNotUpToDateException {
    final Class<?> type = field.getType();
    final boolean wasAccessible = field.isAccessible();
    final int columnIndex = query.getColumnIndex(field.getName());
    field.setAccessible(true);/*from ww w .  j  av a 2s .  co m*/

    /*
     * TODO: There is the potential of a problem here:
     * What happens if the developer changes the type of a field between releases?
     *
     * If he saves first, then the column type will be changed (In the future).
     * If he loads first, we don't know if an Exception will be thrown if the
     * types are incompatible, because it's undocumented in the Cursor documentation.
     */

    try {
        if (type == String.class) {
            field.set(this, query.getString(columnIndex));
        } else if (type == Boolean.TYPE) {
            final boolean value = query.getInt(columnIndex) == 1 ? true : false;
            field.setBoolean(this, value);
        } else if (type == Byte.TYPE) {
            field.setByte(this, (byte) query.getShort(columnIndex));
        } else if (type == Double.TYPE) {
            field.setDouble(this, query.getDouble(columnIndex));
        } else if (type == Float.TYPE) {
            field.setFloat(this, query.getFloat(columnIndex));
        } else if (type == Integer.TYPE) {
            field.setInt(this, query.getInt(columnIndex));
        } else if (type == Long.TYPE) {
            field.setLong(this, query.getLong(columnIndex));
        } else if (type == Short.TYPE) {
            field.setShort(this, query.getShort(columnIndex));
        } else if (type.isEnum()) {
            final String string = query.getString(columnIndex);
            if (string != null && string.length() > 0) {
                final Object[] constants = type.getEnumConstants();
                final Method method = type.getMethod("valueOf", Class.class, String.class);
                final Object value = method.invoke(constants[0], type, string);
                field.set(this, value);
            }
        } else {
            // Try to de-json it (db column must be of type text)
            try {
                final Object value = mMapper.readValue(query.getString(columnIndex), field.getType());
                field.set(this, value);
            } catch (final Exception e) {
                final String msg = String.format("Type %s is not supported for field %s", type,
                        field.getName());
                Ln.w(e, msg);
                throw new IllegalArgumentException(msg);
            }
        }
    } catch (final IllegalAccessException e) {
        final String msg = String.format("Field %s is not accessible", type, field.getName());
        throw new IllegalArgumentException(msg);
    } catch (final NoSuchMethodException e) {
        // Should not happen
        throw new RuntimeException(e);
    } catch (final InvocationTargetException e) {
        // Should not happen
        throw new RuntimeException(e);
    } catch (IllegalStateException e) {
        // This is when there is no column in db, but there is in the model
        throw new DatabaseNotUpToDateException(e);
    } finally {
        field.setAccessible(wasAccessible);
    }
}

From source file:org.briljantframework.data.vector.ConvertTest.java

@Test
public void testTo_convertToInt() throws Exception {
    assertEquals(10, (int) Convert.to(Integer.class, 10));
    assertEquals(10, (int) Convert.to(Integer.class, 10.0));
    assertEquals(10, (int) Convert.to(Integer.class, 10.0f));
    assertEquals(10, (int) Convert.to(Integer.class, 10l));
    assertEquals(10, (int) Convert.to(Integer.class, (short) 10));
    assertEquals(10, (int) Convert.to(Integer.class, (byte) 10));
    assertEquals(10, (int) Convert.to(Integer.class, Complex.valueOf(10)));
    assertEquals(1, (int) Convert.to(Integer.class, Logical.TRUE));
    assertEquals(1, (int) Convert.to(Integer.class, true));

    assertEquals(10, (int) Convert.to(Integer.TYPE, 10));
    assertEquals(10, (int) Convert.to(Integer.TYPE, 10.0));
    assertEquals(10, (int) Convert.to(Integer.TYPE, 10.0f));
    assertEquals(10, (int) Convert.to(Integer.TYPE, 10l));
    assertEquals(10, (int) Convert.to(Integer.TYPE, (short) 10));
    assertEquals(10, (int) Convert.to(Integer.TYPE, (byte) 10));
    assertEquals(10, (int) Convert.to(Integer.TYPE, Complex.valueOf(10)));
    assertEquals(1, (int) Convert.to(Integer.TYPE, Logical.TRUE));
    assertEquals(1, (int) Convert.to(Integer.TYPE, true));
}

From source file:nz.ac.otago.psyanlab.common.designer.program.stage.EditPropPropertiesFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Bundle args = getArguments();/*w w w.  j  a  v a 2 s . com*/
    if (args.containsKey(ARG_PROP)) {
        mProp = args.getParcelable(ARG_PROP);
    } else {
        mProp = mCallbacks.getProp(args.getInt(ARG_PROP_ID)).getProp();
    }

    mFieldMap = new HashMap<String, Field>();
    mViewMap = new HashMap<String, View>();

    // Run through the fields of the prop and build groups and mappings for
    // the fields and views to allow the user to change the property values.
    Field[] fields = mProp.getClass().getFields();
    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];
        PALEPropProperty annotation = field.getAnnotation(PALEPropProperty.class);
        if (annotation != null) {
            String fieldName = annotation.value();
            View view;
            if (field.getType().isAssignableFrom(Integer.TYPE)) {
                try {
                    view = newIntegerInputView(annotation.isSigned(), field.getInt(mProp));
                } catch (IllegalArgumentException e) {
                    throw new RuntimeException("Should never get here.", e);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException("Should never get here.", e);
                }
            } else if (field.getType().isAssignableFrom(Float.TYPE)) {
                try {
                    view = newFloatInputView(annotation.isSigned(), field.getFloat(mProp));
                } catch (IllegalArgumentException e) {
                    throw new RuntimeException("Should never get here.", e);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException("Should never get here.", e);
                }
            } else if (field.getType().isAssignableFrom(String.class)) {
                try {
                    view = newStringInputView((String) field.get(mProp));
                } catch (IllegalArgumentException e) {
                    throw new RuntimeException("Should never get here.", e);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException("Should never get here.", e);
                }
            } else {
                continue;
            }
            mFieldMap.put(fieldName, field);
            mViewMap.put(fieldName, view);

            if (mGroupings.containsKey(annotation.group())) {
                mGroupings.get(annotation.group()).add(fieldName);
            } else {
                ArrayList<String> list = new ArrayList<String>();
                list.add(fieldName);
                mGroupings.put(annotation.group(), list);
            }
        }
    }

    // Initialise grid layout.
    GridLayout grid = new GridLayout(getActivity());
    grid.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));
    grid.setAlignmentMode(GridLayout.ALIGN_BOUNDS);
    grid.setColumnCount(2);
    grid.setUseDefaultMargins(true);

    // Run through groupings of properties adding the created views to the
    // GridLayout in sections.
    for (String group : mGroupings.keySet()) {
        if (!TextUtils.isEmpty(group) && !TextUtils.equals(group, "")) {
            TextView sectionBreak = (TextView) inflater.inflate(R.layout.prop_property_section_break, grid,
                    false);
            sectionBreak.setText(group);
            GridLayout.LayoutParams sectionBreakParams = new GridLayout.LayoutParams();
            sectionBreakParams.columnSpec = GridLayout.spec(0, 2);
            sectionBreak.setLayoutParams(sectionBreakParams);
            grid.addView(sectionBreak);
        }

        for (String name : mGroupings.get(group)) {
            TextView propertyLabel = (TextView) inflater.inflate(R.layout.prop_property_label, grid, false);
            GridLayout.LayoutParams params = new GridLayout.LayoutParams();
            propertyLabel.setLayoutParams(params);
            propertyLabel.setText(getResources().getString(R.string.format_property_label, name));

            View propertyView = mViewMap.get(name);
            params = new GridLayout.LayoutParams();
            params.setGravity(Gravity.FILL_HORIZONTAL);
            propertyView.setLayoutParams(params);

            grid.addView(propertyLabel);
            grid.addView(propertyView);
        }
    }

    return grid;
}

From source file:org.briljantframework.data.resolver.Resolve.java

private static Resolver<Double> initializeDoubleResolver() {
    Resolver<Double> doubleResolver = new Resolver<>(Double.class);
    doubleResolver.put(Number.class, Number::doubleValue);
    doubleResolver.put(Double.class, Number::doubleValue);
    doubleResolver.put(Double.TYPE, Number::doubleValue);
    doubleResolver.put(Float.class, Number::doubleValue);
    doubleResolver.put(Float.TYPE, Number::doubleValue);
    doubleResolver.put(Long.class, Number::doubleValue);
    doubleResolver.put(Long.TYPE, Number::doubleValue);
    doubleResolver.put(Integer.class, Number::doubleValue);
    doubleResolver.put(Integer.TYPE, Number::doubleValue);
    doubleResolver.put(Short.class, Number::doubleValue);
    doubleResolver.put(Short.TYPE, Number::doubleValue);
    doubleResolver.put(Byte.class, Number::doubleValue);
    doubleResolver.put(Byte.TYPE, Number::doubleValue);

    doubleResolver.put(String.class, s -> {
        Number n = toNumber(s);/*from www  .j av a  2 s. c  om*/
        return n != null ? n.doubleValue() : Na.BOXED_DOUBLE;
    });
    return doubleResolver;
}

From source file:com.adobe.acs.commons.mcp.util.AnnotatedFieldDeserializer.java

@SuppressWarnings("squid:S3776")
private static Object convertPrimitiveValue(String value, Class<?> type) throws ParseException {
    if (type.equals(Boolean.class) || type.equals(Boolean.TYPE)) {
        return value.toLowerCase().trim().equals("true");
    } else {//from w w w  .  j a v a  2  s  .  c om
        NumberFormat numberFormat = NumberFormat.getNumberInstance();
        Number num = numberFormat.parse(value);
        if (type.equals(Byte.class) || type.equals(Byte.TYPE)) {
            return num.byteValue();
        } else if (type.equals(Double.class) || type.equals(Double.TYPE)) {
            return num.doubleValue();
        } else if (type.equals(Float.class) || type.equals(Float.TYPE)) {
            return num.floatValue();
        } else if (type.equals(Integer.class) || type.equals(Integer.TYPE)) {
            return num.intValue();
        } else if (type.equals(Long.class) || type.equals(Long.TYPE)) {
            return num.longValue();
        } else if (type.equals(Short.class) || type.equals(Short.TYPE)) {
            return num.shortValue();
        } else {
            return null;
        }
    }
}

From source file:com.link_intersystems.lang.Conversions.java

/**
 * <em>java language specification - 5.1.3 Narrowing Primitive Conversions</em>
 * .//  w  w w. j a va2 s  .  co  m
 *
 * <code>
 * <blockquote>The following 22 specific conversions on primitive types
 *  are called the narrowing primitive conversions:
 * <ul>
 * <li>short to byte or char</li>
 * <li>char to byte or short</li>
 * <li>int to byte, short, or char</li>
 * <li>long to byte, short, char, or int</li>
 * <li>float to byte, short, char, int, or long</li>
 * <li>double to byte, short, char, int, long, or float </li>
 * </blockquote>
 * </code>
 *
 * @param from
 *            the base type
 * @param to
 *            the widening type
 * @return true if from to to is a primitive widening.
 * @since 1.0.0.0
 */
public static boolean isPrimitiveNarrowing(Class<?> from, Class<?> to) {
    Assert.isTrue(Primitives.isPrimitive(from), "mustBeAPrimitive", "form");
    Assert.isTrue(Primitives.isPrimitive(to), "mustBeAPrimitive", "to");
    boolean isPrimitiveNarrowing = false;

    if (isIdentity(from, Double.TYPE)) {
        isPrimitiveNarrowing = isPrimitiveDoubleNarrowing(to);
    } else if (isIdentity(from, Short.TYPE)) {
        isPrimitiveNarrowing = isPrimitiveShortNarrowing(to);
    } else if (isIdentity(from, Character.TYPE)) {
        isPrimitiveNarrowing = isPrimitiveCharacterNarrowing(to);
    } else if (isIdentity(from, Integer.TYPE)) {
        isPrimitiveNarrowing = isPrimitiveIntegerNarrowing(to);
    } else if (isIdentity(from, Long.TYPE)) {
        isPrimitiveNarrowing = isPrimitiveLongNarrowing(to);
    } else if (isIdentity(from, Float.TYPE)) {
        isPrimitiveNarrowing = isPrimitiveFloatNarrowing(to);
    }
    /*
     * must be a byte
     */
    return isPrimitiveNarrowing;
}

From source file:in.hatimi.nosh.support.CmdLineManager.java

private boolean injectString(Object target, Field field, String value) {
    if (field.getType().equals(Boolean.class) || field.getType().equals(Boolean.TYPE)) {
        Boolean vobj = new Boolean(value);
        return injectImpl(target, field, vobj);
    }//w  w  w . j a  v a 2s . c o  m
    if (field.getType().equals(Double.class) || field.getType().equals(Double.TYPE)) {
        Double vobj = Double.valueOf(value);
        return injectImpl(target, field, vobj);
    }
    if (field.getType().equals(Float.class) || field.getType().equals(Float.TYPE)) {
        Float vobj = Float.valueOf(value);
        return injectImpl(target, field, vobj);
    }
    if (field.getType().equals(Long.class) || field.getType().equals(Long.TYPE)) {
        Long vobj = Long.valueOf(value);
        return injectImpl(target, field, vobj);
    }
    if (field.getType().equals(Integer.class) || field.getType().equals(Integer.TYPE)) {
        Integer vobj = Integer.valueOf(value);
        return injectImpl(target, field, vobj);
    }
    if (field.getType().equals(String.class)) {
        return injectImpl(target, field, value);
    }
    if (field.getType().equals(byte[].class)) {
        return injectImpl(target, field, value.getBytes());
    }
    if (field.getType().equals(char[].class)) {
        return injectImpl(target, field, value.toCharArray());
    }
    return false;
}

From source file:com.cyclopsgroup.waterview.utils.TypeUtils.java

private static Map getNonePrimitiveTypeMap() {
    if (nonePrimitiveTypeMap == null) {
        nonePrimitiveTypeMap = new Hashtable();
        nonePrimitiveTypeMap.put(Boolean.TYPE, Boolean.class);
        nonePrimitiveTypeMap.put(Byte.TYPE, Byte.class);
        nonePrimitiveTypeMap.put(Character.TYPE, Character.class);
        nonePrimitiveTypeMap.put(Short.TYPE, Short.class);
        nonePrimitiveTypeMap.put(Integer.TYPE, Integer.class);
        nonePrimitiveTypeMap.put(Long.TYPE, Long.class);
        nonePrimitiveTypeMap.put(Float.TYPE, Float.class);
        nonePrimitiveTypeMap.put(Double.TYPE, Double.class);
    }/*from  w w w  .  j  a  va 2s. com*/

    return nonePrimitiveTypeMap;
}

From source file:com.projity.util.ClassUtils.java

/**
 * Given a type, return a value that signifies that there are multiple values.  This can occur in a dialog which works on multile objects at once.  If type is unknown, a new one is constructed
 * @param clazz/*from   w  w  w  .  j a v a2s.  co  m*/
 * @return
 */
public static Object getMultipleValueForType(Class clazz) {
    if (clazz == String.class)
        return STRING_MULTIPLE_VALUES;
    else if (clazz == Double.class || clazz == Double.TYPE)
        return DOUBLE_MULTIPLE_VALUES;
    else if (clazz == Integer.class || clazz == Integer.TYPE)
        return INTEGER_MULTIPLE_VALUES;
    else if (clazz == Long.class || clazz == Long.TYPE)
        return LONG_MULTIPLE_VALUES;
    else if (clazz == Float.class || clazz == Float.TYPE)
        return FLOAT_MULTIPLE_VALUES;
    else if (clazz == Boolean.class)
        return BOOLEAN_MULTIPLE_VALUES;
    else if (clazz == Rate.class)
        return RATE_MULTIPLE_VALUES;
    else {
        try {
            return clazz.newInstance();
        } catch (InstantiationException e) {
        } catch (IllegalAccessException e) {
        }
        return null;
    }
}