List of usage examples for java.lang Class getComponentType
public Class<?> getComponentType()
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); }/* ww w .jav a 2s . c o m*/ 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.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 ww.j a va 2 s .co m*/ 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:org.apache.sling.models.impl.injectors.OSGiServiceInjector.java
private Object getValue(Object adaptable, Type type, String filterString, DisposalCallbackRegistry callbackRegistry) { if (type instanceof Class) { Class<?> injectedClass = (Class<?>) type; if (injectedClass.isArray()) { Object[] services = getServices(adaptable, injectedClass.getComponentType(), filterString, callbackRegistry);/*www. j ava 2s . c o m*/ if (services == null) { return null; } Object arr = Array.newInstance(injectedClass.getComponentType(), services.length); for (int i = 0; i < services.length; i++) { Array.set(arr, i, services[i]); } return arr; } else { return getService(adaptable, injectedClass, filterString, callbackRegistry); } } else if (type instanceof ParameterizedType) { 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<?> serviceType = (Class<?>) ptype.getActualTypeArguments()[0]; Object[] services = getServices(adaptable, serviceType, filterString, callbackRegistry); if (services == null) { return null; } return Arrays.asList(services); } else { log.warn("Cannot handle type {}", type); return null; } }
From source file:org.al.swagger.v2.AbstractSpecCollector.java
public Object createSchemaObject(Class<?> aClass) { if (aClass.isArray()) { GenericSwaggerSchema schema = new GenericSwaggerSchema(); schema.setType("array"); schema.setCollectionFormat("csv"); schema.setItems(createSchemaObject(aClass.getComponentType())); return schema; } else if (JDKPrimitiveType.isPrimitiveType(aClass)) { SchemaGenerator generator = SimpleTypeFactory.getInstance().getSchemaGenerator(aClass); if (generator != null) { return generator.generateSchema(aClass); } else {//from ww w. ja v a 2s . co m throw new RuntimeException("Type " + aClass + " not supporeted"); } } else { Map<String, Object> schemaMap = new LinkedHashMap<>(); Map<String, Object> properties = new LinkedHashMap<>(); schemaMap.put("properties", properties); List<Field> fields = Stream.of(aClass.getDeclaredFields()) .filter(f -> !f.isAnnotationPresent(JsonIgnore.class) && !Modifier.isStatic(f.getModifiers())) .collect(Collectors.toList()); for (Field f : fields) { if (f.getName().equalsIgnoreCase("rowId")) { properties.put(f.getName(), SimpleTypeFactory.getInstance().getSchemaGenerator(String.class) .generateSchema(String.class)); } else { Object schemaObject = createSchemaObject(f.getType()); properties.put(f.getName(), schemaObject); } } return schemaMap; } }
From source file:com.jaspersoft.jasperserver.export.modules.common.DefaultReportParametersTranslator.java
protected Object toArrayValue(Class valueClass, Object[] beanValue) { Class componentType = valueClass.getComponentType(); Object value;/*from www . j ava 2s . c o m*/ if (beanValue == null) { value = Array.newInstance(componentType, 0); } else { value = Array.newInstance(componentType, beanValue.length); for (int i = 0; i < beanValue.length; i++) { Array.set(value, i, toValue(beanValue[i])); } } return value; }
From source file:org.nabucco.alfresco.enhScriptEnv.common.script.converter.general.ArrayConverter.java
/** * {@inheritDoc}//from w ww . java 2 s.c o m */ @Override public boolean canConvertValueForScript(final Object value, final ValueConverter globalDelegate, final Class<?> expectedClass) { boolean canConvert = expectedClass.isArray(); if (canConvert) { final Class<?> componentClass = expectedClass.getComponentType(); if (value instanceof Iterable<?>) { final Collection<?> coll = (Collection<?>) value; for (final Object element : coll) { canConvert = canConvert && globalDelegate.canConvertValueForScript(element, componentClass); if (!canConvert) { break; } } } else if (value.getClass().isArray()) { final int length = Array.getLength(value); for (int idx = 0; idx < length && canConvert; idx++) { canConvert = canConvert && globalDelegate.canConvertValueForScript(Array.get(value, idx), componentClass); } } else { canConvert = false; } } return canConvert; }
From source file:org.nabucco.alfresco.enhScriptEnv.common.script.converter.general.ArrayConverter.java
/** * {@inheritDoc}/*from ww w . j a va 2 s.c o m*/ */ @Override public boolean canConvertValueForJava(final Object value, final ValueConverter globalDelegate, final Class<?> expectedClass) { boolean canConvert = expectedClass.isArray(); if (canConvert) { final Class<?> componentClass = expectedClass.getComponentType(); if (value instanceof Iterable<?>) { final Collection<?> coll = (Collection<?>) value; for (final Object element : coll) { canConvert = canConvert && globalDelegate.canConvertValueForJava(element, componentClass); if (!canConvert) { break; } } } else if (value.getClass().isArray()) { final int length = Array.getLength(value); for (int idx = 0; idx < length && canConvert; idx++) { canConvert = canConvert && globalDelegate.canConvertValueForJava(Array.get(value, idx), componentClass); } } else { canConvert = false; } } return canConvert; }
From source file:com.astamuse.asta4d.data.DefaultContextDataFinder.java
@SuppressWarnings({ "rawtypes", "unchecked" }) @Override//from ww w .j ava 2 s. c o m public ContextDataHolder findDataInContext(Context context, String scope, String name, Class<?> targetType) throws DataOperationException { ContextDataHolder dataHolder = findByType(context, scope, name, targetType); if (dataHolder != null) { return dataHolder; } if (StringUtils.isEmpty(scope)) { dataHolder = findDataByScopeOrder(context, 0, name); } else { dataHolder = context.getDataHolder(scope, name); } if (dataHolder == null) { return null; } Object foundData = dataHolder.getValue(); Object transformedData = null; UnsupportedValueException usve = null; Class<?> srcType = new TypeInfo(foundData.getClass()).getType(); if (targetType.isAssignableFrom(srcType)) { transformedData = foundData; } else if (srcType.isArray() && targetType.isAssignableFrom(srcType.getComponentType())) { transformedData = Array.get(foundData, 0); } else if (targetType.isArray() && targetType.getComponentType().isAssignableFrom(srcType)) { Object array = Array.newInstance(srcType, 1); Array.set(array, 0, foundData); transformedData = array; } else { try { transformedData = Configuration.getConfiguration().getDataTypeTransformer().transform(srcType, targetType, foundData); } catch (UnsupportedValueException ex) { usve = ex; } } if (usve == null) { dataHolder.setData(dataHolder.getName(), dataHolder.getScope(), foundData, transformedData); } else { dataHolder.setData(dataHolder.getName(), InjectUtil.ContextDataTypeUnMatchScope, foundData, transformedData); } return dataHolder; }
From source file:com.google.feedserver.util.BeanUtil.java
public boolean isBean(Class<?> valueClass) { if (valueClass.isArray()) { return isBean(valueClass.getComponentType()); } else if (valueClass.isPrimitive()) { return false; } else {// w ww.jav a 2s . co m return !primitiveTypes.contains(valueClass); } }
From source file:org.jabsorb.ng.serializer.impl.ArraySerializer.java
@Override public ObjectMatch tryUnmarshall(final SerializerState state, final Class<?> clazz, final Object o) throws UnmarshallException { final JSONArray jso = (JSONArray) o; final Class<?> cc = clazz.getComponentType(); int i = 0;//w ww . ja va 2 s. com final ObjectMatch m = new ObjectMatch(-1); state.setSerialized(o, m); try { for (; i < jso.length(); i++) { m.setMismatch(ser.tryUnmarshall(state, cc, jso.get(i)).max(m).getMismatch()); } } catch (final UnmarshallException e) { throw new UnmarshallException("element " + i + " " + e.getMessage(), e); } catch (final JSONException e) { throw new UnmarshallException("element " + i + " " + e.getMessage() + " not found in json object", e); } return m; }