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: }
|