View Javadoc

1   /*
2   Copyright (C) 2006 Grid Systems, S.A.
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License, version 2, as
6   published by the Free Software Foundation.
7   
8   This program is distributed in the hope that it will be useful,
9   but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12  
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17  package com.gridsystems.beanfilter;
18  
19  import java.lang.reflect.Method;
20  import java.util.ArrayList;
21  import java.util.HashSet;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  /**
27   * Name of the variable or the final field in a path.
28   * <pre>
29   *  EX.1 :   module.parameter.name = "hello"
30   *                            ^^^^
31   *  EX.2 :   name = "John"
32   *           ^^^^
33   * </pre>
34   *
35   * @author Xmas
36   * @version 1.0
37   */
38  public class VariableValue extends EvalValue {
39  
40    /**
41     * Class logger.
42     */
43    private static Log log = LogFactory.getLog(FieldValue.class);
44  
45    /**
46     * The string.
47     */
48    private String s;
49  
50    /**
51     * Creates a new instance.
52     *
53     * @param token  The token
54     * @param s    The string
55     * @param parentClass Parent Class
56     * @throws EvalException If an error occurs
57     */
58    public VariableValue(Token token, String s, Class<?> parentClass)
59      throws EvalException {
60      super(token.beginLine, token.beginColumn);
61      this.s = s;
62      BeanDescriptor bd = BeanDescriptor.getInstance(parentClass);
63      Method method = bd.getMethod(s);
64      if (method == null) {
65        // FTR005=Unknown field ''{2}'' at position [{0}, {1}])
66        Object[] eparams = { getLinePos(), getCharPos(), s};
67        throw new EvalException("FTR005", eparams);
68      }
69    }
70  
71    /**
72     * {@inheritDoc}
73     */
74    @SuppressWarnings("unchecked")
75    @Override public Object getValue(Object parentValues) throws EvalException {
76      if (parentValues == null) {
77        return null;
78      }
79      if (parentValues instanceof HashSet) {
80        ArrayList<Object> values = new ArrayList<Object>((HashSet)parentValues);
81        if (values.size() == 0) {
82          return null;
83        }
84        BeanDescriptor bd = BeanDescriptor.getInstance(values.get(0).getClass());
85        HashSet<Object> results;
86        if (parentValues instanceof AllItemsHashSet) {
87          results = new AllItemsHashSet();
88        } else {
89          results = new AnyItemsHashSet();
90        }
91  
92        Method method = bd.getMethod(s);
93        if (method == null) {
94          // FTR005=Unknown field ''{2}'' at position [{0}, {1}])
95          Object[] eparams = { getLinePos(), getCharPos(), s };
96          throw new EvalException("FTR005", eparams);
97        }
98        try {
99          for (Object item : values) {
100           results.add(method.invoke(item));
101         }
102       } catch (Exception e) {
103         log.info(e.toString(), e);
104         // FTR005=Unknown field ''{2}'' at position [{0}, {1}])
105         Object[] eparams = { getLinePos(), getCharPos(), s };
106         throw new EvalException(e, "FTR005", eparams);
107       }
108 
109       return results;
110     } else {
111       BeanDescriptor bd = BeanDescriptor.getInstance(parentValues.getClass());
112       Object result;
113       Method method = bd.getMethod(s);
114       if (method == null) {
115         // FTR005=Unknown field ''{2}'' at position [{0}, {1}])
116         Object[] eparams = { getLinePos(), getCharPos(), s};
117         throw new EvalException("FTR005", eparams);
118       }
119       try {
120         result = method.invoke(parentValues);
121       } catch (Exception e) {
122         log.info(e.toString(), e);
123         // FTR005=Unknown field ''{2}'' at position [{0}, {1}])
124         Object[] eparams = { getLinePos(), getCharPos(), s};
125         throw new EvalException(e, "FTR005", eparams);
126       }
127       return result;
128     }
129   }
130 
131   /**
132    * {@inheritDoc}
133    */
134   @Override public String toString() {
135     return s;
136   }
137 
138   /**
139    * {@inheritDoc}
140    */
141   @Override
142   public Class<?> getClassValue(Class<?> parentClass) throws EvalException {
143     BeanDescriptor bd = BeanDescriptor.getInstance(parentClass);
144     Method method = bd.getMethod(s);
145     if (method == null) {
146       // FTR005=Unknown field ''{2}'' at position [{0}, {1}])
147       Object[] eparams = { getLinePos(), getCharPos(), s};
148       throw new EvalException("FTR005", eparams);
149     }
150     return method.getReturnType();
151   }
152 }