List of usage examples for org.apache.commons.lang3 ClassUtils wrapperToPrimitive
public static Class<?> wrapperToPrimitive(final Class<?> cls)
Converts the specified wrapper class to its corresponding primitive class.
This method is the counter part of primitiveToWrapper() .
From source file:de.openknowledge.domain.SimpleValueObjectBuilder.java
private SimpleValueObjectBuilder(Class<V> valueObjectClass) { ParameterizedType valueObjectSuperclass = (ParameterizedType) valueObjectClass.getGenericSuperclass(); Class<?> simpleClass = (Class<?>) valueObjectSuperclass.getActualTypeArguments()[0]; try {//from w w w . jav a 2s .c o m valueObjectConstructor = valueObjectClass.getConstructor(simpleClass); } catch (NoSuchMethodException ex) { if (ClassUtils.isPrimitiveWrapper(simpleClass)) { try { valueObjectConstructor = valueObjectClass .getConstructor(ClassUtils.wrapperToPrimitive(simpleClass)); } catch (NoSuchMethodException e) { throw new IllegalStateException("Value Object " + valueObjectClass.getName() + " requires " + simpleClass.getSimpleName() + "-Constructor to be used with Converter/Adapter"); } } else { throw new IllegalStateException("Value Object " + valueObjectClass.getName() + " requires " + simpleClass.getSimpleName() + "-Constructor to be used with Converter/Adapter"); } } if (simpleClass.isPrimitive()) { simpleClass = ClassUtils.primitiveToWrapper(simpleClass); } try { simpleValueConstructor = simpleClass.getConstructor(String.class); } catch (NoSuchMethodException ex) { throw new IllegalStateException("Value Object simple type " + simpleClass.getName() + " requires String-Constructor to be used with JSF Converter"); } }
From source file:io.neba.core.resourcemodels.metadata.MappedFieldMetaData.java
/** * Whether a property cannot be represented by a resource but must stem * from a value map representing the properties of a resource. *//*w w w . j a v a 2 s .c o m*/ private static boolean isPropertyType(Class<?> type) { return type.isPrimitive() || type == String.class || type == Date.class || type == Calendar.class || ClassUtils.wrapperToPrimitive(type) != null; }
From source file:hu.bme.mit.sette.common.model.runner.ParameterType.java
public static ParameterType primitiveFromJavaClass(Class<?> javaClass) { Validate.notNull(javaClass, "The Java class must not be null"); Validate.isTrue(ClassUtils.isPrimitiveOrWrapper(javaClass), "The represented type is not primitive [javaClass: %s]", javaClass.getName()); Class<?> primitiveClass; if (javaClass.isPrimitive()) { primitiveClass = javaClass;/*from ww w. java 2 s .c om*/ } else { primitiveClass = ClassUtils.wrapperToPrimitive(javaClass); } Validate.isTrue(primitiveClass != void.class, "the parameter type must not be void [javaClass: %s]", javaClass.getName()); return fromString(primitiveClass.getCanonicalName()); }
From source file:moe.encode.airblock.commands.arguments.types.PrimitiveParser.java
@Override public Object convert(Executor executor, ArgumentConverter parser, Type type, String value) { Class<?> cls = ReflectionUtils.toClass(type); if (ClassUtils.isPrimitiveWrapper(cls)) cls = ClassUtils.wrapperToPrimitive(cls); if (cls.equals(boolean.class)) return this.isTrue(executor, value); else if (cls.equals(char.class)) { if (value.length() > 0) throw new NumberFormatException("Character arguments cannot be longer than one characters"); return value.charAt(0); }//from ww w .j a va 2 s . c o m // Get the locale of the user and get a number-format according to it. LocaleResolver resolver = TranslationManager.getResolver(executor); Locale locale; if (resolver != null) locale = resolver.getLocale(); else locale = Locale.ENGLISH; NumberFormat nf = NumberFormat.getNumberInstance(locale); nf.setGroupingUsed(true); // Parse the value. Number result; try { result = nf.parse(value); } catch (ParseException e) { NumberFormatException nfe = new NumberFormatException("Invalid number"); nfe.initCause(e); throw nfe; } // Returns the value in the correct type. if (cls.equals(int.class)) return result.intValue(); else if (cls.equals(float.class)) return result.floatValue(); else if (cls.equals(double.class)) return result.doubleValue(); else if (cls.equals(byte.class)) return result.byteValue(); else if (cls.equals(short.class)) return result.shortValue(); else if (cls.equals(long.class)) return result.longValue(); throw new NumberFormatException("Unknown primitive type."); }
From source file:io.github.pellse.decorator.util.reflection.ReflectionUtils.java
@SuppressWarnings("unchecked") public static <T> Class<T> wrapperToPrimitive(Class<T> wrapperClass) { return Optional.ofNullable((Class<T>) ClassUtils.wrapperToPrimitive(wrapperClass)).orElse(wrapperClass); }
From source file:io.github.wysohn.triggerreactor.tools.ReflectionUtil.java
private static boolean compareClass(Class<?> clazz1, Class<?> clazz2) { if (ClassUtils.isPrimitiveWrapper(clazz1)) clazz1 = ClassUtils.wrapperToPrimitive(clazz1); if (ClassUtils.isPrimitiveWrapper(clazz2)) clazz2 = ClassUtils.wrapperToPrimitive(clazz2); if (clazz1 != null) { return clazz1.equals(clazz2); } else if (clazz2 != null) { return clazz2.equals(clazz1); } else {/* ww w . j av a 2s .c o m*/ return true; } }
From source file:com.datatorrent.stram.appdata.AppDataPushAgent.java
private JSONObject getMetricsSchemaData(LogicalPlan.OperatorMeta operatorMeta, Map<String, Object> aggregates) { JSONObject result = new JSONObject(); try {/* w w w .ja va 2 s . com*/ result.put("type", METRICS_SCHEMA); result.put("version", METRICS_SCHEMA_VERSION); result.put("appUser", appContext.getUser()); result.put("appName", dnmgr.getApplicationAttributes().get(DAGContext.APPLICATION_NAME)); result.put("logicalOperatorName", operatorMeta.getName()); MetricAggregatorMeta metricAggregatorMeta = operatorMeta.getMetricAggregatorMeta(); JSONArray valueSchemas = new JSONArray(); for (Map.Entry<String, Object> entry : aggregates.entrySet()) { String metricName = entry.getKey(); Object metricValue = entry.getValue(); JSONObject valueSchema = new JSONObject(); valueSchema.put("name", metricName); Class<?> type = ClassUtils.wrapperToPrimitive(metricValue.getClass()); valueSchema.put("type", type == null ? metricValue.getClass().getCanonicalName() : type); String[] dimensionAggregators = metricAggregatorMeta.getDimensionAggregatorsFor(metricName); if (dimensionAggregators != null) { valueSchema.put("dimensionAggregators", Arrays.asList(dimensionAggregators)); } valueSchemas.put(valueSchema); } result.put("values", valueSchemas); String[] timeBuckets = metricAggregatorMeta.getTimeBuckets(); if (timeBuckets != null) { result.put("timeBuckets", Arrays.asList(timeBuckets)); } } catch (JSONException ex) { throw new RuntimeException(ex); } return result; }
From source file:com.link_intersystems.lang.Conversions.java
/** * Unboxing conversion converts values of reference type to corresponding * values of primitive type. Specifically, the following 8 conversion are * called the unboxing conversions:/*from w w w. java 2s . c om*/ * <ul> * <li>From type Boolean to type boolean</li> * <li>From type Byte to type byte</li> * <li>From type Character to type char</li> * <li>From type Short to type short</li> * <li>From type Integer to type int</li> * <li>From type Long to type long</li> * <li>From type Float to type float</li> * <li>From type Double to type double</li> * </ul> * * @param primitiveWrapper * @return the primitive type for the primitiveWrapper or null if parameter * primitiveWrapper is not a primitive wrapper type. * @since 1.0.0.0 */ public static Class<?> getUnboxingConversion(Class<?> primitiveWrapper) { Class<?> primitive = ClassUtils.wrapperToPrimitive(primitiveWrapper); return primitive; }
From source file:de.hasait.clap.CLAP.java
private void initDefaultConverters() { try {/*from www . j a v a 2 s . com*/ final CLAPConverter<String> stringConverter = new CLAPConverter<String>() { @Override public String convert(final String pInput) { return pInput; } }; addConverter(String.class, stringConverter); final CLAPConverter<Boolean> booleanConverter = new CLAPConverter<Boolean>() { @Override public Boolean convert(final String pInput) { if (pInput.equalsIgnoreCase("true")) { //$NON-NLS-1$ return true; } if (pInput.equalsIgnoreCase("false")) { //$NON-NLS-1$ return false; } if (pInput.equalsIgnoreCase("yes")) { //$NON-NLS-1$ return true; } if (pInput.equalsIgnoreCase("no")) { //$NON-NLS-1$ return false; } if (pInput.equalsIgnoreCase("on")) { //$NON-NLS-1$ return true; } if (pInput.equalsIgnoreCase("off")) { //$NON-NLS-1$ return false; } if (pInput.equalsIgnoreCase("enable")) { //$NON-NLS-1$ return true; } if (pInput.equalsIgnoreCase("disable")) { //$NON-NLS-1$ return false; } throw new RuntimeException(pInput); } }; addConverter(Boolean.class, booleanConverter); addConverter(Boolean.TYPE, booleanConverter); final Class<?>[] someWrapperClasses = new Class<?>[] { Byte.class, Short.class, Integer.class, Long.class, Float.class, Double.class }; for (int i = 0; i < someWrapperClasses.length; i++) { final Class<?> wrapperClass = someWrapperClasses[i]; addStringConstructorConverter(wrapperClass); final Class<?> primitiveClass = ClassUtils.wrapperToPrimitive(wrapperClass); if (primitiveClass != null) { addPrimitiveConverter(wrapperClass, primitiveClass); } } } catch (final Exception e) { throw new RuntimeException(e); } }
From source file:org.apache.sling.contextaware.config.spi.metadata.PropertyMetadata.java
private static Class<?> typeToPrimitive(Class<?> clazz) { if (clazz != String.class && !clazz.isPrimitive()) { Class<?> type = ClassUtils.wrapperToPrimitive(clazz); if (type != null) { return type; }// w w w . j ava2 s . c o m } return clazz; }