View Javadoc

1   package org.apache.velocity.util.introspection;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.    
20   */
21  
22  import java.util.Iterator;
23  
24  import org.apache.velocity.runtime.RuntimeServices;
25  import org.apache.velocity.util.RuntimeServicesAware;
26  
27  /**
28   * <p>
29   * When the runtime.introspection.uberspect configuration property contains several
30   * uberspector class names, it means those uberspectors will be chained. When an
31   * uberspector in the list other than the leftmost does not implement ChainableUberspector,
32   * then this utility class is used to provide a basic default chaining where the
33   * first non-null result is kept for each introspection call.
34   * </p>
35   * 
36   * @since 1.6
37   * @see ChainableUberspector
38   * @version $Id: LinkingUberspector.java 10959 2008-07-01 00:12:29Z sdumitriu $
39   */
40  public class LinkingUberspector extends AbstractChainableUberspector
41  {
42      private Uberspect leftUberspect;
43      private Uberspect rightUberspect;
44  
45      /**
46       * Constructor that takes the two uberspectors to link
47       */
48      public LinkingUberspector(Uberspect left,Uberspect right) {
49          leftUberspect = left;
50          rightUberspect = right;
51      }
52  
53      /**
54       * {@inheritDoc}
55       * <p>
56       * Init both wrapped uberspectors
57       * </p>
58       * 
59       * @see org.apache.velocity.util.introspection.Uberspect#init()
60       */
61      //@Override
62      public void init() throws Exception
63      {
64          leftUberspect.init();
65          rightUberspect.init();
66      }
67  
68      /**
69       * {@inheritDoc}
70       * 
71       * @see org.apache.velocity.util.introspection.Uberspect#getIterator(java.lang.Object,
72       *      org.apache.velocity.util.introspection.Info)
73       */
74      //@SuppressWarnings("unchecked")
75      //@Override
76      public Iterator getIterator(Object obj, Info i) throws Exception
77      {
78          Iterator it = leftUberspect.getIterator(obj,i);
79          return it != null ? it : rightUberspect.getIterator(obj,i);
80      }
81  
82      /**
83       * {@inheritDoc}
84       * 
85       * @see org.apache.velocity.util.introspection.Uberspect#getMethod(java.lang.Object, java.lang.String,
86       *      java.lang.Object[], org.apache.velocity.util.introspection.Info)
87       */
88      //@Override
89      public VelMethod getMethod(Object obj, String methodName, Object[] args, Info i) throws Exception
90      {
91          VelMethod method = leftUberspect.getMethod(obj,methodName,args,i);
92          return method != null ? method : rightUberspect.getMethod(obj,methodName,args,i);
93      }
94  
95      /**
96       * {@inheritDoc}
97       * 
98       * @see org.apache.velocity.util.introspection.Uberspect#getPropertyGet(java.lang.Object, java.lang.String,
99       *      org.apache.velocity.util.introspection.Info)
100      */
101     //@Override
102     public VelPropertyGet getPropertyGet(Object obj, String identifier, Info i) throws Exception
103     {
104         VelPropertyGet getter = leftUberspect.getPropertyGet(obj,identifier,i);
105         return getter != null ? getter : rightUberspect.getPropertyGet(obj,identifier,i);
106     }
107 
108     /**
109      * {@inheritDoc}
110      * 
111      * @see org.apache.velocity.util.introspection.Uberspect#getPropertySet(java.lang.Object, java.lang.String,
112      *      java.lang.Object, org.apache.velocity.util.introspection.Info)
113      */
114     //@Override
115     public VelPropertySet getPropertySet(Object obj, String identifier, Object arg, Info i) throws Exception
116     {
117         VelPropertySet setter = leftUberspect.getPropertySet(obj,identifier,arg,i);
118         return setter != null ? setter : rightUberspect.getPropertySet(obj,identifier,arg,i);
119     }
120 }