00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 package org.objectweb.proactive.core.mop;
00032
00033 import java.io.IOException;
00034 import java.io.Serializable;
00035 import java.lang.reflect.Constructor;
00036 import java.lang.reflect.InvocationTargetException;
00037 import java.lang.reflect.Modifier;
00038
00039
00043 public class ConstructorCallImpl implements ConstructorCall, Serializable {
00044
00048 public Object[] effectiveArguments;
00049
00053 public Constructor reifiedConstructor;
00054
00055
00056
00057
00058
00064 public ConstructorCallImpl(Constructor reifiedConstructor,
00065 Object[] effectiveArguments) {
00066 this.reifiedConstructor = reifiedConstructor;
00067 this.effectiveArguments = effectiveArguments;
00068 }
00069
00070
00071
00072
00073 public String toString() {
00074 StringBuffer sb = new StringBuffer();
00075 sb.append("ConstructorCallImpl\n");
00076 sb.append("reifiedConstructor=");
00077 sb.append(reifiedConstructor);
00078 sb.append("\n");
00079 sb.append("effectiveArguments=");
00080 if (effectiveArguments == null) {
00081 sb.append("null\n");
00082 } else {
00083 sb.append("\n");
00084 for (int i = 0; i < effectiveArguments.length; i++) {
00085 sb.append(" effectiveArguments[");
00086 sb.append(i);
00087 sb.append("]=");
00088 sb.append(effectiveArguments[i]);
00089 sb.append("\n");
00090 }
00091 sb.append("\n");
00092 }
00093 return sb.toString();
00094 }
00095
00096
00097
00098
00099
00103 public void makeDeepCopyOfArguments() throws java.io.IOException {
00104 effectiveArguments = (Object[]) Utils.makeDeepCopy(effectiveArguments);
00105 }
00106
00110 public String getTargetClassName() {
00111 return getReifiedClass().getName();
00112 }
00113
00119 public Object execute()
00120 throws InvocationTargetException,
00121 ConstructorCallExecutionFailedException {
00122
00123 try {
00124 return reifiedConstructor.newInstance(effectiveArguments);
00125 } catch (IllegalAccessException e) {
00126 throw new ConstructorCallExecutionFailedException(
00127 "Access rights to the constructor denied: " + e);
00128 } catch (IllegalArgumentException e) {
00129 throw new ConstructorCallExecutionFailedException(
00130 "Illegal constructor arguments: " + e);
00131 } catch (InstantiationException e) {
00132 if (getReifiedClass().isInterface()) {
00133 throw new ConstructorCallExecutionFailedException(
00134 "Cannot build an instance of an interface: " + e);
00135 } else if (Modifier.isAbstract(getReifiedClass().getModifiers())) {
00136 throw new ConstructorCallExecutionFailedException(
00137 "Cannot build an instance of an abstract class: " + e);
00138 } else {
00139 throw new ConstructorCallExecutionFailedException(
00140 "Instanciation problem: " + e +
00141 ". Strange enough, the reified class is neither abstract nor an interface.");
00142 }
00143 } catch (ExceptionInInitializerError e) {
00144 throw new ConstructorCallExecutionFailedException(
00145 "Cannot build object because the initialization of its class failed: " +
00146 e);
00147 }
00148 }
00149
00150
00151
00152
00153
00158 protected Class getReifiedClass() {
00159 return reifiedConstructor.getDeclaringClass();
00160 }
00161
00162
00163
00164
00165 private void writeObject(java.io.ObjectOutputStream out)
00166 throws IOException {
00167
00168
00169 out.writeObject(this.effectiveArguments);
00170
00171 Class declaringClass;
00172 Class[] parameters;
00173
00174 declaringClass = this.reifiedConstructor.getDeclaringClass();
00175 out.writeObject(declaringClass);
00176
00177 parameters = this.reifiedConstructor.getParameterTypes();
00178 out.writeObject(parameters);
00179 }
00180
00181 private void readObject(java.io.ObjectInputStream in)
00182 throws IOException, ClassNotFoundException {
00183 Class declaringClass = null;
00184 Class[] parameters;
00185 try {
00186 this.effectiveArguments = (Object[]) in.readObject();
00187 } catch (IOException e) {
00188
00189
00190 throw e;
00191 }
00192
00193 declaringClass = (Class) in.readObject();
00194 parameters = (Class[]) in.readObject();
00195
00196 try {
00197 this.reifiedConstructor = declaringClass.getConstructor(parameters);
00198 } catch (NoSuchMethodException e) {
00199 throw new InternalException("Lookup for constructor failed: " + e +
00200 ". This may be caused by having different versions of the same class on different VMs. Check your CLASSPATH settings.");
00201 }
00202 }
00203 }