Example usage for org.springframework.beans NotReadablePropertyException NotReadablePropertyException

List of usage examples for org.springframework.beans NotReadablePropertyException NotReadablePropertyException

Introduction

In this page you can find the example usage for org.springframework.beans NotReadablePropertyException NotReadablePropertyException.

Prototype

public NotReadablePropertyException(Class<?> beanClass, String propertyName) 

Source Link

Document

Create a new NotReadablePropertyException.

Usage

From source file:net.yasion.common.core.bean.wrapper.impl.ExtendedBeanWrapperImpl.java

@SuppressWarnings("unchecked")
private Object getPropertyValue(PropertyTokenHolder tokens) throws BeansException {
    String propertyName = tokens.canonicalName;
    String actualName = tokens.actualName;
    PropertyDescriptor pd = getCachedIntrospectionResults().getPropertyDescriptor(actualName);
    if (pd == null || pd.getReadMethod() == null) {
        throw new NotReadablePropertyException(getRootClass(), this.nestedPath + propertyName);
    }//  w  w w . ja  v a2 s .c o m
    final Method readMethod = pd.getReadMethod();
    try {
        if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers()) && !readMethod.isAccessible()) {
            if (System.getSecurityManager() != null) {
                AccessController.doPrivileged(new PrivilegedAction<Object>() {
                    @Override
                    public Object run() {
                        readMethod.setAccessible(true);
                        return null;
                    }
                });
            } else {
                readMethod.setAccessible(true);
            }
        }

        Object value;
        if (System.getSecurityManager() != null) {
            try {
                value = AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
                    @Override
                    public Object run() throws Exception {
                        return readMethod.invoke(object, (Object[]) null);
                    }
                }, acc);
            } catch (PrivilegedActionException pae) {
                throw pae.getException();
            }
        } else {
            value = readMethod.invoke(object, (Object[]) null);
        }

        if (tokens.keys != null) {
            if (value == null) {
                if (isAutoGrowNestedPaths()) {
                    value = setDefaultValue(tokens.actualName);
                } else {
                    throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + propertyName,
                            "Cannot access indexed value of property referenced in indexed " + "property path '"
                                    + propertyName + "': returned null");
                }
            }
            String indexedPropertyName = tokens.actualName;
            // apply indexes and map keys
            for (int i = 0; i < tokens.keys.length; i++) {
                String key = tokens.keys[i];
                if (value == null) {
                    throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + propertyName,
                            "Cannot access indexed value of property referenced in indexed " + "property path '"
                                    + propertyName + "': returned null");
                } else if (value.getClass().isArray()) {
                    int index = Integer.parseInt(key);
                    value = growArrayIfNecessary(value, index, indexedPropertyName);
                    value = Array.get(value, index);
                } else if (value instanceof List) {
                    int index = Integer.parseInt(key);
                    List<Object> list = (List<Object>) value;
                    growCollectionIfNecessary(list, index, indexedPropertyName, pd, i + 1);
                    value = list.get(index);
                } else if (value instanceof Set) {
                    // Apply index to Iterator in case of a Set.
                    Set<Object> set = (Set<Object>) value;
                    int index = Integer.parseInt(key);
                    if (index < 0 || index >= set.size()) {
                        throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
                                "Cannot get element with index " + index + " from Set of size " + set.size()
                                        + ", accessed using property path '" + propertyName + "'");
                    }
                    Iterator<Object> it = set.iterator();
                    for (int j = 0; it.hasNext(); j++) {
                        Object elem = it.next();
                        if (j == index) {
                            value = elem;
                            break;
                        }
                    }
                } else if (value instanceof Map) {
                    Map<Object, Object> map = (Map<Object, Object>) value;
                    Class<?> mapKeyType = GenericCollectionTypeResolver.getMapKeyReturnType(pd.getReadMethod(),
                            i + 1);
                    // IMPORTANT: Do not pass full property name in here - property editors
                    // must not kick in for map keys but rather only for map values.
                    TypeDescriptor typeDescriptor = TypeDescriptor.valueOf(mapKeyType);
                    Object convertedMapKey = convertIfNecessary(null, null, key, mapKeyType, typeDescriptor);
                    value = map.get(convertedMapKey);
                } else {
                    throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
                            "Property referenced in indexed property path '" + propertyName
                                    + "' is neither an array nor a List nor a Set nor a Map; returned value was ["
                                    + value + "]");
                }
                indexedPropertyName += PROPERTY_KEY_PREFIX + key + PROPERTY_KEY_SUFFIX;
            }
        }
        return value;
    } catch (IndexOutOfBoundsException ex) {
        throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
                "Index of out of bounds in property path '" + propertyName + "'", ex);
    } catch (NumberFormatException ex) {
        throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
                "Invalid index in property path '" + propertyName + "'", ex);
    } catch (TypeMismatchException ex) {
        throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
                "Invalid index in property path '" + propertyName + "'", ex);
    } catch (InvocationTargetException ex) {
        throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
                "Getter for property '" + actualName + "' threw exception", ex);
    } catch (Exception ex) {
        throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
                "Illegal attempt to get property '" + actualName + "' threw exception", ex);
    }
}

From source file:org.nextframework.controller.ExtendedBeanWrapper.java

protected Object getPropertyValue(PropertyTokenHolder tokens) throws BeansException {
    String propertyName = tokens.canonicalName;
    String actualName = tokens.actualName;
    PropertyDescriptor pd = getPropertyDescriptorInternal(tokens.actualName);
    if (pd == null || pd.getReadMethod() == null) {
        throw new NotReadablePropertyException(getRootClass(), this.nestedPath + propertyName);
    }/*from ww w  . j a v  a 2s  .c  om*/
    if (logger.isDebugEnabled())
        logger.debug("About to invoke read method [" + pd.getReadMethod() + "] on object of class ["
                + this.object.getClass().getName() + "]");
    try {
        Object value = pd.getReadMethod().invoke(this.object, (Object[]) null);
        Type genericReturnType = pd.getReadMethod().getGenericReturnType();
        Class rawReturnType = pd.getReadMethod().getReturnType();
        if (tokens.keys != null) {
            // apply indexes and map keys
            for (int i = 0; i < tokens.keys.length; i++) {
                String key = tokens.keys[i];

                //cria listas sob demanda.. no  mais necessrio utilizar o ListSet no pojo
                Class originalClass = null;
                if (value != null) {
                    originalClass = value.getClass();
                }
                if (value == null && rawReturnType != null && genericReturnType instanceof ParameterizedType) {
                    ParameterizedType parameterizedType = (ParameterizedType) genericReturnType;
                    if (Map.class.isAssignableFrom(rawReturnType)) {
                        value = new LinkedHashMap();
                        pd.getWriteMethod().invoke(this.object, value);
                    } else if (List.class.isAssignableFrom(rawReturnType)
                            || Set.class.isAssignableFrom(rawReturnType)) {
                        Type type = parameterizedType.getActualTypeArguments()[0];
                        value = new ListSet(type instanceof Class ? (Class) type
                                : (Class) ((ParameterizedType) type).getRawType());
                        pd.getWriteMethod().invoke(this.object, value);
                    }
                }
                //fim da criacao sob demanda

                if (value == null) {
                    throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + propertyName,
                            "Cannot access indexed value of property referenced in indexed " + "property path '"
                                    + propertyName + "': returned null");
                } else if (value.getClass().isArray()) {
                    value = Array.get(value, Integer.parseInt(key));
                } else if (value instanceof List) {
                    List list = (List) value;
                    try {
                        value = list.get(Integer.parseInt(key));
                    } catch (IndexOutOfBoundsException e) {
                        //tentar instanciar um bean da lista
                        String extraMessage = "";
                        if (genericReturnType instanceof ParameterizedType) {
                            ParameterizedType parameterizedType = (ParameterizedType) genericReturnType;
                            Type type = parameterizedType.getActualTypeArguments()[0];
                            if (type instanceof Class) {
                                Class clazz = (Class) type;
                                extraMessage = "A classe " + clazz.getName()
                                        + " no possui um construtor publico sem argumentos";
                                try {
                                    value = clazz.newInstance();
                                    int index = Integer.parseInt(key);
                                    int insertNulls = index - list.size();
                                    while (insertNulls > 0) { // 11/06/2012
                                        list.add(null);
                                        insertNulls--;
                                    }

                                    list.add(index, value); // CDIGO 15/01/2007
                                } catch (InstantiationException e1) {
                                    throw new RuntimeException(
                                            "Aconteceu um erro ao acessar um elemento da classe "
                                                    + originalClass.getName() + " propriedade " + propertyName
                                                    + "  No foi possvel instanciar um bean para preencher a lista. "
                                                    + extraMessage,
                                            e);
                                }
                            }
                        } else if (originalClass != null) {
                            throw new RuntimeException("Aconteceu um erro ao acessar um elemento da classe "
                                    + originalClass.getName() + " propriedade " + propertyName
                                    + "  Sugesto: Utilize uma lista que cresa quando for necessrio como o ListSet ou no instancie nenhum objeto para essa propriedade",
                                    e);
                        } else {
                            throw e;
                        }
                    }
                } else if (value instanceof Set) {
                    // apply index to Iterator in case of a Set
                    //TODO CRIAR AUTOMATICAMENTE O BEAN DO SET
                    Set set = (Set) value;
                    int index = Integer.parseInt(key);
                    if (index < 0 || index >= set.size()) {
                        throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
                                "Cannot get element with index " + index + " from Set of size " + set.size()
                                        + ", accessed using property path '" + propertyName + "'"
                                        + "  Sugesto: Utilize o ListSet ou no instancie nenhum objeto");
                    }
                    Iterator it = set.iterator();
                    for (int j = 0; it.hasNext(); j++) {
                        Object elem = it.next();
                        if (j == index) {
                            value = elem;
                            break;
                        }
                    }
                } else if (value instanceof Map) {
                    if (!(genericReturnType instanceof ParameterizedType)) {
                        throw new NotParameterizedTypeException(
                                "Path direciona a um Map no parameterizado com generics. " + " Propriedade '"
                                        + this.nestedPath + propertyName + "' da classe ["
                                        + this.rootObject.getClass().getName() + "]");
                    }
                    ParameterizedType parameterizedType = ((ParameterizedType) genericReturnType);
                    Type mapKeyType = parameterizedType.getActualTypeArguments()[0];
                    Type mapValueType = parameterizedType.getActualTypeArguments()[1];
                    Class rawKeyType = mapKeyType instanceof Class ? (Class) mapKeyType
                            : (Class) ((ParameterizedType) mapKeyType).getRawType();
                    Class rawValueType = mapValueType instanceof Class ? (Class) mapValueType
                            : (Class) ((ParameterizedType) mapValueType).getRawType();
                    Object objectKey = doTypeConversionIfNecessary(key, rawKeyType);
                    Map map = (Map) value;
                    value = map.get(objectKey);
                    if (value == null && List.class.isAssignableFrom(rawValueType)) {
                        List mapValue;
                        try {
                            Type listType = ((ParameterizedType) mapValueType).getActualTypeArguments()[0];
                            mapValue = new ListSet(listType instanceof Class ? (Class) listType
                                    : (Class) ((ParameterizedType) listType).getRawType());
                        } catch (ClassCastException e) {
                            throw new RuntimeException(
                                    "Na path " + propertyName + " um mapa contm uma lista no parametrizada");
                        }
                        map.put(objectKey, mapValue);
                        value = mapValue;
                    }
                } else {
                    throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
                            "Property referenced in indexed property path '" + propertyName
                                    + "' is neither an array nor a List nor a Set nor a Map; returned value was ["
                                    + value + "]");
                }
            }
        }
        return value;
    } catch (InvocationTargetException ex) {
        throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
                "Getter for property '" + actualName + "' threw exception", ex);
    } catch (IllegalAccessException ex) {
        throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
                "Illegal attempt to get property '" + actualName + "' threw exception", ex);
    } catch (IndexOutOfBoundsException ex) {
        throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
                "Index of out of bounds in property path '" + propertyName + "'", ex);
    } catch (NumberFormatException ex) {
        throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
                "Invalid index in property path '" + propertyName + "'", ex);
    }
}

From source file:org.nextframework.controller.ExtendedBeanWrapper.java

private Type getPropertyType(PropertyTokenHolder tokens) {
    String propertyName = tokens.canonicalName;
    PropertyDescriptor pd = getPropertyDescriptorInternal(tokens.actualName);
    if (pd == null || pd.getReadMethod() == null) {
        throw new NotReadablePropertyException(getRootClass(), this.nestedPath + propertyName);
    }//from   w ww  .  jav a2  s .  co m
    if (logger.isDebugEnabled())
        logger.debug("About to invoke read method [" + pd.getReadMethod() + "] on object of class ["
                + this.object.getClass().getName() + "]");
    try {
        Type type = pd.getReadMethod().getGenericReturnType();
        if (tokens.keys != null) {
            // apply indexes and map keys
            for (int i = 0; i < tokens.keys.length; i++) {
                if (type == null) {
                    throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + propertyName,
                            "Cannot access indexed value of property referenced in indexed " + "property path '"
                                    + propertyName + "': returned null");
                } else if (type instanceof ParameterizedType) {
                    ParameterizedType parameterizedType = (ParameterizedType) type;
                    Class clazz = (Class) parameterizedType.getRawType();
                    if (Map.class.isAssignableFrom(clazz)) {
                        type = parameterizedType.getActualTypeArguments()[1];
                    } else if (Collection.class.isAssignableFrom(clazz)) {
                        type = parameterizedType.getActualTypeArguments()[0];
                    } else {
                        throw new RuntimeException("Tipo desconhecido " + parameterizedType);
                    }
                } else if (type instanceof Class && ((Class) type).isArray()) {
                    return type;
                } else {
                    throw new RuntimeException("Implementar converso!!!");
                }
            }
        }
        return type;
    } catch (IndexOutOfBoundsException ex) {
        throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
                "Index of out of bounds in property path '" + propertyName + "'", ex);
    } catch (NumberFormatException ex) {
        throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
                "Invalid index in property path '" + propertyName + "'", ex);
    }
}

From source file:org.springframework.beans.AbstractNestablePropertyAccessor.java

@SuppressWarnings("unchecked")
@Nullable//from ww  w .  j a va2  s. c  om
protected Object getPropertyValue(PropertyTokenHolder tokens) throws BeansException {
    String propertyName = tokens.canonicalName;
    String actualName = tokens.actualName;
    PropertyHandler ph = getLocalPropertyHandler(actualName);
    if (ph == null || !ph.isReadable()) {
        throw new NotReadablePropertyException(getRootClass(), this.nestedPath + propertyName);
    }
    try {
        Object value = ph.getValue();
        if (tokens.keys != null) {
            if (value == null) {
                if (isAutoGrowNestedPaths()) {
                    value = setDefaultValue(new PropertyTokenHolder(tokens.actualName));
                } else {
                    throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + propertyName,
                            "Cannot access indexed value of property referenced in indexed " + "property path '"
                                    + propertyName + "': returned null");
                }
            }
            String indexedPropertyName = tokens.actualName;
            // apply indexes and map keys
            for (int i = 0; i < tokens.keys.length; i++) {
                String key = tokens.keys[i];
                if (value == null) {
                    throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + propertyName,
                            "Cannot access indexed value of property referenced in indexed " + "property path '"
                                    + propertyName + "': returned null");
                } else if (value.getClass().isArray()) {
                    int index = Integer.parseInt(key);
                    value = growArrayIfNecessary(value, index, indexedPropertyName);
                    value = Array.get(value, index);
                } else if (value instanceof List) {
                    int index = Integer.parseInt(key);
                    List<Object> list = (List<Object>) value;
                    growCollectionIfNecessary(list, index, indexedPropertyName, ph, i + 1);
                    value = list.get(index);
                } else if (value instanceof Set) {
                    // Apply index to Iterator in case of a Set.
                    Set<Object> set = (Set<Object>) value;
                    int index = Integer.parseInt(key);
                    if (index < 0 || index >= set.size()) {
                        throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
                                "Cannot get element with index " + index + " from Set of size " + set.size()
                                        + ", accessed using property path '" + propertyName + "'");
                    }
                    Iterator<Object> it = set.iterator();
                    for (int j = 0; it.hasNext(); j++) {
                        Object elem = it.next();
                        if (j == index) {
                            value = elem;
                            break;
                        }
                    }
                } else if (value instanceof Map) {
                    Map<Object, Object> map = (Map<Object, Object>) value;
                    Class<?> mapKeyType = ph.getResolvableType().getNested(i + 1).asMap().resolveGeneric(0);
                    // IMPORTANT: Do not pass full property name in here - property editors
                    // must not kick in for map keys but rather only for map values.
                    TypeDescriptor typeDescriptor = TypeDescriptor.valueOf(mapKeyType);
                    Object convertedMapKey = convertIfNecessary(null, null, key, mapKeyType, typeDescriptor);
                    value = map.get(convertedMapKey);
                } else {
                    throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
                            "Property referenced in indexed property path '" + propertyName
                                    + "' is neither an array nor a List nor a Set nor a Map; returned value was ["
                                    + value + "]");
                }
                indexedPropertyName += PROPERTY_KEY_PREFIX + key + PROPERTY_KEY_SUFFIX;
            }
        }
        return value;
    } catch (IndexOutOfBoundsException ex) {
        throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
                "Index of out of bounds in property path '" + propertyName + "'", ex);
    } catch (NumberFormatException ex) {
        throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
                "Invalid index in property path '" + propertyName + "'", ex);
    } catch (TypeMismatchException ex) {
        throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
                "Invalid index in property path '" + propertyName + "'", ex);
    } catch (InvocationTargetException ex) {
        throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
                "Getter for property '" + actualName + "' threw exception", ex);
    } catch (Exception ex) {
        throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
                "Illegal attempt to get property '" + actualName + "' threw exception", ex);
    }
}

From source file:org.tinygroup.weblayer.webcontext.parser.util.BeanWrapperImpl.java

private Object getPropertyValue(PropertyTokenHolder tokens) throws BeansException {
    String propertyName = tokens.canonicalName;
    String actualName = tokens.actualName;
    PropertyDescriptor pd = getCachedIntrospectionResults().getPropertyDescriptor(actualName);
    if (pd == null || pd.getReadMethod() == null) {
        throw new NotReadablePropertyException(getRootClass(), this.nestedPath + propertyName);
    }/*from w  ww.  j  a  va2  s  . c  o m*/
    final Method readMethod = pd.getReadMethod();
    try {
        if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
            readMethod.setAccessible(true);
        }
        Object value = readMethod.invoke(this.object, (Object[]) null);

        if (tokens.keys != null) {
            if (value == null) {
                if (this.autoGrowNestedPaths) {
                    value = setDefaultValue(tokens.actualName);
                } else {
                    throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + propertyName,
                            "Cannot access indexed value of property referenced in indexed " + "property path '"
                                    + propertyName + "': returned null");
                }
            }
            String indexedPropertyName = tokens.actualName;
            // apply indexes and map keys
            for (int i = 0; i < tokens.keys.length; i++) {
                String key = tokens.keys[i];
                if (value == null) {
                    throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + propertyName,
                            "Cannot access indexed value of property referenced in indexed " + "property path '"
                                    + propertyName + "': returned null");
                } else if (value.getClass().isArray()) {
                    int index = Integer.parseInt(key);
                    value = growArrayIfNecessary(value, index, indexedPropertyName);
                    value = Array.get(value, index);
                } else if (value instanceof List) {
                    int index = Integer.parseInt(key);
                    List list = (List) value;
                    growCollectionIfNecessary(list, index, indexedPropertyName, pd, i + 1);
                    value = list.get(index);
                } else if (value instanceof Set) {
                    // Apply index to Iterator in case of a Set.
                    Set set = (Set) value;
                    int index = Integer.parseInt(key);
                    if (index < 0 || index >= set.size()) {
                        throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
                                "Cannot get element with index " + index + " from Set of size " + set.size()
                                        + ", accessed using property path '" + propertyName + "'");
                    }
                    Iterator it = set.iterator();
                    for (int j = 0; it.hasNext(); j++) {
                        Object elem = it.next();
                        if (j == index) {
                            value = elem;
                            break;
                        }
                    }
                } else if (value instanceof Map) {
                    Map map = (Map) value;
                    Class<?> mapKeyType = GenericCollectionTypeResolver.getMapKeyReturnType(pd.getReadMethod(),
                            i + 1);
                    // IMPORTANT: Do not pass full property name in here - property editors
                    // must not kick in for map keys but rather only for map values.
                    Object convertedMapKey = this.typeConverterDelegate.convertIfNecessary(key, mapKeyType);
                    value = map.get(convertedMapKey);
                } else {
                    throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
                            "Property referenced in indexed property path '" + propertyName
                                    + "' is neither an array nor a List nor a Set nor a Map; returned value was ["
                                    + value + "]");
                }
                indexedPropertyName += PROPERTY_KEY_PREFIX + key + PROPERTY_KEY_SUFFIX;
            }
        }
        return value;
    } catch (IndexOutOfBoundsException ex) {
        throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
                "Index of out of bounds in property path '" + propertyName + "'", ex);
    } catch (NumberFormatException ex) {
        throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
                "Invalid index in property path '" + propertyName + "'", ex);
    } catch (TypeMismatchException ex) {
        throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
                "Invalid index in property path '" + propertyName + "'", ex);
    } catch (InvocationTargetException ex) {
        throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
                "Getter for property '" + actualName + "' threw exception", ex);
    } catch (Exception ex) {
        throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
                "Illegal attempt to get property '" + actualName + "' threw exception", ex);
    }
}