001: /**
002: * InstantJ
003: *
004: * Copyright (C) 2002 Nils Meier
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */package instantj.tst;
017:
018: import instantj.compile.CompilationFailedException;
019: import instantj.compile.CompiledClass;
020: import instantj.compile.Compiler;
021: import instantj.compile.Source;
022: import instantj.expression.Expression;
023: import instantj.expression.ExpressionInstance;
024: import instantj.reflect.ReflectAccess;
025:
026: import java.util.Collection;
027: import java.util.Collections;
028: import java.util.HashMap;
029: import java.util.Map;
030:
031: /**
032: * A testcase that shows how to propagate dependencies (reused Compiled
033: * Classes) for compilation of a 3rd.
034: */
035: public class DependenciesTest {
036:
037: /** the source */
038: private final static String[] types = new String[] { "Object",
039: "Fruit", "Apple", "Macintosh" };
040:
041: /** the colors */
042: private final static String[] colors = new String[] { null,
043: "various", "green", "red" };
044:
045: /**
046: * MAIN
047: */
048: public static void main(String[] args) {
049:
050: // Disclaimer
051: System.out.println("InstantJ - "
052: + ReflectAccess.getInstance().calcClassNameOf(
053: DependenciesTest.class));
054:
055: // Loop creating one class after the other
056: CompiledClass cc = null;
057:
058: try {
059: for (int i = 1; i < types.length; i++) {
060:
061: // the current source
062: String source = "public class " + types[i]
063: + " extends " + types[i - 1] + " { "
064: + " public String getColor() { return \""
065: + colors[i] + "\"; } " + "} ";
066:
067: // the current dependency (we'll make a new one everytime)
068: Collection deps = cc == null ? null : Collections
069: .singleton(cc);
070:
071: // message
072: System.out.println("Compiling '" + types[i]
073: + "' extends '" + types[i - 1] + "'");
074:
075: // compiling it - we pass the previous compiled class as a one-collection
076: // dependency for the compilation process
077: cc = Compiler.compile(new Source(source), true, null,
078: deps);
079:
080: }
081: } catch (CompilationFailedException e) {
082: System.out.println("Compilation failed:");
083: e.printErrors(System.out);
084: return;
085: }
086:
087: // Create Java Type and dump hierachy
088: System.out.println("Creating Java Type '" + cc.getName()
089: + "' - it's type hierarchy is");
090: Class clazz = cc.getType();
091: printHierarchy(clazz);
092:
093: // Use it in Expression
094: String expression = " \"this \"+fruit+\" is \"+fruit.getColor() ";
095: System.out.println("Using Java Type '" + cc.getName()
096: + "' in expression:");
097: System.out.println(expression);
098: try {
099:
100: // prepare the expression parameters
101: Map props = new HashMap();
102: props.put("fruit", cc.getType());
103:
104: // .. and dependencies
105: Collection deps = Collections.singleton(cc);
106:
107: // here's the expression
108: ExpressionInstance e = new Expression(expression, props,
109: null, deps).getInstance();
110:
111: // prepare parameter value
112: Object fruit = cc.getType().newInstance();
113:
114: // evaluate
115: System.out.println("for fruit " + fruit + " evaluates to:");
116: System.out.println(" " + e.set("fruit", fruit).evaluate());
117:
118: } catch (CompilationFailedException e) {
119: System.out.println("Compilation failed:");
120: e.printErrors(System.out);
121: return;
122: } catch (Throwable t) {
123: t.printStackTrace();
124: }
125:
126: // Done
127: }
128:
129: /**
130: * Print type hierarchy
131: */
132: private static int printHierarchy(Class clazz) {
133: if (clazz == null)
134: return 1;
135: int level = printHierarchy(clazz.getSuperclass());
136: if (level == 1)
137: println(clazz.getName(), level);
138: else {
139: println("|", level);
140: println("+-" + clazz.getName(), level);
141: }
142: return level + 1;
143: }
144:
145: /**
146: * Print level'd text
147: */
148: private static void println(String s, int level) {
149: for (int i = 0; i < level; i++)
150: System.out.print(" ");
151: System.out.println(s);
152: }
153:
154: } //LibraryCompileTest
|