List of usage examples for java.lang Class isPrimitive
@HotSpotIntrinsicCandidate public native boolean isPrimitive();
From source file:com.laxser.blitz.lama.core.UpdateOperation.java
private Object executeSignle(DataAccess dataAccess, Map<String, Object> parameters, Class<?> returnType) { if (returnGeneratedKeys) { return dataAccess.insertReturnId(sql, modifier, parameters); } else {/* ww w. ja v a 2 s . co m*/ // UPDATE / DELETE int updated = dataAccess.update(sql, modifier, parameters); if (returnType == void.class) { return null; } // ? if (returnType.isPrimitive()) { returnType = ClassUtils.primitiveToWrapper(returnType); } // ? if (returnType == Integer.class) { return Integer.valueOf(updated); } else if (returnType == Long.class) { return Long.valueOf(updated); } else if (returnType == Boolean.class) { return Boolean.valueOf(updated > 0); } else if (returnType == Long.class) { return Long.valueOf(updated); } else if (returnType == Double.class) { return Double.valueOf(updated); } else if (returnType == Float.class) { return Float.valueOf(updated); } else if (Number.class.isAssignableFrom(returnType)) { return updated; } } return null; // }
From source file:com.googlecode.jsonplugin.JSONWriter.java
/** * Detect cyclic references/* ww w .j av a 2 s. c om*/ */ private void value(Object object, Method method) throws JSONException { if (object == null) { this.add("null"); return; } if (this.stack.contains(object)) { Class clazz = object.getClass(); //cyclic reference if (clazz.isPrimitive() || clazz.equals(String.class)) { this.process(object, method); } else { if (log.isDebugEnabled()) { log.debug("Cyclic reference detected on " + object); } this.add("null"); } return; } this.process(object, method); }
From source file:com.github.nmorel.gwtjackson.rebind.RebindConfiguration.java
/** * @param clazz class to find the type/*ww w.j av a 2 s. c om*/ * * @return the {@link JType} denoted by the class given in parameter */ private JType findType(Class<?> clazz) { if (clazz.isPrimitive()) { return JPrimitiveType.parse(clazz.getCanonicalName()); } else if (clazz.isArray()) { try { return context.getTypeOracle().parse(clazz.getCanonicalName()); } catch (TypeOracleException e) { logger.log(TreeLogger.WARN, "Cannot find the array denoted by the class " + clazz.getCanonicalName()); return null; } } else { return findClassType(clazz); } }
From source file:com.opensymphony.xwork2.conversion.impl.XWorkConverter.java
/** * Looks for a TypeConverter in the default mappings. * * @param clazz the class the TypeConverter must handle * @return a TypeConverter to handle the specified class or null if none can be found */// w w w . ja va2s . co m public TypeConverter lookup(Class clazz) { if (clazz.isPrimitive()) { /** * if it is primitive use default converter which allows to define different converters per type * @see XWorkBasicConverter */ return defaultTypeConverter; } return lookup(clazz.getName()); }
From source file:com.jaspersoft.ireport.designer.data.fieldsproviders.hibernate.HQLFieldsReader.java
public Vector readFields() throws Exception { prepareQuery();/*from w w w .j a v a2 s.c om*/ SessionFactory hb_sessionFactory = null; Session hb_session = null; Transaction transaction = null; notScalars.clear(); try { IReportConnection conn = IReportManager.getInstance().getDefaultConnection(); if (!(conn instanceof JRHibernateConnection)) { throw new Exception("No Hibernate connection selected."); } hb_session = ((JRHibernateConnection) conn).createSession(); if (hb_session == null) { throw new Exception("Problem creating the Session object for Hibernate"); } transaction = hb_session.beginTransaction(); Query q = hb_session.createQuery(getQueryString()); Iterator paramIterator = queryParameters.keySet().iterator(); while (paramIterator.hasNext()) { String hqlParamName = "" + paramIterator.next(); setParameter(hb_session, q, hqlParamName, queryParameters.get(hqlParamName)); } q.setFetchSize(1); java.util.Iterator iterator = q.iterate(); // this is a stupid thing: iterator.next(); String[] aliases = q.getReturnAliases(); Type[] types = q.getReturnTypes(); Vector fields = new Vector(); for (int i = 0; i < types.length; ++i) { if (types[i].isComponentType() || types[i].isEntityType()) { // look for alias... String aliasName = null; if (aliases != null && aliases.length > i && !aliases[i].equals(i + "")) { aliasName = aliases[i]; JRDesignField field = new JRDesignField(); field.setName(aliases[i]); Class clazzRT = types[i].getReturnedClass(); if (clazzRT.isPrimitive()) { clazzRT = MethodUtils.getPrimitiveWrapper(clazzRT); } String returnType = clazzRT.getName(); field.setValueClassName(returnType); field.setDescription(aliases[i]); fields.add(field); } // look for fields like for a javabean... java.beans.PropertyDescriptor[] pd = org.apache.commons.beanutils.PropertyUtils .getPropertyDescriptors(types[i].getReturnedClass()); if (aliasName != null) { notScalars.add(new FieldClassWrapper(aliasName, types[i].getReturnedClass().getName())); } else { notScalars.add(types[i].getReturnedClass().getName()); } for (int nd = 0; nd < pd.length; ++nd) { String fieldName = pd[nd].getName(); if (pd[nd].getPropertyType() != null && pd[nd].getReadMethod() != null) { if (fieldName.equals("class")) continue; Class clazzRT = pd[nd].getPropertyType(); if (clazzRT.isPrimitive()) { clazzRT = MethodUtils.getPrimitiveWrapper(clazzRT); } String returnType = clazzRT.getName(); JRDesignField field = new JRDesignField(); field.setName(fieldName); field.setValueClassName(returnType); if (types.length > 1 && aliasName != null) { fieldName = aliasName + "." + fieldName; field.setDescription(fieldName); //Field returned by " +methods[i].getName() + " (real type: "+ returnType +")"); field.setName(fieldName); } fields.add(field); } } } else { String fieldName = types[i].getName(); if (aliases != null && aliases.length > i && !aliases[i].equals("" + i)) fieldName = aliases[i]; Class clazzRT = types[i].getReturnedClass(); if (clazzRT.isPrimitive()) { clazzRT = MethodUtils.getPrimitiveWrapper(clazzRT); } String returnType = clazzRT.getName(); JRDesignField field = new JRDesignField(); field.setName(fieldName); field.setValueClassName(returnType); //field.setDescription(""); fields.add(field); } } /* else { for (int i =0; i<types.length; ++i) { if (aliases != null && aliases.length > 0 && !aliases[0].equals(""+i)) { JRField field = new JRField(aliases[i], types[i].getReturnedClass().getName()); field.setDescription("The whole entity/component object"); fields.add(field); } // out.println(types[i].getName() + " " + types[i].getReturnedClass().getName() + "<br>"); } } */ return fields; } catch (Exception ex) { ex.printStackTrace(); throw ex; } finally { if (transaction != null) try { transaction.rollback(); } catch (Exception ex) { } if (hb_session != null) try { hb_session.close(); } catch (Exception ex) { } } }
From source file:com.bstek.dorado.data.method.MethodAutoMatchingUtils.java
private static int isTypeAssignableFrom(Type targetType, Type sourceType) { Class<?> targetClass = toClass(targetType); Class<?> sourceClass = toClass(sourceType); int rate = 5; if (!targetType.equals(sourceType)) { rate = 4;//from w w w . java2 s. c om boolean b = targetClass.isAssignableFrom(sourceClass); if (b) { Class<?> targetElementClass = getElementType(targetType); if (targetElementClass != null) { Class<?> sourceElementClass = getElementType(sourceType); if (sourceElementClass != null) { return isTypeAssignableFrom(targetElementClass, sourceElementClass); } } } else if (targetClass.isPrimitive()) { targetClass = MethodUtils.toNonPrimitiveClass(targetClass); b = targetClass.isAssignableFrom(sourceClass); } if (!b) { b = Number.class.isAssignableFrom(targetClass) && Number.class.isAssignableFrom(sourceClass); } if (!b) { rate = 0; } } return rate; }
From source file:com.crowsofwar.gorecore.config.ConfigLoader.java
/** * Tries to load the field of the {@link #obj object} with the correct * {@link #data}./*from www . j av a2 s.c om*/ * <p> * If the field isn't marked with @Load, does nothing. Otherwise, will * attempt to set the field's value (with reflection) to the data set in the * map. * * @param field * The field to load */ private <T> void loadField(Field field) { Class<?> cls = field.getDeclaringClass(); Class<?> fieldType = field.getType(); if (fieldType.isPrimitive()) fieldType = ClassUtils.primitiveToWrapper(fieldType); try { if (field.getAnnotation(Load.class) != null) { if (Modifier.isStatic(field.getModifiers())) { GoreCore.LOGGER.log(Level.WARN, "[ConfigLoader] Warning: Not recommended to mark static fields with @Load, may work out weirdly."); GoreCore.LOGGER.log(Level.WARN, "This field is " + field.getDeclaringClass().getName() + "#" + field.getName()); GoreCore.LOGGER.log(Level.WARN, "Use a singleton instead!"); } // Should load this field HasCustomLoader loaderAnnot = fieldType.getAnnotation(HasCustomLoader.class); CustomLoaderSettings loaderInfo = loaderAnnot == null ? new CustomLoaderSettings() : new CustomLoaderSettings(loaderAnnot); Object fromData = data.get(field.getName()); Object setTo; boolean tryDefaultValue = fromData == null || ignoreConfigFile; if (tryDefaultValue) { // Nothing present- try to load default value if (field.get(obj) != null) { setTo = field.get(obj); } else { throw new ConfigurationException.UserMistake( "No configured definition for " + field.getName() + ", no default value"); } } else { // Value present in configuration. // Use the present value from map: fromData Class<Object> from = (Class<Object>) fromData.getClass(); Class<?> to = fieldType; setTo = convert(fromData, to, field.getName()); } usedValues.put(field.getName(), setTo); // If not a java class, probably custom; needs to NOT have the // '!!' in front if (!setTo.getClass().getName().startsWith("java")) { representer.addClassTag(setTo.getClass(), Tag.MAP); classTags.add(setTo.getClass()); } // Try to apply custom loader, if necessary try { if (loaderInfo.hasCustomLoader()) loaderInfo.customLoaderClass.newInstance().load(null, setTo); } catch (InstantiationException | IllegalAccessException e) { throw new ConfigurationException.ReflectionException( "Couldn't create a loader class of loader " + loaderInfo.customLoaderClass.getName(), e); } catch (Exception e) { throw new ConfigurationException.Unexpected( "An unexpected error occurred while using a custom object loader from config. Offending loader is: " + loaderInfo.customLoaderClass, e); } if (loaderInfo.loadFields) field.set(obj, setTo); } } catch (ConfigurationException e) { throw e; } catch (Exception e) { throw new ConfigurationException.Unexpected("An unexpected error occurred while loading field \"" + field.getName() + "\" in class \"" + cls.getName() + "\"", e); } }
From source file:net.sf.json.JSONDynaBean.java
/** * DOCUMENT ME!//from ww w . j a v a 2 s .c om * * @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:eu.qualityontime.commons.MethodUtils.java
/** * <p>/*from w w w . ja v a2 s. c om*/ * Determine whether a type can be used as a parameter in a method * invocation. This method handles primitive conversions correctly. * </p> * * <p> * In order words, it will match a <code>Boolean</code> to a * <code>boolean</code>, a <code>Long</code> to a <code>long</code>, a * <code>Float</code> to a <code>float</code>, a <code>Integer</code> to a * <code>int</code>, and a <code>Double</code> to a <code>double</code>. Now * logic widening matches are allowed. For example, a <code>Long</code> will * not match a <code>int</code>. * * @param parameterType * the type of parameter accepted by the method * @param parameterization * the type of parameter being tested * * @return true if the assignment is compatible. */ public static final boolean isAssignmentCompatible(final Class<?> parameterType, final Class<?> parameterization) { // try plain assignment if (parameterType.isAssignableFrom(parameterization)) { return true; } if (parameterType.isPrimitive()) { // this method does *not* do widening - you must specify exactly // is this the right behaviour? final Class<?> parameterWrapperClazz = getPrimitiveWrapper(parameterType); if (parameterWrapperClazz != null) { return parameterWrapperClazz.equals(parameterization); } } return false; }
From source file:com.gdcn.modules.db.jdbc.processor.CamelBeanProcessor.java
/** * Creates a new object and initializes its fields from the ResultSet. * @param <T> The type of bean to create * @param rs The result set.//from w w w .j a v a2 s.co m * @param type The bean type (the return type of the object). * @param props The property descriptors. * @param columnToProperty The column indices in the result set. * @return An initialized object. * @throws SQLException if a database error occurs. */ private <T> T createBean(ResultSet rs, Class<T> type, PropertyDescriptor[] props, int[] columnToProperty) throws SQLException { T bean = this.newInstance(type); for (int i = 1; i < columnToProperty.length; i++) { if (columnToProperty[i] == PROPERTY_NOT_FOUND) { continue; } PropertyDescriptor prop = props[columnToProperty[i]]; Class<?> propType = prop.getPropertyType(); Object value = this.processColumn(rs, i, propType); if (propType != null && value == null && propType.isPrimitive()) { value = primitiveDefaults.get(propType); } this.callSetter(bean, prop, value); } return bean; }