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:org.evosuite.testcase.ImportsTestCodeVisitor.java
private void collectFromParameters(Type[] parameterTypes, List<VariableReference> parameters, boolean isGenericMethod, boolean isOverloaded, int startPos) { for (int i = startPos; i < parameters.size(); i++) { Type declaredParamType = parameterTypes[i]; Type actualParamType = parameters.get(i).getType(); getClassName(declaredParamType); getClassName(parameters.get(i)); Class<?> rawParamClass = declaredParamType instanceof WildcardType ? Object.class : GenericTypeReflector.erase(declaredParamType); if (rawParamClass.isPrimitive()) { getClassName(rawParamClass); getClassName(ClassUtils.primitiveToWrapper(rawParamClass)); } else if (isGenericMethod && !(declaredParamType instanceof WildcardType)) { } else if (!GenericClass.isAssignable(declaredParamType, actualParamType)) { if (TypeUtils.isArrayType(declaredParamType) && TypeUtils.isArrayType(actualParamType)) { Class<?> componentClass = GenericTypeReflector.erase(declaredParamType).getComponentType(); if (componentClass.equals(Object.class)) { GenericClass genericComponentClass = new GenericClass(componentClass); if (genericComponentClass.hasWildcardOrTypeVariables()) { // If we are assigning a generic array, then we don't need to cast } else { // If we are assigning a non-generic array, then we do need to cast getClassName(declaredParamType); }//from ww w . java 2 s . c o m } else { //if (!GenericClass.isAssignable(GenericTypeReflector.getArrayComponentType(declaredParamType), GenericTypeReflector.getArrayComponentType(actualParamType))) { getClassName(declaredParamType); } } else if (!(actualParamType instanceof ParameterizedType)) { getClassName(declaredParamType); } } else { // We have to cast between wrappers and primitives in case there // are overloaded signatures. This could be optimized by checking // if there actually is a problem of overloaded signatures GenericClass parameterClass = new GenericClass(declaredParamType); if (parameterClass.isWrapperType() && parameters.get(i).isPrimitive()) { getClassName(declaredParamType); } else if (parameterClass.isPrimitive() && parameters.get(i).isWrapperType()) { getClassName(declaredParamType); } else if (isOverloaded) { // If there is an overloaded method, we need to cast to make sure we use the right version if (!declaredParamType.equals(actualParamType)) { getClassName(declaredParamType); } } } } }
From source file:org.evosuite.testcase.TestCodeVisitor.java
private String getPrimitiveNullCast(Class<?> declaredParamType) { String castString = ""; castString += "(" + getTypeName(declaredParamType) + ") "; castString += "(" + getTypeName(ClassUtils.primitiveToWrapper(declaredParamType)) + ") "; return castString; }
From source file:org.evosuite.utils.generic.GenericTypeInference.java
private void determineExactType(ConstructorStatement constructorStatement) { GenericConstructor constructor = constructorStatement.getConstructor(); logger.debug("Inferring types for: " + constructorStatement.getCode() + " at position " + constructorStatement.getPosition()); Map<TypeVariable<?>, Type> typeMap = constructor.getOwnerClass().getTypeVariableMap(); if (constructor.getOwnerClass().hasTypeVariables()) { // if (!typeMap.isEmpty()) { logger.info("Has types: " + constructor.getOwnerClass()); logger.info("Initial type map: " + typeMap); for (TypeVariable<?> var : typeMap.keySet()) { typeMap.put(var, null); }/*from w w w . j ava 2 s. c om*/ Type[] parameterTypes = constructor.getGenericParameterTypes(); //.getParameterTypes(); List<VariableReference> parameterValues = constructorStatement.getParameterReferences(); determineVariablesFromParameters(parameterValues, parameterTypes, typeMap); for (int pos = constructorStatement.getPosition() + 1; pos < test.size(); pos++) { if (test.getStatement(pos) instanceof MethodStatement) { MethodStatement ms = (MethodStatement) test.getStatement(pos); if (ms.isStatic()) continue; if (!ms.getCallee().equals(constructorStatement.getReturnValue())) continue; logger.info("Found relevant statement: " + ms.getCode()); parameterTypes = ms.getMethod().getGenericParameterTypes(); parameterValues = ms.getParameterReferences(); determineVariablesFromParameters(parameterValues, parameterTypes, typeMap); } } logger.info("Setting types based on map: " + typeMap); GenericClass owner = constructor.getOwnerClass(); List<TypeVariable<?>> variables = owner.getTypeVariables(); List<Type> types = new ArrayList<Type>(); for (TypeVariable<?> var : variables) { Type type = typeMap.get(var); if (type == null) { types.add(new WildcardTypeImpl(TypeUtils.getImplicitBounds(var), new Type[] {})); } else { Class<?> paramClass = GenericTypeReflector.erase(type); if (paramClass.isPrimitive()) { types.add(ClassUtils.primitiveToWrapper(paramClass)); } else { types.add(typeMap.get(var)); } } } constructorStatement.setConstructor(constructor.copyWithNewOwner(owner.getWithParameterTypes(types))); logger.info("New type: " + constructorStatement); updateMethodCallsOfGenericOwner(constructorStatement.getReturnValue()); } else { logger.info("Type map empty"); } }
From source file:org.grouplens.lenskit.data.text.Fields.java
/** * Get a field by name. It first looks up the common fields, and if none of them apply, creates * a reflection-based field./* w w w .j a v a2 s . co m*/ * * @param eb The event builder. * @param name The field name. The name can be suffixed with "?" to make it optional. * @return The field, or `null` if no such field can be defined. */ public static Field byName(Class<? extends EventBuilder> eb, String name) { Field field = commonField(name); if (field != null) { return field; } boolean optional = false; if (name.endsWith("?")) { optional = true; name = name.substring(0, name.length() - 1); } String setterName = "set" + StringUtils.capitalize(name); Method setter = null; Method annotated = null; for (Method m : eb.getMethods()) { FieldName nameAnnot = m.getAnnotation(FieldName.class); if (nameAnnot != null) { if (nameAnnot.value().equals(name)) { annotated = m; } } else if (m.getName().equals(setterName) && !m.isBridge()) { if (setter == null) { setter = m; } else { throw new IllegalArgumentException("Multiple methods named " + setterName); } } } if (annotated != null) { setter = annotated; } if (setter == null) { throw new IllegalArgumentException("No method found for field " + name); } Class<?>[] atypes = setter.getParameterTypes(); if (atypes.length != 1) { throw new IllegalArgumentException("Method " + setter.getName() + " takes too many arguments"); } final Method theSetter = setter; Class<?> atype = atypes[0]; if (atype.isPrimitive()) { atype = ClassUtils.primitiveToWrapper(atype); } StringConverter<Object> convert = StringConvert.INSTANCE.findConverterNoGenerics(atype); if (convert == null) { throw new IllegalArgumentException("Field type " + atypes[0] + " not allowed."); } return new ReflectionField(name, theSetter, eb, atype, convert, optional); }
From source file:org.grouplens.lenskit.symbols.TypedSymbol.java
@SuppressWarnings({ "unchecked", "rawtypes" }) public static synchronized <T> TypedSymbol<T> of(Class<T> type, Symbol sym) { Pair<Class, Symbol> key; if (type.isPrimitive()) { key = Pair.of((Class) ClassUtils.primitiveToWrapper(type), sym); } else {/* w w w .j a v a 2 s . c o m*/ key = Pair.of((Class) type, sym); } TypedSymbol tsym = symbolCache.get(key); if (tsym == null) { if (type.isPrimitive()) { type = (Class<T>) ClassUtils.primitiveToWrapper(type); } tsym = new TypedSymbol<>(type, sym); symbolCache.put(key, tsym); } return tsym; }
From source file:org.jmingo.document.id.IdFieldGenerator.java
private boolean instanceOf(Class<?> cls, Class<?> toClass) { cls = ClassUtils.primitiveToWrapper(cls); toClass = ClassUtils.primitiveToWrapper(toClass); return ClassUtils.isAssignable(cls, toClass); }
From source file:org.lenskit.data.entities.TypedName.java
/** * Create a typed name object./*w w w .ja v a 2 s . com*/ * * @param name The name. * @param type The type. * @return An object encapsulating the specified name and type. */ @SuppressWarnings("unchecked") @Nonnull public static <T> TypedName<T> create(String name, Class<T> type) { Preconditions.checkNotNull(name, "name"); Preconditions.checkNotNull(type, "type"); if (type.isPrimitive()) { type = (Class<T>) ClassUtils.primitiveToWrapper(type); } TypedName<T> attribute = new TypedName<>(name.intern(), type); return (TypedName<T>) FIELD_CACHE.intern(attribute); }
From source file:org.protelis.lang.util.ReflectionUtils.java
private static int computePointsForWrapper(final Class<?> primitive, final Class<?> wrapper) { final Class<?> wrapped = ClassUtils.primitiveToWrapper(primitive); if (wrapped.equals(wrapper)) { return 2; }/*ww w. j ava 2 s . c om*/ if (wrapped.isAssignableFrom(wrapper)) { return 1; } return 0; }
From source file:org.vaadin.maddon.ListContainer.java
@Override public Class<?> getType(Object propertyId) { final Class<?> type = getDynaClass().getDynaProperty(propertyId.toString()).getType(); if (type.isPrimitive()) { // Vaadin can't handle primitive types in _all_ places, so use // wrappers instead. FieldGroup works, but e.g. Table in _editable_ // mode fails for some reason return ClassUtils.primitiveToWrapper(type); }//ww w .j av a 2s. c om return type; }
From source file:org.vaadin.viritin.ListContainer.java
@Override public Class<?> getType(Object propertyId) { final String pName = propertyId.toString(); try {/*w ww . j av a 2 s. c om*/ final DynaProperty dynaProperty = getDynaClass().getDynaProperty(pName); final Class<?> type = dynaProperty.getType(); if (type.isPrimitive()) { // Vaadin can't handle primitive types in _all_ places, so use // wrappers instead. FieldGroup works, but e.g. Table in _editable_ // mode fails for some reason return ClassUtils.primitiveToWrapper(type); } return type; } catch (Exception e) { // If type can't be found via dynaClass, it is most likely // nested/indexed/mapped property try { return getNestedPropertyType(getDynaClass(), pName); } catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException | ClassNotFoundException | NoSuchFieldException ex) { throw new RuntimeException(ex); } } }