List of usage examples for java.lang Long TYPE
Class TYPE
To view the source code for java.lang Long TYPE.
Click Source Link
From source file:com.qmetry.qaf.automation.data.BaseDataBean.java
protected void setField(Field field, String val) { try {// w w w .ja va2 s .c o m // deal with IllegalAccessException field.setAccessible(true); if (field.getType() == String.class) { field.set(this, val); } else { Method setter = null; try { setter = this.getClass().getMethod("set" + StringUtil.getTitleCase(field.getName()), String.class); } catch (Exception e) { } if (null != setter) { setter.setAccessible(true); setter.invoke(this, val); } else if (field.getType() == Integer.TYPE) { field.setInt(this, Integer.parseInt(val)); } else if (field.getType() == Float.TYPE) { field.setFloat(this, Float.parseFloat(val)); } else if (field.getType() == Double.TYPE) { field.setDouble(this, Double.parseDouble(val)); } else if (field.getType() == Long.TYPE) { field.setLong(this, Long.parseLong(val)); } else if (field.getType() == Boolean.TYPE) { Boolean bval = StringUtils.isBlank(val) ? null : NumberUtils.isNumber(val) ? (Integer.parseInt(val) != 0) : Boolean.parseBoolean(val) || val.equalsIgnoreCase("T") || val.equalsIgnoreCase("Y") || val.equalsIgnoreCase("YES"); field.setBoolean(this, bval); } else if (field.getType() == Short.TYPE) { field.setShort(this, Short.parseShort(val)); } else if (field.getType() == Date.class) { Date dVal = null; try { dVal = StringUtils.isBlank(val) ? null : NumberUtils.isNumber(val) ? DateUtil.getDate(Integer.parseInt(val)) : DateUtil.parseDate(val, "MM/dd/yyyy"); } catch (ParseException e) { logger.error("Expected date in MM/dd/yyyy format.", e); } field.set(this, dVal); } } } catch (IllegalArgumentException e) { logger.error("Unable to fill random data in field " + field.getName(), e); } catch (IllegalAccessException e) { logger.error("Unable to Access " + field.getName(), e); } catch (InvocationTargetException e) { logger.error("Unable to invoke setter for " + field.getName(), e); } }
From source file:com.icsshs.datatransfer.database.impl.QueryBeanProcessor.java
/** * Convert a <code>ResultSet</code> column into an object. Simple * implementations could just call <code>rs.getObject(index)</code> while * more complex implementations could perform type manipulation to match * the column's type to the bean property type. * * <p>/* ww w .java2 s . co m*/ * This implementation calls the appropriate <code>ResultSet</code> getter * method for the given property type to perform the type conversion. If * the property type doesn't match one of the supported * <code>ResultSet</code> types, <code>getObject</code> is called. * </p> * * @param rs The <code>ResultSet</code> currently being processed. It is * positioned on a valid row before being passed into this method. * * @param index The current column index being processed. * * @param propType The bean property type that this column needs to be * converted into. * * @throws SQLException if a database access error occurs * * @return The object from the <code>ResultSet</code> at the given column * index after optional type processing or <code>null</code> if the column * value was SQL NULL. */ protected Object processColumn(ResultSet rs, int index, Class<?> propType) throws SQLException { if (!propType.isPrimitive() && rs.getObject(index) == null) { return null; } if (propType.equals(String.class)) { return rs.getString(index); } else if (propType.equals(Integer.TYPE) || propType.equals(Integer.class)) { return Integer.valueOf(rs.getInt(index)); } else if (propType.equals(Boolean.TYPE) || propType.equals(Boolean.class)) { return Boolean.valueOf(rs.getBoolean(index)); } else if (propType.equals(Long.TYPE) || propType.equals(Long.class)) { return Long.valueOf(rs.getLong(index)); } else if (propType.equals(Double.TYPE) || propType.equals(Double.class)) { return Double.valueOf(rs.getDouble(index)); } else if (propType.equals(Float.TYPE) || propType.equals(Float.class)) { return Float.valueOf(rs.getFloat(index)); } else if (propType.equals(Short.TYPE) || propType.equals(Short.class)) { return Short.valueOf(rs.getShort(index)); } else if (propType.equals(Byte.TYPE) || propType.equals(Byte.class)) { return Byte.valueOf(rs.getByte(index)); } else if (propType.equals(Timestamp.class)) { return rs.getTimestamp(index); } else if (propType.equals(byte[].class)) { return rs.getBytes(index); } else { return rs.getObject(index); } }
From source file:javadz.beanutils.locale.LocaleConvertUtilsBean.java
/** * Create all {@link LocaleConverter} types for specified locale. * * @param locale The Locale//w w w .j a v a2s . c om * @return The FastHashMap instance contains the all {@link LocaleConverter} types * for the specified locale. * @deprecated This method will be modified to return a Map in the next release. */ protected FastHashMap create(Locale locale) { FastHashMap converter = new DelegateFastHashMap(BeanUtils.createCache()); converter.setFast(false); converter.put(BigDecimal.class, new BigDecimalLocaleConverter(locale, applyLocalized)); converter.put(BigInteger.class, new BigIntegerLocaleConverter(locale, applyLocalized)); converter.put(Byte.class, new ByteLocaleConverter(locale, applyLocalized)); converter.put(Byte.TYPE, new ByteLocaleConverter(locale, applyLocalized)); converter.put(Double.class, new DoubleLocaleConverter(locale, applyLocalized)); converter.put(Double.TYPE, new DoubleLocaleConverter(locale, applyLocalized)); converter.put(Float.class, new FloatLocaleConverter(locale, applyLocalized)); converter.put(Float.TYPE, new FloatLocaleConverter(locale, applyLocalized)); converter.put(Integer.class, new IntegerLocaleConverter(locale, applyLocalized)); converter.put(Integer.TYPE, new IntegerLocaleConverter(locale, applyLocalized)); converter.put(Long.class, new LongLocaleConverter(locale, applyLocalized)); converter.put(Long.TYPE, new LongLocaleConverter(locale, applyLocalized)); converter.put(Short.class, new ShortLocaleConverter(locale, applyLocalized)); converter.put(Short.TYPE, new ShortLocaleConverter(locale, applyLocalized)); converter.put(String.class, new StringLocaleConverter(locale, applyLocalized)); // conversion format patterns of java.sql.* types should correspond to default // behaviour of toString and valueOf methods of these classes converter.put(java.sql.Date.class, new SqlDateLocaleConverter(locale, "yyyy-MM-dd")); converter.put(java.sql.Time.class, new SqlTimeLocaleConverter(locale, "HH:mm:ss")); converter.put(java.sql.Timestamp.class, new SqlTimestampLocaleConverter(locale, "yyyy-MM-dd HH:mm:ss.S")); converter.setFast(true); return converter; }
From source file:net.sourceforge.pmd.typeresolution.ClassTypeResolverTest.java
@Test public void testBinaryNumericPromotion() throws JaxenException { ASTCompilationUnit acu = parseAndTypeResolveForClass15(Promotion.class); List<ASTExpression> expressions = convertList(acu.findChildNodesWithXPath( "//Block[preceding-sibling::MethodDeclarator[@Image = 'binaryNumericPromotion']]//Expression[AdditiveExpression]"), ASTExpression.class); int index = 0; // LHS = byte assertEquals(Integer.TYPE, expressions.get(index++).getType()); assertEquals(Integer.TYPE, expressions.get(index++).getType()); assertEquals(Integer.TYPE, expressions.get(index++).getType()); assertEquals(Integer.TYPE, expressions.get(index++).getType()); assertEquals(Long.TYPE, expressions.get(index++).getType()); assertEquals(Float.TYPE, expressions.get(index++).getType()); assertEquals(Double.TYPE, expressions.get(index++).getType()); // LHS = short assertEquals(Integer.TYPE, expressions.get(index++).getType()); assertEquals(Integer.TYPE, expressions.get(index++).getType()); assertEquals(Integer.TYPE, expressions.get(index++).getType()); assertEquals(Integer.TYPE, expressions.get(index++).getType()); assertEquals(Long.TYPE, expressions.get(index++).getType()); assertEquals(Float.TYPE, expressions.get(index++).getType()); assertEquals(Double.TYPE, expressions.get(index++).getType()); // LHS = char assertEquals(Integer.TYPE, expressions.get(index++).getType()); assertEquals(Integer.TYPE, expressions.get(index++).getType()); assertEquals(Integer.TYPE, expressions.get(index++).getType()); assertEquals(Integer.TYPE, expressions.get(index++).getType()); assertEquals(Long.TYPE, expressions.get(index++).getType()); assertEquals(Float.TYPE, expressions.get(index++).getType()); assertEquals(Double.TYPE, expressions.get(index++).getType()); // LHS = int//from w w w . ja v a 2 s .com assertEquals(Integer.TYPE, expressions.get(index++).getType()); assertEquals(Integer.TYPE, expressions.get(index++).getType()); assertEquals(Integer.TYPE, expressions.get(index++).getType()); assertEquals(Integer.TYPE, expressions.get(index++).getType()); assertEquals(Long.TYPE, expressions.get(index++).getType()); assertEquals(Float.TYPE, expressions.get(index++).getType()); assertEquals(Double.TYPE, expressions.get(index++).getType()); // LHS = long assertEquals(Long.TYPE, expressions.get(index++).getType()); assertEquals(Long.TYPE, expressions.get(index++).getType()); assertEquals(Long.TYPE, expressions.get(index++).getType()); assertEquals(Long.TYPE, expressions.get(index++).getType()); assertEquals(Long.TYPE, expressions.get(index++).getType()); assertEquals(Float.TYPE, expressions.get(index++).getType()); assertEquals(Double.TYPE, expressions.get(index++).getType()); // LHS = float assertEquals(Float.TYPE, expressions.get(index++).getType()); assertEquals(Float.TYPE, expressions.get(index++).getType()); assertEquals(Float.TYPE, expressions.get(index++).getType()); assertEquals(Float.TYPE, expressions.get(index++).getType()); assertEquals(Float.TYPE, expressions.get(index++).getType()); assertEquals(Float.TYPE, expressions.get(index++).getType()); assertEquals(Double.TYPE, expressions.get(index++).getType()); // LHS = double assertEquals(Double.TYPE, expressions.get(index++).getType()); assertEquals(Double.TYPE, expressions.get(index++).getType()); assertEquals(Double.TYPE, expressions.get(index++).getType()); assertEquals(Double.TYPE, expressions.get(index++).getType()); assertEquals(Double.TYPE, expressions.get(index++).getType()); assertEquals(Double.TYPE, expressions.get(index++).getType()); assertEquals(Double.TYPE, expressions.get(index++).getType()); // Make sure we got them all. assertEquals("All expressions not tested", index, expressions.size()); }
From source file:au.com.addstar.cellblock.configuration.AutoConfig.java
public boolean save() { try {/*from w ww. j ava2 s .c o m*/ onPreSave(); YamlConfiguration config = new YamlConfiguration(); Map<String, String> comments = new HashMap<>(); // Add all the category comments comments.putAll(mCategoryComments); // Add all the values for (Field field : getClass().getDeclaredFields()) { ConfigField configField = field.getAnnotation(ConfigField.class); if (configField == null) continue; String optionName = configField.name(); if (optionName.isEmpty()) optionName = field.getName(); field.setAccessible(true); String path = (configField.category().isEmpty() ? "" : configField.category() + ".") + optionName; //$NON-NLS-2$ // Ensure the secion exists if (!configField.category().isEmpty() && !config.contains(configField.category())) config.createSection(configField.category()); if (field.getType().isArray()) { // Integer if (field.getType().getComponentType().equals(Integer.TYPE)) config.set(path, Arrays.asList((Integer[]) field.get(this))); // Float else if (field.getType().getComponentType().equals(Float.TYPE)) config.set(path, Arrays.asList((Float[]) field.get(this))); // Double else if (field.getType().getComponentType().equals(Double.TYPE)) config.set(path, Arrays.asList((Double[]) field.get(this))); // Long else if (field.getType().getComponentType().equals(Long.TYPE)) config.set(path, Arrays.asList((Long[]) field.get(this))); // Short else if (field.getType().getComponentType().equals(Short.TYPE)) config.set(path, Arrays.asList((Short[]) field.get(this))); // Boolean else if (field.getType().getComponentType().equals(Boolean.TYPE)) config.set(path, Arrays.asList((Boolean[]) field.get(this))); // String else if (field.getType().getComponentType().equals(String.class)) config.set(path, Arrays.asList((String[]) field.get(this))); else throw new IllegalArgumentException( "Cannot use type " + field.getType().getSimpleName() + " for AutoConfiguration"); //$NON-NLS-2$ } else if (List.class.isAssignableFrom(field.getType())) { if (field.getGenericType() == null) throw new IllegalArgumentException( "Cannot use type List without specifying generic type for AutoConfiguration"); Type type = ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0]; if (type.equals(Integer.class) || type.equals(Float.class) || type.equals(Double.class) || type.equals(Long.class) || type.equals(Short.class) || type.equals(Boolean.class) || type.equals(String.class)) { config.set(path, field.get(this)); } else throw new IllegalArgumentException("Cannot use type " + field.getType().getSimpleName() + "<" + type.toString() + "> for AutoConfiguration"); //$NON-NLS-1$ //$NON-NLS-2$ } else if (Set.class.isAssignableFrom(field.getType())) { if (field.getGenericType() == null) throw new IllegalArgumentException( "Cannot use type Set without specifying generic type for AutoConfiguration"); Type type = ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0]; if (type.equals(Integer.class) || type.equals(Float.class) || type.equals(Double.class) || type.equals(Long.class) || type.equals(Short.class) || type.equals(Boolean.class) || type.equals(String.class)) { config.set(path, new ArrayList<Object>((Set<?>) field.get(this))); } else throw new IllegalArgumentException("Cannot use type " + field.getType().getSimpleName() + "<" + type.toString() + "> for AutoConfiguration"); //$NON-NLS-1$ //$NON-NLS-2$ } else { // Integer if (field.getType().equals(Integer.TYPE)) config.set(path, field.get(this)); // Float else if (field.getType().equals(Float.TYPE)) config.set(path, field.get(this)); // Double else if (field.getType().equals(Double.TYPE)) config.set(path, field.get(this)); // Long else if (field.getType().equals(Long.TYPE)) config.set(path, field.get(this)); // Short else if (field.getType().equals(Short.TYPE)) config.set(path, field.get(this)); // Boolean else if (field.getType().equals(Boolean.TYPE)) config.set(path, field.get(this)); // ItemStack else if (field.getType().equals(ItemStack.class)) config.set(path, field.get(this)); // String else if (field.getType().equals(String.class)) config.set(path, field.get(this)); else throw new IllegalArgumentException( "Cannot use type " + field.getType().getSimpleName() + " for AutoConfiguration"); //$NON-NLS-2$ } // Record the comment if (!configField.comment().isEmpty()) comments.put(path, configField.comment()); } String output = config.saveToString(); // Apply comments String category = ""; List<String> lines = new ArrayList<>(Arrays.asList(output.split("\n"))); for (int l = 0; l < lines.size(); l++) { String line = lines.get(l); if (line.startsWith("#")) continue; if (line.trim().startsWith("-")) continue; if (!line.contains(":")) continue; String path; line = line.substring(0, line.indexOf(":")); if (line.startsWith(" ")) path = category + "." + line.substring(2).trim(); else { category = line.trim(); path = line.trim(); } if (comments.containsKey(path)) { String indent = ""; for (int i = 0; i < line.length(); i++) { if (line.charAt(i) == ' ') indent += " "; else break; } // Add in the comment lines String[] commentLines = comments.get(path).split("\n"); lines.add(l++, ""); for (int i = 0; i < commentLines.length; i++) { commentLines[i] = indent + "# " + commentLines[i]; lines.add(l++, commentLines[i]); } } } output = ""; for (String line : lines) output += line + "\n"; FileWriter writer = new FileWriter(mFile); writer.write(output); writer.close(); return true; } catch (IllegalArgumentException | IOException | IllegalAccessException e) { e.printStackTrace(); } return false; }
From source file:org.talend.components.netsuite.NetSuiteDatasetRuntimeImpl.java
/** * Infers an Avro schema for the given FieldDesc. This can be an expensive operation so the schema should be * cached where possible. The return type will be the Avro Schema that can contain the fieldDesc data without loss of * precision.//from w w w . ja va 2 s. co m * * @param fieldDesc the <code>FieldDesc</code> to analyse. * @return the schema for data that the fieldDesc describes. */ public static Schema inferSchemaForField(FieldDesc fieldDesc) { Schema base; if (fieldDesc instanceof CustomFieldDesc) { CustomFieldDesc customFieldInfo = (CustomFieldDesc) fieldDesc; CustomFieldRefType customFieldRefType = customFieldInfo.getCustomFieldType(); if (customFieldRefType == CustomFieldRefType.BOOLEAN) { base = AvroUtils._boolean(); } else if (customFieldRefType == CustomFieldRefType.LONG) { base = AvroUtils._long(); } else if (customFieldRefType == CustomFieldRefType.DOUBLE) { base = AvroUtils._double(); } else if (customFieldRefType == CustomFieldRefType.DATE) { base = AvroUtils._logicalTimestamp(); } else if (customFieldRefType == CustomFieldRefType.STRING) { base = AvroUtils._string(); } else { base = AvroUtils._string(); } } else { Class<?> fieldType = fieldDesc.getValueType(); if (fieldType == Boolean.TYPE || fieldType == Boolean.class) { base = AvroUtils._boolean(); } else if (fieldType == Integer.TYPE || fieldType == Integer.class) { base = AvroUtils._int(); } else if (fieldType == Long.TYPE || fieldType == Long.class) { base = AvroUtils._long(); } else if (fieldType == Float.TYPE || fieldType == Float.class) { base = AvroUtils._float(); } else if (fieldType == Double.TYPE || fieldType == Double.class) { base = AvroUtils._double(); } else if (fieldType == XMLGregorianCalendar.class) { base = AvroUtils._logicalTimestamp(); } else if (fieldType == String.class) { base = AvroUtils._string(); } else if (fieldType.isEnum()) { base = AvroUtils._string(); } else { base = AvroUtils._string(); } } base = fieldDesc.isNullable() ? AvroUtils.wrapAsNullable(base) : base; return base; }
From source file:org.liveSense.api.beanprocessors.DbStandardBeanProcessor.java
/** * Convert a <code>ResultSet</code> column into an object. Simple * implementations could just call <code>rs.getObject(index)</code> while * more complex implementations could perform type manipulation to match * the column's type to the bean property type. * /*from ww w. jav a 2 s . co m*/ * <p> * This implementation calls the appropriate <code>ResultSet</code> getter * method for the given property type to perform the type conversion. If * the property type doesn't match one of the supported * <code>ResultSet</code> types, <code>getObject</code> is called. * </p> * * @param rs The <code>ResultSet</code> currently being processed. It is * positioned on a valid row before being passed into this method. * * @param index The current column index being processed. * * @param propType The bean property type that this column needs to be * converted into. * * @throws SQLException if a database access error occurs * * @return The object from the <code>ResultSet</code> at the given column * index after optional type processing or <code>null</code> if the column * value was SQL NULL. */ protected Object processColumn(ResultSet rs, int index, Class<?> propType) throws SQLException { if (!propType.isPrimitive() && rs.getObject(index) == null) { return null; } if (!propType.isPrimitive() && rs.getObject(index) == null) { return null; } if (propType.equals(String.class)) { return rs.getString(index); } else if (propType.equals(Integer.TYPE) || propType.equals(Integer.class)) { return (rs.getInt(index)); } else if (propType.equals(Boolean.TYPE) || propType.equals(Boolean.class)) { return (rs.getBoolean(index)); } else if (propType.equals(Long.TYPE) || propType.equals(Long.class)) { return (rs.getLong(index)); } else if (propType.equals(Double.TYPE) || propType.equals(Double.class)) { return (rs.getDouble(index)); } else if (propType.equals(Float.TYPE) || propType.equals(Float.class)) { return (rs.getFloat(index)); } else if (propType.equals(Short.TYPE) || propType.equals(Short.class)) { return (rs.getShort(index)); } else if (propType.equals(Byte.TYPE) || propType.equals(Byte.class)) { return (rs.getByte(index)); } else if (propType.equals(Timestamp.class)) { return rs.getTimestamp(index); } else { return rs.getObject(index); } }
From source file:org.apache.struts2.jasper.compiler.JspUtil.java
/** * Produces a String representing a call to the EL interpreter. * * @param isTagFile is a tag file// w w w . ja v a 2 s . co m * @param expression a String containing zero or more "${}" expressions * @param expectedType the expected type of the interpreted result * @param fnmapvar Variable pointing to a function map. * @param XmlEscape True if the result should do XML escaping * @return a String representing a call to the EL interpreter. */ public static String interpreterCall(boolean isTagFile, String expression, Class expectedType, String fnmapvar, boolean XmlEscape) { /* * Determine which context object to use. */ String jspCtxt = null; if (isTagFile) jspCtxt = "this.getJspContext()"; else jspCtxt = "_jspx_page_context"; /* * Determine whether to use the expected type's textual name * or, if it's a primitive, the name of its correspondent boxed * type. */ String targetType = expectedType.getName(); String primitiveConverterMethod = null; if (expectedType.isPrimitive()) { if (expectedType.equals(Boolean.TYPE)) { targetType = Boolean.class.getName(); primitiveConverterMethod = "booleanValue"; } else if (expectedType.equals(Byte.TYPE)) { targetType = Byte.class.getName(); primitiveConverterMethod = "byteValue"; } else if (expectedType.equals(Character.TYPE)) { targetType = Character.class.getName(); primitiveConverterMethod = "charValue"; } else if (expectedType.equals(Short.TYPE)) { targetType = Short.class.getName(); primitiveConverterMethod = "shortValue"; } else if (expectedType.equals(Integer.TYPE)) { targetType = Integer.class.getName(); primitiveConverterMethod = "intValue"; } else if (expectedType.equals(Long.TYPE)) { targetType = Long.class.getName(); primitiveConverterMethod = "longValue"; } else if (expectedType.equals(Float.TYPE)) { targetType = Float.class.getName(); primitiveConverterMethod = "floatValue"; } else if (expectedType.equals(Double.TYPE)) { targetType = Double.class.getName(); primitiveConverterMethod = "doubleValue"; } } if (primitiveConverterMethod != null) { XmlEscape = false; } /* * Build up the base call to the interpreter. */ // XXX - We use a proprietary call to the interpreter for now // as the current standard machinery is inefficient and requires // lots of wrappers and adapters. This should all clear up once // the EL interpreter moves out of JSTL and into its own project. // In the future, this should be replaced by code that calls // ExpressionEvaluator.parseExpression() and then cache the resulting // expression objects. The interpreterCall would simply select // one of the pre-cached expressions and evaluate it. // Note that PageContextImpl implements VariableResolver and // the generated Servlet/SimpleTag implements FunctionMapper, so // that machinery is already in place (mroth). targetType = toJavaSourceType(targetType); StringBuilder call = new StringBuilder( "(" + targetType + ") " + "org.apache.struts2.jasper.runtime.PageContextImpl.proprietaryEvaluate" + "(" + Generator.quote(expression) + ", " + targetType + ".class, " + "(PageContext)" + jspCtxt + ", " + fnmapvar + ", " + XmlEscape + ")"); /* * Add the primitive converter method if we need to. */ if (primitiveConverterMethod != null) { call.insert(0, "("); call.append(")." + primitiveConverterMethod + "()"); } return call.toString(); }
From source file:org.apache.hadoop.hbase.security.access.HbaseObjectWritableFor96Migration.java
/** * Write a {@link Writable}, {@link String}, primitive type, or an array of * the preceding.// www .j a v a2 s .co m * @param out * @param instance * @param declaredClass * @param conf * @throws IOException */ @SuppressWarnings("unchecked") static void writeObject(DataOutput out, Object instance, Class declaredClass, Configuration conf) throws IOException { Object instanceObj = instance; Class declClass = declaredClass; if (instanceObj == null) { // null instanceObj = new NullInstance(declClass, conf); declClass = Writable.class; } writeClassCode(out, declClass); if (declClass.isArray()) { // array // If bytearray, just dump it out -- avoid the recursion and // byte-at-a-time we were previously doing. if (declClass.equals(byte[].class)) { Bytes.writeByteArray(out, (byte[]) instanceObj); } else { //if it is a Generic array, write the element's type if (getClassCode(declaredClass) == GENERIC_ARRAY_CODE) { Class<?> componentType = declaredClass.getComponentType(); writeClass(out, componentType); } int length = Array.getLength(instanceObj); out.writeInt(length); for (int i = 0; i < length; i++) { Object item = Array.get(instanceObj, i); writeObject(out, item, item.getClass(), conf); } } } else if (List.class.isAssignableFrom(declClass)) { List list = (List) instanceObj; int length = list.size(); out.writeInt(length); for (int i = 0; i < length; i++) { Object elem = list.get(i); writeObject(out, elem, elem == null ? Writable.class : elem.getClass(), conf); } } else if (declClass == String.class) { // String Text.writeString(out, (String) instanceObj); } else if (declClass.isPrimitive()) { // primitive type if (declClass == Boolean.TYPE) { // boolean out.writeBoolean(((Boolean) instanceObj).booleanValue()); } else if (declClass == Character.TYPE) { // char out.writeChar(((Character) instanceObj).charValue()); } else if (declClass == Byte.TYPE) { // byte out.writeByte(((Byte) instanceObj).byteValue()); } else if (declClass == Short.TYPE) { // short out.writeShort(((Short) instanceObj).shortValue()); } else if (declClass == Integer.TYPE) { // int out.writeInt(((Integer) instanceObj).intValue()); } else if (declClass == Long.TYPE) { // long out.writeLong(((Long) instanceObj).longValue()); } else if (declClass == Float.TYPE) { // float out.writeFloat(((Float) instanceObj).floatValue()); } else if (declClass == Double.TYPE) { // double out.writeDouble(((Double) instanceObj).doubleValue()); } else if (declClass == Void.TYPE) { // void } else { throw new IllegalArgumentException("Not a primitive: " + declClass); } } else if (declClass.isEnum()) { // enum Text.writeString(out, ((Enum) instanceObj).name()); } else if (Message.class.isAssignableFrom(declaredClass)) { Text.writeString(out, instanceObj.getClass().getName()); ((Message) instance).writeDelimitedTo(DataOutputOutputStream.constructOutputStream(out)); } else if (Writable.class.isAssignableFrom(declClass)) { // Writable Class<?> c = instanceObj.getClass(); Integer code = CLASS_TO_CODE.get(c); if (code == null) { out.writeByte(NOT_ENCODED); Text.writeString(out, c.getName()); } else { writeClassCode(out, c); } ((Writable) instanceObj).write(out); } else if (Serializable.class.isAssignableFrom(declClass)) { Class<?> c = instanceObj.getClass(); Integer code = CLASS_TO_CODE.get(c); if (code == null) { out.writeByte(NOT_ENCODED); Text.writeString(out, c.getName()); } else { writeClassCode(out, c); } ByteArrayOutputStream bos = null; ObjectOutputStream oos = null; try { bos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(bos); oos.writeObject(instanceObj); byte[] value = bos.toByteArray(); out.writeInt(value.length); out.write(value); } finally { if (bos != null) bos.close(); if (oos != null) oos.close(); } } else if (Scan.class.isAssignableFrom(declClass)) { Scan scan = (Scan) instanceObj; byte[] scanBytes = ProtobufUtil.toScan(scan).toByteArray(); out.writeInt(scanBytes.length); out.write(scanBytes); } else { throw new IOException("Can't write: " + instanceObj + " as " + declClass); } }
From source file:org.apache.hadoop.hbase.io.HbaseObjectWritable.java
/** * Write a {@link Writable}, {@link String}, primitive type, or an array of * the preceding./*from w w w .j a v a 2 s . c o m*/ * @param out * @param instance * @param declaredClass * @param conf * @throws IOException */ @SuppressWarnings("unchecked") public static void writeObject(DataOutput out, Object instance, Class declaredClass, Configuration conf) throws IOException { Object instanceObj = instance; Class declClass = declaredClass; if (instanceObj == null) { // null instanceObj = new NullInstance(declClass, conf); declClass = Writable.class; } writeClassCode(out, declClass); if (declClass.isArray()) { // array // If bytearray, just dump it out -- avoid the recursion and // byte-at-a-time we were previously doing. if (declClass.equals(byte[].class)) { Bytes.writeByteArray(out, (byte[]) instanceObj); } else if (declClass.equals(Result[].class)) { Result.writeArray(out, (Result[]) instanceObj); } else { //if it is a Generic array, write the element's type if (getClassCode(declaredClass) == GENERIC_ARRAY_CODE) { Class<?> componentType = declaredClass.getComponentType(); writeClass(out, componentType); } int length = Array.getLength(instanceObj); out.writeInt(length); for (int i = 0; i < length; i++) { Object item = Array.get(instanceObj, i); writeObject(out, item, item.getClass(), conf); } } } else if (List.class.isAssignableFrom(declClass)) { List list = (List) instanceObj; int length = list.size(); out.writeInt(length); for (int i = 0; i < length; i++) { Object elem = list.get(i); writeObject(out, elem, elem == null ? Writable.class : elem.getClass(), conf); } } else if (declClass == String.class) { // String Text.writeString(out, (String) instanceObj); } else if (declClass.isPrimitive()) { // primitive type if (declClass == Boolean.TYPE) { // boolean out.writeBoolean(((Boolean) instanceObj).booleanValue()); } else if (declClass == Character.TYPE) { // char out.writeChar(((Character) instanceObj).charValue()); } else if (declClass == Byte.TYPE) { // byte out.writeByte(((Byte) instanceObj).byteValue()); } else if (declClass == Short.TYPE) { // short out.writeShort(((Short) instanceObj).shortValue()); } else if (declClass == Integer.TYPE) { // int out.writeInt(((Integer) instanceObj).intValue()); } else if (declClass == Long.TYPE) { // long out.writeLong(((Long) instanceObj).longValue()); } else if (declClass == Float.TYPE) { // float out.writeFloat(((Float) instanceObj).floatValue()); } else if (declClass == Double.TYPE) { // double out.writeDouble(((Double) instanceObj).doubleValue()); } else if (declClass == Void.TYPE) { // void } else { throw new IllegalArgumentException("Not a primitive: " + declClass); } } else if (declClass.isEnum()) { // enum Text.writeString(out, ((Enum) instanceObj).name()); } else if (Message.class.isAssignableFrom(declaredClass)) { Text.writeString(out, instanceObj.getClass().getName()); ((Message) instance).writeDelimitedTo(DataOutputOutputStream.constructOutputStream(out)); } else if (Writable.class.isAssignableFrom(declClass)) { // Writable Class<?> c = instanceObj.getClass(); Integer code = CLASS_TO_CODE.get(c); if (code == null) { out.writeByte(NOT_ENCODED); Text.writeString(out, c.getName()); } else { writeClassCode(out, c); } ((Writable) instanceObj).write(out); } else if (Serializable.class.isAssignableFrom(declClass)) { Class<?> c = instanceObj.getClass(); Integer code = CLASS_TO_CODE.get(c); if (code == null) { out.writeByte(NOT_ENCODED); Text.writeString(out, c.getName()); } else { writeClassCode(out, c); } ByteArrayOutputStream bos = null; ObjectOutputStream oos = null; try { bos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(bos); oos.writeObject(instanceObj); byte[] value = bos.toByteArray(); out.writeInt(value.length); out.write(value); } finally { if (bos != null) bos.close(); if (oos != null) oos.close(); } } else { throw new IOException("Can't write: " + instanceObj + " as " + declClass); } }