KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > ConstructorFunction


1 /*****************************************************************************
2  * Copyright (C) Codehaus.org. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  *****************************************************************************/

8 /*****************************************************************************
9  * Copyright (C) Codehaus.org. All rights reserved. *
10  * ------------------------------------------------------------------------- *
11  * The software in this package is published under the terms of the BSD *
12  * style license a copy of which has been included with this distribution in *
13  * the LICENSE.txt file. *
14  *****************************************************************************/

15 /*
16  * Created on Feb 28, 2005
17  *
18  * Author Ben Yu
19  * ZBS
20  */

21 package jfun.yan;
22
23 import jfun.yan.function.Function;
24
25
26 /**
27  * Codehaus.org.
28  *
29  * @author Ben Yu
30  *
31  */

32 final class ConstructorFunction<T> implements Function<T> {
33   public boolean isConcrete(){
34     return true;
35   }
36   public T call(Object JavaDoc[] args)
37   throws Throwable JavaDoc{
38     try{
39       return (T)ctor.getConstructor().newInstance(args);
40     }
41     catch(java.lang.reflect.InvocationTargetException JavaDoc e){
42       throw Utils.wrapInvocationException(e);
43     }
44   }
45   public Class JavaDoc[] getParameterTypes() {
46     return ctor.getConstructor().getParameterTypes();
47   }
48   public Class JavaDoc<T> getReturnType() {
49     return ctor.getConstructor().getDeclaringClass();
50   }
51   
52   ConstructorFunction(final java.lang.reflect.Constructor JavaDoc<T> ctor) {
53     this.ctor = new jfun.util.SerializableConstructor(ctor);
54   }
55   private final jfun.util.SerializableConstructor ctor;
56   /*
57   private void writeObject(java.io.ObjectOutputStream out)
58   throws java.io.IOException{
59     out.defaultWriteObject();
60     out.writeObject(ctor.getDeclaringClass());
61     out.writeObject(ctor.getParameterTypes());
62   }
63   private void readObject(java.io.ObjectInputStream in)
64     throws java.io.IOException, ClassNotFoundException{
65     in.defaultReadObject();
66     try{
67       final Class c = (Class)in.readObject();
68       final Class[] ptypes = (Class[])in.readObject();
69       this.ctor = c.getConstructor(ptypes);
70     }
71     catch(NoSuchMethodException e){
72       throw new IllegalStateException(e.getMessage());
73     }
74   }*/

75   public boolean equals(Object JavaDoc other) {
76     if(other instanceof ConstructorFunction){
77       final ConstructorFunction m2 = (ConstructorFunction)other;
78       return ctor.equals(m2.ctor);
79     }
80     else return false;
81   }
82   public String JavaDoc getName() {
83     return ctor.getConstructor().getName();
84   }
85   public int hashCode() {
86     return ctor.hashCode();
87   }
88   public String JavaDoc toString() {
89     return ctor.toString();
90   }
91 }
92
Popular Tags