001: /*
002: * Janino - An embedded Java[TM] compiler
003: *
004: * Copyright (c) 2001-2007, 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.io.*;
037: import java.lang.reflect.*;
038:
039: import org.codehaus.janino.*;
040:
041: public class ClassBodyDemo {
042: public static void main(String[] args) throws Exception {
043: if (args.length > 0 && args[0].equals("-help")) {
044: System.out
045: .println("Usage: ClassBodyDemo <class-body> { <argument> }");
046: System.out.println(" ClassBodyDemo -help");
047: System.out
048: .println("If <class-body> starts with a '@', then the class body is read");
049: System.out.println("from the named file.");
050: System.out
051: .println("The <class-body> must declare a method \"public static main(String[])\"");
052: System.out
053: .println("to which the <argument>s are passed. If the return type of that method is");
054: System.out
055: .println("not VOID, then the returned value is printed to STDOUT.");
056: System.exit(0);
057: }
058:
059: int i = 0;
060:
061: // Get class body.
062: if (i >= args.length) {
063: System.err.println("Class body missing; try \"-help\".");
064: }
065: String classBody = args[i++];
066: if (classBody.startsWith("@"))
067: classBody = ClassBodyDemo.readFileToString(classBody
068: .substring(1));
069:
070: // Get arguments.
071: String[] arguments = new String[args.length - i];
072: System.arraycopy(args, i, arguments, 0, arguments.length);
073:
074: // Compile the class body.
075: Class c = new ClassBodyEvaluator(classBody).getClazz();
076:
077: // Invoke the "public static main(String[])" method.
078: Method m = c.getMethod("main", new Class[] { String[].class });
079: Integer returnValue = (Integer) m.invoke(null,
080: new Object[] { arguments });
081:
082: // If non-VOID, print the return value.
083: if (m.getReturnType() != Void.TYPE)
084: System.out.println(DemoBase.toString(returnValue));
085: }
086:
087: private ClassBodyDemo() {
088: }
089:
090: private static String readFileToString(String fileName)
091: throws IOException {
092: Reader r = new FileReader(fileName);
093: try {
094: StringBuffer sb = new StringBuffer();
095: char[] ca = new char[1024];
096: for (;;) {
097: int cnt = r.read(ca, 0, ca.length);
098: if (cnt == -1)
099: break;
100: sb.append(ca, 0, cnt);
101: }
102: return sb.toString();
103: } finally {
104: r.close();
105: }
106: }
107: }
|