001: /*
002: * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package org.mandarax.kernel.meta;
020:
021: import java.io.IOException;
022: import java.io.ObjectInputStream;
023: import java.io.ObjectOutputStream;
024: import java.lang.reflect.Method;
025:
026: import org.mandarax.kernel.Function;
027:
028: /**
029: * A JFunction is a function defined by a java method. E.g., if <code>'Max'</code>
030: * is a constant term (Max itself being an instance of a class <code>Person</code>),
031: * and <code>father()</code> is a method defined in this class returning another
032: * instance, then we can build a <code>JFunction</code> based on the method
033: * <code>father()</code> with a well defined semantic: the term <code>father('Max')</code>
034: * can be simplified to a constant term that wrappes the result of invoking
035: * <code>father()</code> with the parameter array <code>{Max}</code> (and that is me, Jens :-)).
036: * <br>
037: * In version 3.2, session support has been added. The last parameter (found in the wrapoped method)
038: * is treated separately if its type is Session (or a subclass of session). Then this parameter is used
039: * to pass a session reference at query time to the function or predicate!
040: * @see org.mandarax.kernel.Session
041: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
042: * @version 3.4 <7 March 05>
043: * @since 1.0
044: */
045: public final class JFunction extends JConstructor implements Function {
046:
047: /**
048: * Constructor.
049: * @param aMethod the method used
050: */
051: public JFunction() {
052: super ();
053: }
054:
055: /**
056: * Constructor.
057: * @param aMethod the method used
058: */
059: public JFunction(Method aMethod) {
060: super (aMethod);
061: }
062:
063: /**
064: * Constructor.
065: * @param aMethod the method used
066: * @param aName the name of the object
067: */
068: public JFunction(Method aMethod, String aName) {
069: super (aMethod, aName);
070: }
071:
072: /**
073: * Indicates whether this function equals the object.
074: * @param obj the object to compare this object with
075: * @return true if the objects are equal, false otherwise
076: */
077: public boolean equals(Object obj) {
078: if (obj instanceof org.mandarax.kernel.Function) {
079: Function f = (Function) obj;
080: Class[] c1 = f.getStructure();
081: Class[] c2 = getStructure();
082: boolean result = f.getName().equals(getName());
083:
084: result = result && (c1.length == c2.length);
085:
086: for (int i = 0; i < c1.length; i++) {
087: result = result && (c1[i] == c2[i]);
088: }
089:
090: return result;
091: }
092:
093: return false;
094: }
095:
096: /**
097: * Get the return type.
098: * @return the return type
099: */
100: public Class getReturnType() {
101: return convertType(method.getReturnType());
102: }
103:
104: /**
105: * Read the object from an object input stream.
106: * @param in an input stream
107: */
108: private void readObject(ObjectInputStream in) throws IOException,
109: ClassNotFoundException {
110: in.defaultReadObject();
111:
112: String methodName = (String) in.readObject();
113: Class declaringClass = (Class) in.readObject();
114: Class[] parTypes = (Class[]) in.readObject();
115:
116: try {
117: Method m = declaringClass.getMethod(methodName, parTypes);
118:
119: method = m;
120: } catch (NoSuchMethodException x) {
121: throw new IOException("Cannot find method " + name);
122: }
123: }
124:
125: /**
126: * Write the object to an object output stream.
127: * @param out an output stream
128: */
129: private void writeObject(ObjectOutputStream out)
130: throws IOException, ClassNotFoundException {
131: out.defaultWriteObject();
132: out.writeObject(method.getName());
133: out.writeObject(method.getDeclaringClass());
134: out.writeObject(method.getParameterTypes());
135: }
136: }
|