Source Code Cross Referenced for JavaMethod.java in  » Scripting » jscheme » jsint » 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 » Scripting » jscheme » jsint 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package jsint;
002:
003:        import java.lang.reflect.Method;
004:        import java.util.Hashtable;
005:
006:        /** This class allows you to call any Java method, just by naming it,
007:         * and doing the dispatch at runtime.
008:         * @author Peter Norvig, Copyright 1998, peter@norvig.com, <a href="license.txt">license</a>
009:         * subsequently modified by Jscheme project members
010:         * licensed under zlib licence (see license.txt)
011:         **/
012:
013:        public class JavaMethod extends Reflector {
014:
015:            public static final Object[] ZERO_ARGS = new Object[0];
016:
017:            private String methodClass;
018:            /** Parameter/method table for a specific method. **/
019:            private transient Object[] methodTable;
020:            private boolean isStatic;
021:            /** Do we know the Class that this method applies to? **/
022:            private boolean isSpecific;
023:            /** Class -> methodTable map. **/
024:            private transient Hashtable classMethodTable;
025:
026:            public boolean isStatic() {
027:                return this .isStatic;
028:            }
029:
030:            /**
031:
032:               If the method is static then Class c is not null.  For instance
033:               methods, if Class c is not null, then it is used at construction
034:               time to create a method table.  Otherwise, the class of the
035:               method is determined at call time from the target, and the method
036:               table is constructed then and cached. Examples (see DynamicVariable.java):
037:
038:                <pre>
039:                new JavaMethod("getProperties", System.class, true) - static method
040:                new JavaMethod("put", Hashtable.class,false)        - specific instance method.
041:                new JavaMethod("put", null, false)                  - unspecified instance method
042:                </pre>
043:             **/
044:
045:            public JavaMethod(String name, Class c, boolean isStatic,
046:                    boolean isPrivileged) {
047:                this .name = name;
048:                if (c != null)
049:                    this .methodClass = c.getName();
050:                this .isStatic = isStatic;
051:                this .isSpecific = (c != null);
052:                this .minArgs = isStatic ? 0 : 1;
053:                this .isPrivileged = isPrivileged;
054:                reset();
055:            }
056:
057:            public JavaMethod(String name, Class c, boolean isStatic) {
058:                this (name, c, isStatic, false);
059:            }
060:
061:            public JavaMethod(String name, Class c) {
062:                this (name, c, (c != null));
063:            }
064:
065:            protected synchronized void reset() {
066:                if (isSpecific) {
067:                    methodTable = Invoke.methodTable0(Import
068:                            .classNamed(methodClass), name, isStatic,
069:                            isPrivileged);
070:                    if (methodTable.length == 0) {
071:                        methodTable = null;
072:                        E.warn("No such "
073:                                + (isStatic ? " static " : " instance ")
074:                                + " method \""
075:                                + name
076:                                + (isSpecific ? ("\" in class " + methodClass)
077:                                        : ""));
078:                    }
079:                } else
080:                    classMethodTable = new Hashtable(5);
081:            }
082:
083:            public Object[] instanceMethodTable(Class c) {
084:                Object[] ms = ((Object[]) classMethodTable.get(c));
085:                if (ms != null)
086:                    return ms;
087:                ms = Invoke.methodTable0(c, name, isStatic, isPrivileged);
088:                if (ms != null && ms.length > 0) {
089:                    classMethodTable.put(c, ms);
090:                    return ms;
091:                } else
092:                    return (Object[]) E.error(c + " has no methods for "
093:                            + this .name);
094:            }
095:
096:            /**
097:               For a static method, args is an Object[] of arguments.
098:               For an instance method, args is (vector target (vector arguments));
099:             **/
100:            public Object apply(Object[] args) {
101:                if (!(isSpecific)) {
102:                    Object[] methodTable = instanceMethodTable(args[0]
103:                            .getClass());
104:                    Object[] as = (Object[]) args[1];
105:                    Method m = (Method) Invoke.findMethod(methodTable, as);
106:                    return Invoke.invokeRawMethod(m, args[0], as);
107:                } else {
108:                    if (methodTable == null)
109:                        return E.error(this  + " has no methods");
110:                    if (isStatic) {
111:                        Method m = (Method) Invoke
112:                                .findMethod(methodTable, args);
113:                        return Invoke.invokeRawMethod(m, null, args);
114:                    } else {
115:                        Object[] as = (Object[]) args[1];
116:                        Method m = (Method) Invoke.findMethod(methodTable, as);
117:                        return Invoke.invokeRawMethod(m, args[0], as);
118:                    }
119:                }
120:            }
121:
122:            public Object[] makeArgArray(Object[] code, Evaluator eval,
123:                    LexicalEnvironment lexenv) {
124:                if (isStatic) {
125:                    int L = code.length - 1;
126:                    if (L == 0)
127:                        return ZERO_ARGS;
128:
129:                    Object[] args = new Object[L];
130:                    for (int i = 0; i < L; i++)
131:                        args[i] = eval.execute(code[i + 1], lexenv);
132:                    return args;
133:                } else {
134:                    int L = code.length - 2;
135:                    if (L < 0)
136:                        return ((Object[]) E
137:                                .error("Wrong number of arguments in application: "
138:                                        + U.stringify(code)));
139:                    Object target = eval.execute(code[1], lexenv);
140:                    if (L == 0)
141:                        return new Object[] { target, ZERO_ARGS };
142:
143:                    Object[] args = new Object[L];
144:                    for (int i = 0; i < L; i++)
145:                        args[i] = eval.execute(code[i + 2], lexenv);
146:                    return new Object[] { target, args };
147:                }
148:            }
149:
150:            public Object[] makeArgArray(Pair args) {
151:                if (isStatic)
152:                    return U.listToVector(args);
153:                else
154:                    return new Object[] { args.first, U.listToVector(args.rest) };
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.