List of usage examples for org.apache.commons.lang ClassUtils primitiveToWrapper
public static Class<?> primitiveToWrapper(Class<?> cls)
Converts the specified primitive Class object to its corresponding wrapper Class object.
NOTE: From v2.2, this method handles Void.TYPE
, returning Void.TYPE
.
From source file:org.carrot2.util.attribute.AttributeDescriptor.java
/** * /* w w w . j a v a 2s.c o m*/ */ AttributeDescriptor(Field field, Object defaultValue, List<Annotation> constraints, AttributeMetadata metadata) { this.attributeField = field; this.attributeDeclaringClassString = field.getDeclaringClass().getName(); this.key = BindableUtils.getKey(field); this.type = ClassUtils.primitiveToWrapper(field.getType()); this.defaultValue = defaultValue; this.constraints = constraints; this.metadata = metadata; this.inputAttribute = field.getAnnotation(Input.class) != null; this.outputAttribute = field.getAnnotation(Output.class) != null; this.requiredAttribute = field.getAnnotation(Required.class) != null; prepareForSerialization(); }
From source file:org.codice.ddf.test.common.matchers.CastingMatchers.java
public static <T, S extends T> Matcher<T> cast(Class<S> clazz, Matcher<? extends S> matcher) { return new TypeSafeMatcher<T>() { private final Class<?> matchableClass = ClassUtils.primitiveToWrapper(clazz); @Override// ww w .j a v a 2 s .c om protected boolean matchesSafely(T item) { if (!matchableClass.isInstance(item)) { return false; } return matcher.matches(clazz.cast(item)); } @Override protected void describeMismatchSafely(T item, Description description) { if (!matchableClass.isInstance(item)) { description.appendValue(item).appendText(" is a ").appendText(item.getClass().getName()); } else { description.appendText("as an instance of ").appendText(clazz.getName()).appendText(" "); matcher.describeMismatch(item, description); } } @Override public void describeTo(Description description) { description.appendText("as an instance of ").appendText(clazz.getName()).appendText(" "); matcher.describeTo(description); } }; }
From source file:org.eclipse.reqcycle.predicates.core.util.PredicatesUtil.java
/** * @param attribute/* w w w.ja va 2 s .c om*/ * @param type * @return The boolean value indicating whether objects of the specified type "type" can be assigned to objects of * this attribute. * */ public static boolean isSubType(final EAttribute attribute, final Class<?> type) { if (attribute == null || type == null) { throw new NullPointerException("The attribute or the type cannot be null."); } EDataType dataType = null; try { dataType = (EDataType) attribute.getEType(); } catch (ClassCastException e) { return false; } Class<?> attributeClass = dataType.getInstanceClass(); if (attributeClass.isPrimitive()) { attributeClass = ClassUtils.primitiveToWrapper(attributeClass); } boolean result = false; if (type.isPrimitive()) { result = attributeClass.isAssignableFrom(ClassUtils.primitiveToWrapper(type)); } else { result = attributeClass.isAssignableFrom(type); } return result; }
From source file:org.eclipse.reqcycle.ui.eattrpropseditor.EAttrPropsEditorPlugin.java
public static String getEditorType(final EClassifier eType) { if (eType instanceof EClass) { throw new UnsupportedOperationException("EClass not yet supported ..."); }/*from w w w. jav a2s . c o m*/ String editorType = eType.getInstanceClassName(); if (eType instanceof EEnum) { editorType = EEnum.class.getName(); } else { try { Class<?> javaClass = ClassUtils.getClass(editorType); if (javaClass.isPrimitive()) { javaClass = ClassUtils.primitiveToWrapper(javaClass); } editorType = javaClass.getName(); } catch (ClassNotFoundException e) { e.printStackTrace(System.err); // Change or remove return null; // Bad bad bad ... } } return editorType; }
From source file:org.eiichiro.bootleg.AbstractRequest.java
/** * Constructs the value of Web endpoint parameter. * Web endpoint parameter declaration falls into the several patterns and * each pattern has the own construction logic as below: * <ol>/*from ww w.j a v a2 s.c o m*/ * <li>Named array type - Unsupported. Always returns <code>null</code>.</li> * <li>Named collection of core value type - Supported. The collection * instance is constructed according to the default implementation type * (See {@code Types#getDefaultImplementationType(Type)}). If the collection * type is not supported (See {@code Types#isSupportedCollection(Type)}), * this method returns <code>null</code>. Each of the collection elements is * constructed from the values that {@code values} has returned as the same * way of the following core value type. If the constructed collection has * no element, this method returns <code>null</code>.</li> * <li>Named collection of user-defined value type - Supported. The * collection instance is constructed according to the default * implementation type (as the same as collection of core value type). Each * of the collection elements is constructed from the values that * {@code values} has returned as the same way of following user-defined * value type. * </li> * <li>Named collection of user-defined object type - Unsupported. Always * returns <code>null</code>.</li> * <li>Named core value type - Supported. The value that {@code value} has * returned is converted to the core value type. If the conversion * failed, returns <code>null</code></li> * <li>Named user-defined value type - Supported. If the value that * {@code value} has returned is assignable to the user-defined value type, * returns it. If the value that {@code value} has returned is * {@code String.class}, this method constructs the user-defined value type * with public constructor that takes one String.class parameter or public * static <code>valueOf(String.class)</code> method. If the conversion * failed, returns <code>null</code>. * </li> * <li>Named user-defined object type - Partially supported. If the value * that {@code value} method has returned is assignable to the user-defined * object type, returns it. Otherwise, returns <code>null</code> (does not * any type conversion). </li> * <li>Not named array type - Unsupported. Always returns <code>null</code>. * </li> * <li>Not named collection of core value type - Unsupported. Always returns * <code>null</code>.</li> * <li>Not named collection of user-defined value type - Unsupported. Always * returns <code>null</code>.</li> * <li>Not named collection of user-defined object type - Unsupported. * Always returns <code>null</code>.</li> * <li>Not named core value type - Unsupported. Always returns * <code>null</code>.</li> * <li>Not named user-defined value type - Unsupported. Always returns * <code>null</code>.</li> * <li>Not named user-defined object type - Supported. First, this method * instantiates the user-defined object instance form public default * constructor, and then constructs each of the instances' fields value * according to the named type construction described above (by invoking * {@code #newParameter(WebContext, Type, String)} with the field * type and field name). If the instantiation is failed, returns <code>null * </code>. * </li> * </ol> * This method is overridable. You can provide your own value construction * to Web endpoint parameter by overriding this method. * * @param type The parameter type. * @param name The parameter name. * @param value The {@code Function} that returns the value corresponding to * the specified name from its own source. * The returned value is converted to appropriate parameter type. * @param values The {@code Function} that returns the value corresponding * to the specified name from its own source, as {@code Collection} view. * @return The value of Web endpoint parameter. */ @SuppressWarnings("unchecked") protected Object parameter(Type type, String name, Function<String, Object> value, Function<String, Collection<Object>> values) { if (name != null && !name.isEmpty()) { // Named parameter construction. if (Types.isArray(type)) { // Named array type. logger.debug("Array type [" + type + "] is not supported in [" + getClass() + "]"); } else if (Types.isCollection(type)) { // Named collection type. if (Types.isSupportedCollection(type)) { Collection<Object> objects = values.apply(name); if (objects == null) { logger.debug("Collection named [" + name + "] not found"); } else { Class<?> elementType = Types.getElementType(type); boolean coreValueType = Types.isCoreValueType(elementType); if (!coreValueType && !Types.isUserDefinedValueType(elementType)) { // Named collection of user-defined object. logger.debug("Collection element type [" + elementType + "] is not supported in [" + getClass() + "]"); } else { try { Class<?> implementationType = Types.getDefaultImplementationType(type); Collection<Object> collection = (Collection<Object>) implementationType .newInstance(); for (Object object : objects) { // Named collection of core value type. // Named collection of user-defined value type. Object convert = (coreValueType) ? convert(object, elementType) : convertUserDefinedValueType(object, elementType); if (convert != null && ClassUtils.primitiveToWrapper(elementType) .isAssignableFrom(convert.getClass())) { collection.add(convert); } else { logger.debug("Parameter [" + convert + "] cannot be converted to [" + elementType + "]"); } } return (!collection.isEmpty()) ? collection : null; } catch (Exception e) { logger.debug("Cannot instantiate [" + Types.getDefaultImplementationType(type) + "] (Default implementation type of [" + type + "])", e); } } } } else { logger.debug("Parameter type [" + type + "] is not supported in [" + getClass() + "]"); } } else if (Types.isCoreValueType(type)) { // Named core value type. Class<?> rawType = Types.getRawType(type); Object object = value.apply(name); if (object == null) { logger.debug("Value named [" + name + "] not found"); } else { Object convert = convert(object, rawType); if (convert != null && ClassUtils.primitiveToWrapper(rawType).isAssignableFrom(convert.getClass())) { return convert; } else { logger.warn("Parameter [" + convert + "] cannot be converted to [" + type + "]"); } } } else if (Types.isUserDefinedValueType(type)) { // Named user-defined value type. Object object = value.apply(name); Class<?> rawType = Types.getRawType(type); if (object == null) { logger.debug("Value named [" + name + "] not found"); } else { Object userDefinedValueType = convertUserDefinedValueType(object, rawType); if (userDefinedValueType == null) { logger.warn("Parameter [" + object + "] cannot be converted to [" + type + "]"); } return userDefinedValueType; } } else { // Named user-defined object type. Object object = value.apply(name); if (object == null) { logger.debug("Value named [" + name + "] not found"); } else if (Types.getRawType(type).isAssignableFrom(object.getClass())) { return object; } else { logger.warn("Parameter [" + object + "] cannot be converted to [" + type + "]"); } } } else { // Non-named parameter construction. if (Types.isArray(type) || Types.isCollection(type) || Types.isCoreValueType(type) || Types.isUserDefinedValueType(type)) { // Not named array type. // Not named collection (of core value type or user-defined object type). // Not named core value type. // Not named user-defined value type. logger.debug("Non-named parameter type [" + type + "] is not supported in [" + getClass() + "]"); } else { // Not named user-defined object type. Class<?> rawType = Types.getRawType(type); try { Object instance = rawType.newInstance(); for (Field field : rawType.getDeclaredFields()) { Object object = parameter(field.getGenericType(), field.getName(), value, values); if (object != null) { field.setAccessible(true); field.set(instance, object); } } return instance; } catch (Exception e) { logger.warn("Cannot instantiate [" + type + "]", e); } } } return null; }
From source file:org.eiichiro.bootleg.json.JSONRequest.java
@SuppressWarnings("unchecked") private Object body(Type type, String name, final JsonElement element) { Function<String, Object> value = new Function<String, Object>() { public Object apply(String name) { if (element instanceof JsonObject) { JsonObject jsonObject = (JsonObject) element; return jsonObject.get(name); } else { return null; }/*from w w w . j a v a 2s. c o m*/ } }; Function<String, Collection<Object>> values = new Function<String, Collection<Object>>() { public Collection<Object> apply(String name) { if (element instanceof JsonObject) { JsonObject jsonObject = (JsonObject) element; JsonElement jsonElement = jsonObject.get(name); if (jsonElement != null && jsonElement.isJsonArray()) { Collection<Object> collection = new ArrayList<Object>(); Iterator<JsonElement> iterator = jsonElement.getAsJsonArray().iterator(); while (iterator.hasNext()) { collection.add(iterator.next()); } return (collection.isEmpty()) ? null : collection; } else { logger.warn("Element [" + name + "] is not a JSON array"); return null; } } else if ((name == null || name.isEmpty()) && element instanceof JsonArray) { JsonArray array = (JsonArray) element; Collection<Object> collection = new ArrayList<Object>(); Iterator<JsonElement> iterator = array.iterator(); while (iterator.hasNext()) { collection.add(iterator.next()); } return (collection.isEmpty()) ? null : collection; } else { return null; } } }; if (name != null && !name.isEmpty()) { if (Types.isCollection(type)) { if (Types.isSupportedCollection(type)) { Class<?> elementType = Types.getElementType(type); if (!Types.isCoreValueType(elementType) && !Types.isUserDefinedValueType(elementType)) { // Named collection of user-defined object type. Collection<Object> jsonElements = values.apply(name); if (jsonElements == null) { logger.debug("Collection named [" + name + "] not found"); return jsonElements; } try { Class<?> implementationType = Types.getDefaultImplementationType(type); Collection<Object> collection = (Collection<Object>) implementationType.newInstance(); try { for (Object jsonElement : jsonElements) { Object instance = elementType.newInstance(); for (Field field : elementType.getDeclaredFields()) { Object object = body(field.getGenericType(), field.getName(), (JsonElement) jsonElement); if (object != null) { field.setAccessible(true); field.set(instance, object); } } collection.add(instance); } return (collection.isEmpty()) ? null : collection; } catch (Exception e) { logger.warn("Cannot instantiate [" + elementType + "] (Collection element type of [" + type + "])", e); } } catch (Exception e) { logger.debug("Cannot instantiate [" + Types.getDefaultImplementationType(type) + "] (Default implementation type of [" + type + "])", e); } return null; } } } else if (!Types.isCoreValueType(type) && !Types.isUserDefinedValueType(type)) { // Named user-defined object type. Class<?> rawType = Types.getRawType(type); Object jsonElement = value.apply(name); try { Object instance = rawType.newInstance(); for (Field field : rawType.getDeclaredFields()) { Object object = body(field.getGenericType(), field.getName(), (JsonElement) jsonElement); if (object != null) { field.setAccessible(true); field.set(instance, object); } } return instance; } catch (Exception e) { logger.warn("Cannot instantiate [" + type + "]", e); } return null; } } else { if (Types.isCollection(type)) { if (Types.isSupportedCollection(type)) { Collection<Object> objects = values.apply(name); if (objects == null) { logger.debug("Posted JSON element is not a JSON array"); return objects; } try { Class<?> implementationType = Types.getDefaultImplementationType(type); Collection<Object> collection = (Collection<Object>) implementationType.newInstance(); Class<?> elementType = Types.getElementType(type); boolean coreValueType = Types.isCoreValueType(elementType); try { if (coreValueType || Types.isUserDefinedValueType(elementType)) { // No-named collection of core value type. // No-named collection of user-defined value type. for (Object object : objects) { Object convert = (coreValueType) ? convert(object, elementType) : convertUserDefinedValueType(object, elementType); if (convert != null && ClassUtils.primitiveToWrapper(elementType) .isAssignableFrom(convert.getClass())) { collection.add(convert); } else { logger.debug("Parameter [" + convert + "] cannot be converted to [" + elementType + "]"); } } } else { // No-named collection of user-defined object type. for (Object jsonElement : objects) { Object instance = elementType.newInstance(); for (Field field : elementType.getDeclaredFields()) { Object object = body(field.getGenericType(), field.getName(), (JsonElement) jsonElement); if (object != null) { field.setAccessible(true); field.set(instance, object); } } collection.add(instance); } } return (collection.isEmpty()) ? null : collection; } catch (Exception e) { logger.warn("Cannot instantiate [" + elementType + "] (Collection element type of [" + type + "])", e); } } catch (Exception e) { logger.debug("Cannot instantiate [" + Types.getDefaultImplementationType(type) + "] (Default implementation type of [" + type + "])", e); } } return null; } } return parameter(type, name, value, values); }
From source file:org.jdto.impl.ValueConversionHelper.java
private static Object nullToPrimitive(Class targetType) { if (targetType == boolean.class) { return false; }// w w w .j a v a 2 s . c o m if (targetType == char.class) { return (char) 0; } Class wrapper = ClassUtils.primitiveToWrapper(targetType); Constructor c = BeanClassUtils.safeGetConstructor(wrapper, strType); return BeanClassUtils.createInstance(wrapper, c, ZERO); }
From source file:org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovyCallSiteSelector.java
private static boolean matches(@Nonnull Class<?>[] parameterTypes, @Nonnull Object[] parameters) { if (parameters.length != parameterTypes.length) { return false; }//www .ja v a 2 s. c o m for (int i = 0; i < parameterTypes.length; i++) { if (parameters[i] == null) { if (parameterTypes[i].isPrimitive()) { return false; } else { // A null argument is assignable to any reference-typed parameter. continue; } } if (parameterTypes[i].isInstance(parameters[i])) { // OK, this parameter matches. continue; } if (parameterTypes[i].isPrimitive() && parameters[i] != null && ClassUtils.primitiveToWrapper(parameterTypes[i]).isInstance(parameters[i])) { // Groovy passes primitive values as objects (for example, passes 0 as Integer(0)) // The prior test fails as int.class.isInstance(new Integer(0)) returns false. continue; } // TODO what about a primitive parameter type and a wrapped parameter? if (parameterTypes[i] == String.class && parameters[i] instanceof GString) { // Cf. SandboxInterceptorTest and class Javadoc. continue; } // Mismatch. return false; } return true; }
From source file:org.kuali.rice.krad.datadictionary.DataDictionary.java
/** * This method gets the property type of the given attributeName when the bo class is a concrete class * * @param boClass//from w w w.j av a 2 s. c o m * @param attributeName * @return property type */ private static Class<?> getAttributeClassWhenBOIsClass(Class<?> boClass, String attributeName) { Object boInstance; try { //KULRICE-11351 should not differentiate between primitive types and their wrappers during DD validation if (boClass.isPrimitive()) { boClass = ClassUtils.primitiveToWrapper(boClass); } boInstance = boClass.newInstance(); } catch (Exception e) { throw new RuntimeException("Unable to instantiate Data Object: " + boClass, e); } // attempt to retrieve the class of the property try { return getLegacyDataAdapter().getPropertyType(boInstance, attributeName); } catch (Exception e) { throw new RuntimeException( "Unable to determine property type for: " + boClass.getName() + "." + attributeName, e); } }
From source file:org.netbeans.orm.converter.compiler.VariableDefSnippet.java
public Class getWrapper(String premitive) throws ClassNotFoundException { return ClassUtils.primitiveToWrapper(ClassUtils.getClass(premitive)); }