Source Code Cross Referenced for MethodFunction.java in  » Database-ORM » MMBase » org » mmbase » util » functions » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database ORM » MMBase » org.mmbase.util.functions 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:
003:        This software is OSI Certified Open Source Software.
004:        OSI Certified is a certification mark of the Open Source Initiative.
005:
006:        The license (Mozilla version 1.0) can be read at the MMBase site.
007:        See http://www.MMBase.org/license
008:
009:         */
010:        package org.mmbase.util.functions;
011:
012:        import java.lang.reflect.*;
013:        import java.lang.annotation.*;
014:
015:        /**
016:         * A function based on an abritrary method. Since the name of the parameters cannot be found by
017:         * reflection, this is only of limited use. Normally you would probably better use BeanFunction. A
018:         * method-function can come in handy on JSP's. With the advent of java 1.5 we can use annotations to
019:         * annotate acutal parameter names.
020:         *
021:         * @author Michiel Meeuwissen
022:         * @version $Id: MethodFunction.java,v 1.11 2007/11/25 18:25:49 nklasens Exp $
023:         * @see org.mmbase.module.core.MMObjectBuilder#executeFunction
024:         * @see org.mmbase.bridge.Node#getFunctionValue
025:         * @see org.mmbase.util.functions.BeanFunction
026:         * @since MMBase-1.7
027:         */
028:        public class MethodFunction extends AbstractFunction<Object> {
029:
030:            public static Function<Object> getFunction(Method method,
031:                    String name) {
032:                return new MethodFunction(method, name); // could be cached...
033:            }
034:
035:            /**
036:             * @since MMBase-1.9
037:             */
038:            public static Function<Object> getFunction(Method method,
039:                    String name, Object instance) {
040:                return new MethodFunction(method, name, instance); // could be cached...
041:            }
042:
043:            /**
044:             * Returns the MethodFunction representing the method 'name' in class 'clazz'. If there are more
045:             * methods whith that name, the one with the largest number of by name annotated parameters is taken.
046:             * @since MMBase-1.9
047:             */
048:            public static Function<Object> getFunction(Class<?> clazz,
049:                    String name) {
050:                // Finding method to use
051:                Method method = getMethod(clazz, name);
052:                return getFunction(method, method.getName());
053:            }
054:
055:            public static Method getMethod(Class<?> clazz, String name) {
056:                // Finding method to use
057:                Method method = null;
058:                float score = -1.0f;
059:                for (Method m : clazz.getMethods()) {
060:                    String methodName = m.getName();
061:                    if (methodName.equals(name)) {
062:                        Annotation[][] annots = m.getParameterAnnotations();
063:                        int found = 0;
064:                        int total = 1; // avoids division by zero and ensures that methods with more parameters are better.
065:                        for (Annotation[] anot : annots) {
066:                            for (Annotation a : anot) {
067:                                if (a.annotationType().equals(Name.class)) {
068:                                    found++;
069:                                }
070:                            }
071:                            total++;
072:                        }
073:                        if ((float) found / total > score) {
074:                            method = m;
075:                            score = (float) found / total;
076:                        }
077:                    }
078:                }
079:                return method;
080:            }
081:
082:            private final Method method;
083:            private final Object instance;
084:
085:            /**
086:             * @since MMBase-1.9
087:             */
088:            public MethodFunction(Method method) {
089:                this (method, method.getName(), null);
090:            }
091:
092:            public MethodFunction(Method method, String name) {
093:                this (method, name, null);
094:            }
095:
096:            /**
097:             * @since MMBase-1.9
098:             */
099:            public MethodFunction(Method method, Object instance) {
100:                this (method, method.getName(), instance);
101:            }
102:
103:            /**
104:             * @since MMBase-1.9
105:             */
106:            public MethodFunction(Method method, String name, Object instance) {
107:                super (name, null, null);
108:                this .method = method;
109:                this .instance = instance;
110:                if (instance == null) {
111:                    if (!Modifier.isStatic(method.getModifiers())) {
112:                        throw new IllegalArgumentException("The method "
113:                                + method + " is not static"); // otherwise NPE in getFunctionValue
114:                    }
115:                } else {
116:                    if (!method.getDeclaringClass().isInstance(instance)) {
117:                        throw new IllegalArgumentException("The object "
118:                                + instance
119:                                + " is not an instance of the class of  "
120:                                + method);
121:                    }
122:                }
123:
124:                Annotation[][] annots = method.getParameterAnnotations();
125:                Class[] parameters = method.getParameterTypes();
126:                Parameter<?>[] def = new Parameter[parameters.length];
127:                for (int i = 0; i < parameters.length; i++) {
128:                    String paramName = null;
129:                    for (Annotation annot : annots[i]) {
130:                        if (annot.annotationType().equals(Name.class)) {
131:                            paramName = ((Name) annot).value();
132:                        }
133:                    }
134:                    if (paramName == null)
135:                        paramName = "parameter" + (i + 1);
136:
137:                    def[i] = new Parameter<String>(paramName, parameters[i]); // no way to find the name of the parameter
138:                }
139:
140:                setParameterDefinition(def);
141:
142:                ReturnType returnType = ReturnType.getReturnType(method
143:                        .getReturnType());
144:                setReturnType(returnType);
145:
146:            }
147:
148:            public Object getFunctionValue(Parameters parameters) {
149:                try {
150:                    return method.invoke(instance, parameters.toArray());
151:                } catch (Exception e) {
152:                    throw new RuntimeException(e);
153:                }
154:            }
155:
156:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.