KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jga > fn > property > GetProperty


1 // ============================================================================
2
// $Id: GetProperty.java,v 1.13 2006/01/08 00:52:25 davidahall Exp $
3
// Copyright (c) 2002-2005 David A. Hall
4
// ============================================================================
5
// The contents of this file are subject to the Common Development and
6
// Distribution License (CDDL), Version 1.0 (the License); you may not use this
7
// file except in compliance with the License. You should have received a copy
8
// of the the License along with this file: if not, a copy of the License is
9
// available from Sun Microsystems, Inc.
10
//
11
// http://www.sun.com/cddl/cddl.html
12
//
13
// From time to time, the license steward (initially Sun Microsystems, Inc.) may
14
// publish revised and/or new versions of the License. You may not use,
15
// distribute, or otherwise make this file available under subsequent versions
16
// of the License.
17
//
18
// Alternatively, the contents of this file may be used under the terms of the
19
// GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
20
// case the provisions of the LGPL are applicable instead of those above. If you
21
// wish to allow use of your version of this file only under the terms of the
22
// LGPL, and not to allow others to use your version of this file under the
23
// terms of the CDDL, indicate your decision by deleting the provisions above
24
// and replace them with the notice and other provisions required by the LGPL.
25
// If you do not delete the provisions above, a recipient may use your version
26
// of this file under the terms of either the CDDL or the LGPL.
27
//
28
// This library is distributed in the hope that it will be useful,
29
// but WITHOUT ANY WARRANTY; without even the implied warranty of
30
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
31
// ============================================================================
32

33 package net.sf.jga.fn.property;
34
35 import java.lang.reflect.InvocationTargetException JavaDoc;
36 import java.lang.reflect.Method JavaDoc;
37 import java.text.MessageFormat JavaDoc;
38 import net.sf.jga.fn.EvaluationException;
39 import net.sf.jga.fn.UnaryFunctor;
40
41 /**
42  * Unary Functor that returns the value of the named property for the argument.
43  * The property name and type are set at construction.
44  * <p>
45  * Copyright &copy; 2002-2005 David A. Hall
46  *
47  * @author <a HREF="mailto:davidahall@users.sourceforge.net">David A. Hall</a>
48  **/

49
50 public class GetProperty<T,R> extends UnaryFunctor<T,R> {
51     
52     static final long serialVersionUID = -6249123644692001840L;
53
54     // The name of the property to be returned
55
private String JavaDoc _propName;
56
57     // The name of the method corresponding to the property
58
private String JavaDoc _methName;
59
60     // The class from which the property is retrieved.
61
private Class JavaDoc<T> _argtype;
62
63     // The method called to get the property
64
private transient Method JavaDoc _meth;
65
66     /**
67      * Builds a GetProperty for the given property, using the given class to
68      * find the desired method.
69      * @throws IllegalArgumentException if the property name is null or empty,
70      * or if there is no getter method for the given property name.
71      */

72     public GetProperty(Class JavaDoc<T> argclass, String JavaDoc propName) {
73         if (propName == null || propName.length() == 0) {
74             throw new IllegalArgumentException JavaDoc("Must supply property name");
75         }
76
77         _argtype = argclass;
78         
79         if (propName.startsWith("get")) {
80             _methName = propName;
81             _propName = propName.substring(3);
82         }
83         else if (argclass.equals(Boolean JavaDoc.class) && propName.startsWith("is")) {
84             _methName = propName;
85             _propName = propName.substring(2);
86         }
87         else {
88             _propName = propName;
89             _methName = "get" + propName;
90         }
91         
92         try {
93             _meth = argclass.getMethod(_methName, new Class JavaDoc[0]);
94         }
95         catch (NoSuchMethodException JavaDoc x) {
96             String JavaDoc msg = "class {0} does not have property \"{1}\"";
97             Object JavaDoc[] args = new Object JavaDoc[]{ argclass.getName(), propName };
98             IllegalArgumentException JavaDoc iax =
99                 new IllegalArgumentException JavaDoc(MessageFormat.format(msg,args));
100             iax.initCause(x);
101             throw iax;
102         }
103     }
104
105     /**
106      * Returns the name of the property that this functor retrieves.
107      */

108     public String JavaDoc getPropertyName() {
109         return _propName;
110     }
111
112     // Unary interface
113

114     /**
115      * Returns the value of the designated property of the argument
116      * @return the value of the designated property of the argument
117      * @throws EvaluationException if the argument does not have the designated
118      * public property, or if it is not of the correct type.
119      */

120     public R fn(T arg) {
121         try {
122             // @SuppressWarnings
123
// There's nothing we can do about this other than warn the users
124
// to make sure that they don't use an inappropriate return type
125
R val = (R) getMethod().invoke(arg, new Object JavaDoc[0]);
126             return val;
127         }
128         catch (ClassCastException JavaDoc x) {
129             String JavaDoc msg = "{0}.{1} returns type {2}";
130             Method JavaDoc m = getMethod();
131             Object JavaDoc[] args = new Object JavaDoc[]{ _argtype.getName(), m.getName(),
132                                           m.getReturnType().getName() };
133             throw new EvaluationException(MessageFormat.format(msg,args), x);
134         }
135         catch (IllegalAccessException JavaDoc x) {
136             String JavaDoc msg = "{0}.{1} is not accessible";
137             Object JavaDoc[] args = new Object JavaDoc[]{ _argtype.getName(), getMethod().getName()};
138             throw new EvaluationException(MessageFormat.format(msg,args), x);
139         }
140         catch (InvocationTargetException JavaDoc x) {
141             String JavaDoc msg = "{0}.{1} failed : "+x.getMessage();
142             Object JavaDoc[] args = new Object JavaDoc[]{ _argtype.getName(), getMethod().getName()};
143             throw new EvaluationException(MessageFormat.format(msg,args), x);
144         }
145     }
146
147     private Method JavaDoc getMethod() {
148         if (_meth == null) {
149             try {
150                 _meth = _argtype.getMethod(_methName, new Class JavaDoc[0]);
151             }
152             catch (NoSuchMethodException JavaDoc x) {
153                 String JavaDoc msg = "class {0} does not have method {1}";
154                 Object JavaDoc[] args = new Object JavaDoc[]{ _argtype.getName(), _methName};
155                 throw new EvaluationException(MessageFormat.format(msg,args), x);
156             }
157         }
158
159         return _meth;
160     }
161
162     /**
163      * Calls the Visitor's <code>visit(GetProperty)</code> method, if it
164      * implements the nested Visitor interface.
165      */

166     public void accept(net.sf.jga.fn.Visitor v) {
167         if (v instanceof GetProperty.Visitor)
168             ((GetProperty.Visitor)v).visit(this);
169         else
170             v.visit(this);
171     }
172     
173     // Object overrides
174

175     public String JavaDoc toString() {
176         return "GetProperty["+_propName+"]";
177     }
178     
179    // AcyclicVisitor
180

181     /**
182      * Interface for classes that may interpret a <b>GetProperty</b>
183      * function.
184      */

185     public interface Visitor extends net.sf.jga.fn.Visitor {
186         public void visit(GetProperty host);
187     }
188 }
189
Popular Tags