List of usage examples for java.lang Class isArray
@HotSpotIntrinsicCandidate public native boolean isArray();
From source file:com.astamuse.asta4d.web.form.flow.base.AbstractFormFlowSnippet.java
/** * Sub classes could override this method to customize how to handle the injection trace data for type unmatch errors. * // ww w . ja v a 2 s . c o m * @param fieldName * @param fieldDataType * @param rawTraceData * @return */ protected Object convertRawInjectionTraceDataToRenderingData(String fieldName, Class fieldDataType, Object rawTraceData) { if (fieldDataType.isArray() && rawTraceData.getClass().isArray()) { return rawTraceData; } else if (rawTraceData.getClass().isArray()) {// but field data type is // not array if (Array.getLength(rawTraceData) > 0) { return Array.get(rawTraceData, 0); } else { return null; } } else { return rawTraceData; } }
From source file:jef.database.DefaultSqlProcessor.java
@SuppressWarnings({ "unchecked", "rawtypes" }) public Object collectValueToContainer(List<? extends IQueryableEntity> records, Class<?> containerType, String targetField) {// w w w . j a va 2 s .c om Collection c = null; if (containerType == Set.class) { c = new HashSet(); } else if (containerType == List.class || containerType.isArray()) { c = new ArrayList(); } else { if (!records.isEmpty()) { BeanWrapper bean = BeanWrapper.wrap(records.get(0)); return bean.getPropertyValue(targetField); } return null; // throw new IllegalArgumentException(containerType + // " is not a known collection type."); } for (IQueryableEntity d : records) { BeanWrapper bean = BeanWrapper.wrap(d); c.add(bean.getPropertyValue(targetField)); } if (containerType.isArray()) { return c.toArray((Object[]) Array.newInstance(containerType.getComponentType(), c.size())); } else { return c; } }
From source file:com.examples.with.different.packagename.testcarver.AbstractConverter.java
/** * Provide a String representation of a <code>java.lang.Class</code>. * @param type The <code>java.lang.Class</code>. * @return The String representation.//from w ww. ja va2s. co m */ String toString(Class type) { String typeName = null; if (type == null) { typeName = "null"; } else if (type.isArray()) { Class elementType = type.getComponentType(); int count = 1; while (elementType.isArray()) { elementType = elementType.getComponentType(); count++; } typeName = elementType.getName(); for (int i = 0; i < count; i++) { typeName += "[]"; } } else { typeName = type.getName(); } if (typeName.startsWith("java.lang.") || typeName.startsWith("java.util.") || typeName.startsWith("java.math.")) { typeName = typeName.substring("java.lang.".length()); } else if (typeName.startsWith(PACKAGE)) { typeName = typeName.substring(PACKAGE.length()); } return typeName; }
From source file:com.github.erchu.beancp.MapperImpl.java
@Override @SuppressWarnings("TooBroadCatch") public <S, D> Optional<D> mapIfMapperAvailable(final S source, final Class<D> destinationClass) throws MappingException { notNull(source, "source"); notNull(destinationClass, "destinationClass"); Class sourceClass = source.getClass(); try {//from w ww . j av a 2s . c o m Converter<S, D> converter = (Converter<S, D>) MapperExecutorSelector .getBestMatchingConverter(sourceClass, destinationClass, _converters); if (converter != null) { if (sourceClass.isArray() && sourceClass.getComponentType().isPrimitive()) { Object[] sourceWrapper = getArrayOfPrimitiveTypeWrapper(sourceClass, source); return Optional.of(((Converter<Object, D>) converter).convert(this, sourceWrapper)); } else { return Optional.of(converter.convert(this, source)); } } DeclarativeMapImpl<S, D> map = (DeclarativeMapImpl<S, D>) MapperExecutorSelector .getBestMatchingDeclarativeMap(sourceClass, destinationClass, _maps); D destination = null; if (map != null && map.getDestinationObjectBuilder() != null) { destination = constructObjectUsingDestinationObjectBuilder(map.getDestinationObjectBuilder(), destinationClass); } // if DeclarativeMapImpl is not available or has no specific destination object builder if (destination == null) { destination = constructObjectUsingDefaultConstructor(destinationClass); } if (mapIfMapperAvailable(source, destination)) { return Optional.of(destination); } else { return Optional.empty(); } } catch (Exception ex) { throw new MappingException(String.format("Failed to map from %s to %s", sourceClass, destinationClass), ex); } }
From source file:de.micromata.genome.util.bean.PrivateBeanUtils.java
/** * Gets the bean size intern./* www . ja va 2 s.c om*/ * * @param bean the bean * @param clazz the clazz * @param m the m * @param classNameMatcher the class name matcher * @param fieldNameMatcher the field name matcher * @return the bean size intern */ public static int getBeanSizeIntern(Object bean, Class<?> clazz, IdentityHashMap<Object, Object> m, Matcher<String> classNameMatcher, Matcher<String> fieldNameMatcher) { if (classNameMatcher.match(clazz.getName()) == false) { return 0; } if (clazz.isArray() == true) { if (clazz == boolean[].class) { return (((boolean[]) bean).length * 4); } else if (clazz == char[].class) { return (((char[]) bean).length * 2); } else if (clazz == byte[].class) { return (((byte[]) bean).length * 1); } else if (clazz == short[].class) { return (((short[]) bean).length * 2); } else if (clazz == int[].class) { return (((int[]) bean).length * 4); } else if (clazz == long[].class) { return (((long[]) bean).length * 4); } else if (clazz == float[].class) { return (((float[]) bean).length * 4); } else if (clazz == double[].class) { return (((double[]) bean).length * 8); } else { int length = Array.getLength(bean); int ret = (length * 4); for (int i = 0; i < length; ++i) { ret += getBeanSize(Array.get(bean, i), m, classNameMatcher, fieldNameMatcher); } return ret; } } int ret = 0; try { for (Field f : clazz.getDeclaredFields()) { int mod = f.getModifiers(); if (Modifier.isStatic(mod) == true) { continue; } if (fieldNameMatcher.match(clazz.getName() + "." + f.getName()) == false) { continue; } if (f.getType() == Boolean.TYPE) { ret += 4; } else if (f.getType() == Character.TYPE) { ret += 2; } else if (f.getType() == Byte.TYPE) { ret += 1; } else if (f.getType() == Short.TYPE) { ret += 2; } else if (f.getType() == Integer.TYPE) { ret += 4; } else if (f.getType() == Long.TYPE) { ret += 8; } else if (f.getType() == Float.TYPE) { ret += 4; } else if (f.getType() == Double.TYPE) { ret += 8; } else { ret += 4; Object o = null; try { o = readField(bean, f); if (o == null) { continue; } } catch (NoClassDefFoundError ex) { // nothing continue; } int nestedsize = getBeanSize(o, o.getClass(), m, classNameMatcher, fieldNameMatcher); ret += nestedsize; } } } catch (NoClassDefFoundError ex) { // ignore here. } if (clazz == Object.class || clazz.getSuperclass() == null) { return ret; } ret += getBeanSizeIntern(bean, clazz.getSuperclass(), m, classNameMatcher, fieldNameMatcher); return ret; }
From source file:com.feilong.core.lang.ClassUtilTest.java
/** * class info map for LOGGER.// w w w . j a v a 2 s. c o m * * @param klass * the klass * @return <code>klass</code> nullempty, {@link Collections#emptyMap()}<br> */ public static Map<String, Object> getClassInfoMapForLog(Class<?> klass) { if (isNullOrEmpty(klass)) { return Collections.emptyMap(); } Map<String, Object> map = new LinkedHashMap<String, Object>(); map.put("clz.getCanonicalName()", klass.getCanonicalName());//"com.feilong.core.date.DatePattern" map.put("clz.getName()", klass.getName());//"com.feilong.core.date.DatePattern" map.put("clz.getSimpleName()", klass.getSimpleName());//"DatePattern" map.put("clz.getComponentType()", klass.getComponentType()); // ?"". ,voidboolean?byte?char?short?int?long?float double?. map.put("clz.isPrimitive()", klass.isPrimitive()); // ?"".,. map.put("clz.isLocalClass()", klass.isLocalClass()); // ?"?".?,?,?"""??". map.put("clz.isMemberClass()", klass.isMemberClass()); //isSynthetic()?Class?"??".java??false,?true.,JVM???,java??"??"? map.put("clz.isSynthetic()", klass.isSynthetic()); map.put("clz.isArray()", klass.isArray()); map.put("clz.isAnnotation()", klass.isAnnotation()); //??true. map.put("clz.isAnonymousClass()", klass.isAnonymousClass()); map.put("clz.isEnum()", klass.isEnum()); return map; }
From source file:de.escalon.hypermedia.spring.ActionInputParameter.java
/** * Allows convenient access to multiple call values in case that this input parameter is an array or collection. * Make sure to check {@link #isArrayOrCollection()} before calling this method. * * @return call values//w w w. j a va 2 s .c o m * @throws UnsupportedOperationException * if this input parameter is not an array or collection */ public Object[] getCallValues() { Object[] callValues; if (!isArrayOrCollection()) { throw new UnsupportedOperationException("parameter is not an array or collection"); } Object callValue = getCallValue(); if (callValue == null) { callValues = new Object[0]; } else { Class<?> parameterType = getParameterType(); if (parameterType.isArray()) { callValues = (Object[]) callValue; } else { callValues = ((Collection<?>) callValue).toArray(); } } return callValues; }
From source file:com.jada.admin.AdminLookupDispatchAction.java
@SuppressWarnings("unchecked") protected void encodeForm(ActionForm form) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException, SecurityException, NoSuchFieldException { Method methods[] = form.getClass().getDeclaredMethods(); String encodeFields[] = {};/* w w w. j ava 2s. c om*/ try { Field field = form.getClass().getField("encodeFields"); encodeFields = (String[]) field.get(form); } catch (java.lang.NoSuchFieldException e) { } for (Method m : methods) { if (!m.getName().startsWith("get")) { continue; } if (m.getParameterTypes().length > 0) { continue; } Class type = m.getReturnType(); if (type.isArray()) { Object objects[] = (Object[]) m.invoke(form); if (objects != null) { for (Object object : objects) { if (isActionForm(object.getClass())) { encodeForm((ActionForm) object); } } } continue; } if (isActionForm(type)) { ActionForm object = (ActionForm) m.invoke(form); encodeForm((ActionForm) object); continue; } if (type != String.class) { continue; } String fieldname = m.getName().substring(3, 4).toLowerCase() + m.getName().substring(4); boolean found = false; for (String f : encodeFields) { if (f.equals(fieldname)) { found = true; break; } } if (!found) { continue; } String value = (String) m.invoke(form); value = StringEscapeUtils.escapeHtml(value); String methodName = "set" + m.getName().substring(3); Class[] args = new Class[] { String.class }; Method setMethod = form.getClass().getDeclaredMethod(methodName, args); setMethod.invoke(form, value); } }
From source file:com.espertech.esper.event.xml.SchemaXMLEventType.java
private EventPropertyGetter doResolvePropertyGetter(String propertyExpression, boolean allowSimpleProperties) { EventPropertyGetter getter = propertyGetterCache.get(propertyExpression); if (getter != null) { return getter; }//from w ww . ja va2s . c om if (!allowSimpleProperties) { // see if this is an indexed property int index = ASTFilterSpecHelper.unescapedIndexOfDot(propertyExpression); if (index == -1) { // parse, can be an indexed property Property property = PropertyParser.parse(propertyExpression, false); if (!property.isDynamic()) { if (!(property instanceof IndexedProperty)) { return null; } IndexedProperty indexedProp = (IndexedProperty) property; getter = this.propertyGetters.get(indexedProp.getPropertyNameAtomic()); if (null == getter) { return null; } EventPropertyDescriptor descriptor = this.propertyDescriptorMap .get(indexedProp.getPropertyNameAtomic()); if (descriptor == null) { return null; } if (!descriptor.isIndexed()) { return null; } if (descriptor.getPropertyType() == NodeList.class) { FragmentFactory fragmentFactory = new FragmentFactoryDOMGetter( this.getEventAdapterService(), this, indexedProp.getPropertyNameAtomic()); return new XPathPropertyArrayItemGetter(getter, indexedProp.getIndex(), fragmentFactory); } } } } if (!isPropertyExpressionXPath) { Property prop = PropertyParser.parse(propertyExpression, false); boolean isDynamic = prop.isDynamic(); if (!isDynamic) { SchemaItem item = prop.getPropertyTypeSchema(schemaModelRoot, this.getEventAdapterService()); if (item == null) { return null; } getter = prop.getGetterDOM(schemaModelRoot, this.getEventAdapterService(), this, propertyExpression); if (getter == null) { return null; } Class returnType = SchemaUtil.toReturnType(item); if ((returnType != Node.class) && (returnType != NodeList.class)) { if (!returnType.isArray()) { getter = new DOMConvertingGetter(propertyExpression, (DOMPropertyGetter) getter, returnType); } else { getter = new DOMConvertingArrayGetter((DOMPropertyGetter) getter, returnType.getComponentType()); } } } else { return prop.getGetterDOM(); } } else { boolean allowFragments = !this.getConfigurationEventTypeXMLDOM().isXPathPropertyExpr(); getter = SchemaXMLPropertyParser.getXPathResolution(propertyExpression, getXPathFactory(), getRootElementName(), rootElementNamespace, schemaModel, this.getEventAdapterService(), this, allowFragments, this.getConfigurationEventTypeXMLDOM().getDefaultNamespace()); } propertyGetterCache.put(propertyExpression, getter); return getter; }
From source file:com.netspective.sparx.form.action.ActionWrapper.java
public FieldMutator locateMutator(String attributeName) { try {//from ww w. j a v a2s.co m final Method method = (Method) actionSchema.getAttributeSetterMethods().get(attributeName); if (method != null) { Class[] args = method.getParameterTypes(); if (args.length == 1) { Class arg = args[0]; if (java.lang.String.class.equals(arg) && arg.isArray()) { return new FieldMutator() { public void set(Object instance, Value value) throws Exception { method.invoke(instance, new Object[] { value.getTextValues() }); } }; } else if (Value.class.equals(arg)) { return new FieldMutator() { public void set(Object instance, Value value) throws Exception { method.invoke(instance, new Object[] { value }); } }; } else if (Date.class.equals(arg)) { return new FieldMutator() { public void set(Object instance, Value value) throws Exception { // assuming that if a date is expected then the Object of the value.getValue() will be a date method.invoke(instance, new Object[] { (Date) value.getValue() }); } }; } else { final XmlDataModelSchema.AttributeSetter as = (XmlDataModelSchema.AttributeSetter) actionSchema .getAttributeSetters().get(attributeName); if (as != null) { return new FieldMutator() { public void set(Object instance, Value value) throws Exception { if (value.getTextValue() != null) as.set(null, instance, value.getTextValue()); } }; } } } } else actionDialog.getLog().warn( "No setter method found for field " + attributeName + " in " + actionSchema.getBean()); } catch (Exception e) { actionDialog.getLog().error("Unable to create mutator '" + attributeName + "' for " + actionSchema.getBean() + ": " + e.getMessage(), e); throw new NestableRuntimeException(e); } // if we get to here, unable to set the attribute actionDialog.getLog() .warn("Unable to locate mutator '" + attributeName + "' for " + actionSchema.getBean()); return null; }