Example usage for java.lang Class isPrimitive

List of usage examples for java.lang Class isPrimitive

Introduction

In this page you can find the example usage for java.lang Class isPrimitive.

Prototype

@HotSpotIntrinsicCandidate
public native boolean isPrimitive();

Source Link

Document

Determines if the specified Class object represents a primitive type.

Usage

From source file:org.nekorp.workflow.desktop.view.resource.imp.CostoServicioTableModel.java

@Override
public Class<?> getColumnClass(int columnIndex) {
    Class<?> r = MethodUtils
            .getAccessibleMethod(RegistroCostoVB.class, metodosGet.get(columnIndex), new Class[] {})
            .getReturnType();/*from www  .  j  a  v a  2  s .  c o m*/
    if (r.isPrimitive() && r.getName().equals("boolean")) {
        return Boolean.class;
    }
    return r;
}

From source file:com.liferay.portal.jsonwebservice.JSONWebServiceConfigurator.java

private boolean _isJSONWebServiceClass(Class<?> clazz) {
    if (!clazz.isAnonymousClass() && !clazz.isArray() && !clazz.isEnum() && !clazz.isLocalClass()
            && !clazz.isPrimitive() && !(clazz.isMemberClass() ^ Modifier.isStatic(clazz.getModifiers()))) {

        return true;
    }/*from  w  w  w .j a v a  2  s.  co m*/

    return false;
}

From source file:jp.furplag.util.commons.ObjectUtils.java

/**
 * substitute for {@link java.lang.Class#newInstance()}.
 *
 * @param type the Class object, return false if null.
 * @return empty instance of specified {@link java.lang.Class}.
 * @throws IllegalArgumentException/*from   w  w w .  j av a  2 s.co  m*/
 * @throws InstantiationException
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 * @throws ClassNotFoundException
 * @throws NegativeArraySizeException
 */
@SuppressWarnings("unchecked")
public static <T> T newInstance(final Class<T> type) throws InstantiationException {
    if (type == null)
        return null;
    if (type.isArray())
        return (T) Array.newInstance(type.getComponentType(), 0);
    if (Void.class.equals(ClassUtils.primitiveToWrapper(type))) {
        try {
            Constructor<Void> c = Void.class.getDeclaredConstructor();
            c.setAccessible(true);

            return (T) c.newInstance();
        } catch (SecurityException e) {
        } catch (NoSuchMethodException e) {
        } catch (InvocationTargetException e) {
        } catch (IllegalAccessException e) {
        }

        return null;
    }

    if (type.isInterface()) {
        if (!Collection.class.isAssignableFrom(type))
            throw new InstantiationException(
                    "could not create instance, the type \"" + type.getName() + "\" is an interface.");
        if (List.class.isAssignableFrom(type))
            return (T) Lists.newArrayList();
        if (Map.class.isAssignableFrom(type))
            return (T) Maps.newHashMap();
        if (Set.class.isAssignableFrom(type))
            return (T) Sets.newHashSet();
    }

    if (type.isPrimitive()) {
        if (boolean.class.equals(type))
            return (T) Boolean.FALSE;
        if (char.class.equals(type))
            return (T) Character.valueOf(Character.MIN_VALUE);

        return (T) NumberUtils.valueOf("0", (Class<? extends Number>) type);
    }
    if (ClassUtils.isPrimitiveOrWrapper(type))
        return null;
    if (Modifier.isAbstract(type.getModifiers()))
        throw new InstantiationException(
                "could not create instance, the type \"" + type.getName() + "\" is an abstract class.");

    try {
        Constructor<?> c = type.getDeclaredConstructor();
        c.setAccessible(true);

        return (T) c.newInstance();
    } catch (SecurityException e) {
    } catch (NoSuchMethodException e) {
    } catch (InvocationTargetException e) {
    } catch (IllegalAccessException e) {
    }

    throw new InstantiationException("could not create instance, the default constructor of \"" + type.getName()
            + "()\" is not accessible ( or undefined ).");
}

From source file:eu.sathra.io.IO.java

private Object getValue(JSONArray array, Class<?> clazz) throws Exception {

    Object parsedArray = Array.newInstance(clazz, array.length());

    for (int c = 0; c < array.length(); ++c) {
        if ((clazz.equals(String.class) || clazz.isPrimitive()) && !clazz.equals(float.class)) {
            Array.set(parsedArray, c, array.get(c));
        } else if (clazz.equals(float.class)) {
            Array.set(parsedArray, c, (float) array.getDouble(c));
        } else if (clazz.isArray()) {
            // nested array
            Array.set(parsedArray, c, getValue(array.getJSONArray(c), float.class)); // TODO
        } else {//from   w w w.j  av  a  2s.com
            Array.set(parsedArray, c, load(array.getJSONObject(c), clazz));
        }
    }

    return parsedArray;
}

From source file:net.stuxcrystal.simpledev.commands.arguments.ArgumentList.java

/**
 * Returns the converted argument at the given index.<p />
 *
 * The index argument has some additional features: If the index is greater or equals 0 the default
 * Java&trade;-Indexing of Arrays. If the index is under zero, the index is counted from
 * the last item on.<p />//from ww w.j av a 2  s  .com
 *
 * If the given argument was not found or couldn't be converted, null will be returned. If the
 * given class is the Class-instance for a primitive type, a NumberFormatException will be thrown
 * if the number couldn't be parsed.
 *
 * @param index The index of the argument.
 * @param cls   The type of the argument.
 * @return The argument passed to the command.
 * @throws NumberFormatException          If the parsing of the argument failed.
 * @throws IllegalArgumentException       The given type is not supported.
 * @throws ArrayIndexOutOfBoundsException If the index is invalid.
 */
@Override
@SuppressWarnings("unchecked")
public <T> T get(int index, Class<? extends T> cls) {
    Class<?> clazz = cls;

    int preIndex = index;
    index = this.getRealIndex(index);
    if (index == -1)
        throw new IndexOutOfBoundsException(this.outOfBoundsMsg(preIndex));

    // Make the Class-Instance the Class-Reference to the Wrapper-Type.
    if (clazz.isPrimitive()) {
        clazz = PrimitiveType.wrap((Class<?>) cls);
    }

    // Use the current argument handler.
    ArgumentHandler handler = this.handler.getArgumentHandler();

    // Make sure the handler supports the given type.
    if (!handler.supportsType(clazz)) {
        throw new NumberFormatException("Unsupported type: " + cls);
    }

    // Convert and return the specified result.
    return (T) handler.convertType(arguments[index], clazz, this.executor, this.handler.getServerBackend());
}

From source file:org.amplafi.flow.flowproperty.FixedFlowPropertyValueProvider.java

public FixedFlowPropertyValueProvider(Class<?> defaultClass) {
    ApplicationIllegalArgumentException.notNull(defaultClass, "Fixed defaultClass cannot be null.");
    ApplicationIllegalArgumentException.valid(
            !defaultClass.isAnnotation() && !defaultClass.isInterface() && !defaultClass.isPrimitive(),
            defaultClass, ": cannot be instantiated.");
    this.defaultObject = null;
    this.defaultClass = defaultClass;
}

From source file:net.sourceforge.vulcan.web.struts.forms.PluginConfigForm.java

private boolean isPrimitive(Class<?> cls) {
    if (cls.isPrimitive()) {
        return true;
    } else if ("java.lang".equals(cls.getPackage().getName())) {
        return true;
    } else if (Date.class.isAssignableFrom(cls)) {
        return true;
    }/*from w ww .  java 2 s  .co  m*/

    return false;
}

From source file:com.stehno.sjdbcx.reflection.extractor.RowMapperExtractor.java

public RowMapper extract(final Method method) {
    final com.stehno.sjdbcx.annotation.RowMapper mapper = AnnotationUtils.getAnnotation(method,
            com.stehno.sjdbcx.annotation.RowMapper.class);
    if (mapper == null) {
        Class mappedType = method.getReturnType();

        if (Collection.class.isAssignableFrom(mappedType)) {
            mappedType = (Class) ((ParameterizedType) method.getGenericReturnType())
                    .getActualTypeArguments()[0];

        } else if (mappedType.isArray()) {
            throw new UnsupportedOperationException("Auto-mapping for array return types is not yet supported");

        } else if (mappedType.isPrimitive()) {
            if (mappedType == int.class || mappedType == long.class) {
                return new SingleColumnRowMapper();

            } else if (mappedType == boolean.class) {
                return new RowMapper<Boolean>() {
                    @Override//  w w  w  .j av  a  2  s.  c  o m
                    public Boolean mapRow(final ResultSet resultSet, final int i) throws SQLException {
                        return resultSet.getBoolean(1);
                    }
                };
            }
        }

        return new BeanPropertyRowMapper(mappedType);

    } else {
        final String extractKey = mapper.value();
        if (StringUtils.isEmpty(extractKey)) {
            return resolve((Class<RowMapper>) mapper.type());
        } else {
            return resolve(extractKey);
        }
    }
}

From source file:com.icsshs.datatransfer.database.impl.QueryBeanProcessor.java

/**
 * Convert a <code>ResultSet</code> column into an object.  Simple
 * implementations could just call <code>rs.getObject(index)</code> while
 * more complex implementations could perform type manipulation to match
 * the column's type to the bean property type.
 *
 * <p>/*  ww  w .  j av a2s. c o m*/
 * This implementation calls the appropriate <code>ResultSet</code> getter
 * method for the given property type to perform the type conversion.  If
 * the property type doesn't match one of the supported
 * <code>ResultSet</code> types, <code>getObject</code> is called.
 * </p>
 *
 * @param rs The <code>ResultSet</code> currently being processed.  It is
 * positioned on a valid row before being passed into this method.
 *
 * @param index The current column index being processed.
 *
 * @param propType The bean property type that this column needs to be
 * converted into.
 *
 * @throws SQLException if a database access error occurs
 *
 * @return The object from the <code>ResultSet</code> at the given column
 * index after optional type processing or <code>null</code> if the column
 * value was SQL NULL.
 */
protected Object processColumn(ResultSet rs, int index, Class<?> propType) throws SQLException {

    if (!propType.isPrimitive() && rs.getObject(index) == null) {
        return null;
    }

    if (propType.equals(String.class)) {
        return rs.getString(index);

    } else if (propType.equals(Integer.TYPE) || propType.equals(Integer.class)) {
        return Integer.valueOf(rs.getInt(index));

    } else if (propType.equals(Boolean.TYPE) || propType.equals(Boolean.class)) {
        return Boolean.valueOf(rs.getBoolean(index));

    } else if (propType.equals(Long.TYPE) || propType.equals(Long.class)) {
        return Long.valueOf(rs.getLong(index));

    } else if (propType.equals(Double.TYPE) || propType.equals(Double.class)) {
        return Double.valueOf(rs.getDouble(index));

    } else if (propType.equals(Float.TYPE) || propType.equals(Float.class)) {
        return Float.valueOf(rs.getFloat(index));

    } else if (propType.equals(Short.TYPE) || propType.equals(Short.class)) {
        return Short.valueOf(rs.getShort(index));

    } else if (propType.equals(Byte.TYPE) || propType.equals(Byte.class)) {
        return Byte.valueOf(rs.getByte(index));

    } else if (propType.equals(Timestamp.class)) {
        return rs.getTimestamp(index);

    } else if (propType.equals(byte[].class)) {
        return rs.getBytes(index);

    } else {
        return rs.getObject(index);

    }

}

From source file:objenome.util.ClassUtils.java

/**
 * <p>Checks if one {@code Class} can be assigned to a variable of
 * another {@code Class}.</p>//from  w w w  .  j  a va2s.  c o  m
 *
 * <p>Unlike the {@link Class#isAssignableFrom(Class)} method,
 * this method takes into account widenings of primitive classes and
 * {@code null}s.</p>
 *
 * <p>Primitive widenings allow an int to be assigned to a long, float or
 * double. This method returns the correct result for these cases.</p>
 *
 * <p>{@code Null} may be assigned to any reference type. This method
 * will return {@code true} if {@code null} is passed in and the
 * toClass is non-primitive.</p>
 *
 * <p>Specifically, this method tests whether the type represented by the
 * specified {@code Class} parameter can be converted to the type
 * represented by this {@code Class} object via an identity conversion
 * widening primitive or widening reference conversion. See
 * <em><a href="http://docs.oracle.com/javase/specs/">The Java Language Specification</a></em>,
 * sections 5.1.1, 5.1.2 and 5.1.4 for details.</p>
 *
 * @param cls  the Class to check, may be null
 * @param toClass  the Class to try to assign into, returns false if null
 * @param autoboxing  whether to use implicit autoboxing/unboxing between primitives and wrappers
 * @return {@code true} if assignment possible
 */
public static boolean isAssignable(Class<?> cls, Class<?> toClass, boolean autoboxing) {
    if (toClass == null) {
        return false;
    }
    // have to check for null, as isAssignableFrom doesn't
    if (cls == null) {
        return !toClass.isPrimitive();
    }
    //autoboxing:
    if (autoboxing) {
        if (cls.isPrimitive() && !toClass.isPrimitive()) {
            cls = primitiveToWrapper(cls);
            if (cls == null) {
                return false;
            }
        }
        if (toClass.isPrimitive() && !cls.isPrimitive()) {
            cls = wrapperToPrimitive(cls);
            if (cls == null) {
                return false;
            }
        }
    }
    if (cls.equals(toClass)) {
        return true;
    }
    if (cls.isPrimitive()) {
        if (!toClass.isPrimitive()) {
            return false;
        }
        if (Integer.TYPE.equals(cls)) {
            return Long.TYPE.equals(toClass) || Float.TYPE.equals(toClass) || Double.TYPE.equals(toClass);
        }
        if (Long.TYPE.equals(cls)) {
            return Float.TYPE.equals(toClass) || Double.TYPE.equals(toClass);
        }
        if (Boolean.TYPE.equals(cls)) {
            return false;
        }
        if (Double.TYPE.equals(cls)) {
            return false;
        }
        if (Float.TYPE.equals(cls)) {
            return Double.TYPE.equals(toClass);
        }
        if (Character.TYPE.equals(cls)) {
            return Integer.TYPE.equals(toClass) || Long.TYPE.equals(toClass) || Float.TYPE.equals(toClass)
                    || Double.TYPE.equals(toClass);
        }
        if (Short.TYPE.equals(cls)) {
            return Integer.TYPE.equals(toClass) || Long.TYPE.equals(toClass) || Float.TYPE.equals(toClass)
                    || Double.TYPE.equals(toClass);
        }
        if (Byte.TYPE.equals(cls)) {
            return Short.TYPE.equals(toClass) || Integer.TYPE.equals(toClass) || Long.TYPE.equals(toClass)
                    || Float.TYPE.equals(toClass) || Double.TYPE.equals(toClass);
        }
        // should never get here
        return false;
    }
    return toClass.isAssignableFrom(cls);
}