List of usage examples for java.lang.reflect Array newInstance
public static Object newInstance(Class<?> componentType, int... dimensions) throws IllegalArgumentException, NegativeArraySizeException
From source file:com.sun.faces.renderkit.html_basic.MenuRenderer.java
protected Object handleArrayCase(FacesContext context, UISelectMany uiSelectMany, Class arrayClass, String[] newValues) throws ConverterException { Object result = null;/*w ww. j a v a 2 s . co m*/ Class elementType = null; Converter converter = null; int i = 0, len = (null != newValues ? newValues.length : 0); elementType = arrayClass.getComponentType(); // Optimization: If the elementType is String, we don't need // conversion. Just return newValues. if (elementType.equals(String.class)) { return newValues; } try { result = Array.newInstance(elementType, len); } catch (Exception e) { throw new ConverterException(e); } // bail out now if we have no new values, returning our // oh-so-useful zero-length array. if (null == newValues) { return result; } // obtain a converter. // attached converter takes priority if (null == (converter = uiSelectMany.getConverter())) { // Otherwise, look for a by-type converter if (null == (converter = Util.getConverterForClass(elementType, context))) { // if that fails, and the attached values are of Object type, // we don't need conversion. if (elementType.equals(Object.class)) { return newValues; } String valueStr = ""; for (i = 0; i < newValues.length; i++) { valueStr = valueStr + " " + newValues[i]; } Object[] params = { valueStr, "null Converter" }; throw new ConverterException(Util.getExceptionMessage(Util.CONVERSION_ERROR_MESSAGE_ID, params)); } } Util.doAssert(null != result); if (elementType.isPrimitive()) { for (i = 0; i < len; i++) { if (elementType.equals(Boolean.TYPE)) { Array.setBoolean(result, i, ((Boolean) converter.getAsObject(context, uiSelectMany, newValues[i])).booleanValue()); } else if (elementType.equals(Byte.TYPE)) { Array.setByte(result, i, ((Byte) converter.getAsObject(context, uiSelectMany, newValues[i])).byteValue()); } else if (elementType.equals(Double.TYPE)) { Array.setDouble(result, i, ((Double) converter.getAsObject(context, uiSelectMany, newValues[i])).doubleValue()); } else if (elementType.equals(Float.TYPE)) { Array.setFloat(result, i, ((Float) converter.getAsObject(context, uiSelectMany, newValues[i])).floatValue()); } else if (elementType.equals(Integer.TYPE)) { Array.setInt(result, i, ((Integer) converter.getAsObject(context, uiSelectMany, newValues[i])).intValue()); } else if (elementType.equals(Character.TYPE)) { Array.setChar(result, i, ((Character) converter.getAsObject(context, uiSelectMany, newValues[i])).charValue()); } else if (elementType.equals(Short.TYPE)) { Array.setShort(result, i, ((Short) converter.getAsObject(context, uiSelectMany, newValues[i])).shortValue()); } else if (elementType.equals(Long.TYPE)) { Array.setLong(result, i, ((Long) converter.getAsObject(context, uiSelectMany, newValues[i])).longValue()); } } } else { for (i = 0; i < len; i++) { if (log.isDebugEnabled()) { Object converted = converter.getAsObject(context, uiSelectMany, newValues[i]); log.debug("String value: " + newValues[i] + " converts to : " + converted.toString()); } Array.set(result, i, converter.getAsObject(context, uiSelectMany, newValues[i])); } } return result; }
From source file:io.neba.core.resourcemodels.metadata.MappedFieldMetaData.java
private Class<?> resolveArrayTypeOfComponentType() { if (this.typeParameter != null) { return Array.newInstance(typeParameter, 0).getClass(); }//from ww w.j a v a 2s . c om return null; }
From source file:com.astamuse.asta4d.web.form.flow.base.AbstractFormFlowHandler.java
/** * Sub classes can override this method to do some interception around form instance generation, especially some post processes. * <p>//from w w w.j a v a 2 s . c om * <b>NOTE:</b> DO NOT replace this method completely at sub class, if you want to do some customized form retrieving, override the * method {@link #retrieveFormInstance(Map, String)} instead. * * @return */ protected T generateFormInstanceFromContext() { try { final T form = (T) InjectUtil.retrieveContextDataSetInstance(formCls, FORM_PRE_DEFINED, ""); List<AnnotatedPropertyInfo> list = AnnotatedPropertyUtil.retrieveProperties(formCls); Context currentContext = Context.getCurrentThreadContext(); for (final AnnotatedPropertyInfo field : list) { CascadeFormField cff = field.getAnnotation(CascadeFormField.class); if (cff != null) { if (field.retrieveValue(form) != null) { continue; } if (StringUtils.isEmpty(cff.arrayLengthField())) { continue; } AnnotatedPropertyInfo arrayLengthField = AnnotatedPropertyUtil.retrievePropertyByName(formCls, cff.arrayLengthField()); if (arrayLengthField == null) { throw new NullPointerException( "specified array length field [" + cff.arrayLengthField() + "] was not found"); } Integer len = (Integer) arrayLengthField.retrieveValue(form); if (len == null) { // throw new NullPointerException("specified array length field [" + cff.arrayLengthField() + "] is null"); len = 0; } final Object[] array = (Object[]) Array.newInstance(field.getType().getComponentType(), len); for (int i = 0; i < len; i++) { final int seq = i; Context.with(new DelatedContext(currentContext) { protected String convertKey(String scope, String key) { if (scope.equals(WebApplicationContext.SCOPE_QUERYPARAM)) { return rewriteArrayIndexPlaceHolder(key, seq); } else { return key; } } }, new Runnable() { @Override public void run() { try { Object subform = field.getType().getComponentType().newInstance(); InjectUtil.injectToInstance(subform); Array.set(array, seq, subform); } catch (Exception e) { throw new RuntimeException(e); } } });// end runnable and context.with } // end for loop field.assginValue(form, array); } } return form; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.adobe.acs.commons.wcm.impl.PropertyMergePostProcessor.java
/** * Merges the values found in the the source properties into the destination * property as a multi-value. The values of the source properties and * destination properties must all be the same property type. * * The unique set of properties will be stored in * * @param resource the resource to look for the source and destination * properties on//from ww w .jav a 2 s . c o m * @param destination the property to store the collected properties. * @param sources the properties to collect values from for merging * @param typeHint the data type that should be used when reading and * storing the data * @param allowDuplicates true to allow duplicates values in the destination * property; false to make values unique * @return Optional resource updated, if any */ protected final <T> Optional<Resource> merge(final Resource resource, final String destination, final Collection<String> sources, final Class<T> typeHint, final boolean allowDuplicates) throws PersistenceException { ResourceResolver rr = resource.getResourceResolver(); // Create an empty array of type T @SuppressWarnings("unchecked") final T[] emptyArray = (T[]) Array.newInstance(typeHint, 0); Collection<T> collectedValues; if (allowDuplicates) { collectedValues = new ArrayList<>(); } else { collectedValues = new LinkedHashSet<>(); } for (final String source : sources) { Resource sourceProperties = resource; String sourceParam = source; if (source.contains("/")) { sourceParam = StringUtils.substringAfterLast(source, "/"); sourceProperties = rr.getResource(resource, StringUtils.substringBeforeLast(source, "/")); } T[] tmp = sourceProperties.adaptTo(ModifiableValueMap.class).get(sourceParam, emptyArray); collectedValues.addAll(Arrays.asList(tmp)); } Resource targetResource = resource; String targetProperty = destination; if (destination.contains("/")) { targetProperty = StringUtils.substringAfterLast(destination, "/"); targetResource = rr.getResource(resource, StringUtils.substringBeforeLast(destination, "/")); } ModifiableValueMap targetProperties = targetResource.adaptTo(ModifiableValueMap.class); final T[] currentValues = targetProperties.get(targetProperty, emptyArray); if (!collectedValues.equals(Arrays.asList(currentValues))) { targetProperties.put(targetProperty, collectedValues.toArray(emptyArray)); return Optional.of(targetResource); } else { return Optional.empty(); } }