List of usage examples for java.lang Class getComponentType
public Class<?> getComponentType()
From source file:com.glaf.core.util.GetterUtils.java
public static boolean[] getBooleanValues(Object value, boolean[] defaultValue) { if (value == null) { return defaultValue; }//from www .j a v a 2 s . c o m Class<?> clazz = value.getClass(); if (clazz.isArray()) { Class<?> componentType = clazz.getComponentType(); if (String.class.isAssignableFrom(componentType)) { return getBooleanValues((String[]) value, defaultValue); } else if (Boolean.class.isAssignableFrom(componentType)) { return (boolean[]) value; } } return defaultValue; }
From source file:com.glaf.core.util.GetterUtils.java
public static Date[] getDateValues(Object value, DateFormat dateFormat, Date[] defaultValue) { if (value == null) { return defaultValue; }/*from w ww . ja v a 2 s . c o m*/ Class<?> clazz = value.getClass(); if (clazz.isArray()) { Class<?> componentType = clazz.getComponentType(); if (String.class.isAssignableFrom(componentType)) { return getDateValues((String[]) value, dateFormat, defaultValue); } else if (Date.class.isAssignableFrom(componentType)) { return (Date[]) value; } } return defaultValue; }
From source file:com.glaf.core.util.GetterUtils.java
public static double[] getDoubleValues(Object value, double[] defaultValue) { if (value == null) { return defaultValue; }/*from w w w . j a v a2 s . c om*/ Class<?> clazz = value.getClass(); if (clazz.isArray()) { Class<?> componentType = clazz.getComponentType(); if (String.class.isAssignableFrom(componentType)) { return getDoubleValues((String[]) value, defaultValue); } else if (Double.class.isAssignableFrom(componentType)) { return (double[]) value; } } return defaultValue; }
From source file:com.glaf.core.util.GetterUtils.java
public static Number[] getNumberValues(Object value, Number[] defaultValue) { if (value == null) { return defaultValue; }/*from w w w. j a va2 s.c o m*/ Class<?> clazz = value.getClass(); if (clazz.isArray()) { Class<?> componentType = clazz.getComponentType(); if (String.class.isAssignableFrom(componentType)) { return getNumberValues((String[]) value, defaultValue); } else if (Number.class.isAssignableFrom(componentType)) { return (Number[]) value; } } return defaultValue; }
From source file:jp.go.nict.langrid.client.soap.io.SoapResponseParser.java
private static <T> T nodeToType(XPathWorkspace w, Node node, Class<T> clazz, Converter converter) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, ConversionException, DOMException, ParseException { if (clazz.isPrimitive()) { return converter.convert(resolveHref(w, node).getTextContent(), clazz); } else if (clazz.equals(String.class)) { return clazz.cast(resolveHref(w, node).getTextContent()); } else if (clazz.equals(byte[].class)) { try {//from w ww .j a va2 s . com return clazz.cast(Base64.decodeBase64(resolveHref(w, node).getTextContent().getBytes("ISO8859-1"))); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } else if (clazz.equals(Calendar.class)) { Date date = fmt.get().parse(resolveHref(w, node).getTextContent()); Calendar c = Calendar.getInstance(); c.setTime(date); return clazz.cast(c); } else if (clazz.isArray()) { Class<?> ct = clazz.getComponentType(); List<Object> elements = new ArrayList<Object>(); node = resolveHref(w, node); for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { if (!(child instanceof Element)) continue; elements.add(nodeToType(w, child, ct, converter)); } return clazz.cast(elements.toArray((Object[]) Array.newInstance(ct, elements.size()))); } else { T instance = clazz.newInstance(); node = resolveHref(w, node); for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { if (!(child instanceof Element)) continue; String nn = child.getLocalName(); Method setter = ClassUtil.findSetter(clazz, nn); setter.invoke(instance, nodeToType(w, resolveHref(w, child), setter.getParameterTypes()[0], converter)); } return instance; } }
From source file:org.eclipse.emf.teneo.hibernate.annotations.HbEDataTypeAnnotator.java
/** * If the specified classifier represents a custom data type not handled natively by Hibernate, * get the Java class name of the class used to represent the custom data type. * //from w w w . j a v a2s .c o m * @param classifier * non-null classifier to inspect. * @return Class name of the custom data type or null if the classifier does not represent a * custom data type that is not already handled by Hibernate. */ private String getCustomDataType(EDataType eDataType) { final String typeClassName = eDataType.getInstanceClassName(); if (EcoreDataTypes.INSTANCE.isSimpleType(eDataType, getPersistenceOptions())) { return null; } else if (EcoreDataTypes.INSTANCE.isEnum(eDataType)) { return null; } else if (eDataType.getInstanceClass() == Object.class) { return null; // } else if (typeClassName != null && TypeFactory.basic(typeClassName) != null) { // // If Hibernate natively handles the type then don't bother creating // // a typedef. // return null; } if (typeClassName != null) { final Class<?> instanceClass = eDataType.getInstanceClass(); if (instanceClass != null && instanceClass.isArray()) { final Class<?> compClass = instanceClass.getComponentType(); if (EcoreDataTypes.INSTANCE.isPrimitive(compClass)) { return null; } // get rid of the [] at the end // final String primType = typeClassName.substring(0, typeClassName.length() - 2); // check if hb supports it // no dots is primitve // if (TypeFactory.basic(primType) != null) { // return null; // if so let hb do it // } } } return typeClassName; }
From source file:org.mstc.zmq.json.Decoder.java
@SuppressWarnings("unchecked") public static void decode(String input, Field[] fields, Object b) throws IOException { ObjectMapper mapper = new ObjectMapper(); mapper.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS); if (logger.isDebugEnabled()) logger.debug(input);// w w w .j a v a 2 s . c om JsonFactory factory = mapper.getFactory(); try (JsonParser jp = factory.createParser(input)) { /* Sanity check: verify that we got "Json Object" */ if (jp.nextToken() != JsonToken.START_OBJECT) { throw new IOException("Expected data to start with an Object"); } /* Iterate over object fields */ while (jp.nextToken() != JsonToken.END_OBJECT) { String fieldName = jp.getCurrentName(); jp.nextToken(); Field field = getField(fieldName, fields); if (field == null) { throw new IOException( "Could not find field [" + fieldName + "] on class " + b.getClass().getName()); } try { if (field.getType().isAssignableFrom(List.class)) { String adder = getAdder(fieldName); TypeFactory t = TypeFactory.defaultInstance(); ParameterizedType listType = (ParameterizedType) field.getGenericType(); Class<?> listClass = (Class<?>) listType.getActualTypeArguments()[0]; List list = mapper.readValue(jp.getValueAsString(), t.constructCollectionType(List.class, listClass)); Method m = b.getClass().getDeclaredMethod(adder, Collection.class); m.invoke(b, list); } else if (field.getType().isArray()) { Class<?> type = field.getType(); String setter = getSetter(fieldName); Method m = b.getClass().getDeclaredMethod(setter, field.getType()); logger.info("Field {} is array of {}[], {}, using method {}", field.getName(), field.getType().getComponentType(), jp.getCurrentToken().name(), m); if (jp.getCurrentToken().id() == JsonToken.START_ARRAY.id()) { List list = new ArrayList(); while (jp.nextToken() != JsonToken.END_ARRAY) { String value = jp.getText(); switch (jp.getCurrentToken()) { case VALUE_STRING: list.add(value); break; case VALUE_NUMBER_INT: if (type.getComponentType().isAssignableFrom(double.class)) { list.add(Double.parseDouble(value)); } else if (type.getComponentType().isAssignableFrom(float.class)) { list.add(Float.parseFloat(value)); } else { list.add(Integer.parseInt(value)); } break; case VALUE_NUMBER_FLOAT: logger.info("Add float"); list.add(jp.getFloatValue()); break; case VALUE_NULL: break; default: logger.warn("[3] Not sure how to handle {} yet", jp.getCurrentToken().name()); } } Object array = Array.newInstance(field.getType().getComponentType(), list.size()); for (int i = 0; i < list.size(); i++) { Object val = list.get(i); Array.set(array, i, val); } m.invoke(b, array); } else { if (type.getComponentType().isAssignableFrom(byte.class)) { m.invoke(b, jp.getBinaryValue()); } } } else { String setter = getSetter(fieldName); logger.debug("{}: {}", setter, field.getType().getName()); Method m = b.getClass().getDeclaredMethod(setter, field.getType()); switch (jp.getCurrentToken()) { case VALUE_STRING: m.invoke(b, jp.getText()); break; case VALUE_NUMBER_INT: m.invoke(b, jp.getIntValue()); break; case VALUE_NUMBER_FLOAT: m.invoke(b, jp.getFloatValue()); break; case VALUE_NULL: logger.debug("Skip invoking {}.{}, property is null", b.getClass().getName(), m.getName()); break; case START_OBJECT: StringBuilder sb = new StringBuilder(); while (jp.nextToken() != JsonToken.END_OBJECT) { switch (jp.getCurrentToken()) { case VALUE_STRING: sb.append("\"").append(jp.getText()).append("\""); break; case FIELD_NAME: if (sb.length() > 0) sb.append(","); sb.append("\"").append(jp.getText()).append("\"").append(":"); break; case VALUE_NUMBER_INT: sb.append(jp.getIntValue()); break; case VALUE_NUMBER_FLOAT: sb.append(jp.getFloatValue()); break; case VALUE_NULL: sb.append("null"); break; default: logger.warn("[2] Not sure how to handle {} yet", jp.getCurrentToken().name()); } } String s = String.format("%s%s%s", JsonToken.START_OBJECT.asString(), sb.toString(), JsonToken.END_OBJECT.asString()); Object parsed = getNested(field.getType(), s.getBytes()); m.invoke(b, parsed); break; default: logger.warn("[1] Not sure how to handle {} yet", jp.getCurrentToken().name()); } } } catch (InvocationTargetException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException e) { logger.error("Failed setting field [{}], builder: {}", fieldName, b.getClass().getName(), e); } } } }
From source file:microsoft.exchange.webservices.data.core.EwsUtilities.java
public static int getDim(Object array) { int dim = 0;//from w w w . j av a 2 s . co m Class<?> c = array.getClass(); while (c.isArray()) { c = c.getComponentType(); dim++; } return (dim); }
From source file:com.dianping.resource.io.util.ClassUtils.java
/** * Build a nice qualified name for an array: * component type class name + "[]"./*w w w . jav a2 s . co m*/ * @param clazz the array class * @return a qualified name for the array class */ private static String getQualifiedNameForArray(Class<?> clazz) { StringBuilder result = new StringBuilder(); while (clazz.isArray()) { clazz = clazz.getComponentType(); result.append(ClassUtils.ARRAY_SUFFIX); } result.insert(0, clazz.getName()); return result.toString(); }
From source file:com.dianping.resource.io.util.ClassUtils.java
/** * Check if the given class represents an array of primitive wrappers, * i.e. Boolean, Byte, Character, Short, Integer, Long, Float, or Double. * @param clazz the class to check//w w w. j a v a 2s. c o m * @return whether the given class is a primitive wrapper array class */ public static boolean isPrimitiveWrapperArray(Class<?> clazz) { Assert.notNull(clazz, "Class must not be null"); return (clazz.isArray() && isPrimitiveWrapper(clazz.getComponentType())); }