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.parleys.io.amf.client.AMFClientProxy.java
@SuppressWarnings("SuspiciousSystemArraycopy") private Object convertReturnValueToArray(Method method, Object callResult) { Class<?> componentType = method.getReturnType().getComponentType(); int length = ((Object[]) callResult).length; Object[] arrayOfValidType = (Object[]) Array.newInstance(componentType, length); System.arraycopy(callResult, 0, arrayOfValidType, 0, ((Object[]) callResult).length); callResult = arrayOfValidType;/* w w w. ja va 2 s . c om*/ return callResult; }
From source file:EventListenerList.java
/** * Returns an array of listeners registered as the listeners of the given * type. Note that the returned array is an array of the actual given type * so it can be safely casted to that type once instead of casting each of the * listener.//from w w w .jav a 2 s . c o m */ public EventListener[] getListeners(Class listenerType) { EventListener[] listeners = (EventListener[]) Array.newInstance(listenerType, getListenerCount(listenerType)); int count = 0; for (int i = 0; i < listenerList.length; i += 2) { if (listenerType == listenerList[i]) listeners[count++] = (EventListener) listenerList[i + 1]; } return listeners; }
From source file:com.github.helenusdriver.commons.collections.iterators.CombinationIterator.java
/** * {@inheritDoc}/*from ww w. ja v a 2 s . co m*/ * * @author paouelle * * @see java.util.Iterator#hasNext() */ @SuppressWarnings({ "cast", "unchecked" }) @Override public boolean hasNext() { if (hasNext) { return true; } if (finished) { return false; } if (iterators == null) { // starting up this.iterators = (Iterator<T>[]) new Iterator[size]; this.current = (T[]) Array.newInstance(clazz, size); for (int j = 0; j < size; j++) { final Iterator<T> i = items[j].iterator(); iterators[j] = i; if (!i.hasNext()) { // we can't even start with a value this.finished = true; this.current = null; this.iterators = null; return false; } current[j] = i.next(); } this.hasNext = true; return true; } if (current == null) { this.current = (T[]) Array.newInstance(clazz, size); } // iterate the list in reverse for (int j = size - 1; j >= 0; j--) { Iterator<T> i = iterators[j]; if (i.hasNext()) { current[j] = i.next(); this.hasNext = true; return true; } // if we get here then that level is done iterating so reset its iterator // and move the previous one forward if (i instanceof ResettableIterator) { ((ResettableIterator<T>) i).reset(); } else { // create a new one i = items[j].iterator(); } if (!i.hasNext()) { // we can't even restart with a value break; } current[j] = i.next(); iterators[j] = i; } // if we get here then we are done this.finished = true; this.current = null; this.iterators = null; return false; }
From source file:com.microsoft.tfs.core.internal.wrappers.WrapperUtils.java
/** * <p>/*from www . j av a2 s . c o m*/ * Takes an array of web service wrapper (for example, an array of * {@link Item}) and returns an array of the wrapped web service objects * that were inside the given type (for instance, {@link _Item}). * </p> * <p> * A public method named {@link #UNWRAP_METHOD_NAME} which takes no * arguments and returns the wrapped web service object must exist. * </p> * <p> * <code>null</code> values in the wrapper objects will be persisted in the * returned array. * </p> * * @param webServiceObjectType * the type of the wrapped web service object (not array type) (not * null) * @param wrapperObjects * the wrapper objects to get the contents from(if null, null is * returned) * @return a new array of web service objects, each extracted from the given * wrapper objects */ public static Object unwrap(final Class webServiceObjectType, final Object[] wrapperObjects) { Check.notNull(webServiceObjectType, "webServiceObjectType"); //$NON-NLS-1$ if (wrapperObjects == null) { return null; } final Object ret = Array.newInstance(webServiceObjectType, wrapperObjects.length); if (wrapperObjects.length > 0) { try { Method method = null; for (int i = 0; i < wrapperObjects.length; i++) { if (method == null && wrapperObjects[i] != null) { method = wrapperObjects[i].getClass().getMethod(UNWRAP_METHOD_NAME, new Class[0]); } /* * Persist null values. */ Array.set(ret, i, (wrapperObjects[i] != null) ? method.invoke(wrapperObjects[i], new Object[0]) : null); } } catch (final NoSuchMethodException e) { final String message = MessageFormat.format( "Wrapper error: the given wrapper class {0} does not have a method named {1} that returns a {2}", //$NON-NLS-1$ wrapperObjects[0].getClass(), UNWRAP_METHOD_NAME, webServiceObjectType); log.error(message, e); throw new RuntimeException(message, e); } catch (final Exception e) { final String message = MessageFormat.format("Error unwrapping {0} from {1}", //$NON-NLS-1$ webServiceObjectType, wrapperObjects); log.error(message, e); throw new RuntimeException(message, e); } } return ret; }
From source file:com.espertech.esper.event.EventTypeUtility.java
public static LinkedHashMap<String, Object> buildType(List<ColumnDesc> columns, EventAdapterService eventAdapterService, Set<String> copyFrom, EngineImportService engineImportService) throws ExprValidationException { LinkedHashMap<String, Object> typing = new LinkedHashMap<String, Object>(); Set<String> columnNames = new HashSet<String>(); for (ColumnDesc column : columns) { boolean added = columnNames.add(column.getName()); if (!added) { throw new ExprValidationException("Duplicate column name '" + column.getName() + "'"); }//from w ww.j a v a 2s . c om Class plain = JavaClassHelper.getClassForSimpleName(column.getType()); if (plain != null) { if (column.isArray()) { plain = Array.newInstance(plain, 0).getClass(); } typing.put(column.getName(), plain); } else { // try imports first Class resolved = null; try { resolved = engineImportService.resolveClass(column.getType()); } catch (EngineImportException e) { // expected } // resolve from classpath when not found if (resolved == null) { try { resolved = JavaClassHelper.getClassForName(column.getType()); } catch (ClassNotFoundException e) { // expected } } // Handle resolved classes here if (resolved != null) { if (column.isArray()) { resolved = Array.newInstance(resolved, 0).getClass(); } typing.put(column.getName(), resolved); } // Event types fall into here else { if (column.isArray()) { typing.put(column.getName(), column.getType() + "[]"); } else { typing.put(column.getName(), column.getType()); } } } } if (copyFrom != null && !copyFrom.isEmpty()) { for (String copyFromName : copyFrom) { EventType type = eventAdapterService.getExistsTypeByName(copyFromName); if (type == null) { throw new ExprValidationException("Type by name '" + copyFromName + "' could not be located"); } mergeType(typing, type); } } return typing; }
From source file:com.nfwork.dbfound.json.converter.NumberArrayConverter.java
protected int[] createDimensions(int length, int initial) { Object dims = Array.newInstance(int.class, length); Array.set(dims, 0, new Integer(initial)); return (int[]) dims; }
From source file:jef.database.DefaultSqlProcessor.java
@SuppressWarnings({ "unchecked", "rawtypes" }) public Object collectValueToContainer(List<? extends IQueryableEntity> records, Class<?> containerType, String targetField) {// www . j av a2 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.jaspersoft.jasperserver.remote.utils.RepositoryHelper.java
protected static Object getMultiParameterValues(JRParameter parameter, Collection values) { Object parameterValue;/*from w w w. jav a2s .c o m*/ Class parameterType = parameter.getValueClass(); if (parameterType.equals(Object.class) || parameterType.equals(Collection.class) || parameterType.equals(Set.class) || parameterType.equals(List.class)) { Collection paramValues; if (parameterType.equals(List.class)) { //if the parameter type is list, use a list paramValues = new ArrayList(values.size()); } else { //else use an ordered set paramValues = new ListOrderedSet(); } Class componentType = parameter.getNestedType(); for (Iterator it = values.iterator(); it.hasNext();) { Object val = (Object) it.next(); Object paramValue; if (componentType == null || !(val instanceof String)) { //no conversion if no nested type set for the parameter paramValue = val; } else { paramValue = stringToValue((String) val, componentType); } paramValues.add(paramValue); } parameterValue = paramValues; } else if (parameterType.isArray()) { Class componentType = parameterType.getComponentType(); parameterValue = Array.newInstance(componentType, values.size()); int idx = 0; for (Iterator iter = values.iterator(); iter.hasNext(); ++idx) { Object val = iter.next(); Object paramValue; if (val instanceof String) { paramValue = stringToValue((String) val, componentType); } else { paramValue = val; } Array.set(parameterValue, idx, paramValue); } } else { parameterValue = values; } return parameterValue; }
From source file:com.evolveum.midpoint.prism.util.CloneUtil.java
private static <T> T cloneArray(T orig) { int length = Array.getLength(orig); T clone = (T) Array.newInstance(orig.getClass().getComponentType(), length); System.arraycopy(orig, 0, clone, 0, length); return clone; }
From source file:jp.go.nict.langrid.testresource.loader.NodeLoader_1_2.java
/** * ?/*from w w w . j ava 2 s .co m*/ * @param resource ? * @return * @throws IOException ??? */ protected Attribute[] loadAttributes(Nodes_1_2 resource, Class<Attribute> clazz) throws IOException { Map<String, String> props = new PropertyFileReader(CharsetUtil.newUTF8Decoder()) .read(resource.loadProperties()); for (String name : getProfileProperties()) { props.remove(name); } for (String name : getProfileTupleProperties()) { props.remove(name); } props.remove("nodeId"); List<Attribute> attrs = new ArrayList<Attribute>(); try { for (Map.Entry<String, String> entry : props.entrySet()) { attrs.add(clazz.getConstructor(String.class, String.class).newInstance(entry.getKey(), entry.getValue())); } } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InstantiationException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } return attrs.toArray((Attribute[]) Array.newInstance(clazz, 0)); }