List of usage examples for java.lang Class isArray
@HotSpotIntrinsicCandidate public native boolean isArray();
From source file:org.nabucco.alfresco.enhScriptEnv.common.script.converter.general.ArrayConverter.java
/** * {@inheritDoc}//from w w w .ja v a 2s .c om */ @Override public int getForJavaConversionConfidence(final Class<?> valueInstanceClass, final Class<?> expectedClass) { final int confidence; if ((valueInstanceClass.isArray() || Collection.class.isAssignableFrom(valueInstanceClass) || Iterable.class.isAssignableFrom(valueInstanceClass)) && expectedClass.isArray()) { confidence = LOW_CONFIDENCE; } else { confidence = LOWEST_CONFIDENCE; } return confidence; }
From source file:com.haulmont.restapi.service.QueriesControllerManager.java
protected Object toObject(Class clazz, String value) throws ParseException { if (clazz.isArray()) { Class componentType = clazz.getComponentType(); JsonParser jsonParser = new JsonParser(); JsonArray jsonArray = jsonParser.parse(value).getAsJsonArray(); List result = new ArrayList(); for (JsonElement jsonElement : jsonArray) { String stringValue = (jsonElement.isJsonPrimitive() && jsonElement.getAsJsonPrimitive().isString()) ? jsonElement.getAsJsonPrimitive().getAsString() : jsonElement.toString(); Object arrayElementValue = toObject(componentType, stringValue); result.add(arrayElementValue); }// w w w . j ava2 s . c om return result; } if (String.class == clazz) return value; if (Integer.class == clazz || Integer.TYPE == clazz || Byte.class == clazz || Byte.TYPE == clazz || Short.class == clazz || Short.TYPE == clazz) return Datatypes.getNN(Integer.class).parse(value); if (Date.class == clazz) { try { return Datatypes.getNN(Date.class).parse(value); } catch (ParseException e) { try { return Datatypes.getNN(java.sql.Date.class).parse(value); } catch (ParseException e1) { return Datatypes.getNN(Time.class).parse(value); } } } if (BigDecimal.class == clazz) return Datatypes.getNN(BigDecimal.class).parse(value); if (Boolean.class == clazz || Boolean.TYPE == clazz) return Datatypes.getNN(Boolean.class).parse(value); if (Long.class == clazz || Long.TYPE == clazz) return Datatypes.getNN(Long.class).parse(value); if (Double.class == clazz || Double.TYPE == clazz || Float.class == clazz || Float.TYPE == clazz) return Datatypes.getNN(Double.class).parse(value); if (UUID.class == clazz) return UUID.fromString(value); throw new IllegalArgumentException("Parameters of type " + clazz.getName() + " are not supported"); }
From source file:com.scit.sling.test.MockValueMap.java
@SuppressWarnings("unchecked") private <T> T convertType(Object o, Class<T> type) { if (o == null) { return null; }/* w w w . j a va 2 s. c o m*/ if (o.getClass().isArray() && type.isArray()) { if (type.getComponentType().isAssignableFrom(o.getClass().getComponentType())) { return (T) o; } else { // we need to convert the elements in the array Object array = Array.newInstance(type.getComponentType(), Array.getLength(o)); for (int i = 0; i < Array.getLength(o); i++) { Array.set(array, i, convertType(Array.get(o, i), type.getComponentType())); } return (T) array; } } if (o.getClass().isAssignableFrom(type)) { return (T) o; } if (String.class.isAssignableFrom(type)) { // Format dates if (o instanceof Calendar) { return (T) formatDate((Calendar) o); } else if (o instanceof Date) { return (T) formatDate((Date) o); } else if (o instanceof DateTime) { return (T) formatDate((DateTime) o); } return (T) String.valueOf(o); } else if (o instanceof DateTime) { DateTime dt = (DateTime) o; if (Calendar.class.isAssignableFrom(type)) { return (T) dt.toCalendar(Locale.getDefault()); } else if (Date.class.isAssignableFrom(type)) { return (T) dt.toDate(); } else if (Number.class.isAssignableFrom(type)) { return convertType(dt.getMillis(), type); } } else if (o instanceof Number && Number.class.isAssignableFrom(type)) { if (Byte.class.isAssignableFrom(type)) { return (T) (Byte) ((Number) o).byteValue(); } else if (Double.class.isAssignableFrom(type)) { return (T) (Double) ((Number) o).doubleValue(); } else if (Float.class.isAssignableFrom(type)) { return (T) (Float) ((Number) o).floatValue(); } else if (Integer.class.isAssignableFrom(type)) { return (T) (Integer) ((Number) o).intValue(); } else if (Long.class.isAssignableFrom(type)) { return (T) (Long) ((Number) o).longValue(); } else if (Short.class.isAssignableFrom(type)) { return (T) (Short) ((Number) o).shortValue(); } else if (BigDecimal.class.isAssignableFrom(type)) { return (T) new BigDecimal(o.toString()); } } else if (o instanceof Number && type.isPrimitive()) { final Number num = (Number) o; if (type == byte.class) { return (T) new Byte(num.byteValue()); } else if (type == double.class) { return (T) new Double(num.doubleValue()); } else if (type == float.class) { return (T) new Float(num.floatValue()); } else if (type == int.class) { return (T) new Integer(num.intValue()); } else if (type == long.class) { return (T) new Long(num.longValue()); } else if (type == short.class) { return (T) new Short(num.shortValue()); } } else if (o instanceof String && Number.class.isAssignableFrom(type)) { if (Byte.class.isAssignableFrom(type)) { return (T) new Byte((String) o); } else if (Double.class.isAssignableFrom(type)) { return (T) new Double((String) o); } else if (Float.class.isAssignableFrom(type)) { return (T) new Float((String) o); } else if (Integer.class.isAssignableFrom(type)) { return (T) new Integer((String) o); } else if (Long.class.isAssignableFrom(type)) { return (T) new Long((String) o); } else if (Short.class.isAssignableFrom(type)) { return (T) new Short((String) o); } else if (BigDecimal.class.isAssignableFrom(type)) { return (T) new BigDecimal((String) o); } } throw new NotImplementedException( "Can't handle conversion from " + o.getClass().getName() + " to " + type.getName()); }
From source file:com.espertech.esper.event.EventTypeUtility.java
public static PropertySetDescriptor getNestableProperties(Map<String, Object> propertiesToAdd, EventAdapterService eventAdapterService, EventTypeNestableGetterFactory factory, EventType[] optionalSuperTypes) throws EPException { List<String> propertyNameList = new ArrayList<String>(); List<EventPropertyDescriptor> propertyDescriptors = new ArrayList<EventPropertyDescriptor>(); Map<String, Object> nestableTypes = new LinkedHashMap<String, Object>(); Map<String, PropertySetDescriptorItem> propertyItems = new HashMap<String, PropertySetDescriptorItem>(); // handle super-types first, such that the order of properties is well-defined from super-type to subtype if (optionalSuperTypes != null) { for (int i = 0; i < optionalSuperTypes.length; i++) { BaseNestableEventType superType = (BaseNestableEventType) optionalSuperTypes[i]; for (String propertyName : superType.getPropertyNames()) { if (nestableTypes.containsKey(propertyName)) { continue; }/*from www. j ava2s.co m*/ propertyNameList.add(propertyName); } for (EventPropertyDescriptor descriptor : superType.getPropertyDescriptors()) { if (nestableTypes.containsKey(descriptor.getPropertyName())) { continue; } propertyDescriptors.add(descriptor); } propertyItems.putAll(superType.propertyItems); nestableTypes.putAll(superType.nestableTypes); } } nestableTypes.putAll(propertiesToAdd); // Initialize getters and names array: at this time we do not care about nested types, // these are handled at the time someone is asking for them for (Map.Entry<String, Object> entry : propertiesToAdd.entrySet()) { if (!(entry.getKey() instanceof String)) { throw new EPException("Invalid type configuration: property name is not a String-type value"); } String name = entry.getKey(); // handle types that are String values if (entry.getValue() instanceof String) { String value = entry.getValue().toString().trim(); Class clazz = JavaClassHelper.getPrimitiveClassForName(value); if (clazz != null) { entry.setValue(clazz); } } if (entry.getValue() instanceof Class) { Class classType = (Class) entry.getValue(); boolean isArray = classType.isArray(); Class componentType = null; if (isArray) { componentType = classType.getComponentType(); } boolean isMapped = JavaClassHelper.isImplementsInterface(classType, Map.class); if (isMapped) { componentType = Object.class; // Cannot determine the type at runtime } boolean isFragment = JavaClassHelper.isFragmentableType(classType); BeanEventType nativeFragmentType = null; FragmentEventType fragmentType = null; if (isFragment) { fragmentType = EventBeanUtility.createNativeFragmentType(classType, null, eventAdapterService); if (fragmentType != null) { nativeFragmentType = (BeanEventType) fragmentType.getFragmentType(); } else { isFragment = false; } } EventPropertyGetter getter = factory.getGetterProperty(name, nativeFragmentType, eventAdapterService); EventPropertyDescriptor descriptor = new EventPropertyDescriptor(name, classType, componentType, false, false, isArray, isMapped, isFragment); PropertySetDescriptorItem item = new PropertySetDescriptorItem(descriptor, classType, getter, fragmentType); propertyNameList.add(name); propertyDescriptors.add(descriptor); propertyItems.put(name, item); continue; } // A null-type is also allowed if (entry.getValue() == null) { EventPropertyGetter getter = factory.getGetterProperty(name, null, null); EventPropertyDescriptor descriptor = new EventPropertyDescriptor(name, null, null, false, false, false, false, false); PropertySetDescriptorItem item = new PropertySetDescriptorItem(descriptor, null, getter, null); propertyNameList.add(name); propertyDescriptors.add(descriptor); propertyItems.put(name, item); continue; } // Add Map itself as a property if (entry.getValue() instanceof Map) { EventPropertyGetter getter = factory.getGetterProperty(name, null, null); EventPropertyDescriptor descriptor = new EventPropertyDescriptor(name, Map.class, null, false, false, false, true, false); PropertySetDescriptorItem item = new PropertySetDescriptorItem(descriptor, Map.class, getter, null); propertyNameList.add(name); propertyDescriptors.add(descriptor); propertyItems.put(name, item); continue; } if (entry.getValue() instanceof EventType) { // Add EventType itself as a property EventPropertyGetter getter = factory.getGetterEventBean(name); EventType eventType = (EventType) entry.getValue(); EventPropertyDescriptor descriptor = new EventPropertyDescriptor(name, eventType.getUnderlyingType(), null, false, false, false, false, true); FragmentEventType fragmentEventType = new FragmentEventType(eventType, false, false); PropertySetDescriptorItem item = new PropertySetDescriptorItem(descriptor, eventType.getUnderlyingType(), getter, fragmentEventType); propertyNameList.add(name); propertyDescriptors.add(descriptor); propertyItems.put(name, item); continue; } if (entry.getValue() instanceof EventType[]) { // Add EventType array itself as a property, type is expected to be first array element EventType eventType = ((EventType[]) entry.getValue())[0]; Object prototypeArray = Array.newInstance(eventType.getUnderlyingType(), 0); EventPropertyGetter getter = factory.getGetterEventBeanArray(name, eventType); EventPropertyDescriptor descriptor = new EventPropertyDescriptor(name, prototypeArray.getClass(), eventType.getUnderlyingType(), false, false, true, false, true); FragmentEventType fragmentEventType = new FragmentEventType(eventType, true, false); PropertySetDescriptorItem item = new PropertySetDescriptorItem(descriptor, prototypeArray.getClass(), getter, fragmentEventType); propertyNameList.add(name); propertyDescriptors.add(descriptor); propertyItems.put(name, item); continue; } if (entry.getValue() instanceof String) { String propertyName = entry.getValue().toString(); boolean isArray = EventTypeUtility.isPropertyArray(propertyName); if (isArray) { propertyName = EventTypeUtility.getPropertyRemoveArray(propertyName); } // Add EventType itself as a property EventType eventType = eventAdapterService.getExistsTypeByName(propertyName); if (!(eventType instanceof BaseNestableEventType)) { throw new EPException( "Nestable type configuration encountered an unexpected property type name '" + entry.getValue() + "' for property '" + name + "', expected java.lang.Class or java.util.Map or the name of a previously-declared Map or ObjectArray type"); } Class underlyingType = eventType.getUnderlyingType(); if (isArray) { underlyingType = Array.newInstance(underlyingType, 0).getClass(); } EventPropertyGetter getter; if (!isArray) { getter = factory.getGetterBeanNested(name, eventType, eventAdapterService); } else { getter = factory.getGetterBeanNestedArray(name, eventType, eventAdapterService); } EventPropertyDescriptor descriptor = new EventPropertyDescriptor(name, underlyingType, null, false, false, isArray, false, true); FragmentEventType fragmentEventType = new FragmentEventType(eventType, isArray, false); PropertySetDescriptorItem item = new PropertySetDescriptorItem(descriptor, underlyingType, getter, fragmentEventType); propertyNameList.add(name); propertyDescriptors.add(descriptor); propertyItems.put(name, item); continue; } generateExceptionNestedProp(name, entry.getValue()); } return new PropertySetDescriptor(propertyNameList, propertyDescriptors, propertyItems, nestableTypes); }
From source file:es.caib.zkib.jxpath.ri.model.dynabeans.DynaBeanPropertyPointer.java
/** * Convert a value to the appropriate property type. * @param value to convert/*from ww w. j av a2s .c o m*/ * @param element whether this should be a collection element. * @return conversion result */ private Object convert(Object value, boolean element) { DynaClass dynaClass = (DynaClass) dynaBean.getDynaClass(); DynaProperty property = dynaClass.getDynaProperty(getPropertyName()); Class type = property.getType(); if (element) { if (type.isArray()) { type = type.getComponentType(); } else { return value; // No need to convert } } try { return TypeUtils.convert(value, type); } catch (Exception ex) { String string = value == null ? "null" : value.getClass().getName(); throw new JXPathTypeConversionException("Cannot convert value of class " + string + " to type " + type, ex); } }
From source file:ch.algotrader.esper.aggregation.GenericTALibFunction.java
@Override public Class<?> getValueType() { // if we only have one outPutParam return that value // otherwise return the dynamically generated class if (this.outputParams.size() == 1) { Class<?> clazz = this.outputParams.values().iterator().next().getClass(); if (clazz.isArray()) { return clazz.getComponentType(); } else {/*from w w w. j av a2 s. c om*/ return clazz; } } else { return this.outputClass; } }
From source file:com.hzc.framework.ssh.controller.WebUtil.java
private static <T> T pbRecurrence(Class<T> c, String prefix) throws RuntimeException { try {/*ww w . j a v a2 s. co m*/ HttpServletRequest request = ActionContext.getReq(); T newInstance = c.newInstance(); Field[] declaredFields = c.getDeclaredFields(); for (Field field : declaredFields) { field.setAccessible(true); String name = field.getName(); String key = "".equals(prefix) ? name : prefix + "." + name; String value = request.getParameter(key); Class<?> type = field.getType(); if (type.isArray()) {//?string file Class<?> componentType = type.getComponentType(); if (componentType == String.class) { String[] values = request.getParameterValues(name); // if (null == value || "".equals(value)) continue; // String[] split = value.split(","); field.set(newInstance, values); } else if (componentType == File.class) { ServletContext servletContext = getServletContext(); File file = new File(servletContext.getRealPath("upload")); if (!file.exists()) file.mkdir(); MultipartRequest multi = new MultipartRequest(request, file.getAbsolutePath(), 1024 * 1024 * 1024, "UTF-8", new DefaultFileRenamePolicy()); Enumeration files = multi.getFileNames(); List<File> fileList = new ArrayList<File>(); if (files.hasMoreElements()) { File f = multi.getFile((String) files.nextElement()); fileList.add(f); } field.set(newInstance, fileList.toArray(new File[] {})); } } // ? validation(field, name, value); if (null == value) { continue; } if (type == Long.class) { field.set(newInstance, Long.parseLong(value)); } else if (type == String.class) { field.set(newInstance, value); } else if (type == Byte.class) { field.set(newInstance, Byte.parseByte(value)); } else if (type == Integer.class) { field.set(newInstance, Integer.parseInt(value)); } else if (type == Character.class) { field.set(newInstance, value.charAt(0)); } else if (type == Boolean.class) { field.set(newInstance, Boolean.parseBoolean(value)); } else if (type == Double.class) { field.set(newInstance, Double.parseDouble(value)); } else if (type == Float.class) { field.set(newInstance, Float.parseFloat(value)); } else if (type == Date.class) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); field.set(newInstance, sdf.parse(value)); } else { // Object obj = pbRecurrence(field.getType(), name);// field.set(newInstance, obj); } } return newInstance; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.laxser.blitz.lama.provider.jdbc.PreparedStatementCallbackReturnId.java
public PreparedStatementCallbackReturnId(PreparedStatementSetter setter, Class<?> returnType) { this.setter = setter; if (returnType.isPrimitive()) { returnType = ClassUtils.primitiveToWrapper(returnType); }/*from w w w .ja v a 2 s. com*/ this.returnType = returnType; Class<?> idType = returnType; if (returnType.isArray()) { idType = returnType.getComponentType(); } this.idType = idType; if (idType.isPrimitive()) { idType = ClassUtils.primitiveToWrapper(idType); } this.wrappedIdType = idType; this.mapper = new SingleColumnRowMapper(idType); if (wrappedIdType != Integer.class && wrappedIdType != Long.class) { throw new IllegalArgumentException( "wrong return type(int/long type or its array type only): " + returnType); } }
From source file:com.dianping.resource.io.util.ClassUtils.java
/** * Check whether the given class matches the user-specified type name. * @param clazz the class to check/*from w ww. j a v a2 s.c o m*/ * @param typeName the type name to match */ public static boolean matchesTypeName(Class<?> clazz, String typeName) { return (typeName != null && (typeName.equals(clazz.getName()) || typeName.equals(clazz.getSimpleName()) || (clazz.isArray() && typeName.equals(getQualifiedNameForArray(clazz))))); }
From source file:com.hzc.framework.ssh.controller.WebUtil.java
/** * ?????bean<br/>/*from w w w . j a v a 2s. c o m*/ * ??? * * @param <T> * @throws ValidationException */ public static <T> T packBean(Class<T> c) throws RuntimeException { try { HttpServletRequest request = ActionContext.getReq(); T newInstance = c.newInstance(); Field[] fields = c.getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); String name = field.getName(); String value = request.getParameter(name); Class<?> type = field.getType(); if (type.isArray()) {//?string file Class<?> componentType = type.getComponentType(); if (componentType == String.class) { String[] values = request.getParameterValues(name); // if (null == value || "".equals(value)) continue; // String[] split = value.split(","); field.set(newInstance, values); } else if (componentType == File.class) { ServletContext servletContext = getServletContext(); File file = new File(servletContext.getRealPath("upload")); if (!file.exists()) file.mkdir(); MultipartRequest multi = new MultipartRequest(request, file.getAbsolutePath(), MAX_POST_SIZE, "UTF-8", new DefaultFileRenamePolicy()); Enumeration files = multi.getFileNames(); List<File> fileList = new ArrayList<File>(); if (files.hasMoreElements()) { File f = multi.getFile((String) files.nextElement()); fileList.add(f); } field.set(newInstance, fileList.toArray(new File[] {})); } } // else if (type == File.class) {//? // ServletContext servletContext = getServletContext(); // File file = new File(servletContext.getRealPath("upload")); // if (!file.exists()) // file.mkdir(); // MultipartRequest multi = // new MultipartRequest(request, file.getAbsolutePath(), 10 * 1024 * 1024, "UTF-8", new DefaultFileRenamePolicy()); // Enumeration files = multi.getFileNames(); // if (files.hasMoreElements()) { // field.set(newInstance, multi.getFile((String) files.nextElement())); // } // } // ? validation(field, name, value); if (null == value || "".equals(value.trim())) { continue; } if (type == Long.class) { field.set(newInstance, Long.parseLong(value)); } else if (type == String.class) { field.set(newInstance, value); } else if (type == Byte.class) { field.set(newInstance, Byte.parseByte(value)); } else if (type == Integer.class) { field.set(newInstance, Integer.parseInt(value)); } else if (type == Character.class) { field.set(newInstance, value.charAt(0)); } else if (type == Boolean.class) { field.set(newInstance, Boolean.parseBoolean(value)); } else if (type == Double.class) { field.set(newInstance, Double.parseDouble(value)); } else if (type == Float.class) { field.set(newInstance, Float.parseFloat(value)); } else if (type == Date.class) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); field.set(newInstance, sdf.parse(value)); } } return newInstance; } catch (Exception e) { throw new RuntimeException(e); } }