Example usage for java.lang Class isArray

List of usage examples for java.lang Class isArray

Introduction

In this page you can find the example usage for java.lang Class isArray.

Prototype

@HotSpotIntrinsicCandidate
public native boolean isArray();

Source Link

Document

Determines if this Class object represents an array class.

Usage

From source file:fit.TypeAdapter.java

public static TypeAdapter adapterFor(Class<?> type) throws UnsupportedOperationException {
    if (type.isPrimitive()) {
        if (type.equals(byte.class))
            return new ByteAdapter();
        if (type.equals(short.class))
            return new ShortAdapter();
        if (type.equals(int.class))
            return new IntAdapter();
        if (type.equals(long.class))
            return new LongAdapter();
        if (type.equals(float.class))
            return new FloatAdapter();
        if (type.equals(double.class))
            return new DoubleAdapter();
        if (type.equals(char.class))
            return new CharAdapter();
        if (type.equals(boolean.class))
            return new BooleanAdapter();
        throw new UnsupportedOperationException("can't yet adapt " + type);
    } else {/*  ww w.  ja va 2s  .c o m*/
        Object delegate = PARSE_DELEGATES.get(type);
        if (delegate instanceof DelegateClassAdapter)
            return (TypeAdapter) ((DelegateClassAdapter) delegate).clone();
        if (delegate instanceof DelegateObjectAdapter)
            return (TypeAdapter) ((DelegateObjectAdapter) delegate).clone();
        if (type.equals(Byte.class))
            return new ClassByteAdapter();
        if (type.equals(Short.class))
            return new ClassShortAdapter();
        if (type.equals(Integer.class))
            return new ClassIntegerAdapter();
        if (type.equals(Long.class))
            return new ClassLongAdapter();
        if (type.equals(Float.class))
            return new ClassFloatAdapter();
        if (type.equals(Double.class))
            return new ClassDoubleAdapter();
        if (type.equals(Character.class))
            return new ClassCharacterAdapter();
        if (type.equals(Boolean.class))
            return new ClassBooleanAdapter();
        if (type.isArray())
            return new ArrayAdapter();
        return new TypeAdapter();
    }
}

From source file:com.seleniumtests.reporter.SeleniumTestsReporter.java

protected int getDim(Class<?> cls) {
    int dim = 0;//w  w  w.j  a v a 2  s.c  om

    while (cls.isArray()) {
        dim++;
        cls = cls.getComponentType();
    }

    return dim;
}

From source file:com.fengduo.bee.commons.component.FormModelMethodArgumentResolver.java

/**
 * {@inheritDoc}// w w  w  . j  a  v a2s .c  om
 * <p>
 * Downcast {@link org.springframework.web.bind.WebDataBinder} to
 * {@link org.springframework.web.bind.ServletRequestDataBinder} before binding.
 * 
 * @throws Exception
 * @see org.springframework.web.servlet.mvc.method.annotation.ServletRequestDataBinderFactory
 */
protected void bindRequestParameters(ModelAndViewContainer mavContainer, WebDataBinderFactory binderFactory,
        WebDataBinder binder, NativeWebRequest request, MethodParameter parameter) throws Exception {

    // Map<String, Boolean> hasProcessedPrefixMap = new HashMap<String, Boolean>();
    //
    // Class<?> targetType = binder.getTarget().getClass();
    // WebDataBinder simpleBinder = binderFactory.createBinder(request, null, null);
    Collection target = (Collection) binder.getTarget();

    Class<?>[] paramTypes = parameter.getMethod().getParameterTypes();
    Method method = parameter.getMethod();
    Object[] args = new Object[paramTypes.length];
    Map<String, Object> argMap = new HashMap<String, Object>(args.length);
    MapBindingResult errors = new MapBindingResult(argMap, "");
    ParameterNameDiscoverer parameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer();

    for (int i = 0; i < paramTypes.length; i++) {
        Class<?> paramType = paramTypes[i];

        MethodParameter methodParam = new MethodParameter(method, i);
        methodParam.initParameterNameDiscovery(parameterNameDiscoverer);
        String paramName = methodParam.getParameterName();
        // ??
        if (BeanUtils.isSimpleProperty(paramType)) {
            SimpleTypeConverter converter = new SimpleTypeConverter();
            Object value;
            // ?
            if (paramType.isArray()) {
                value = request.getParameterValues(paramName);
            } else {
                value = request.getParameter(paramName);
            }
            try {
                args[i] = converter.convertIfNecessary(value, paramType, methodParam);
            } catch (TypeMismatchException e) {
                errors.addError(new FieldError(paramName, paramName, e.getMessage()));
            }
        } else {
            // ???POJO
            if (paramType.isArray()) {
                ObjectArrayDataBinder binders = new ObjectArrayDataBinder(paramType.getComponentType(),
                        paramName);
                target.addAll(ArrayUtils.arrayConvert(binders.bind(request)));
            }
        }
    }

    // if (Collection.class.isAssignableFrom(targetType)) {// bind collection or array
    //
    // Type type = parameter.getGenericParameterType();
    // Class<?> componentType = Object.class;
    //
    // Collection target = (Collection) binder.getTarget();
    //
    // List targetList = new ArrayList(target);
    //
    // if (type instanceof ParameterizedType) {
    // componentType = (Class<?>) ((ParameterizedType) type).getActualTypeArguments()[0];
    // }
    //
    // if (parameter.getParameterType().isArray()) {
    // componentType = parameter.getParameterType().getComponentType();
    // }
    //
    // for (Object key : servletRequest.getParameterMap().keySet()) {
    // String prefixName = getPrefixName((String) key);
    //
    // // ?prefix ??
    // if (hasProcessedPrefixMap.containsKey(prefixName)) {
    // continue;
    // } else {
    // hasProcessedPrefixMap.put(prefixName, Boolean.TRUE);
    // }
    //
    // if (isSimpleComponent(prefixName)) { // bind simple type
    // Map<String, Object> paramValues = WebUtils.getParametersStartingWith(servletRequest, prefixName);
    // Matcher matcher = INDEX_PATTERN.matcher(prefixName);
    // if (!matcher.matches()) { // ? array=1&array=2
    // for (Object value : paramValues.values()) {
    // targetList.add(simpleBinder.convertIfNecessary(value, componentType));
    // }
    // } else { // ? array[0]=1&array[1]=2
    // int index = Integer.valueOf(matcher.group(1));
    //
    // if (targetList.size() <= index) {
    // growCollectionIfNecessary(targetList, index);
    // }
    // targetList.set(index, simpleBinder.convertIfNecessary(paramValues.values(), componentType));
    // }
    // } else { // ? votes[1].title=votes[1].title&votes[0].title=votes[0].title&votes[0].id=0&votes[1].id=1
    // Object component = null;
    // // ? ?????
    // Matcher matcher = INDEX_PATTERN.matcher(prefixName);
    // if (!matcher.matches()) {
    // throw new IllegalArgumentException("bind collection error, need integer index, key:" + key);
    // }
    // int index = Integer.valueOf(matcher.group(1));
    // if (targetList.size() <= index) {
    // growCollectionIfNecessary(targetList, index);
    // }
    // Iterator iterator = targetList.iterator();
    // for (int i = 0; i < index; i++) {
    // iterator.next();
    // }
    // component = iterator.next();
    //
    // if (component == null) {
    // component = BeanUtils.instantiate(componentType);
    // }
    //
    // WebDataBinder componentBinder = binderFactory.createBinder(request, component, null);
    // component = componentBinder.getTarget();
    //
    // if (component != null) {
    // ServletRequestParameterPropertyValues pvs = new ServletRequestParameterPropertyValues(
    // servletRequest,
    // prefixName,
    // "");
    // componentBinder.bind(pvs);
    // validateIfApplicable(componentBinder, parameter);
    // if (componentBinder.getBindingResult().hasErrors()) {
    // if (isBindExceptionRequired(componentBinder, parameter)) {
    // throw new BindException(componentBinder.getBindingResult());
    // }
    // }
    // targetList.set(index, component);
    // }
    // }
    // target.clear();
    // target.addAll(targetList);
    // }
    // } else if (MapWapper.class.isAssignableFrom(targetType)) {
    //
    // Type type = parameter.getGenericParameterType();
    // Class<?> keyType = Object.class;
    // Class<?> valueType = Object.class;
    //
    // if (type instanceof ParameterizedType) {
    // keyType = (Class<?>) ((ParameterizedType) type).getActualTypeArguments()[0];
    // valueType = (Class<?>) ((ParameterizedType) type).getActualTypeArguments()[1];
    // }
    //
    // MapWapper mapWapper = ((MapWapper) binder.getTarget());
    // Map target = mapWapper.getInnerMap();
    // if (target == null) {
    // target = new HashMap();
    // mapWapper.setInnerMap(target);
    // }
    //
    // for (Object key : servletRequest.getParameterMap().keySet()) {
    // String prefixName = getPrefixName((String) key);
    //
    // // ?prefix ??
    // if (hasProcessedPrefixMap.containsKey(prefixName)) {
    // continue;
    // } else {
    // hasProcessedPrefixMap.put(prefixName, Boolean.TRUE);
    // }
    //
    // Object keyValue = simpleBinder.convertIfNecessary(getMapKey(prefixName), keyType);
    //
    // if (isSimpleComponent(prefixName)) { // bind simple type
    // Map<String, Object> paramValues = WebUtils.getParametersStartingWith(servletRequest, prefixName);
    //
    // for (Object value : paramValues.values()) {
    // target.put(keyValue, simpleBinder.convertIfNecessary(value, valueType));
    // }
    // } else {
    //
    // Object component = target.get(keyValue);
    // if (component == null) {
    // component = BeanUtils.instantiate(valueType);
    // }
    //
    // WebDataBinder componentBinder = binderFactory.createBinder(request, component, null);
    // component = componentBinder.getTarget();
    //
    // if (component != null) {
    // ServletRequestParameterPropertyValues pvs = new ServletRequestParameterPropertyValues(
    // servletRequest,
    // prefixName,
    // "");
    // componentBinder.bind(pvs);
    //
    // validateComponent(componentBinder, parameter);
    //
    // target.put(keyValue, component);
    // }
    // }
    // }
    // } else {// bind model
    // ServletRequestDataBinder servletBinder = (ServletRequestDataBinder) binder;
    // servletBinder.bind(servletRequest);
    // }
}

From source file:com.joliciel.talismane.machineLearning.features.AbstractFeatureParser.java

/**
 * Get the features corresponding to a particular descriptor by performing
 * reflection on the corresponding feature class to be instantiated.
 * @param descriptor//from  w w w.ja  va2  s  . c o  m
 * @param featureClass
 * @return
 */
final List<Feature<T, ?>> getFeatures(FunctionDescriptor descriptor,
        @SuppressWarnings("rawtypes") Class<? extends Feature> featureClass,
        FunctionDescriptor topLevelDescriptor) {
    if (featureClass == null)
        throw new FeatureSyntaxException("No class provided for", descriptor, topLevelDescriptor);

    List<Feature<T, ?>> features = new ArrayList<Feature<T, ?>>();
    int i = 0;
    List<List<Object>> argumentLists = new ArrayList<List<Object>>();
    List<Object> initialArguments = new ArrayList<Object>();
    argumentLists.add(initialArguments);

    for (FunctionDescriptor argumentDescriptor : descriptor.getArguments()) {
        List<List<Object>> newArgumentLists = new ArrayList<List<Object>>();
        for (List<Object> arguments : argumentLists) {
            if (!argumentDescriptor.isFunction()) {
                Object literal = argumentDescriptor.getObject();
                Object convertedObject = literal;
                if (literal instanceof String) {
                    StringLiteralFeature<T> stringLiteralFeature = new StringLiteralFeature<T>(
                            (String) literal);
                    convertedObject = stringLiteralFeature;
                } else if (literal instanceof Boolean) {
                    BooleanLiteralFeature<T> booleanLiteralFeature = new BooleanLiteralFeature<T>(
                            (Boolean) literal);
                    convertedObject = booleanLiteralFeature;
                } else if (literal instanceof Double) {
                    DoubleLiteralFeature<T> doubleLiteralFeature = new DoubleLiteralFeature<T>(
                            (Double) literal);
                    convertedObject = doubleLiteralFeature;
                } else if (literal instanceof Integer) {
                    IntegerLiteralFeature<T> integerLiteralFeature = new IntegerLiteralFeature<T>(
                            (Integer) literal);
                    convertedObject = integerLiteralFeature;
                } else {
                    // do nothing - this was some sort of other object added by getModifiedDescriptors that should
                    // be handled as is.
                }
                arguments.add(convertedObject);
                newArgumentLists.add(arguments);

            } else {
                List<Feature<T, ?>> featureArguments = this.parseInternal(argumentDescriptor,
                        topLevelDescriptor);
                // a single argument descriptor could produce multiple arguments
                // e.g. when a function with an array argument is mapped onto multiple function calls
                for (Feature<T, ?> featureArgument : featureArguments) {
                    List<Object> newArguments = new ArrayList<Object>(arguments);
                    newArguments.add(featureArgument);
                    newArgumentLists.add(newArguments);
                }
            } // function or object?

        } // next argument list (under construction from original arguments)
        argumentLists = newArgumentLists;
    } // next argument

    for (List<Object> originalArgumentList : argumentLists) {
        // add the argument types (i.e. classes)
        // and convert arrays to multiple constructor calls
        List<Object[]> argumentsList = new ArrayList<Object[]>();
        argumentsList.add(new Object[originalArgumentList.size()]);

        Class<?>[] argumentTypes = new Class<?>[originalArgumentList.size()];
        List<Object[]> newArgumentsList = new ArrayList<Object[]>();
        for (i = 0; i < originalArgumentList.size(); i++) {
            Object arg = originalArgumentList.get(i);

            if (arg.getClass().isArray()) {
                // arrays represent multiple constructor calls
                Object[] argArray = (Object[]) arg;
                for (Object oneArg : argArray) {
                    for (Object[] arguments : argumentsList) {
                        Object[] newArguments = arguments.clone();
                        newArguments[i] = oneArg;
                        newArgumentsList.add(newArguments);
                    }
                }
                argumentTypes[i] = arg.getClass().getComponentType();
            } else {
                for (Object[] myArguments : argumentsList) {
                    newArgumentsList.add(myArguments);
                    myArguments[i] = arg;
                }
                argumentTypes[i] = arg.getClass();
            }
            argumentsList = newArgumentsList;
            newArgumentsList = new ArrayList<Object[]>();
        } // next argument

        @SuppressWarnings("rawtypes")
        Constructor<? extends Feature> constructor = null;
        MONITOR.startTask("findContructor");
        try {
            constructor = ConstructorUtils.getMatchingAccessibleConstructor(featureClass, argumentTypes);

            if (constructor == null) {
                Constructor<?>[] constructors = featureClass.getConstructors();

                // check if there's a variable argument constructor
                for (Constructor<?> oneConstructor : constructors) {
                    Class<?>[] parameterTypes = oneConstructor.getParameterTypes();

                    if (parameterTypes.length >= 1 && argumentsList.size() == 1
                            && argumentsList.get(0).length >= parameterTypes.length) {
                        Object[] arguments = argumentsList.get(0);
                        Class<?> parameterType = parameterTypes[parameterTypes.length - 1];
                        if (parameterType.isArray()) {
                            // assume it's a variable-argument constructor
                            // build the argument for this constructor
                            // find a common type for all of the arguments.
                            Object argument = arguments[parameterTypes.length - 1];
                            Class<?> clazz = null;
                            if (argument instanceof StringFeature)
                                clazz = StringFeature.class;
                            else if (argument instanceof BooleanFeature)
                                clazz = BooleanFeature.class;
                            else if (argument instanceof DoubleFeature)
                                clazz = DoubleFeature.class;
                            else if (argument instanceof IntegerFeature)
                                clazz = IntegerFeature.class;
                            else if (argument instanceof StringCollectionFeature)
                                clazz = StringFeature.class;
                            else {
                                // no good, can't make arrays of this type
                                continue;
                            }

                            Object[] argumentArray = (Object[]) Array.newInstance(clazz,
                                    (arguments.length - parameterTypes.length) + 1);
                            int j = 0;
                            for (int k = parameterTypes.length - 1; k < arguments.length; k++) {
                                Object oneArgument = arguments[k];
                                if (oneArgument instanceof StringCollectionFeature) {
                                    @SuppressWarnings("unchecked")
                                    StringCollectionFeature<T> stringCollectionFeature = (StringCollectionFeature<T>) oneArgument;
                                    StringCollectionFeatureProxy<T> proxy = new StringCollectionFeatureProxy<T>(
                                            stringCollectionFeature);
                                    oneArgument = proxy;
                                }
                                if (!clazz.isAssignableFrom(oneArgument.getClass())) {
                                    throw new FeatureSyntaxException(
                                            "Mismatched array types: " + clazz.getSimpleName() + ", "
                                                    + oneArgument.getClass().getSimpleName(),
                                            descriptor, topLevelDescriptor);
                                }
                                argumentArray[j++] = oneArgument;
                            } // next argument

                            Class<?>[] argumentTypesWithArray = new Class<?>[parameterTypes.length];
                            for (int k = 0; k < parameterTypes.length - 1; k++) {
                                Object oneArgument = arguments[k];
                                argumentTypesWithArray[k] = oneArgument.getClass();
                            }
                            argumentTypesWithArray[argumentTypesWithArray.length - 1] = argumentArray
                                    .getClass();
                            constructor = ConstructorUtils.getMatchingAccessibleConstructor(featureClass,
                                    argumentTypesWithArray);

                            if (constructor != null) {
                                argumentsList = new ArrayList<Object[]>();
                                Object[] argumentsWithArray = new Object[parameterTypes.length];
                                for (int k = 0; k < parameterTypes.length - 1; k++) {
                                    Object oneArgument = arguments[k];
                                    argumentsWithArray[k] = oneArgument;
                                }
                                argumentsWithArray[parameterTypes.length - 1] = argumentArray;
                                argumentsList.add(argumentsWithArray);
                                break;
                            }
                        } // constructor takes an array
                    } // exactly one parameter for constructor
                } // next constructor

                if (constructor == null) {
                    // See if various conversions allow us to find a constructor
                    // Integer to Double
                    // StringCollectionFeature to StringFeature
                    for (Constructor<?> oneConstructor : constructors) {
                        Class<?>[] parameterTypes = oneConstructor.getParameterTypes();
                        boolean isMatchingConstructor = false;
                        List<Integer> intParametersToConvert = new ArrayList<Integer>();
                        List<Integer> stringCollectionParametersToConvert = new ArrayList<Integer>();
                        List<Integer> customParametersToConvert = new ArrayList<Integer>();
                        if (parameterTypes.length == argumentTypes.length) {
                            int j = 0;
                            isMatchingConstructor = true;

                            for (Class<?> parameterType : parameterTypes) {
                                if (parameterType.isAssignableFrom(argumentTypes[j])
                                        && !StringCollectionFeature.class.isAssignableFrom(argumentTypes[j])) {
                                    // nothing to do here
                                } else if (parameterType.equals(DoubleFeature.class)
                                        && IntegerFeature.class.isAssignableFrom(argumentTypes[j])) {
                                    intParametersToConvert.add(j);
                                } else if ((parameterType.equals(StringFeature.class)
                                        || parameterType.equals(Feature.class))
                                        && StringCollectionFeature.class.isAssignableFrom(argumentTypes[j])) {
                                    stringCollectionParametersToConvert.add(j);
                                } else if (this.canConvert(parameterType, argumentTypes[j])) {
                                    customParametersToConvert.add(j);
                                } else {
                                    isMatchingConstructor = false;
                                    break;
                                }
                                j++;
                            }
                        }
                        if (isMatchingConstructor) {
                            @SuppressWarnings({ "rawtypes", "unchecked" })
                            Constructor<? extends Feature> matchingConstructor = (Constructor<? extends Feature>) oneConstructor;
                            constructor = matchingConstructor;

                            for (Object[] myArguments : argumentsList) {
                                for (int indexToConvert : intParametersToConvert) {
                                    @SuppressWarnings("unchecked")
                                    IntegerFeature<T> integerFeature = (IntegerFeature<T>) myArguments[indexToConvert];
                                    IntegerToDoubleFeature<T> intToDoubleFeature = new IntegerToDoubleFeature<T>(
                                            integerFeature);
                                    myArguments[indexToConvert] = intToDoubleFeature;
                                }
                                for (int indexToConvert : stringCollectionParametersToConvert) {
                                    @SuppressWarnings("unchecked")
                                    StringCollectionFeature<T> stringCollectionFeature = (StringCollectionFeature<T>) myArguments[indexToConvert];
                                    StringCollectionFeatureProxy<T> proxy = new StringCollectionFeatureProxy<T>(
                                            stringCollectionFeature);
                                    myArguments[indexToConvert] = proxy;
                                }
                                for (int indexToConvert : customParametersToConvert) {
                                    @SuppressWarnings("unchecked")
                                    Feature<T, ?> argumentToConvert = (Feature<T, ?>) myArguments[indexToConvert];
                                    Feature<T, ?> customArgument = this
                                            .convertArgument(parameterTypes[indexToConvert], argumentToConvert);
                                    myArguments[indexToConvert] = customArgument;
                                    customArgument.addArgument(argumentToConvert);
                                }
                            }
                            break;
                        } // found a matching constructor
                    } // next possible constructor
                } // still haven't found a constructor, what next?
            } // didn't find a constructor yet
        } finally {
            MONITOR.endTask("findContructor");
        }

        if (constructor == null)
            throw new NoConstructorFoundException("No constructor found for " + descriptor.getFunctionName()
                    + " (" + featureClass.getName() + ") matching the arguments provided", descriptor,
                    topLevelDescriptor);

        for (Object[] myArguments : argumentsList) {
            @SuppressWarnings("rawtypes")
            Feature feature;
            try {
                feature = constructor.newInstance(myArguments);
            } catch (IllegalArgumentException e) {
                throw new RuntimeException(e);
            } catch (InstantiationException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            }

            @SuppressWarnings("unchecked")
            Feature<T, ?> genericFeature = (Feature<T, ?>) feature;
            this.injectDependencies(feature);
            if (genericFeature instanceof ExternalResourceFeature) {
                if (this.getExternalResourceFinder() == null) {
                    throw new JolicielException("No external resource finder set.");
                }
                @SuppressWarnings("unchecked")
                ExternalResourceFeature<T> externalResourceFeature = (ExternalResourceFeature<T>) genericFeature;
                externalResourceFeature.setExternalResourceFinder(this.getExternalResourceFinder());
            } else if (genericFeature instanceof ExternalResourceDoubleFeature) {
                if (this.getExternalResourceFinder() == null) {
                    throw new JolicielException("No external resource finder set.");
                }
                @SuppressWarnings("unchecked")
                ExternalResourceDoubleFeature<T> externalResourceFeature = (ExternalResourceDoubleFeature<T>) genericFeature;
                externalResourceFeature.setExternalResourceFinder(this.getExternalResourceFinder());
            } else if (genericFeature instanceof MultivaluedExternalResourceFeature) {
                if (this.getExternalResourceFinder() == null) {
                    throw new JolicielException("No external resource finder set.");
                }
                @SuppressWarnings("unchecked")
                MultivaluedExternalResourceFeature<T> externalResourceFeature = (MultivaluedExternalResourceFeature<T>) genericFeature;
                externalResourceFeature.setExternalResourceFinder(this.getExternalResourceFinder());
            }

            // add this feature's arguments
            for (Object argument : myArguments) {
                if (argument instanceof Feature[]) {
                    @SuppressWarnings("unchecked")
                    Feature<T, ?>[] featureArray = (Feature<T, ?>[]) argument;
                    for (Feature<T, ?> oneFeature : featureArray) {
                        genericFeature.addArgument(oneFeature);
                    }
                } else {
                    @SuppressWarnings("unchecked")
                    Feature<T, ?> featureArgument = (Feature<T, ?>) argument;
                    genericFeature.addArgument(featureArgument);
                }
            }

            Feature<T, ?> convertedFeature = this.convertFeature(genericFeature);
            features.add(convertedFeature);
        } // next internal argument list
    } // next argument list
    return features;
}

From source file:loxia.support.json.JSONObject.java

@SuppressWarnings("unchecked")
public void setObject(Object bean, String propFilterStr) {
    JSONPropFilter filter = new JSONPropFilter(propFilterStr, objStrTransferMap.keySet());
    this.myHashMap = new HashMap<String, Object>();
    PropertyDescriptor[] props = PropertyUtils.getPropertyDescriptors(bean.getClass());
    for (PropertyDescriptor prop : props) {
        if ("class".equals(prop.getName()))
            continue;
        if (prop.getReadMethod() != null) {
            String key = prop.getName();
            try {
                Object value = prop.getReadMethod().invoke(bean, (Object[]) null);

                if (filter.isValid(key, value)) {
                    if (value == null) {
                        Class<? extends Object> c = prop.getReadMethod().getReturnType();
                        if (Map.class.isAssignableFrom(c) || Collection.class.isAssignableFrom(c)
                                || c.isArray())
                            continue;
                        this.myHashMap.put(key, NULL);
                    } else if (value instanceof Map) {
                        Map<String, Object> m = (Map<String, Object>) value;
                        this.myHashMap.put(key, new JSONObject(m, filter.getFilterStr(key), objStrTransferMap));
                    } else if (value instanceof Collection) {
                        Collection<? extends Object> c = (Collection<? extends Object>) value;
                        this.myHashMap.put(key, new JSONArray(c, filter.getFilterStr(key), objStrTransferMap));
                    } else if (value.getClass().isArray()) {
                        this.myHashMap.put(key,
                                new JSONArray(value, filter.getFilterStr(key), objStrTransferMap));
                    } else if (filter.isSupportedClass(value)) {
                        this.put(key, value);
                    } else {
                        this.myHashMap.put(key,
                                new JSONObject(value, filter.getFilterStr(key), objStrTransferMap));
                    }/*from w  ww.  j a  va 2  s .com*/
                }
            } catch (Exception e) {
                e.printStackTrace();
                //do nothing
            }
        }
    }

    /*Class<? extends Object> klass = bean.getClass();
     Method[] methods = klass.getMethods();
     for (int i = 0; i < methods.length; i += 1) {
    try {
        Method method = methods[i];
        String name = method.getName();
        String key = "";
        if (name.startsWith("get")) {
            key = name.substring(3);
        } else if (name.startsWith("is")) {
            key = name.substring(2);
        }
        if (key.length() > 0 &&
                Character.isUpperCase(key.charAt(0)) &&
                method.getParameterTypes().length == 0) {
            if (key.length() == 1) {
                key = key.toLowerCase();
            } else if (!Character.isUpperCase(key.charAt(1))) {
                key = key.substring(0, 1).toLowerCase() +
                    key.substring(1);
            }
            Object value = method.invoke(bean, (Object[])null);
                    
            if(filter.isValid(key,value)){ 
               if(value == null){ 
                  Class<? extends Object> c = method.getReturnType();
                  if(Map.class.isAssignableFrom(c) ||
                        Collection.class.isAssignableFrom(c) ||
                        c.isArray()) continue;
                  this.myHashMap.put(key, NULL);                          
               }else if(value instanceof Map){
                Map<String,Object> m = (Map<String,Object>) value;
                this.myHashMap.put(key, new JSONObject(m,filter.getFilterStr(key),objStrTransferMap));
             }else if(value instanceof Collection){
                Collection<? extends Object> c = (Collection<? extends Object>)value;
                this.myHashMap.put(key, new JSONArray(c,filter.getFilterStr(key),objStrTransferMap));
             }else if(value.getClass().isArray()){
                this.myHashMap.put(key, new JSONArray(value,filter.getFilterStr(key),objStrTransferMap));
             }else if(filter.isSupportedClass(value)){
                this.put(key, value);
             }else{
                this.myHashMap.put(key, new JSONObject(value,filter.getFilterStr(key),objStrTransferMap));
             }
          }
        }
    } catch (Exception e) {
       e.printStackTrace();
                
    }
     }*/
}

From source file:net.openkoncept.vroom.VroomController.java

private Object getParameterValue(Class propType, String id, Object[] paramValues, String format, Map paramMap) {
    Object value = null;//from   w ww  .java2s . c om
    if (paramValues == null) {
        if (paramMap != null) {
            value = paramMap.get(id);
            if (value instanceof List) {
                List l = (List) value;
                paramValues = l.toArray();
                value = null;
            }
        }
    }
    SimpleDateFormat sdf = null;
    try {
        String typeName = propType.getName();
        if (typeName.indexOf("java.lang.String") != -1) {
            if (!propType.isArray()) {
                value = paramValues[0];
            } else {
                value = new String[paramValues.length];
                for (int i = 0; i < paramValues.length; i++) {
                    ((Object[]) value)[i] = paramValues[i];
                }
            }
        } else if (typeName.indexOf("java.lang.Character") != -1) {
            if (!propType.isArray()) {
                value = new Character(((String) paramValues[0]).charAt(0));
            } else {
                value = new Character[paramValues.length];
                for (int i = 0; i < paramValues.length; i++) {
                    ((Object[]) value)[i] = new Character(((String) paramValues[i]).charAt(0));
                }
            }
        } else if (typeName.indexOf("java.lang.Short") != -1) {
            if (!propType.isArray()) {
                value = Short.valueOf((String) paramValues[0]);
            } else {
                value = new Short[paramValues.length];
                for (int i = 0; i < paramValues.length; i++) {
                    ((Object[]) value)[i] = Short.valueOf((String) paramValues[i]);
                }
            }
        } else if (typeName.indexOf("java.lang.Integer") != -1) {
            if (!propType.isArray()) {
                value = Integer.valueOf((String) paramValues[0]);
            } else {
                value = new Integer[paramValues.length];
                for (int i = 0; i < paramValues.length; i++) {
                    ((Object[]) value)[i] = Integer.valueOf((String) paramValues[i]);
                }
            }
        } else if (typeName.indexOf("java.lang.Long") != -1) {
            if (!propType.isArray()) {
                value = Long.valueOf((String) paramValues[0]);
            } else {
                value = new Long[paramValues.length];
                for (int i = 0; i < paramValues.length; i++) {
                    ((Object[]) value)[i] = Long.valueOf((String) paramValues[i]);
                }
            }
        } else if (typeName.indexOf("java.lang.Float") != -1) {
            if (!propType.isArray()) {
                value = Float.valueOf((String) paramValues[0]);
            } else {
                value = new Float[paramValues.length];
                for (int i = 0; i < paramValues.length; i++) {
                    ((Object[]) value)[i] = Float.valueOf((String) paramValues[i]);
                }
            }
        } else if (typeName.indexOf("java.lang.Double") != -1) {
            if (!propType.isArray()) {
                value = Double.valueOf((String) paramValues[0]);
            } else {
                value = new Double[paramValues.length];
                for (int i = 0; i < paramValues.length; i++) {
                    ((Object[]) value)[i] = Double.valueOf((String) paramValues[i]);
                }
            }
        } else if (typeName.indexOf("java.lang.Boolean") != -1) {
            if (!propType.isArray()) {
                value = Boolean.valueOf((String) paramValues[0]);
            } else {
                value = new Boolean[paramValues.length];
                for (int i = 0; i < paramValues.length; i++) {
                    ((Object[]) value)[i] = Boolean.valueOf((String) paramValues[i]);
                }
            }
        } else if (typeName.indexOf("java.util.Date") != -1) {
            if (format != null) {
                sdf = new SimpleDateFormat(format);
            } else {
                sdf = new SimpleDateFormat();
            }
            if (!propType.isArray()) {
                value = sdf.parse((String) paramValues[0]);
            } else {
                value = new Date[paramValues.length];
                for (int i = 0; i < paramValues.length; i++) {
                    ((Object[]) value)[i] = sdf.parse((String) paramValues[i]);
                }
            }
        } else if (typeName.indexOf("org.apache.commons.fileupload.FileItem") != -1) {
            if (!propType.isArray()) {
                value = paramValues[0];
            } else {
                value = new org.apache.commons.fileupload.FileItem[paramValues.length];
                for (int i = 0; i < paramValues.length; i++) {
                    ((Object[]) value)[i] = paramValues[i];
                }
            }
        }
    } catch (Exception ex) {
        value = null;
    }
    return value;
}

From source file:com.frank.search.solr.core.convert.MappingSolrConverter.java

private Collection<SolrInputField> writeWildcardMapPropertyToTarget(Map<? super Object, ? super Object> target,
        SolrPersistentProperty persistentProperty, Map<?, ?> fieldValue) {

    TypeInformation<?> mapTypeInformation = persistentProperty.getTypeInformation().getMapValueType();
    Class<?> rawMapType = mapTypeInformation.getType();
    String fieldName = persistentProperty.getFieldName();

    Collection<SolrInputField> fields = new ArrayList<SolrInputField>();

    for (Map.Entry<?, ?> entry : fieldValue.entrySet()) {

        Object value = entry.getValue();
        String key = entry.getKey().toString();

        if (persistentProperty.isDynamicProperty()) {
            key = WildcardPosition.getAppropriate(key).createName(fieldName, key);
        }/*  w w  w  .  j  a v  a  2 s  .c  o  m*/

        SolrInputField field = new SolrInputField(key);

        if (value instanceof Iterable) {

            for (Object o : (Iterable<?>) value) {
                field.addValue(convertToSolrType(rawMapType, o), 1f);
            }
        } else {

            if (rawMapType.isArray()) {
                for (Object o : (Object[]) value) {
                    field.addValue(convertToSolrType(rawMapType, o), 1f);
                }
            } else {
                field.addValue(convertToSolrType(rawMapType, value), 1f);
            }

        }

        target.put(key, field);
        fields.add(field);
    }

    return fields;
}

From source file:com.silverwrist.dynamo.app.ApplicationContainer.java

private final RegisteredRenderer searchClassRenderers(Class klass) {
    if (klass.isPrimitive() || (klass == Object.class))
        return null; // should have been picked up already

    // look at this level for the class member
    RegisteredRenderer rc = (RegisteredRenderer) (m_class_renderers.get(klass));
    if (rc != null)
        return rc;

    if (klass.isArray()) { // for arrays, use the parallel function to search back over the component
                           // class's hierarchy
        Class component = getUltimateComponent(klass);
        if (component.isPrimitive() || (component == Object.class))
            return null; // no chance this should have been picked up
        String template = StringUtils.replace(klass.getName(), component.getName(), TEMPLATE_CLASSNAME);
        return searchArrayRenderers(component, template);

    } // end if//www .java 2s . com

    // Try all interfaces implemented by the object.
    Class[] ifaces = klass.getInterfaces();
    for (int i = 0; i < ifaces.length; i++) { // look for interfaces implemented by the object
        rc = searchClassRenderers(ifaces[i]);
        if (rc != null)
            return rc;

    } // end for

    Class superclass = klass.getSuperclass();
    if (superclass != null) { // try the superclass now
        rc = searchClassRenderers(superclass);
        if (rc != null)
            return rc;

    } // end if

    return null; // give up

}