KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > mop > ConstructorCallImpl


1 /*
2  * ################################################################
3  *
4  * ProActive: The Java(TM) library for Parallel, Distributed,
5  * Concurrent computing with Security and Mobility
6  *
7  * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8  * Contact: proactive-support@inria.fr
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23  * USA
24  *
25  * Initial developer(s): The ProActive Team
26  * http://www.inria.fr/oasis/ProActive/contacts.html
27  * Contributor(s):
28  *
29  * ################################################################
30  */

31 package org.objectweb.proactive.core.mop;
32
33 import java.io.IOException JavaDoc;
34 import java.io.Serializable JavaDoc;
35
36 import java.lang.reflect.Constructor JavaDoc;
37 import java.lang.reflect.InvocationTargetException JavaDoc;
38 import java.lang.reflect.Modifier JavaDoc;
39
40
41 /**
42  * A reified constructor call.
43  */

44 public class ConstructorCallImpl implements ConstructorCall, Serializable JavaDoc {
45
46     /**
47      * The array holding the arguments og the constructor
48      */

49     public Object JavaDoc[] effectiveArguments;
50
51     /**
52      * The corresponding constructor object
53      */

54     public Constructor JavaDoc reifiedConstructor;
55
56     //
57
// -- CONSTRUCTORS -----------------------------------------------
58
//
59

60     /**
61      * Effective constructor
62      * @param reifiedConstructor the constructor object which is called
63      * @param the array holding the effective args
64      */

65     public ConstructorCallImpl(Constructor JavaDoc reifiedConstructor,
66         Object JavaDoc[] effectiveArguments) {
67         this.reifiedConstructor = reifiedConstructor;
68         this.effectiveArguments = effectiveArguments;
69     }
70
71     //
72
// -- PUBLIC METHODS -----------------------------------------------
73
//
74
public String JavaDoc toString() {
75         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
76         sb.append("ConstructorCallImpl\n");
77         sb.append("reifiedConstructor=");
78         sb.append(reifiedConstructor);
79         sb.append("\n");
80         sb.append("effectiveArguments=");
81         if (effectiveArguments == null) {
82             sb.append("null\n");
83         } else {
84             sb.append("\n");
85             for (int i = 0; i < effectiveArguments.length; i++) {
86                 sb.append(" effectiveArguments[");
87                 sb.append(i);
88                 sb.append("]=");
89                 sb.append(effectiveArguments[i]);
90                 sb.append("\n");
91             }
92             sb.append("\n");
93         }
94         return sb.toString();
95     }
96
97     //
98
// -- implements ConstructorCall -----------------------------------------------
99
//
100

101     /**
102      * Make a deep copy of all arguments of the constructor
103      */

104     public void makeDeepCopyOfArguments() throws java.io.IOException JavaDoc {
105         effectiveArguments = (Object JavaDoc[]) Utils.makeDeepCopy(effectiveArguments);
106     }
107
108     /**
109      * Return the name of the target class that constructor is for
110      */

111     public String JavaDoc getTargetClassName() {
112         return getReifiedClass().getName();
113     }
114
115     /**
116      * Performs the object construction that is reified vy this object
117      * @throws InvocationTargetException
118      * @throws ConstructorCallExecutionFailedException
119      */

120     public Object JavaDoc execute()
121         throws InvocationTargetException JavaDoc,
122             ConstructorCallExecutionFailedException {
123         // System.out.println("ConstructorCall: The constructor is " + reifiedConstructor);
124
try {
125             return reifiedConstructor.newInstance(effectiveArguments);
126         } catch (IllegalAccessException JavaDoc e) {
127             throw new ConstructorCallExecutionFailedException(
128                 "Access rights to the constructor denied: " + e);
129         } catch (IllegalArgumentException JavaDoc e) {
130             throw new ConstructorCallExecutionFailedException(
131                 "Illegal constructor arguments: " + e);
132         } catch (InstantiationException JavaDoc e) {
133             if (getReifiedClass().isInterface()) {
134                 throw new ConstructorCallExecutionFailedException(
135                     "Cannot build an instance of an interface: " + e);
136             } else if (Modifier.isAbstract(getReifiedClass().getModifiers())) {
137                 throw new ConstructorCallExecutionFailedException(
138                     "Cannot build an instance of an abstract class: " + e);
139             } else {
140                 throw new ConstructorCallExecutionFailedException(
141                     "Instanciation problem: " + e +
142                     ". Strange enough, the reified class is neither abstract nor an interface.");
143             }
144         } catch (ExceptionInInitializerError JavaDoc e) {
145             throw new ConstructorCallExecutionFailedException(
146                 "Cannot build object because the initialization of its class failed: " +
147                 e);
148         }
149     }
150
151     //
152
// -- PROTECTED METHODS -----------------------------------------------
153
//
154

155     /**
156      * Returns a <code>Class</code> object representing the type of
157      * the object this reified constructor will build when reflected
158      */

159     protected Class JavaDoc getReifiedClass() {
160         return reifiedConstructor.getDeclaringClass();
161     }
162
163     //
164
// -- PRIVATE METHODS -----------------------------------------------
165
//
166
private void writeObject(java.io.ObjectOutputStream JavaDoc out)
167         throws IOException JavaDoc {
168         // We want to implement a workaround the Constructor
169
// not being Serializable
170
out.writeObject(this.effectiveArguments);
171         // Constructor needs to be converted because it is not serializable
172
Class JavaDoc declaringClass;
173         Class JavaDoc[] parameters;
174
175         declaringClass = this.reifiedConstructor.getDeclaringClass();
176         out.writeObject(declaringClass);
177
178         parameters = this.reifiedConstructor.getParameterTypes();
179         out.writeObject(parameters);
180     }
181
182     private void readObject(java.io.ObjectInputStream JavaDoc in)
183         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
184         Class JavaDoc declaringClass = null;
185         Class JavaDoc[] parameters;
186         try {
187             this.effectiveArguments = (Object JavaDoc[]) in.readObject();
188         } catch (IOException JavaDoc e) {
189           // System.out.println("Stream is " + in.getClass().getName());
190
// e.printStackTrace();
191
throw e;
192         }
193 // Object test =
194
// System.out.println("----- ConstructorCallImpl.readObject() class " + test.getClass().getName() + " loaded by " + test.getClass().getClassLoader());
195
declaringClass = (Class JavaDoc) in.readObject();
196         parameters = (Class JavaDoc[]) in.readObject();
197
198         try {
199             this.reifiedConstructor = declaringClass.getConstructor(parameters);
200         } catch (NoSuchMethodException JavaDoc e) {
201             throw new InternalException("Lookup for constructor failed: " + e +
202                 ". This may be caused by having different versions of the same class on different VMs. Check your CLASSPATH settings.");
203         }
204     }
205 }
206
Popular Tags