List of usage examples for java.lang Byte TYPE
Class TYPE
To view the source code for java.lang Byte TYPE.
Click Source Link
From source file:io.coala.guice.config.ConfigMembersInjector.java
@Override public void injectMembers(final T instance) { final InjectConfig injectConfigAnnotation = field.getAnnotation(InjectConfig.class); final String configurationParameterName = injectConfigAnnotation.name(); try {/*from ww w .j a v a2s .co m*/ final Class<?> type = this.field.getType(); if (type == Integer.TYPE) { if (this.configuration.containsKey(configurationParameterName)) { this.field.setInt(instance, this.configuration.getInt(configurationParameterName)); } else { LOG.debug(NO_PROPERTY_FOUND_LOG_MESSAGE, configurationParameterName); this.field.setInt(instance, injectConfigAnnotation.defaultIntValue()); } } else if (type == Boolean.TYPE) { if (this.configuration.containsKey(configurationParameterName)) { this.field.setBoolean(instance, this.configuration.getBoolean(configurationParameterName)); } else { LOG.debug(NO_PROPERTY_FOUND_LOG_MESSAGE, configurationParameterName); this.field.setBoolean(instance, injectConfigAnnotation.defaultBooleanValue()); } } else if (type == Short.TYPE) { if (configuration.containsKey(configurationParameterName)) { this.field.setShort(instance, this.configuration.getShort(configurationParameterName)); } else { LOG.debug(NO_PROPERTY_FOUND_LOG_MESSAGE, configurationParameterName); this.field.setShort(instance, injectConfigAnnotation.defaultShortValue()); } } else if (type == Byte.TYPE) { if (configuration.containsKey(configurationParameterName)) { this.field.setByte(instance, this.configuration.getByte(configurationParameterName)); } else { LOG.debug(NO_PROPERTY_FOUND_LOG_MESSAGE, configurationParameterName); this.field.setByte(instance, injectConfigAnnotation.defaultByteValue()); } } else if (type == Long.TYPE) { if (this.configuration.containsKey(configurationParameterName)) { this.field.setLong(instance, this.configuration.getLong(configurationParameterName)); } else { LOG.debug(NO_PROPERTY_FOUND_LOG_MESSAGE, configurationParameterName); this.field.setLong(instance, injectConfigAnnotation.defaultLongValue()); } } else if (type == Float.TYPE) { if (this.configuration.containsKey(configurationParameterName)) { this.field.setFloat(instance, this.configuration.getFloat(configurationParameterName)); } else { LOG.debug(NO_PROPERTY_FOUND_LOG_MESSAGE, configurationParameterName); this.field.setFloat(instance, injectConfigAnnotation.defaultFloatValue()); } } else if (type == Double.TYPE) { if (configuration.containsKey(configurationParameterName)) { this.field.setDouble(instance, this.configuration.getDouble(configurationParameterName)); } else { LOG.debug(NO_PROPERTY_FOUND_LOG_MESSAGE, configurationParameterName); this.field.setDouble(instance, injectConfigAnnotation.defaultDoubleValue()); } } else if (type == Character.TYPE) { if (configuration.containsKey(configurationParameterName)) { this.field.setChar(instance, this.configuration.getString(configurationParameterName).charAt(0)); } } else { final Object property = getProperty(configurationParameterName, injectConfigAnnotation); this.field.set(instance, property); } } catch (final Throwable ex) { LOG.error("Problem injecting configuration", ex); } }
From source file:foundation.stack.datamill.configuration.impl.Classes.java
public static boolean isAssignable(Class<?> clazz, final Class<?> toClass) { if (toClass == null) { return false; }//from ww w . java 2s . co m if (clazz == null) { return !toClass.isPrimitive(); } if (clazz.isPrimitive() && !toClass.isPrimitive()) { clazz = primitiveToWrapper(clazz); if (clazz == null) { return false; } } if (toClass.isPrimitive() && !clazz.isPrimitive()) { clazz = wrapperToPrimitive(clazz); if (clazz == null) { return false; } } if (clazz.equals(toClass)) { return true; } if (clazz.isPrimitive()) { if (!toClass.isPrimitive()) { return false; } if (Integer.TYPE.equals(clazz)) { return Long.TYPE.equals(toClass) || Float.TYPE.equals(toClass) || Double.TYPE.equals(toClass); } if (Long.TYPE.equals(clazz)) { return Float.TYPE.equals(toClass) || Double.TYPE.equals(toClass); } if (Boolean.TYPE.equals(clazz)) { return false; } if (Double.TYPE.equals(clazz)) { return false; } if (Float.TYPE.equals(clazz)) { return Double.TYPE.equals(toClass); } if (Character.TYPE.equals(clazz)) { return Integer.TYPE.equals(toClass) || Long.TYPE.equals(toClass) || Float.TYPE.equals(toClass) || Double.TYPE.equals(toClass); } if (Short.TYPE.equals(clazz)) { return Integer.TYPE.equals(toClass) || Long.TYPE.equals(toClass) || Float.TYPE.equals(toClass) || Double.TYPE.equals(toClass); } if (Byte.TYPE.equals(clazz)) { return Short.TYPE.equals(toClass) || Integer.TYPE.equals(toClass) || Long.TYPE.equals(toClass) || Float.TYPE.equals(toClass) || Double.TYPE.equals(toClass); } // should never get here return false; } return toClass.isAssignableFrom(clazz); }
From source file:be.fedict.commons.eid.consumer.tlv.TlvParser.java
private static <T> T parseThrowing(final byte[] file, final Class<T> tlvClass) throws InstantiationException, IllegalAccessException, DataConvertorException, UnsupportedEncodingException { final Field[] fields = tlvClass.getDeclaredFields(); final Map<Integer, Field> tlvFields = new HashMap<Integer, Field>(); final T tlvObject = tlvClass.newInstance(); for (Field field : fields) { final TlvField tlvFieldAnnotation = field.getAnnotation(TlvField.class); if (null != tlvFieldAnnotation) { final int tagId = tlvFieldAnnotation.value(); if (tlvFields.containsKey(new Integer(tagId))) { throw new IllegalArgumentException("TLV field duplicate: " + tagId); }/* w ww. ja va2s. c o m*/ tlvFields.put(new Integer(tagId), field); } final OriginalData originalDataAnnotation = field.getAnnotation(OriginalData.class); if (null != originalDataAnnotation) { field.setAccessible(true); field.set(tlvObject, file); } } int idx = 0; while (idx < file.length - 1) { final byte tag = file[idx]; idx++; byte lengthByte = file[idx]; int length = lengthByte & 0x7f; while ((lengthByte & 0x80) == 0x80) { idx++; lengthByte = file[idx]; length = (length << 7) + (lengthByte & 0x7f); } idx++; if (0 == tag) { idx += length; continue; } if (tlvFields.containsKey(new Integer(tag))) { final Field tlvField = tlvFields.get(new Integer(tag)); final Class<?> tlvType = tlvField.getType(); final ConvertData convertDataAnnotation = tlvField.getAnnotation(ConvertData.class); final byte[] tlvValue = copy(file, idx, length); Object fieldValue; if (null != convertDataAnnotation) { final Class<? extends DataConvertor<?>> dataConvertorClass = convertDataAnnotation.value(); final DataConvertor<?> dataConvertor = dataConvertorClass.newInstance(); fieldValue = dataConvertor.convert(tlvValue); } else if (String.class == tlvType) { fieldValue = new String(tlvValue, "UTF-8"); } else if (Boolean.TYPE == tlvType) { fieldValue = true; } else if (tlvType.isArray() && Byte.TYPE == tlvType.getComponentType()) { fieldValue = tlvValue; } else { throw new IllegalArgumentException("unsupported field type: " + tlvType.getName()); } if (null != tlvField.get(tlvObject) && false == tlvField.getType().isPrimitive()) { throw new RuntimeException("field was already set: " + tlvField.getName()); } tlvField.setAccessible(true); tlvField.set(tlvObject, fieldValue); } else { LOG.debug("unknown tag: " + (tag & 0xff) + ", length: " + length); } idx += length; } return tlvObject; }
From source file:org.romaframework.core.schema.SchemaClassElement.java
@SuppressWarnings({ "rawtypes", "unchecked" }) protected Object convertValue(Object iFieldValue, SchemaClassDefinition expectedType) { if (expectedType == null || expectedType.getSchemaClass().isArray()) return iFieldValue; SchemaClass typeClass = expectedType.getSchemaClass(); if (typeClass.equals(Roma.schema().getSchemaClass(iFieldValue))) return iFieldValue; String textValue = null;//from w ww. j ava2 s .c om if (iFieldValue instanceof String) { textValue = (String) iFieldValue; } else if (iFieldValue != null) { textValue = iFieldValue.toString(); } Object value = null; if (textValue != null) { // TRY A SOFT CONVERSION if (typeClass.isOfType(Integer.class) || typeClass.isOfType(Integer.TYPE)) { try { value = textValue.equals("") ? null : Integer.parseInt(textValue); } catch (Exception e) { value = textValue.equals("") ? null : Double.valueOf(textValue).intValue(); } } else if (typeClass.isOfType(Long.class) || typeClass.isOfType(Long.TYPE)) { value = textValue.equals("") ? null : Long.parseLong(textValue); } else if (typeClass.isOfType(Short.class) || typeClass.isOfType(Short.TYPE)) { value = textValue.equals("") ? null : Short.parseShort(textValue); } else if (typeClass.isOfType(Byte.class) || typeClass.isOfType(Byte.TYPE)) { value = textValue.equals("") ? null : Byte.parseByte(textValue); } else if (typeClass.isOfType(Character.class) || typeClass.isOfType(Character.TYPE)) { if (textValue.length() > 0) { value = new Character(textValue.charAt(0)); } } else if (typeClass.isOfType(Float.class) || typeClass.isOfType(Float.TYPE)) { value = textValue.equals("") ? null : Float.parseFloat(textValue); } else if (typeClass.isOfType(Double.class) || typeClass.isOfType(Double.TYPE)) { value = textValue.equals("") ? null : Double.parseDouble(textValue); } else if (typeClass.isOfType(BigDecimal.class)) { value = textValue.equals("") ? null : new BigDecimal(textValue); } else if (iFieldValue != null && !typeClass.isArray() && iFieldValue.getClass().isArray()) { // DESTINATION VALUE IS NOT AN ARRAY: ASSIGN THE FIRST ONE ELEMENT value = ((Object[]) iFieldValue)[0]; } else if (typeClass.isEnum()) { value = Enum.valueOf((Class) typeClass.getLanguageType(), textValue.toUpperCase()); } else { value = iFieldValue; } } if (value != null) { // TODO is this the right place to do this...? Class<?> valueClass = value.getClass(); // SUCH A MONSTER!!! MOVE THIS LOGIC IN SchemaClass.isAssignableFrom... if (value instanceof VirtualObject && !(typeClass.getLanguageType() instanceof Class<?> && ((Class<?>) typeClass.getLanguageType()).isAssignableFrom(VirtualObject.class)) && ((VirtualObject) value).getSuperClassObject() != null) { if (ComposedEntity.class .isAssignableFrom(((VirtualObject) value).getSuperClassObject().getClass())) { value = ((VirtualObject) value).getSuperClassObject(); valueClass = value.getClass(); } } if (value instanceof ComposedEntity<?> && !typeClass.isAssignableFrom(valueClass)) { value = ((ComposedEntity<?>) value).getEntity(); } } if (value == null && typeClass.isPrimitive()) { log.warn("Cannot set the field value to null for primitive types! Field: " + getEntity() + "." + name + " of class " + expectedType.getName() + ". Setting value to 0."); // SET THE VALUE TO 0 value = SchemaHelper.assignDefaultValueToLiteral(typeClass); } return value; }
From source file:org.eclipse.gyrex.monitoring.internal.mbeans.MetricSetMBean.java
private OpenType detectType(final Class type) { if ((Long.class == type) || (Long.TYPE == type)) { return SimpleType.LONG; } else if ((Integer.class == type) || (Integer.TYPE == type)) { return SimpleType.INTEGER; } else if ((Double.class == type) || (Double.TYPE == type)) { return SimpleType.DOUBLE; } else if ((Float.class == type) || (Float.TYPE == type)) { return SimpleType.FLOAT; } else if ((Byte.class == type) || (Byte.TYPE == type)) { return SimpleType.BYTE; } else if ((Short.class == type) || (Short.TYPE == type)) { return SimpleType.SHORT; } else if ((Boolean.class == type) || (Boolean.TYPE == type)) { return SimpleType.BOOLEAN; } else if (BigDecimal.class == type) { return SimpleType.BIGDECIMAL; } else if (BigInteger.class == type) { return SimpleType.BIGINTEGER; } else if ((Character.class == type) || (Character.TYPE == type)) { return SimpleType.CHARACTER; }// www .j a va 2 s . co m // last fallback to strings if (isConvertibleToString(type)) { return SimpleType.STRING; } // give up return null; }
From source file:net.yck.wkrdb.common.shared.PropertyConverter.java
/** * Performs a data type conversion from the specified value object to the * given target data class. If additional information is required for this * conversion, it is obtained from {@code DefaultConversionHandler.INSTANCE} * object. If the class is a primitive type (Integer.TYPE, Boolean.TYPE, * etc), the value returned will use the wrapper type (Integer.class, * Boolean.class, etc)./* ww w . j a v a 2 s .com*/ * * @param cls * the target class of the converted value * @param value * the value to convert * @return the converted value * @throws ConversionException * if the value is not compatible with the requested type */ public static Object to(Class<?> cls, Object value) throws ConversionException { if (cls.isInstance(value)) { return value; // no conversion needed } if (String.class.equals(cls)) { return String.valueOf(value); } if (Boolean.class.equals(cls) || Boolean.TYPE.equals(cls)) { return toBoolean(value); } else if (Character.class.equals(cls) || Character.TYPE.equals(cls)) { return toCharacter(value); } else if (Number.class.isAssignableFrom(cls) || cls.isPrimitive()) { if (Integer.class.equals(cls) || Integer.TYPE.equals(cls)) { return toInteger(value); } else if (Long.class.equals(cls) || Long.TYPE.equals(cls)) { return toLong(value); } else if (Byte.class.equals(cls) || Byte.TYPE.equals(cls)) { return toByte(value); } else if (Short.class.equals(cls) || Short.TYPE.equals(cls)) { return toShort(value); } else if (Float.class.equals(cls) || Float.TYPE.equals(cls)) { return toFloat(value); } else if (Double.class.equals(cls) || Double.TYPE.equals(cls)) { return toDouble(value); } else if (BigInteger.class.equals(cls)) { return toBigInteger(value); } else if (BigDecimal.class.equals(cls)) { return toBigDecimal(value); } } else if (Date.class.equals(cls)) { return toDate(value, DefaultConversionHandler.INSTANCE.getDateFormat()); } else if (Calendar.class.equals(cls)) { return toCalendar(value, DefaultConversionHandler.INSTANCE.getDateFormat()); } else if (URL.class.equals(cls)) { return toURL(value); } else if (Locale.class.equals(cls)) { return toLocale(value); } else if (isEnum(cls)) { return convertToEnum(cls, value); } else if (Color.class.equals(cls)) { return toColor(value); } else if (cls.getName().equals(INTERNET_ADDRESS_CLASSNAME)) { return toInternetAddress(value); } else if (InetAddress.class.isAssignableFrom(cls)) { return toInetAddress(value); } throw new ConversionException("The value '" + value + "' (" + value.getClass() + ")" + " can't be converted to a " + cls.getName() + " object"); }
From source file:org.rhq.core.domain.server.PersistenceUtility.java
@SuppressWarnings("unchecked") // used in hibernate.jsp public static Object cast(String value, Type hibernateType) { if (hibernateType instanceof PrimitiveType) { Class<?> type = ((PrimitiveType) hibernateType).getPrimitiveClass(); if (type.equals(Byte.TYPE)) { return Byte.valueOf(value); } else if (type.equals(Short.TYPE)) { return Short.valueOf(value); } else if (type.equals(Integer.TYPE)) { return Integer.valueOf(value); } else if (type.equals(Long.TYPE)) { return Long.valueOf(value); } else if (type.equals(Float.TYPE)) { return Float.valueOf(value); } else if (type.equals(Double.TYPE)) { return Double.valueOf(value); } else if (type.equals(Boolean.TYPE)) { return Boolean.valueOf(value); }/*from w w w. ja v a 2 s .c o m*/ } else if (hibernateType instanceof EntityType) { String entityName = ((EntityType) hibernateType).getAssociatedEntityName(); try { Class<?> entityClass = Class.forName(entityName); Object entity = entityClass.newInstance(); Field primaryKeyField = entityClass.getDeclaredField("id"); primaryKeyField.setAccessible(true); primaryKeyField.setInt(entity, Integer.valueOf(value)); return entity; } catch (Throwable t) { throw new IllegalArgumentException("Type[" + entityName + "] must have PK field named 'id'"); } } else if (hibernateType instanceof CustomType) { if (Enum.class.isAssignableFrom(hibernateType.getReturnedClass())) { Class<? extends Enum<?>> enumClass = hibernateType.getReturnedClass(); Enum<?>[] enumValues = enumClass.getEnumConstants(); try { int enumOrdinal = Integer.valueOf(value); try { return enumValues[enumOrdinal]; } catch (ArrayIndexOutOfBoundsException aioobe) { throw new IllegalArgumentException("There is no " + enumClass.getSimpleName() + " enum with ordinal '" + enumOrdinal + "'"); } } catch (NumberFormatException nfe) { String ucaseValue = value.toUpperCase(); for (Enum<?> nextEnum : enumValues) { if (nextEnum.name().toUpperCase().equals(ucaseValue)) { return nextEnum; } } throw new IllegalArgumentException( "There is no " + enumClass.getSimpleName() + " enum with name '" + value + "'"); } } } return value; }
From source file:com.browseengine.bobo.serialize.JSONSerializer.java
private static void loadObject(Object retObj, Field f, JSONObject jsonObj) throws JSONSerializationException { String key = f.getName();//from www . java 2s. c o m Class type = f.getType(); try { if (type.isPrimitive()) { if (type == Integer.TYPE) { f.setInt(retObj, jsonObj.getInt(key)); } else if (type == Long.TYPE) { f.setLong(retObj, jsonObj.getInt(key)); } else if (type == Short.TYPE) { f.setShort(retObj, (short) jsonObj.getInt(key)); } else if (type == Boolean.TYPE) { f.setBoolean(retObj, jsonObj.getBoolean(key)); } else if (type == Double.TYPE) { f.setDouble(retObj, jsonObj.getDouble(key)); } else if (type == Float.TYPE) { f.setFloat(retObj, (float) jsonObj.getDouble(key)); } else if (type == Character.TYPE) { char ch = jsonObj.getString(key).charAt(0); f.setChar(retObj, ch); } else if (type == Byte.TYPE) { f.setByte(retObj, (byte) jsonObj.getInt(key)); } else { throw new JSONSerializationException("Unknown primitive: " + type); } } else if (type == String.class) { f.set(retObj, jsonObj.getString(key)); } else if (JSONSerializable.class.isAssignableFrom(type)) { JSONObject jObj = jsonObj.getJSONObject(key); JSONSerializable serObj = deSerialize(type, jObj); f.set(retObj, serObj); } } catch (Exception e) { throw new JSONSerializationException(e.getMessage(), e); } }
From source file:net.sf.json.JSONDynaBean.java
/** * DOCUMENT ME!/*from w ww .java2 s.c o m*/ * * @param name DOCUMENT ME! * * @return DOCUMENT ME! */ public Object get(String name) { Object value = dynaValues.get(name); if (value != null) { return value; } Class type = getDynaProperty(name).getType(); if (type == null) { throw new NullPointerException("Unspecified property type for " + name); } if (!type.isPrimitive()) { return value; } if (type == Boolean.TYPE) { return Boolean.FALSE; } else if (type == Byte.TYPE) { return new Byte((byte) 0); } else if (type == Character.TYPE) { return new Character((char) 0); } else if (type == Short.TYPE) { return new Short((short) 0); } else if (type == Integer.TYPE) { return new Integer(0); } else if (type == Long.TYPE) { return new Long(0); } else if (type == Float.TYPE) { return new Float(0.0); } else if (type == Double.TYPE) { return new Double(0); } return null; }
From source file:org.atemsource.atem.impl.common.attribute.primitive.PrimitiveTypeFactory.java
public PrimitiveType getPrimitiveType(Class primitiveType) { if (String.class.isAssignableFrom(primitiveType)) { return new SimpleTextType(); } else if (Boolean.TYPE.isAssignableFrom(primitiveType)) { final BooleanTypeImpl booleanTypeImpl = new BooleanTypeImpl(); booleanTypeImpl.setNullable(false); return booleanTypeImpl; } else if (Boolean.class.isAssignableFrom(primitiveType)) { return new BooleanTypeImpl(); } else if (double.class.isAssignableFrom(primitiveType)) { final DoubleType doubleTypeImpl = new DoubleType(); doubleTypeImpl.setNullable(false); return doubleTypeImpl; } else if (Double.class.isAssignableFrom(primitiveType)) { return new DoubleType(); } else if (float.class.isAssignableFrom(primitiveType)) { final FloatTypeImpl typeImpl = new FloatTypeImpl(); typeImpl.setNullable(false);/*w ww.ja va2s . co m*/ return typeImpl; } else if (Float.class.isAssignableFrom(primitiveType)) { return new FloatTypeImpl(); } else if (BigDecimal.class.isAssignableFrom(primitiveType)) { return new BigDecimalTypeImpl(); } else if (int.class.isAssignableFrom(primitiveType)) { final IntegerType integerTypeImpl = new IntegerType(); integerTypeImpl.setNullable(false); return integerTypeImpl; } else if (Integer.class.isAssignableFrom(primitiveType)) { return new IntegerType(); } else if (long.class.isAssignableFrom(primitiveType)) { return new LongType(false); } else if (Long.class.isAssignableFrom(primitiveType)) { return new LongType(true); } else if (Number.class.isAssignableFrom(primitiveType)) { return new SimplePrimitiveType(Number.class, true); } else if (Character.TYPE.isAssignableFrom(primitiveType)) { return new SimplePrimitiveType(Character.TYPE, false); } else if (Character.class.isAssignableFrom(primitiveType)) { return new SimplePrimitiveType(Character.class, true); } else if (BigDecimal.class.isAssignableFrom(primitiveType)) { return new SimplePrimitiveType(BigDecimal.class, true); } else if (BigInteger.class.isAssignableFrom(primitiveType)) { return new SimplePrimitiveType(BigInteger.class, true); } else if (Byte.TYPE.isAssignableFrom(primitiveType)) { return new SimplePrimitiveType(Byte.TYPE, false); } else if (byte[].class.isAssignableFrom(primitiveType)) { return new SimplePrimitiveType(byte[].class, true); } else if (Byte.class.isAssignableFrom(primitiveType)) { return new SimplePrimitiveType(Byte.class, true); } else if (Enum.class.isAssignableFrom(primitiveType)) { return new SimpleEnumType(primitiveType); } else { PrimitiveType found = classToType.get(primitiveType); Class parentClass = primitiveType; while (found == null && parentClass.getSuperclass() != null) { found = classToType.get(parentClass); parentClass = parentClass.getSuperclass(); } if (found == null) { for (Class<?> interfaze : primitiveType.getInterfaces()) { found = classToType.get(interfaze); if (found != null) { return found; } } } return found; } }