List of usage examples for java.lang Class isArray
@HotSpotIntrinsicCandidate public native boolean isArray();
From source file:com.ery.ertc.estorm.util.ClassSize.java
/** * The estimate of the size of a class instance depends on whether the JVM uses 32 or 64 bit addresses, that is it depends on the size * of an object reference. It is a linear function of the size of a reference, e.g. 24 + 5*r where r is the size of a reference (usually * 4 or 8 bytes).//from w w w .j a v a 2 s. co m * * This method returns the coefficients of the linear function, e.g. {24, 5} in the above example. * * @param cl * A class whose instance size is to be estimated * @param debug * debug flag * @return an array of 3 integers. The first integer is the size of the primitives, the second the number of arrays and the third the * number of references. */ @SuppressWarnings("unchecked") private static int[] getSizeCoefficients(Class cl, boolean debug) { int primitives = 0; int arrays = 0; // The number of references that a new object takes int references = nrOfRefsPerObj; int index = 0; for (; null != cl; cl = cl.getSuperclass()) { Field[] field = cl.getDeclaredFields(); if (null != field) { for (Field aField : field) { if (Modifier.isStatic(aField.getModifiers())) continue; Class fieldClass = aField.getType(); if (fieldClass.isArray()) { arrays++; references++; } else if (!fieldClass.isPrimitive()) { references++; } else {// Is simple primitive String name = fieldClass.getName(); if (name.equals("int") || name.equals("I")) primitives += Bytes.SIZEOF_INT; else if (name.equals("long") || name.equals("J")) primitives += Bytes.SIZEOF_LONG; else if (name.equals("boolean") || name.equals("Z")) primitives += Bytes.SIZEOF_BOOLEAN; else if (name.equals("short") || name.equals("S")) primitives += Bytes.SIZEOF_SHORT; else if (name.equals("byte") || name.equals("B")) primitives += Bytes.SIZEOF_BYTE; else if (name.equals("char") || name.equals("C")) primitives += Bytes.SIZEOF_CHAR; else if (name.equals("float") || name.equals("F")) primitives += Bytes.SIZEOF_FLOAT; else if (name.equals("double") || name.equals("D")) primitives += Bytes.SIZEOF_DOUBLE; } if (debug) { if (LOG.isDebugEnabled()) { LOG.debug("" + index + " " + aField.getName() + " " + aField.getType()); } } index++; } } } return new int[] { primitives, arrays, references }; }
From source file:net.servicefixture.util.ReflectionUtils.java
/** * Returns the extra [0] suffix(s) for an multi-dimensional array. *//*from w ww . j a v a2s . c o m*/ public static String getExtraMultiDimensionalArraySuffix(Class type) { StringBuilder sb = new StringBuilder(); int i = 0; while (type.isArray()) { type = type.getComponentType(); if (++i > 1) { sb.append("[0]"); } } return sb.toString(); }
From source file:io.github.benas.randombeans.util.ReflectionUtils.java
/** * Check if a type is an array type./*from w w w.ja v a 2 s .c o m*/ * * @param type the type to check. * @return true if the type is an array type, false otherwise. */ public static boolean isArrayType(final Class<?> type) { return type.isArray(); }
From source file:net.kemuri9.sling.filesystemprovider.impl.PersistenceHelper.java
/** * Create a {@link JSONObject} that represents the specified object. * @param obj the object to create a JSONObject representation for. * @return the JSONObject representation of the object. * @throws JSONException if an error occurs on creating the JSON data *///w w w.ja v a 2 s. c o m static JSONObject createJSONPropertyObject(Object obj) throws JSONException { String type = Object.class.getName().toString(); boolean isArray = false; if (obj != null) { if (obj.getClass().isArray()) { isArray = true; Class<?> elemType = obj.getClass().getComponentType(); if (elemType.isArray()) { throw new IllegalArgumentException("nested array types are not supported"); } type = elemType.getName(); } else { type = obj.getClass().getName().toString(); } } JSONObject jsonObj = new JSONObject(); jsonObj.put(FSPConstants.JSON_KEY_TYPE, type); boolean isBinary = false; if (isArray) { int arrSize = Array.getLength(obj); JSONArray arr = new JSONArray(); jsonObj.put(FSPConstants.JSON_KEY_VALUES, arr); for (int arr_i = 0; arr_i < arrSize; ++arr_i) { Object arr_val_i = Array.get(obj, arr_i); Object storage = convertToJSONStorage(arr_val_i); if (storage instanceof JSONStorage) { JSONStorage jsonStore = (JSONStorage) storage; isBinary |= jsonStore.isBinary; storage = jsonStore.value; } arr.put(storage); } } else { // singly valued type Object storage = convertToJSONStorage(obj); if (storage instanceof JSONStorage) { JSONStorage jsonStore = (JSONStorage) storage; storage = jsonStore.value; isBinary = jsonStore.isBinary; } jsonObj.put(FSPConstants.JSON_KEY_VALUE, storage); } if (isBinary) { jsonObj.put(FSPConstants.JSON_KEY_BINARY, true); } return jsonObj; }
From source file:ReflectUtils.java
/** * //from w w w . ja v a 2 s.c o m * @param type * * @return The unqualified (local) name of the class/interface. If the type is * an array, the [] characters will be appended. * */ public static String getShortName(Class type) { if (type.isArray()) { Class base = getClassFromArrayClass(type); String name = getShortName(base); return name + "[]"; } return getShortName(type.getName()); }
From source file:com.sf.ddao.orm.RSMapperFactoryRegistry.java
public static RSMapper createReusable(Method method) { final UseRSMapper useRSMapper = method.getAnnotation(UseRSMapper.class); if (useRSMapper != null) { try {//from w w w. j av a 2s. c om return useRSMapper.value().newInstance(); } catch (Exception e) { throw new RuntimeException(e); } } Class<?> returnClass = method.getReturnType(); if (returnClass.isArray()) { Class itemType = returnClass.getComponentType(); RowMapperFactory rowMapperFactory = getRowMapperFactory(itemType); return new ArrayRSMapper(rowMapperFactory, itemType); } Type returnType = method.getGenericReturnType(); if (Collection.class.isAssignableFrom(returnClass)) { Type[] actualTypeArguments = ((ParameterizedType) returnType).getActualTypeArguments(); Type itemType = actualTypeArguments[0]; return getCollectionORMapper(itemType); } if (Map.class.isAssignableFrom(returnClass)) { Type[] actualTypeArguments = ((ParameterizedType) returnType).getActualTypeArguments(); Type keyType = actualTypeArguments[0]; Type valueType = actualTypeArguments[1]; RowMapperFactory keyMapperFactory = getScalarMapper(keyType, 1, true); RowMapperFactory valueMapperFactory = getRowMapperFactory(valueType, 2); return new MapRSMapper(keyMapperFactory, valueMapperFactory); } return new SingleRowRSMapper(getRowMapperFactory(returnType)); }
From source file:ReflectUtils.java
/** * // w w w.j a v a2s.c om * @param theClass * A "normal", non-array type. * * @return The array version of the given type. For example, if you pass * <em>String.class</em>, you get <em>String[].class</em>. If * you pass <em>int.class</em>, you get <em>int[].class</em>. If * the given class is already an array type, it is returned. * * @see #getClassFromArrayClass(Class) * */ public static Class getArrayClassFromClass(Class theClass) { if (theClass.isArray()) return theClass; return Array.newInstance(theClass, 0).getClass(); }
From source file:com.phoenixnap.oss.ramlapisync.naming.SchemaHelper.java
/** * Maps primitives and other simple Java types into simple types supported by RAML * // w w w. ja v a2 s. com * @param clazz The Class to map * @return The Simple RAML ParamType which maps to this class or null if one is not found */ public static ParamType mapSimpleType(Class<?> clazz) { Class<?> targetClazz = clazz; if (targetClazz.isArray() && clazz.getComponentType() != null) { targetClazz = clazz.getComponentType(); } if (targetClazz.equals(Long.TYPE) || targetClazz.equals(Long.class) || targetClazz.equals(Integer.TYPE) || targetClazz.equals(Integer.class) || targetClazz.equals(Short.TYPE) || targetClazz.equals(Short.class) || targetClazz.equals(Byte.TYPE) || targetClazz.equals(Byte.class)) { return ParamType.INTEGER; } else if (targetClazz.equals(Float.TYPE) || targetClazz.equals(Float.class) || targetClazz.equals(Double.TYPE) || targetClazz.equals(Double.class) || targetClazz.equals(BigDecimal.class)) { return ParamType.NUMBER; } else if (targetClazz.equals(Boolean.class) || targetClazz.equals(Boolean.TYPE)) { return ParamType.BOOLEAN; } else if (targetClazz.equals(String.class)) { return ParamType.STRING; } return null; // default to string }
From source file:com.browseengine.bobo.serialize.JSONSerializer.java
public static JSONObject serializeJSONObject(JSONSerializable obj) throws JSONSerializationException, JSONException { if (obj instanceof JSONExternalizable) { return ((JSONExternalizable) obj).toJSON(); }//from ww w . j a va2 s . co m JSONObject jsonObj = new JSONObject(); Class clz = obj.getClass(); Field[] fields = clz.getDeclaredFields(); for (int i = 0; i < fields.length; ++i) { fields[i].setAccessible(true); int modifiers = fields[i].getModifiers(); if (!Modifier.isTransient(modifiers) && !Modifier.isStatic(modifiers)) { String name = fields[i].getName(); Class type = fields[i].getType(); try { Object value = fields[i].get(obj); if (type.isArray() && value != null) { int len = Array.getLength(value); Class cls = type.getComponentType(); JSONArray array = new JSONArray(); for (int k = 0; k < len; ++k) { dumpObject(array, cls, value, k); } jsonObj.put(name, array); } else { dumpObject(jsonObj, fields[i], obj); } } catch (Exception e) { throw new JSONSerializationException(e.getMessage(), e); } } } return jsonObj; }
From source file:org.kmnet.com.fw.web.el.Functions.java
/** * build query string from map or bean.//from ww w . j a v a2 s . c om * <p> * query string is encoded with "UTF-8". * </p> * @param params map or bean * @return query string. returns empty string if <code>params</code> is <code>null</code> or empty string or * {@link Iterable} or {@link BeanUtils#isSimpleValueType(Class)}. */ @SuppressWarnings("unchecked") public static String query(Object params) { if (params == null) { return ""; } Class<?> clazz = params.getClass(); if (clazz.isArray() || params instanceof Iterable || BeanUtils.isSimpleValueType(clazz)) { return ""; } String query; if (params instanceof Map) { query = mapToQuery((Map<String, Object>) params); } else { Map<String, Object> map = new LinkedHashMap<String, Object>(); BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(params); PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(clazz); for (PropertyDescriptor pd : pds) { String name = pd.getName(); if (!"class".equals(name)) { Object value = beanWrapper.getPropertyValue(name); map.put(name, value); } } query = mapToQuery(map, beanWrapper); } return query; }