View Javadoc

1   /*
2    * Copyright 2002-2004 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.clazz.bean;
17  
18  import org.apache.commons.clazz.Clazz;
19  import org.apache.commons.clazz.ClazzLoader;
20  import org.apache.commons.clazz.ClazzOperation;
21  import org.apache.commons.clazz.common.ClazzFeatureSupport;
22  
23  /**
24   * 
25   * @author <a href="mailto:dmitri@apache.org">Dmitri Plotnikov</a>
26   * @version $Id: BeanClazzOperation.java 155436 2005-02-26 13:17:48Z dirkv $
27   */
28  public abstract class BeanClazzOperation
29      extends ClazzFeatureSupport
30      implements ClazzOperation 
31  {
32      private String returnClazzName;
33      private Clazz returnClazz;
34      private String name;
35      private static final String[] EMPTY_STRING_ARRAY = new String[0];
36      private String[] parameterClazzNames;
37      private Clazz[] parameterClazzes;
38      private String signature;
39      
40      /**
41       * Constructor for BeanClazzOperaiton.
42       * 
43       * @param declaringClazz the Clazz this Operation belongs to
44       * @param returnClazzName the return type of the operation, pass
45       * <code>null</code> for <code>void</code>.
46       * @param name the name of the operation.
47       * @param parameterClazzNames types of operation parameters, pass
48       * <code>null</code> if the operation does not take any parameters.
49       */
50      public BeanClazzOperation(
51              Clazz declaringClazz,
52              String returnClazzName,
53              String name,
54              String[] parameterClazzNames) 
55      {
56          super(declaringClazz);
57          this.returnClazzName = returnClazzName;
58          this.name = name;
59          this.parameterClazzNames =
60              (parameterClazzNames != null
61                  ? parameterClazzNames
62                  : EMPTY_STRING_ARRAY);
63      }
64  
65      /**
66       * @see ClazzOperation#getSignature()
67       */
68      public String getSignature() {
69          if (signature == null) {
70              signature = Clazz.constructSignature(name, getParameterClazzes());
71          }
72  
73          return signature;
74      }
75  
76      /**
77       * @see ClazzOperation#getName()
78       */
79      public String getName() {
80          return name;
81      }
82  
83      /**
84       * @see ClazzOperation#getParameterClazzes()
85       */
86      public Clazz[] getParameterClazzes() {
87          if (parameterClazzes == null) {
88              parameterClazzes = new Clazz[parameterClazzNames.length];
89              ClazzLoader loader = getDeclaringClazz().getClazzLoader();
90              for (int i = 0; i < parameterClazzes.length; i++) {
91                  parameterClazzes[i] =
92                      loader.getClazzForName(parameterClazzNames[i]);
93                  if (parameterClazzes[i] == null) {
94                      throw new BeanClazzConfigurationException(
95                          "Invalid argument type: "
96                              + parameterClazzNames[i]
97                              + ". Clazz not found.");
98                  }
99              }
100         }
101 
102         return parameterClazzes;
103     }
104 
105     /**
106      * @see ClazzOperation#getReturnClazz()
107      */
108     public Clazz getReturnClazz() {
109         if (returnClazzName == null) {
110             return null;
111         }
112         
113         if (returnClazz == null) {
114             ClazzLoader loader = getDeclaringClazz().getClazzLoader();
115             returnClazz = loader.getClazzForName(returnClazzName);            
116         }
117 
118         return returnClazz;
119     }
120 
121     /**
122      * @see ClazzOperation#invoke(Object, Object[])
123      */
124     public abstract Object invoke(Object target, Object[] parameters);
125 }