Java Code Examples for org.apache.commons.lang3.ClassUtils#primitiveToWrapper()
The following examples show how to use
org.apache.commons.lang3.ClassUtils#primitiveToWrapper() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Script2Class.java From fastquery with Apache License 2.0 | 6 votes |
/** * 处理脚本中的冒号表达式 * @param script 脚本 * @param method 脚本的所属方法 * @return 被处理后的脚本 */ private static String processParam(String script, Method method) { List<String> names = TypeUtil.matches(script, Placeholder.COLON_REG); for (String paramName : names) { String name = paramName.replace(":", ""); Class<?> oldC = Judge.getParamType(name, method); Class<?> newC = ClassUtils.primitiveToWrapper(oldC); if(oldC == newC) { // 包装类型 如果后面跟着 script = script.replaceAll(paramName, "(("+newC.getName()+")this.getParameter(\""+name+"\"))"); } else { // 基本类型 -> 包装类型 那么就要解包 // 加上解包方法 script = script.replaceAll(paramName, "(("+newC.getName()+")this.getParameter(\""+name+"\"))." + oldC.getName() + "Value()"); } } return script; }
Example 2
Source File: Lang_15_TypeUtils_s.java From coming with MIT License | 5 votes |
/** * <p> Return a map of the type arguments of a class in the context of <code>toClass</code>. </p> * * @param cls the class in question * @param toClass the context class * @param subtypeVarAssigns a map with type variables * @return the map with type arguments */ private static Map<TypeVariable<?>, Type> getTypeArguments(Class<?> cls, Class<?> toClass, Map<TypeVariable<?>, Type> subtypeVarAssigns) { // make sure they're assignable if (!isAssignable(cls, toClass)) { return null; } // can't work with primitives if (cls.isPrimitive()) { // both classes are primitives? if (toClass.isPrimitive()) { // dealing with widening here. No type arguments to be // harvested with these two types. return new HashMap<TypeVariable<?>, Type>(); } // work with wrapper the wrapper class instead of the primitive cls = ClassUtils.primitiveToWrapper(cls); } // create a copy of the incoming map, or an empty one if it's null HashMap<TypeVariable<?>, Type> typeVarAssigns = subtypeVarAssigns == null ? new HashMap<TypeVariable<?>, Type>() : new HashMap<TypeVariable<?>, Type>(subtypeVarAssigns); // has target class been reached? if (cls.getTypeParameters().length > 0 || toClass.equals(cls)) { return typeVarAssigns; } // walk the inheritance hierarchy until the target class is reached return getTypeArguments(getClosestParentType(cls, toClass), toClass, typeVarAssigns); }
Example 3
Source File: ProjectionOperator.java From attic-apex-malhar with Apache License 2.0 | 5 votes |
/** * addRemainderField: Add field details (name, type, getter and setter) for field with given name * in remainderFields list */ protected void addRemainderField(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(remainderClazz, t.name, t.type); remainderFields.add(t); } catch (NoSuchFieldException e) { throw new RuntimeException("Field " + s + " not found in class " + inClazz, e); } }
Example 4
Source File: ProjectionOperator.java From attic-apex-malhar with Apache License 2.0 | 5 votes |
/** * addProjectedField: Add field details (name, type, getter and setter) for field with given name * in projectedFields list */ 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); } }
Example 5
Source File: ParquetFilePOJOReaderTest.java From attic-apex-malhar with Apache License 2.0 | 5 votes |
private Getter generateGettersForField(Class<?> klass, String inputFieldName) throws NoSuchFieldException, SecurityException { Field f = klass.getDeclaredField(inputFieldName); Class c = ClassUtils.primitiveToWrapper(f.getType()); Getter classGetter = PojoUtils.createGetter(klass, inputFieldName, c); return classGetter; }
Example 6
Source File: TypeUtils.java From astor with GNU General Public License v2.0 | 5 votes |
/** * <p> Return a map of the type arguments of a class in the context of <code>toClass</code>. </p> * * @param cls the class in question * @param toClass the context class * @param subtypeVarAssigns a map with type variables * @return the map with type arguments */ private static Map<TypeVariable<?>, Type> getTypeArguments(Class<?> cls, final Class<?> toClass, final Map<TypeVariable<?>, Type> subtypeVarAssigns) { // make sure they're assignable if (!isAssignable(cls, toClass)) { return null; } // can't work with primitives if (cls.isPrimitive()) { // both classes are primitives? if (toClass.isPrimitive()) { // dealing with widening here. No type arguments to be // harvested with these two types. return new HashMap<TypeVariable<?>, Type>(); } // work with wrapper the wrapper class instead of the primitive cls = ClassUtils.primitiveToWrapper(cls); } // create a copy of the incoming map, or an empty one if it's null final HashMap<TypeVariable<?>, Type> typeVarAssigns = subtypeVarAssigns == null ? new HashMap<TypeVariable<?>, Type>() : new HashMap<TypeVariable<?>, Type>(subtypeVarAssigns); // has target class been reached? if (toClass.equals(cls)) { return typeVarAssigns; } // walk the inheritance hierarchy until the target class is reached return getTypeArguments(getClosestParentType(cls, toClass), toClass, typeVarAssigns); }
Example 7
Source File: ParquetFilePOJOReader.java From attic-apex-malhar with Apache License 2.0 | 5 votes |
/** * Use reflection to generate field info values if the user has not provided * the inputs mapping. * * @return String representing the Parquet field name to POJO field name * mapping */ private String generateFieldInfoInputs() { java.lang.reflect.Field[] fields = pojoClass.getDeclaredFields(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < fields.length; i++) { java.lang.reflect.Field f = fields[i]; Class<?> c = ClassUtils.primitiveToWrapper(f.getType()); sb.append(f.getName() + FIELD_SEPARATOR + f.getName() + FIELD_SEPARATOR + c.getSimpleName().toUpperCase() + RECORD_SEPARATOR); } return sb.substring(0, sb.length() - 1); }
Example 8
Source File: POJOEnricher.java From attic-apex-malhar with Apache License 2.0 | 5 votes |
@SuppressWarnings({ "unchecked", "rawtypes" }) private PojoUtils.Getter generateGettersForField(Class<?> klass, String inputFieldName) throws NoSuchFieldException, SecurityException { Field f = klass.getDeclaredField(inputFieldName); Class c = ClassUtils.primitiveToWrapper(f.getType()); return PojoUtils.createGetter(klass, inputFieldName, c); }
Example 9
Source File: POJOEnricher.java From attic-apex-malhar with Apache License 2.0 | 5 votes |
@SuppressWarnings({ "unchecked", "rawtypes" }) private PojoUtils.Setter generateSettersForField(Class<?> klass, String outputFieldName) throws NoSuchFieldException, SecurityException { Field f = klass.getDeclaredField(outputFieldName); Class c = ClassUtils.primitiveToWrapper(f.getType()); return PojoUtils.createSetter(klass, outputFieldName, c); }
Example 10
Source File: TypeUtils.java From astor with GNU General Public License v2.0 | 5 votes |
/** * <p> Return a map of the type arguments of a class in the context of <code>toClass</code>. </p> * * @param cls the class in question * @param toClass the context class * @param subtypeVarAssigns a map with type variables * @return the map with type arguments */ private static Map<TypeVariable<?>, Type> getTypeArguments(Class<?> cls, Class<?> toClass, Map<TypeVariable<?>, Type> subtypeVarAssigns) { // make sure they're assignable if (!isAssignable(cls, toClass)) { return null; } // can't work with primitives if (cls.isPrimitive()) { // both classes are primitives? if (toClass.isPrimitive()) { // dealing with widening here. No type arguments to be // harvested with these two types. return new HashMap<TypeVariable<?>, Type>(); } // work with wrapper the wrapper class instead of the primitive cls = ClassUtils.primitiveToWrapper(cls); } // create a copy of the incoming map, or an empty one if it's null HashMap<TypeVariable<?>, Type> typeVarAssigns = subtypeVarAssigns == null ? new HashMap<TypeVariable<?>, Type>() : new HashMap<TypeVariable<?>, Type>(subtypeVarAssigns); // has target class been reached? if (toClass.equals(cls)) { return typeVarAssigns; } // walk the inheritance hierarchy until the target class is reached return getTypeArguments(getClosestParentType(cls, toClass), toClass, typeVarAssigns); }
Example 11
Source File: AvroToPojo.java From attic-apex-malhar with Apache License 2.0 | 5 votes |
/** * Use reflection to generate field info values if the user has not provided * the inputs mapping * * @return String representing the POJO field to Avro field mapping */ private String generateFieldInfoInputs(Class<?> cls) { java.lang.reflect.Field[] fields = cls.getDeclaredFields(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < fields.length; i++) { java.lang.reflect.Field f = fields[i]; Class<?> c = ClassUtils.primitiveToWrapper(f.getType()); sb.append(f.getName()).append(FIELD_SEPARATOR).append(f.getName()).append(FIELD_SEPARATOR) .append(c.getSimpleName().toUpperCase()).append(RECORD_SEPARATOR); } return sb.substring(0, sb.length() - 1); }
Example 12
Source File: StreamingJsonParser.java From attic-apex-malhar with Apache License 2.0 | 5 votes |
/** * Use reflection to generate field info values if the user has not provided * the inputs mapping * * @return String representing the POJO field to JSON field mapping */ private String generateFieldInfoInputs(Class<?> cls) { java.lang.reflect.Field[] fields = cls.getDeclaredFields(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < fields.length; i++) { java.lang.reflect.Field f = fields[i]; Class<?> c = ClassUtils.primitiveToWrapper(f.getType()); sb.append(f.getName()).append(FIELD_SEPARATOR).append(f.getName()).append(FIELD_SEPARATOR) .append(c.getSimpleName().toUpperCase()).append(RECORD_SEPARATOR); } return sb.substring(0, sb.length() - 1); }
Example 13
Source File: TypeUtils.java From astor with GNU General Public License v2.0 | 5 votes |
/** * <p> Return a map of the type arguments of a class in the context of <code>toClass</code>. </p> * * @param cls the class in question * @param toClass the context class * @param subtypeVarAssigns a map with type variables * @return the map with type arguments */ private static Map<TypeVariable<?>, Type> getTypeArguments(Class<?> cls, Class<?> toClass, Map<TypeVariable<?>, Type> subtypeVarAssigns) { // make sure they're assignable if (!isAssignable(cls, toClass)) { return null; } // can't work with primitives if (cls.isPrimitive()) { // both classes are primitives? if (toClass.isPrimitive()) { // dealing with widening here. No type arguments to be // harvested with these two types. return new HashMap<TypeVariable<?>, Type>(); } // work with wrapper the wrapper class instead of the primitive cls = ClassUtils.primitiveToWrapper(cls); } // create a copy of the incoming map, or an empty one if it's null HashMap<TypeVariable<?>, Type> typeVarAssigns = subtypeVarAssigns == null ? new HashMap<TypeVariable<?>, Type>() : new HashMap<TypeVariable<?>, Type>(subtypeVarAssigns); // has target class been reached? if (toClass.equals(cls)) { return typeVarAssigns; } // walk the inheritance hierarchy until the target class is reached return getTypeArguments(getClosestParentType(cls, toClass), toClass, typeVarAssigns); }
Example 14
Source File: TypeUtils.java From astor with GNU General Public License v2.0 | 5 votes |
/** * <p> </p> * * @param cls * @param toClass * @param subtypeVarAssigns * @return */ private static Map<TypeVariable<?>, Type> getTypeArguments(Class<?> cls, Class<?> toClass, Map<TypeVariable<?>, Type> subtypeVarAssigns) { // make sure they're assignable if (!isAssignable(cls, toClass)) { return null; } // can't work with primitives if (cls.isPrimitive()) { // both classes are primitives? if (toClass.isPrimitive()) { // dealing with widening here. No type arguments to be // harvested with these two types. return new HashMap<TypeVariable<?>, Type>(); } // work with wrapper the wrapper class instead of the primitive cls = ClassUtils.primitiveToWrapper(cls); } // create a copy of the incoming map, or an empty one if it's null HashMap<TypeVariable<?>, Type> typeVarAssigns = subtypeVarAssigns == null ? new HashMap<TypeVariable<?>, Type>() : new HashMap<TypeVariable<?>, Type>(subtypeVarAssigns); // no arguments for the parameters, or target class has been reached if (cls.getTypeParameters().length > 0 || toClass.equals(cls)) { return typeVarAssigns; } // walk the inheritance hierarchy until the target class is reached return getTypeArguments(getClosestParentType(cls, toClass), toClass, typeVarAssigns); }
Example 15
Source File: StdUdfWrapper.java From transport with BSD 2-Clause "Simplified" License | 5 votes |
private Class getJavaTypeForNullability(Type prestoType, boolean nullableArgument) { if (nullableArgument) { return ClassUtils.primitiveToWrapper(prestoType.getJavaType()); } else { return prestoType.getJavaType(); } }
Example 16
Source File: ByteBuddyUtils.java From beam with Apache License 2.0 | 4 votes |
@Override protected Type convertPrimitive(TypeDescriptor<?> type) { return ClassUtils.primitiveToWrapper(type.getRawType()); }
Example 17
Source File: ValueMapInjector.java From sling-org-apache-sling-models-impl with Apache License 2.0 | 4 votes |
@Override public Object getValue(@NotNull Object adaptable, String name, @NotNull Type type, @NotNull AnnotatedElement element, @NotNull DisposalCallbackRegistry callbackRegistry) { if (adaptable == ObjectUtils.NULL) { return null; } ValueMap map = getValueMap(adaptable); if (map == null) { return null; } else if (type instanceof Class<?>) { Class<?> clazz = (Class<?>) type; try { return map.get(name, clazz); } catch (ClassCastException e) { // handle case of primitive/wrapper arrays if (clazz.isArray()) { Class<?> componentType = clazz.getComponentType(); if (componentType.isPrimitive()) { Class<?> wrapper = ClassUtils.primitiveToWrapper(componentType); if (wrapper != componentType) { Object wrapperArray = map.get(name, Array.newInstance(wrapper, 0).getClass()); if (wrapperArray != null) { return unwrapArray(wrapperArray, componentType); } } } else { Class<?> primitiveType = ClassUtils.wrapperToPrimitive(componentType); if (primitiveType != componentType) { Object primitiveArray = map.get(name, Array.newInstance(primitiveType, 0).getClass()); if (primitiveArray != null) { return wrapArray(primitiveArray, componentType); } } } } return null; } } else if (type instanceof ParameterizedType) { // list support ParameterizedType pType = (ParameterizedType) type; if (pType.getActualTypeArguments().length != 1) { return null; } Class<?> collectionType = (Class<?>) pType.getRawType(); if (!(collectionType.equals(Collection.class) || collectionType.equals(List.class))) { return null; } Class<?> itemType = (Class<?>) pType.getActualTypeArguments()[0]; Object array = map.get(name, Array.newInstance(itemType, 0).getClass()); if (array == null) { return null; } return Arrays.asList((Object[]) array); } else { log.debug("ValueMapInjector doesn't support non-class types {}", type); return null; } }
Example 18
Source File: SourcePropertyTemplate.java From dremio-oss with Apache License 2.0 | 4 votes |
private static TemplatePropertyType getFieldType(Field field) { final Class<?> fieldType = ClassUtils.primitiveToWrapper(field.getType()); if (String.class.isAssignableFrom(fieldType)) { return TemplatePropertyType.TEXT; } if (Number.class.isAssignableFrom(fieldType)) { return TemplatePropertyType.NUMBER; } if (Boolean.class.isAssignableFrom(fieldType)) { return TemplatePropertyType.BOOLEAN; } // AuthenticationType = credentials if (AuthenticationType.class.equals(fieldType)) { return TemplatePropertyType.CREDENTIALS; } // handle array types if (Collection.class.isAssignableFrom(fieldType)) { final ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType(); if (parameterizedType.getActualTypeArguments().length == 1) { final Type elementType = parameterizedType.getActualTypeArguments()[0]; if (Host.class.equals(elementType)) { return TemplatePropertyType.HOST_LIST; } if (Property.class.equals(elementType)) { return TemplatePropertyType.PROPERTY_LIST; } if (String.class.equals(elementType)) { return TemplatePropertyType.VALUE_LIST; } } } // handle enums if (fieldType.isEnum()) { return TemplatePropertyType.ENUM; } throw new UnsupportedOperationException(String.format("Found source property [%s] with unsupported type [%s]", field.getName(), fieldType.getTypeName())); }
Example 19
Source File: TypeExtractor.java From Flink-CEPplus with Apache License 2.0 | 4 votes |
/** * Checks if the given field is a valid pojo field: * - it is public * OR * - there are getter and setter methods for the field. * * @param f field to check * @param clazz class of field * @param typeHierarchy type hierarchy for materializing generic types */ private boolean isValidPojoField(Field f, Class<?> clazz, ArrayList<Type> typeHierarchy) { if(Modifier.isPublic(f.getModifiers())) { return true; } else { boolean hasGetter = false, hasSetter = false; final String fieldNameLow = f.getName().toLowerCase().replaceAll("_", ""); Type fieldType = f.getGenericType(); Class<?> fieldTypeWrapper = ClassUtils.primitiveToWrapper(f.getType()); TypeVariable<?> fieldTypeGeneric = null; if(fieldType instanceof TypeVariable) { fieldTypeGeneric = (TypeVariable<?>) fieldType; fieldType = materializeTypeVariable(typeHierarchy, (TypeVariable<?>)fieldType); } for(Method m : clazz.getMethods()) { final String methodNameLow = m.getName().endsWith("_$eq") ? m.getName().toLowerCase().replaceAll("_", "").replaceFirst("\\$eq$", "_\\$eq") : m.getName().toLowerCase().replaceAll("_", ""); // check for getter if( // The name should be "get<FieldName>" or "<fieldName>" (for scala) or "is<fieldName>" for boolean fields. (methodNameLow.equals("get"+fieldNameLow) || methodNameLow.equals("is"+fieldNameLow) || methodNameLow.equals(fieldNameLow)) && // no arguments for the getter m.getParameterTypes().length == 0 && // return type is same as field type (or the generic variant of it) (m.getGenericReturnType().equals( fieldType ) || (fieldTypeWrapper != null && m.getReturnType().equals( fieldTypeWrapper )) || (fieldTypeGeneric != null && m.getGenericReturnType().equals(fieldTypeGeneric)) ) ) { hasGetter = true; } // check for setters (<FieldName>_$eq for scala) if((methodNameLow.equals("set"+fieldNameLow) || methodNameLow.equals(fieldNameLow+"_$eq")) && m.getParameterTypes().length == 1 && // one parameter of the field's type (m.getGenericParameterTypes()[0].equals( fieldType ) || (fieldTypeWrapper != null && m.getParameterTypes()[0].equals( fieldTypeWrapper )) || (fieldTypeGeneric != null && m.getGenericParameterTypes()[0].equals(fieldTypeGeneric) ) )&& // return type is void. m.getReturnType().equals(Void.TYPE) ) { hasSetter = true; } } if(hasGetter && hasSetter) { return true; } else { if(!hasGetter) { LOG.info(clazz+" does not contain a getter for field "+f.getName() ); } if(!hasSetter) { LOG.info(clazz+" does not contain a setter for field "+f.getName() ); } return false; } } }
Example 20
Source File: ConnConfPropertyListView.java From syncope with Apache License 2.0 | 4 votes |
@Override @SuppressWarnings({ "unchecked", "rawtypes" }) protected void populateItem(final ListItem<ConnConfProperty> item) { final ConnConfProperty property = item.getModelObject(); final String label = StringUtils.isBlank(property.getSchema().getDisplayName()) ? property.getSchema().getName() : property.getSchema().getDisplayName(); final FieldPanel<? extends Serializable> field; boolean required = false; boolean isArray = false; if (property.getSchema().isConfidential() || IdMConstants.GUARDED_STRING.equalsIgnoreCase(property.getSchema().getType()) || IdMConstants.GUARDED_BYTE_ARRAY.equalsIgnoreCase(property.getSchema().getType())) { field = new AjaxPasswordFieldPanel("panel", label, new Model<>(), false); ((PasswordTextField) field.getField()).setResetPassword(false); required = property.getSchema().isRequired(); } else { Class<?> propertySchemaClass; try { propertySchemaClass = ClassUtils.getClass(property.getSchema().getType()); if (ClassUtils.isPrimitiveOrWrapper(propertySchemaClass)) { propertySchemaClass = ClassUtils.primitiveToWrapper(propertySchemaClass); } } catch (ClassNotFoundException e) { LOG.error("Error parsing attribute type", e); propertySchemaClass = String.class; } if (ClassUtils.isAssignable(Number.class, propertySchemaClass)) { @SuppressWarnings("unchecked") Class<Number> numberClass = (Class<Number>) propertySchemaClass; field = new AjaxSpinnerFieldPanel.Builder<>().build("panel", label, numberClass, new Model<>()); required = property.getSchema().isRequired(); } else if (ClassUtils.isAssignable(Boolean.class, propertySchemaClass)) { field = new AjaxCheckBoxPanel("panel", label, new Model<>()); } else { field = new AjaxTextFieldPanel("panel", label, new Model<>()); required = property.getSchema().isRequired(); } if (propertySchemaClass.isArray()) { isArray = true; } } field.setIndex(item.getIndex()); field.setTitle(property.getSchema().getHelpMessage(), true); final AbstractFieldPanel<? extends Serializable> fieldPanel; if (isArray) { final MultiFieldPanel multiFieldPanel = new MultiFieldPanel.Builder( new PropertyModel<>(property, "values")).setEventTemplate(true).build("panel", label, field); item.add(multiFieldPanel); fieldPanel = multiFieldPanel; } else { setNewFieldModel(field, property.getValues()); item.add(field); fieldPanel = field; } if (required) { fieldPanel.addRequiredLabel(); } if (withOverridable) { fieldPanel.showExternAction(addCheckboxToggle(property)); } }