List of usage examples for java.lang Class isPrimitive
@HotSpotIntrinsicCandidate public native boolean isPrimitive();
From source file:com.streamsets.datacollector.definition.ConfigDefinitionExtractor.java
@SuppressWarnings("unchecked") List<ErrorMessage> validateConfigDefBean(String configPrefix, Field field, List<String> stageGroups, boolean isComplexField, Object contextMsg) { List<ErrorMessage> errors = new ArrayList<>(); Class klass = field.getType(); try {/* w ww . j ava2s . c o m*/ if (klass.isPrimitive()) { errors.add(new ErrorMessage(DefinitionError.DEF_162, contextMsg, klass.getSimpleName())); } else { klass.getConstructor(); List<String> beanGroups = getGroups(field, stageGroups, contextMsg, errors); errors.addAll(validate(configPrefix, klass, beanGroups, false, true, isComplexField, contextMsg)); } } catch (NoSuchMethodException ex) { errors.add(new ErrorMessage(DefinitionError.DEF_156, contextMsg, klass.getSimpleName())); } return errors; }
From source file:com.evolveum.midpoint.repo.sql.query.restriction.ItemRestriction.java
/** * This method provides transformation from {@link String} value defined in * {@link com.evolveum.midpoint.repo.sql.query.definition.VirtualQueryParam#value()} to real object. Currently only * to simple types and enum values.//from w w w .ja va 2 s . co m * * @param param * @param propPath * @return real value * @throws QueryException */ private Object createQueryParamValue(VirtualQueryParam param, ItemPath propPath) throws QueryException { Class type = param.type(); String value = param.value(); try { if (type.isPrimitive()) { return type.getMethod("valueOf", new Class[] { String.class }).invoke(null, new Object[] { value }); } if (type.isEnum()) { return Enum.valueOf(type, value); } } catch (Exception ex) { throw new QueryException("Couldn't transform virtual query parameter '" + param.name() + "' from String to '" + type + "', reason: " + ex.getMessage(), ex); } throw new QueryException("Couldn't transform virtual query parameter '" + param.name() + "' from String to '" + type + "', it's not yet implemented."); }
From source file:io.fabric8.forge.camel.commands.project.helper.CamelCommandsHelper.java
/** * Converts a java type as a string to a valid input type and returns the class or null if its not supported *//* w ww . j a va 2 s . c o m*/ public static Class<Object> loadValidInputTypes(String javaType, String type) { // we have generics in the javatype, if so remove it so its loadable from a classloader int idx = javaType.indexOf('<'); if (idx > 0) { javaType = javaType.substring(0, idx); } try { Class<Object> clazz = getPrimitiveWrapperClassType(type); if (clazz == null) { clazz = loadPrimitiveWrapperType(javaType); } if (clazz == null) { clazz = loadStringSupportedType(javaType); } if (clazz == null) { try { clazz = (Class<Object>) Class.forName(javaType); } catch (Throwable e) { // its a custom java type so use String as the input type, so you can refer to it using # lookup if ("object".equals(type)) { clazz = loadPrimitiveWrapperType("java.lang.String"); } } } // favor specialized UI for these types if (clazz != null && (clazz.equals(String.class) || clazz.equals(Date.class) || clazz.equals(Boolean.class) || clazz.isPrimitive() || Number.class.isAssignableFrom(clazz))) { return clazz; } // its a custom java type so use String as the input type, so you can refer to it using # lookup if ("object".equals(type)) { clazz = loadPrimitiveWrapperType("java.lang.String"); return clazz; } } catch (Throwable e) { // ignore errors } return null; }
From source file:org.codehaus.griffon.commons.GriffonClassUtils.java
/** * <p>Tests whether or not the left hand type is compatible with the right hand type in Groovy * terms, i.e. can the left type be assigned a value of the right hand type in Groovy.</p> * <p>This handles Java primitive type equivalence and uses isAssignableFrom for all other types, * with a bit of magic for native types and polymorphism i.e. Number assigned an int. * If either parameter is null an exception is thrown</p> * * @param leftType The type of the left hand part of a notional assignment * @param rightType The type of the right hand part of a notional assignment * @return True if values of the right hand type can be assigned in Groovy to variables of the left hand type. *//*from w ww.j av a 2 s . c o m*/ public static boolean isGroovyAssignableFrom(Class leftType, Class rightType) { if (leftType == null) { throw new NullPointerException("Left type is null!"); } else if (rightType == null) { throw new NullPointerException("Right type is null!"); } else if (leftType == Object.class) { return true; } else if (leftType == rightType) { return true; } else { // check for primitive type equivalence Class r = (Class) PRIMITIVE_TYPE_COMPATIBLE_CLASSES.get(leftType); boolean result = r == rightType; if (!result) { // If no primitive <-> wrapper match, it may still be assignable // from polymorphic primitives i.e. Number -> int (AKA Integer) if (rightType.isPrimitive()) { // see if incompatible r = (Class) PRIMITIVE_TYPE_COMPATIBLE_CLASSES.get(rightType); if (r != null) { result = leftType.isAssignableFrom(r); } } else { // Otherwise it may just be assignable using normal Java polymorphism result = leftType.isAssignableFrom(rightType); } } return result; } }
From source file:com.evolveum.midpoint.prism.marshaller.BeanMarshaller.java
public void visit(Object bean, Handler<Object> handler) { if (bean == null) { return;//from www.ja v a2s . c o m } Class<? extends Object> beanClass = bean.getClass(); handler.handle(bean); if (beanClass.isEnum() || beanClass.isPrimitive()) { //nothing more to do return; } // TODO: implement special handling for RawType, if necessary (it has no XmlType annotation any more) XmlType xmlType = beanClass.getAnnotation(XmlType.class); if (xmlType == null) { // no @XmlType annotation, we are not interested to go any deeper return; } List<String> propOrder = inspector.getPropOrder(beanClass); for (String fieldName : propOrder) { Method getter = inspector.findPropertyGetter(beanClass, fieldName); if (getter == null) { throw new IllegalStateException("No getter for field " + fieldName + " in " + beanClass); } Object getterResult = getValue(bean, getter, fieldName); if (getterResult == null) { continue; } if (getterResult instanceof Collection<?>) { Collection col = (Collection) getterResult; if (col.isEmpty()) { continue; } for (Object element : col) { visitValue(element, handler); } } else { visitValue(getterResult, handler); } } }
From source file:es.caib.zkib.jxpath.util.BasicTypeConverter.java
/** * Converts the supplied object to the specified * type. Throws a runtime exception if the conversion is * not possible./*ww w . ja v a2 s. c o m*/ * @param object to convert * @param toType destination class * @return converted object */ public Object convert(Object object, final Class toType) { if (object == null) { return toType.isPrimitive() ? convertNullToPrimitive(toType) : null; } if (toType == Object.class) { if (object instanceof NodeSet) { return convert(((NodeSet) object).getValues(), toType); } if (object instanceof Pointer) { return convert(((Pointer) object).getValue(), toType); } return object; } final Class useType = TypeUtils.wrapPrimitive(toType); Class fromType = object.getClass(); if (useType.isAssignableFrom(fromType)) { return object; } if (fromType.isArray()) { int length = Array.getLength(object); if (useType.isArray()) { Class cType = useType.getComponentType(); Object array = Array.newInstance(cType, length); for (int i = 0; i < length; i++) { Object value = Array.get(object, i); Array.set(array, i, convert(value, cType)); } return array; } if (Collection.class.isAssignableFrom(useType)) { Collection collection = allocateCollection(useType); for (int i = 0; i < length; i++) { collection.add(Array.get(object, i)); } return unmodifiableCollection(collection); } if (length > 0) { Object value = Array.get(object, 0); return convert(value, useType); } return convert("", useType); } if (object instanceof Collection) { int length = ((Collection) object).size(); if (useType.isArray()) { Class cType = useType.getComponentType(); Object array = Array.newInstance(cType, length); Iterator it = ((Collection) object).iterator(); for (int i = 0; i < length; i++) { Object value = it.next(); Array.set(array, i, convert(value, cType)); } return array; } if (Collection.class.isAssignableFrom(useType)) { Collection collection = allocateCollection(useType); collection.addAll((Collection) object); return unmodifiableCollection(collection); } if (length > 0) { Object value; if (object instanceof List) { value = ((List) object).get(0); } else { Iterator it = ((Collection) object).iterator(); value = it.next(); } return convert(value, useType); } return convert("", useType); } if (object instanceof NodeSet) { return convert(((NodeSet) object).getValues(), useType); } if (object instanceof Pointer) { return convert(((Pointer) object).getValue(), useType); } if (useType == String.class) { return object.toString(); } if (object instanceof Boolean) { if (Number.class.isAssignableFrom(useType)) { return allocateNumber(useType, ((Boolean) object).booleanValue() ? 1 : 0); } if ("java.util.concurrent.atomic.AtomicBoolean".equals(useType.getName())) { try { return useType.getConstructor(new Class[] { boolean.class }) .newInstance(new Object[] { object }); } catch (Exception e) { throw new JXPathTypeConversionException(useType.getName(), e); } } } if (object instanceof Number) { double value = ((Number) object).doubleValue(); if (useType == Boolean.class) { return value == 0.0 ? Boolean.FALSE : Boolean.TRUE; } if (Number.class.isAssignableFrom(useType)) { return allocateNumber(useType, value); } } if (object instanceof String) { Object value = convertStringToPrimitive(object, useType); if (value != null || "".equals(object)) { return value; } } Converter converter = ConvertUtils.lookup(useType); if (converter != null) { return converter.convert(useType, object); } throw new JXPathTypeConversionException("Cannot convert " + object.getClass() + " to " + useType); }
From source file:nl.knaw.dans.common.ldap.repo.LdapMapper.java
@SuppressWarnings("unchecked") private Object getSingleValue(Class<?> type, Object o) throws NamingException { Object value = null;//from www. j a va2 s . c o m if (o != null) { if (type.isPrimitive()) { value = getPrimitive(type, (String) o); } else if (type.isEnum()) { value = Enum.valueOf(type.asSubclass(Enum.class), (String) o); } else { value = o; } } return value; }
From source file:com.datos.vfs.util.DelegatingFileSystemOptionsBuilder.java
/** * tries to convert the value and pass it to the given method *///from w w w . j a va2s .c o m private boolean convertValuesAndInvoke(final Method configSetter, final Context ctx) throws FileSystemException { final Class<?>[] parameters = configSetter.getParameterTypes(); if (parameters.length < 2) { return false; } if (!parameters[0].isAssignableFrom(FileSystemOptions.class)) { return false; } final Class<?> valueParameter = parameters[1]; Class<?> type; if (valueParameter.isArray()) { type = valueParameter.getComponentType(); } else { if (ctx.values.length > 1) { return false; } type = valueParameter; } if (type.isPrimitive()) { final Class<?> objectType = PRIMATIVE_TO_OBJECT.get(type.getName()); if (objectType == null) { log.warn(Messages.getString("vfs.provider/config-unexpected-primitive.error", type.getName())); return false; } type = objectType; } final Class<? extends Object> valueClass = ctx.values[0].getClass(); if (type.isAssignableFrom(valueClass)) { // can set value directly invokeSetter(valueParameter, ctx, configSetter, ctx.values); return true; } if (valueClass != String.class) { log.warn(Messages.getString("vfs.provider/config-unexpected-value-class.error", valueClass.getName(), ctx.scheme, ctx.name)); return false; } final Object convertedValues = Array.newInstance(type, ctx.values.length); Constructor<?> valueConstructor; try { valueConstructor = type.getConstructor(STRING_PARAM); } catch (final NoSuchMethodException e) { valueConstructor = null; } if (valueConstructor != null) { // can convert using constructor for (int iterValues = 0; iterValues < ctx.values.length; iterValues++) { try { Array.set(convertedValues, iterValues, valueConstructor.newInstance(new Object[] { ctx.values[iterValues] })); } catch (final InstantiationException e) { throw new FileSystemException(e); } catch (final IllegalAccessException e) { throw new FileSystemException(e); } catch (final InvocationTargetException e) { throw new FileSystemException(e); } } invokeSetter(valueParameter, ctx, configSetter, convertedValues); return true; } Method valueFactory; try { valueFactory = type.getMethod("valueOf", STRING_PARAM); if (!Modifier.isStatic(valueFactory.getModifiers())) { valueFactory = null; } } catch (final NoSuchMethodException e) { valueFactory = null; } if (valueFactory != null) { // can convert using factory method (valueOf) for (int iterValues = 0; iterValues < ctx.values.length; iterValues++) { try { Array.set(convertedValues, iterValues, valueFactory.invoke(null, new Object[] { ctx.values[iterValues] })); } catch (final IllegalAccessException e) { throw new FileSystemException(e); } catch (final InvocationTargetException e) { throw new FileSystemException(e); } } invokeSetter(valueParameter, ctx, configSetter, convertedValues); return true; } return false; }
From source file:com.link_intersystems.lang.reflect.Member2.java
private Object createVarargsArray(Parameter varargsParameter, int varargsParamIndex, Object[] callParameters) { Class2<?> parameterClass2 = varargsParameter.getParameterClass2(); Class<?> type = parameterClass2.getType(); Class<?> componentType = type.getComponentType(); Object varargsArray = Array.newInstance(type.getComponentType(), callParameters.length - varargsParamIndex); if (componentType.isPrimitive()) { PrimitiveArrayCallback primitiveCallback = new PrimitiveArrayCallback(varargsArray); for (int varargsIndex = varargsParamIndex; varargsIndex < callParameters.length; varargsIndex++) { Object object = callParameters[varargsIndex]; Conversions.unbox(object, primitiveCallback); }//from ww w .j ava 2 s .com } else { System.arraycopy(callParameters, varargsParamIndex, varargsArray, 0, Array.getLength(varargsArray)); } return varargsArray; }
From source file:grails.util.GrailsClassUtils.java
/** * <p>Tests whether or not the left hand type is compatible with the right hand type in Groovy * terms, i.e. can the left type be assigned a value of the right hand type in Groovy.</p> * <p>This handles Java primitive type equivalence and uses isAssignableFrom for all other types, * with a bit of magic for native types and polymorphism i.e. Number assigned an int. * If either parameter is null an exception is thrown</p> * * @param leftType The type of the left hand part of a notional assignment * @param rightType The type of the right hand part of a notional assignment * @return true if values of the right hand type can be assigned in Groovy to variables of the left hand type. *///w ww .j a v a 2s .c o m public static boolean isGroovyAssignableFrom(Class<?> leftType, Class<?> rightType) { if (leftType == null) { throw new NullPointerException("Left type is null!"); } if (rightType == null) { throw new NullPointerException("Right type is null!"); } if (leftType == Object.class) { return true; } if (leftType == rightType) { return true; } // check for primitive type equivalence Class<?> r = PRIMITIVE_TYPE_COMPATIBLE_CLASSES.get(leftType); boolean result = r == rightType; if (!result) { // If no primitive <-> wrapper match, it may still be assignable // from polymorphic primitives i.e. Number -> int (AKA Integer) if (rightType.isPrimitive()) { // see if incompatible r = PRIMITIVE_TYPE_COMPATIBLE_CLASSES.get(rightType); if (r != null) { result = leftType.isAssignableFrom(r); } } else { // Otherwise it may just be assignable using normal Java polymorphism result = leftType.isAssignableFrom(rightType); } } return result; }