Source Code Cross Referenced for DemoBase.java in  » Scripting » jacl » org » codehaus » janino » samples » 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 » jacl » org.codehaus.janino.samples 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Janino - An embedded Java[TM] compiler
003:         *
004:         * Copyright (c) 2006, Arno Unkrig
005:         * All rights reserved.
006:         *
007:         * Redistribution and use in source and binary forms, with or without
008:         * modification, are permitted provided that the following conditions
009:         * are met:
010:         *
011:         *    1. Redistributions of source code must retain the above copyright
012:         *       notice, this list of conditions and the following disclaimer.
013:         *    2. Redistributions in binary form must reproduce the above
014:         *       copyright notice, this list of conditions and the following
015:         *       disclaimer in the documentation and/or other materials
016:         *       provided with the distribution.
017:         *    3. The name of the author may not be used to endorse or promote
018:         *       products derived from this software without specific prior
019:         *       written permission.
020:         *
021:         * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
022:         * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
023:         * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
024:         * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
025:         * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
026:         * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
027:         * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
028:         * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
029:         * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
030:         * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
031:         * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
032:         */
033:
034:        package org.codehaus.janino.samples;
035:
036:        import java.util.*;
037:        import java.lang.reflect.*;
038:
039:        /**
040:         * Common base class for the "...Demo" classes that demostrate Janino.
041:         */
042:
043:        public class DemoBase {
044:            protected DemoBase() {
045:            }
046:
047:            public static Object createObject(Class type, String value)
048:                    throws NoSuchMethodException, InstantiationException,
049:                    InvocationTargetException, IllegalAccessException {
050:
051:                // Wrap primitive parameters.
052:                if (type.isPrimitive()) {
053:                    type = (type == boolean.class ? Boolean.class
054:                            : type == char.class ? Character.class
055:                                    : type == byte.class ? Byte.class
056:                                            : type == short.class ? Short.class
057:                                                    : type == int.class ? Integer.class
058:                                                            : type == long.class ? Long.class
059:                                                                    : type == float.class ? Float.class
060:                                                                            : type == double.class ? Double.class
061:                                                                                    : void.class);
062:                }
063:
064:                // Construct object, assuming it has a default constructor or a
065:                // constructor with one single "String" argument.
066:                if (value.equals("")) {
067:                    return type.getConstructor(new Class[0]).newInstance(
068:                            new Object[0]);
069:                } else {
070:                    return type.getConstructor(new Class[] { String.class })
071:                            .newInstance(new Object[] { value });
072:                }
073:            }
074:
075:            public static String[] explode(String s) {
076:                StringTokenizer st = new StringTokenizer(s, ",");
077:                List l = new ArrayList();
078:                while (st.hasMoreTokens())
079:                    l.add(st.nextToken());
080:                return (String[]) l.toArray(new String[l.size()]);
081:            }
082:
083:            public static Class stringToType(String s) {
084:                int brackets = 0;
085:                while (s.endsWith("[]")) {
086:                    ++brackets;
087:                    s = s.substring(0, s.length() - 2);
088:                }
089:
090:                if (brackets == 0) {
091:                    // "Class.forName("C")" does not work.
092:                    if (s.equals("void"))
093:                        return void.class;
094:                    if (s.equals("boolean"))
095:                        return boolean.class;
096:                    if (s.equals("char"))
097:                        return char.class;
098:                    if (s.equals("byte"))
099:                        return byte.class;
100:                    if (s.equals("short"))
101:                        return short.class;
102:                    if (s.equals("int"))
103:                        return int.class;
104:                    if (s.equals("long"))
105:                        return long.class;
106:                    if (s.equals("float"))
107:                        return float.class;
108:                    if (s.equals("double"))
109:                        return double.class;
110:                }
111:
112:                // Automagically convert primitive type names.
113:                if (s.equals("void")) {
114:                    s = "V";
115:                } else if (s.equals("boolean")) {
116:                    s = "Z";
117:                } else if (s.equals("char")) {
118:                    s = "C";
119:                } else if (s.equals("byte")) {
120:                    s = "B";
121:                } else if (s.equals("short")) {
122:                    s = "S";
123:                } else if (s.equals("int")) {
124:                    s = "I";
125:                } else if (s.equals("long")) {
126:                    s = "J";
127:                } else if (s.equals("float")) {
128:                    s = "F";
129:                } else if (s.equals("double")) {
130:                    s = "D";
131:                }
132:
133:                while (--brackets >= 0)
134:                    s = '[' + s;
135:                try {
136:                    return Class.forName(s);
137:                } catch (ClassNotFoundException ex) {
138:                    ex.printStackTrace();
139:                    System.exit(1);
140:                    throw new RuntimeException(); // Never reached.
141:                }
142:            }
143:
144:            public static Class[] stringToTypes(String s) {
145:                StringTokenizer st = new StringTokenizer(s, ",");
146:                List l = new ArrayList();
147:                while (st.hasMoreTokens())
148:                    l.add(DemoBase.stringToType(st.nextToken()));
149:                Class[] res = new Class[l.size()];
150:                l.toArray(res);
151:                return res;
152:            }
153:
154:            public static String toString(Object o) {
155:                if (o == null)
156:                    return "(null)";
157:
158:                // Pretty-print array.
159:                Class clazz = o.getClass();
160:                if (clazz.isArray()) {
161:                    StringBuffer sb = new StringBuffer(clazz.getComponentType()
162:                            .toString()).append("[] { ");
163:                    for (int i = 0; i < Array.getLength(o); ++i) {
164:                        if (i > 0)
165:                            sb.append(", ");
166:                        sb.append(DemoBase.toString(Array.get(o, i)));
167:                    }
168:                    sb.append(" }");
169:                    return sb.toString();
170:                }
171:
172:                // Apply default "toString()" method.
173:                return o.toString();
174:            }
175:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.