List of usage examples for org.apache.commons.lang3 ClassUtils primitiveToWrapper
public static Class<?> primitiveToWrapper(final 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:com.github.erchu.beancp.MapperExecutorSelector.java
private static boolean classEqualsOrWrapper(final Class objectClass, final Class supportedClass) { if (objectClass.isPrimitive()) { Class<?> primitiveTypeWrapper = ClassUtils.primitiveToWrapper(objectClass); return objectClass.equals(supportedClass) || primitiveTypeWrapper.equals(supportedClass); } else if (supportedClass.isPrimitive()) { Class<?> primitiveTypeWrapper = ClassUtils.primitiveToWrapper(supportedClass); return objectClass.equals(supportedClass) || objectClass.equals(primitiveTypeWrapper); } else {// w w w . j av a 2s . c o m return supportedClass.equals(objectClass); } }
From source file:hu.bme.mit.sette.tools.spf.SpfGenerator.java
static String getParameterLiteral(Class<?> javaClass) { if (javaClass.isPrimitive()) { javaClass = ClassUtils.primitiveToWrapper(javaClass); }// w ww. ja v a2 s . co m if (javaClass.equals(Byte.class)) { return "(byte) 1"; } else if (javaClass.equals(Short.class)) { return "(short) 1"; } else if (javaClass.equals(Integer.class)) { return "1"; } else if (javaClass.equals(Long.class)) { return "1L"; } else if (javaClass.equals(Float.class)) { return "1.0f"; } else if (javaClass.equals(Double.class)) { return "1.0"; } else if (javaClass.equals(Boolean.class)) { return "false"; } else if (javaClass.equals(Character.class)) { return "' '"; } else { return "null"; } }
From source file:com.github.erchu.beancp.MapperImpl.java
private <S> Object[] getArrayOfPrimitiveTypeWrapper(Class sourceClass, final S source) throws IllegalArgumentException, NegativeArraySizeException, ArrayIndexOutOfBoundsException { Class<?> arrayElementWrapperClass = ClassUtils.primitiveToWrapper(sourceClass.getComponentType()); int arrayLength = Array.getLength(source); Object[] sourceWrapper = (Object[]) Array.newInstance(arrayElementWrapperClass, arrayLength); for (int i = 0; i < arrayLength; i++) { sourceWrapper[i] = Array.get(source, i); }/*from ww w . ja v a 2s .c o m*/ return sourceWrapper; }
From source file:com.twosigma.beaker.kdb.KdbShell.java
/** * kdb result conversions to standard beaker types. * * TODO it would be better if this was handled by customizing a * serializer module, but I don't see a clean way of doing that. */// w w w . j a va 2 s. co m private Object convert(Object o) { // Convert flips of simple lists into TableDisplays. if (c.t(o) == 98) to_tabledisplay: { Flip f = (Flip) o; // Make sure each column is an array and a type we can handle. List<String> columns = Arrays.asList(f.x); List<String> classes = new ArrayList<>(); List<List<?>> colLists = new ArrayList<>(); for (Object c : f.y) { List<?> values = toList(c); if (values == null) { break to_tabledisplay; } String type = objConv .convertType(ClassUtils.primitiveToWrapper(c.getClass().getComponentType()).getName()); if (type == null) { break to_tabledisplay; } // Special case - convert Dates to nanosecond times. if (BasicObjectSerializer.TYPE_TIME.equals(type)) { List<Long> timestamps = new ArrayList<>(values.size()); for (Object d : values) { timestamps .add(TimeUnit.NANOSECONDS.convert(((Date) d).getTime(), TimeUnit.MILLISECONDS)); } values = timestamps; } classes.add(type); colLists.add(values); } // Columns to rows. int rows = colLists.get(0).size(); List<List<?>> values = new ArrayList<>(); for (int row = 0; row < rows; ++row) { List<Object> rowValues = new ArrayList<>(); for (List<?> col : colLists) { rowValues.add(col.get(row)); } values.add(rowValues); } return new TableDisplay(values, columns, classes); } // Convert Dicts to maps. if (c.t(o) == 99) to_map: { Dict f = (Dict) o; // Special case - keyed tables are dicts of flips. if ((c.t(f.x) == 98) && (c.t(f.y) == 98)) to_table: { Flip keys = (Flip) f.x; Flip cols = (Flip) f.y; return convert(new Flip( new Dict(ArrayUtils.addAll(keys.x, cols.x), ArrayUtils.addAll(keys.y, cols.y)))); } List<?> keys = toList(f.x); if (keys == null) break to_map; List<?> values = toList(f.y); if (values == null) break to_map; Map<Object, Object> map = new HashMap<>(); for (int i = 0; i < values.size(); ++i) { map.put(keys.get(i), values.get(i)); } return map; } // No conversion. return o; }
From source file:com.wavemaker.tools.apidocs.tools.parser.impl.PropertyParserImpl.java
private Property feedPrimitiveProperty(Class<?> type) { Class<?> wrapperType = (type.isPrimitive()) ? ClassUtils.primitiveToWrapper(type) : type; Property property;/*from www . j a v a2 s . com*/ if (Boolean.class.equals(wrapperType)) { property = new BooleanProperty(); } else if (Character.class.equals(wrapperType)) { property = new StringProperty(); ((StringProperty) property).setMaxLength(1); } else if (Byte.class.equals(wrapperType)) { property = new StringProperty(StringProperty.Format.BYTE); } else if (Number.class.isAssignableFrom(wrapperType)) { if (Long.class.equals(wrapperType) || BigInteger.class.equals(wrapperType)) { property = new LongProperty(); } else if (Double.class.equals(wrapperType) || BigDecimal.class.equals(wrapperType)) { property = new DoubleProperty(); } else if (Float.class.equals(wrapperType)) { property = new FloatProperty(); } else { property = new IntegerProperty(); // cases like Integer, Short and any remaining } } else { property = new ObjectProperty(); // cases like Void.class } return property; }
From source file:com.datatorrent.lib.projection.ProjectionOperator.java
/** * addProjectedField: Add field details (name, type, getter and setter) for field with given name * in projectedFields list// w ww. jav a 2 s. c o m */ protected void addProjectedField(String s) { try { Field f = inClazz.getDeclaredField(s); TypeInfo t = new TypeInfo(f.getName(), ClassUtils.primitiveToWrapper(f.getType())); t.getter = PojoUtils.createGetter(inClazz, t.name, t.type); t.setter = PojoUtils.createSetter(projectedClazz, t.name, t.type); projectedFields.add(t); } catch (NoSuchFieldException e) { throw new RuntimeException("Field " + s + " not found in class " + inClazz, e); } }
From source file:com.wrmsr.search.dsl.util.DerivedSuppliers.java
private static void compileGetter(ClassDefinition classDefinition, Map<String, FieldDefinition> classFieldDefinitionMap, java.lang.reflect.Method target, List<TargetParameter> targetParameters) { java.lang.reflect.Type targetReturnType = target.getGenericReturnType(); Class<?> targetReturnClass = (Class<?>) targetReturnType; Class<?> boxedTargetReturnClass = requireNonNull(ClassUtils.primitiveToWrapper(targetReturnClass)); checkArgument(targetReturnClass == boxedTargetReturnClass || targetReturnClass.isPrimitive()); MethodDefinition methodDefinition = classDefinition.declareMethod(a(PUBLIC, FINAL), "get", type(Object.class)); methodDefinition.declareAnnotation(Override.class); Scope scope = methodDefinition.getScope(); BytecodeBlock body = methodDefinition.getBody(); if (targetReturnClass.isPrimitive()) { body.newObject(boxedTargetReturnClass).dup(); }// www . j a v a 2s. c o m for (TargetParameter targetParameter : targetParameters) { body.getVariable(scope.getThis()).getField(classFieldDefinitionMap.get(targetParameter.name)) .invokeInterface(Supplier.class, "get", Object.class) .checkCast(targetParameter.parameterizedBoxedType); Class<?> targetParameterClass = (Class<?>) targetParameter.type; unboxValue(body, targetParameterClass); } body.invokeStatic(target); if (targetReturnClass.isPrimitive()) { body.invokeConstructor(boxedTargetReturnClass, targetReturnClass); } body.retObject(); }
From source file:com.datatorrent.lib.transform.TransformOperator.java
@Override public void activate(Context context) { if (copyMatchingFields) { Field[] declaredFields = outputClass.getDeclaredFields(); for (Field outputField : declaredFields) { String outputFieldName = outputField.getName(); if (!expressionMap.containsKey(outputFieldName)) { try { Field inputField = inputClass.getDeclaredField(outputFieldName); if (inputField.getType() == outputField.getType()) { expressionMap.put(outputFieldName, inputField.getName()); }/*from w ww .j av a 2 s. c o m*/ } catch (NoSuchFieldException e) { continue; } } } } for (Map.Entry<String, String> entry : expressionMap.entrySet()) { String field = entry.getKey(); String expr = entry.getValue(); // Generate output setter Field f; try { f = outputClass.getDeclaredField(field); } catch (NoSuchFieldException e) { throw new RuntimeException("Failed to get output field info", e); } Class c = ClassUtils.primitiveToWrapper(f.getType()); PojoUtils.Setter setter = PojoUtils.createSetter(outputClass, field, c); // Generate evaluated expression Expression expression = PojoUtils.createExpression(inputClass, expr, c, expressionFunctions.toArray(new String[expressionFunctions.size()])); transformationMap.put(setter, expression); } }
From source file:com.datatorrent.contrib.kafka.POJOKafkaOutputOperator.java
private PojoUtils.Getter generateGetterForKeyField() throws NoSuchFieldException, SecurityException { Field f = pojoClass.getDeclaredField(keyField); Class c = ClassUtils.primitiveToWrapper(f.getType()); PojoUtils.Getter classGetter = PojoUtils.createGetter(pojoClass, keyField, c); return classGetter; }
From source file:com.robertsmieja.test.utils.junit.GenericObjectFactory.java
protected static Class<?> convertPrimitiveToWrapperOrReturn(Class<?> primitiveClass) { if (primitiveClass.isPrimitive()) { return ClassUtils.primitiveToWrapper(primitiveClass); }//from w ww . ja v a 2 s . co m return primitiveClass; }