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.expression;
017:
018: import instantj.reflect.IllegalPropertyException;
019: import instantj.reflect.InaccessiblePropertyException;
020: import instantj.reflect.ReflectAccess;
021:
022: /**
023: * Supertype of all dynamically created Expressions. It offers
024: * one main method to evaluate the expression. Any expression
025: * arguments have to be set on that instance at this point.
026: *
027: * @author <A href="mailto:nils@meiers.net">Nils Meier</A>
028: */
029: public abstract class ExpressionInstance {
030:
031: /**
032: * The evaluation of the concrete expression instance
033: */
034: public final Object evaluate() throws EvaluationFailedException {
035:
036: try {
037: return internalEvaluate();
038: } catch (Throwable throwable) {
039: throw new EvaluationFailedException(throwable);
040: }
041:
042: }
043:
044: /**
045: * The concrete evaluation
046: */
047: protected abstract Object internalEvaluate();
048:
049: /**
050: * Overloaded method for converting primitive type into object
051: */
052: protected final Byte objectify(byte b) {
053: return new Byte(b);
054: }
055:
056: /**
057: * Overloaded method for converting primitive type into object
058: */
059: protected final Character objectify(char c) {
060: return new Character(c);
061: }
062:
063: /**
064: * Overloaded method for converting primitive type into object
065: */
066: protected final Double objectify(double d) {
067: return new Double(d);
068: }
069:
070: /**
071: * Overloaded method for converting primitive type into object
072: */
073: protected final Float objectify(float d) {
074: return new Float(d);
075: }
076:
077: /**
078: * Overloaded method for converting primitive type into object
079: */
080: protected final Integer objectify(int i) {
081: return new Integer(i);
082: }
083:
084: /**
085: * Overloaded method for converting primitive type into object
086: */
087: protected final Long objectify(long l) {
088: return new Long(l);
089: }
090:
091: /**
092: * Overloaded method for converting primitive type into object
093: */
094: protected final Object objectify(Object o) {
095: return o;
096: }
097:
098: /**
099: * Overloaded method for converting primitive type into object
100: */
101: protected final Short objectify(short s) {
102: return new Short(s);
103: }
104:
105: /**
106: * Overloaded method for converting primitive type into object
107: */
108: protected final Boolean objectify(boolean b) {
109: return new Boolean(b);
110: }
111:
112: /**
113: * Sets a property on the instance
114: */
115: public final ExpressionInstance set(String property, Object value)
116: throws InaccessiblePropertyException,
117: IllegalPropertyException {
118: ReflectAccess.getInstance().setField(this , property, value,
119: true);
120: return this ;
121: }
122:
123: /**
124: * Sets a primitive property on the instance
125: */
126: public final ExpressionInstance set(String property, int value)
127: throws InaccessiblePropertyException,
128: IllegalPropertyException {
129: return set(property, new Integer(value));
130: }
131:
132: /**
133: * Sets a primitive property on the instance
134: */
135: public final ExpressionInstance set(String property, double value)
136: throws InaccessiblePropertyException,
137: IllegalPropertyException {
138: return set(property, new Double(value));
139: }
140:
141: /**
142: * Sets a primitive property on the instance
143: */
144: public final ExpressionInstance set(String property, float value)
145: throws InaccessiblePropertyException,
146: IllegalPropertyException {
147: return set(property, new Float(value));
148: }
149:
150: /**
151: * Sets a primitive property on the instance
152: */
153: public final ExpressionInstance set(String property, boolean value)
154: throws InaccessiblePropertyException,
155: IllegalPropertyException {
156: return set(property, new Boolean(value));
157: }
158: }
|