Package org.springframework.expression

Examples of org.springframework.expression.AccessException


    return false;
  }

  public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException {
    if (target == null) {
      throw new AccessException("Cannot read property of null target");
    }
    Class<?> type = (target instanceof Class ? (Class<?>) target : target.getClass());

    if (type.isArray() && name.equals("length")) {
      if (target instanceof Class) {
        throw new AccessException("Cannot access length on array class itself");
      }
      return new TypedValue(Array.getLength(target),TypeDescriptor.valueOf(Integer.TYPE));
    }

    if (this.readerCache==null) {
      this.readerCache = new ConcurrentHashMap<CacheKey, InvokerPair>();
    }
    CacheKey cacheKey = new CacheKey(type, name);
    InvokerPair invoker = this.readerCache.get(cacheKey);

    if (invoker == null || invoker.member instanceof Method) {
      Method method = (Method) (invoker==null?null:invoker.member);
      if (method == null) {
        method = findGetterForProperty(name, type, target instanceof Class);
        if (method != null) {
          // TODO remove the duplication here between canRead and read
          // Treat it like a property
          try {
            // The readerCache will only contain gettable properties (let's not worry about setters for now)
            PropertyDescriptor propertyDescriptor = new PropertyDescriptor(name, method, null);
            TypeDescriptor typeDescriptor =
                new PropertyTypeDescriptor(propertyDescriptor, new MethodParameter(method, -1));
            invoker = new InvokerPair(method, typeDescriptor);
            this.readerCache.put(cacheKey, invoker);
          }
          catch (IntrospectionException ex) {
            throw new AccessException(
                "Unable to access property '" + name + "' through getter " + method, ex);
          }
        }
      }
      if (method != null) {
        try {
          ReflectionUtils.makeAccessible(method);
          return new TypedValue(method.invoke(target),invoker.typeDescriptor);
        }
        catch (Exception ex) {
          throw new AccessException("Unable to access property '" + name + "' through getter", ex);
        }
      }
    }

    if (invoker == null || invoker.member instanceof Field) {
      Field field = (Field) (invoker==null?null:invoker.member);
      if (field == null) {
        field = findField(name, type, target instanceof Class);
        if (field != null) {
          invoker = new InvokerPair(field, new TypeDescriptor(field));
          this.readerCache.put(cacheKey, invoker);
        }
      }
      if (field != null) {
        try {
          ReflectionUtils.makeAccessible(field);
          return new TypedValue(field.get(target),invoker.typeDescriptor);
        }
        catch (Exception ex) {
          throw new AccessException("Unable to access field: " + name, ex);
        }
      }
    }

    throw new AccessException("Neither getter nor field found for property '" + name + "'");
  }
View Full Code Here


      // Treat it like a property
      PropertyDescriptor propertyDescriptor = null;
      try {
        propertyDescriptor = new PropertyDescriptor(name,null,method);
      } catch (IntrospectionException ex) {
        throw new AccessException("Unable to access property '" + name + "' through setter "+method, ex);
      }
      MethodParameter mp = new MethodParameter(method,0);
      TypeDescriptor typeDescriptor = new PropertyTypeDescriptor(propertyDescriptor, mp);
      this.writerCache.put(cacheKey, method);
      this.typeDescriptorCache.put(cacheKey, typeDescriptor);
View Full Code Here

    return false;
  }

  public void write(EvaluationContext context, Object target, String name, Object newValue) throws AccessException {
    if (target == null) {
      throw new AccessException("Cannot write property on null target");
    }
    Class<?> type = (target instanceof Class ? (Class<?>) target : target.getClass());

    Object possiblyConvertedNewValue = newValue;
    TypeDescriptor typeDescriptor = getTypeDescriptor(context, target, name);
    if (typeDescriptor != null) {
      try {
        possiblyConvertedNewValue = context.getTypeConverter().convertValue(newValue, TypeDescriptor.forObject(newValue), typeDescriptor);
      } catch (EvaluationException evaluationException) {
        throw new AccessException("Type conversion failure",evaluationException);
      }
    }
    if (this.writerCache == null) {
      this.writerCache = new ConcurrentHashMap<CacheKey, Member>();
    }
    CacheKey cacheKey = new CacheKey(type, name);
    Member cachedMember = this.writerCache.get(cacheKey);

    if (cachedMember == null || cachedMember instanceof Method) {
      Method method = (Method) cachedMember;
      if (method == null) {
        method = findSetterForProperty(name, type, target instanceof Class);
        if (method != null) {
          cachedMember = method;
          this.writerCache.put(cacheKey, cachedMember);
        }
      }
      if (method != null) {
        try {
          ReflectionUtils.makeAccessible(method);
          method.invoke(target, possiblyConvertedNewValue);
          return;
        }
        catch (Exception ex) {
          throw new AccessException("Unable to access property '" + name + "' through setter", ex);
        }
      }
    }

    if (cachedMember == null || cachedMember instanceof Field) {
      Field field = (Field) cachedMember;
      if (field == null) {
        field = findField(name, type, target instanceof Class);
        if (field != null) {
          cachedMember = field;
          this.writerCache.put(cacheKey, cachedMember);
        }
      }
      if (field != null) {
        try {
          ReflectionUtils.makeAccessible(field);
          field.set(target, possiblyConvertedNewValue);
          return;
        }
        catch (Exception ex) {
          throw new AccessException("Unable to access field: " + name, ex);
        }
      }
    }

    throw new AccessException("Neither setter nor field found for property '" + name + "'");
  }
View Full Code Here

          if (needsToBeMadeAccessible) {
            ReflectionUtils.makeAccessible((Method)member);
          }
          return new TypedValue(((Method)member).invoke(target),typeDescriptor);
        } catch (Exception e) {
          throw new AccessException("Unable to access property '" + name + "' through getter", e);
        }
      }   
      if (member instanceof Field) {
        try {
          if (needsToBeMadeAccessible) {
            ReflectionUtils.makeAccessible((Field)member);
          }
          return new TypedValue(((Field)member).get(target),typeDescriptor);
        }
        catch (Exception ex) {
          throw new AccessException("Unable to access field: " + name, ex);
        }
      }
      throw new AccessException("Neither getter nor field found for property '" + name + "'");
    }
View Full Code Here

      }
      ReflectionUtils.makeAccessible(this.ctor);
      return new TypedValue(this.ctor.newInstance(arguments), TypeDescriptor.valueOf(this.ctor.getDeclaringClass()));
    }
    catch (Exception ex) {
      throw new AccessException("Problem invoking constructor: " + this.ctor, ex);
    }
  }
View Full Code Here

      }
      ReflectionUtils.makeAccessible(this.method);
      return new TypedValue(this.method.invoke(target, arguments), new TypeDescriptor(new MethodParameter(this.method, -1)));
    }
    catch (Exception ex) {
      throw new AccessException("Problem invoking method: " + this.method, ex);
    }
  }
View Full Code Here

      ReflectionUtils.makeAccessible(this.method);
      Object value = this.method.invoke(target, arguments);
      return new TypedValue(value, new TypeDescriptor(new MethodParameter(this.method, -1)).narrow(value));
    }
    catch (Exception ex) {
      throw new AccessException("Problem invoking method: " + this.method, ex);
    }
  }
View Full Code Here

  }

  @Override
  public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException {
    if (target == null) {
      throw new AccessException("Cannot read property of null target");
    }
    Class<?> type = (target instanceof Class ? (Class<?>) target : target.getClass());

    if (type.isArray() && name.equals("length")) {
      if (target instanceof Class) {
        throw new AccessException("Cannot access length on array class itself");
      }
      return new TypedValue(Array.getLength(target));
    }

    CacheKey cacheKey = new CacheKey(type, name, target instanceof Class);
    InvokerPair invoker = this.readerCache.get(cacheKey);
    lastReadInvokerPair = invoker;

    if (invoker == null || invoker.member instanceof Method) {
      Method method = (Method) (invoker != null ? invoker.member : null);
      if (method == null) {
        method = findGetterForProperty(name, type, target);
        if (method != null) {
          // TODO remove the duplication here between canRead and read
          // Treat it like a property...
          // The readerCache will only contain gettable properties (let's not worry about setters for now).
          Property property = new Property(type, method, null);
          TypeDescriptor typeDescriptor = new TypeDescriptor(property);
          invoker = new InvokerPair(method, typeDescriptor);
          lastReadInvokerPair = invoker;
          this.readerCache.put(cacheKey, invoker);
        }
      }
      if (method != null) {
        try {
          ReflectionUtils.makeAccessible(method);
          Object value = method.invoke(target);
          return new TypedValue(value, invoker.typeDescriptor.narrow(value));
        }
        catch (Exception ex) {
          throw new AccessException("Unable to access property '" + name + "' through getter", ex);
        }
      }
    }

    if (invoker == null || invoker.member instanceof Field) {
      Field field = (Field) (invoker == null ? null : invoker.member);
      if (field == null) {
        field = findField(name, type, target);
        if (field != null) {
          invoker = new InvokerPair(field, new TypeDescriptor(field));
          lastReadInvokerPair = invoker;
          this.readerCache.put(cacheKey, invoker);
        }
      }
      if (field != null) {
        try {
          ReflectionUtils.makeAccessible(field);
          Object value = field.get(target);
          return new TypedValue(value, invoker.typeDescriptor.narrow(value));
        }
        catch (Exception ex) {
          throw new AccessException("Unable to access field: " + name, ex);
        }
      }
    }

    throw new AccessException("Neither getter nor field found for property '" + name + "'");
  }
View Full Code Here

  }

  @Override
  public void write(EvaluationContext context, Object target, String name, Object newValue) throws AccessException {
    if (target == null) {
      throw new AccessException("Cannot write property on null target");
    }
    Class<?> type = (target instanceof Class ? (Class<?>) target : target.getClass());

    Object possiblyConvertedNewValue = newValue;
    TypeDescriptor typeDescriptor = getTypeDescriptor(context, target, name);
    if (typeDescriptor != null) {
      try {
        possiblyConvertedNewValue = context.getTypeConverter().convertValue(
            newValue, TypeDescriptor.forObject(newValue), typeDescriptor);
      }
      catch (EvaluationException evaluationException) {
        throw new AccessException("Type conversion failure",evaluationException);
      }
    }
    CacheKey cacheKey = new CacheKey(type, name, target instanceof Class);
    Member cachedMember = this.writerCache.get(cacheKey);

    if (cachedMember == null || cachedMember instanceof Method) {
      Method method = (Method) cachedMember;
      if (method == null) {
        method = findSetterForProperty(name, type, target);
        if (method != null) {
          cachedMember = method;
          this.writerCache.put(cacheKey, cachedMember);
        }
      }
      if (method != null) {
        try {
          ReflectionUtils.makeAccessible(method);
          method.invoke(target, possiblyConvertedNewValue);
          return;
        }
        catch (Exception ex) {
          throw new AccessException("Unable to access property '" + name + "' through setter", ex);
        }
      }
    }

    if (cachedMember == null || cachedMember instanceof Field) {
      Field field = (Field) cachedMember;
      if (field == null) {
        field = findField(name, type, target);
        if (field != null) {
          cachedMember = field;
          this.writerCache.put(cacheKey, cachedMember);
        }
      }
      if (field != null) {
        try {
          ReflectionUtils.makeAccessible(field);
          field.set(target, possiblyConvertedNewValue);
          return;
        }
        catch (Exception ex) {
          throw new AccessException("Unable to access field: " + name, ex);
        }
      }
    }

    throw new AccessException("Neither setter nor field found for property '" + name + "'");
  }
View Full Code Here

          }
          Object value = ((Method) this.member).invoke(target);
          return new TypedValue(value, this.typeDescriptor.narrow(value));
        }
        catch (Exception ex) {
          throw new AccessException("Unable to access property '" + name + "' through getter", ex);
        }
      }
      if (this.member instanceof Field) {
        try {
          if (this.needsToBeMadeAccessible) {
            ReflectionUtils.makeAccessible((Field) this.member);
          }
          Object value = ((Field) this.member).get(target);
          return new TypedValue(value, this.typeDescriptor.narrow(value));
        }
        catch (Exception ex) {
          throw new AccessException("Unable to access field: " + name, ex);
        }
      }
      throw new AccessException("Neither getter nor field found for property '" + name + "'");
    }
View Full Code Here

TOP

Related Classes of org.springframework.expression.AccessException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.