1 31 package org.objectweb.proactive.core.mop; 32 33 import java.io.IOException ; 34 import java.io.Serializable ; 35 36 import java.lang.reflect.Constructor ; 37 import java.lang.reflect.InvocationTargetException ; 38 import java.lang.reflect.Modifier ; 39 40 41 44 public class ConstructorCallImpl implements ConstructorCall, Serializable { 45 46 49 public Object [] effectiveArguments; 50 51 54 public Constructor reifiedConstructor; 55 56 60 65 public ConstructorCallImpl(Constructor reifiedConstructor, 66 Object [] effectiveArguments) { 67 this.reifiedConstructor = reifiedConstructor; 68 this.effectiveArguments = effectiveArguments; 69 } 70 71 public String toString() { 75 StringBuffer sb = new StringBuffer (); 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 101 104 public void makeDeepCopyOfArguments() throws java.io.IOException { 105 effectiveArguments = (Object []) Utils.makeDeepCopy(effectiveArguments); 106 } 107 108 111 public String getTargetClassName() { 112 return getReifiedClass().getName(); 113 } 114 115 120 public Object execute() 121 throws InvocationTargetException , 122 ConstructorCallExecutionFailedException { 123 try { 125 return reifiedConstructor.newInstance(effectiveArguments); 126 } catch (IllegalAccessException e) { 127 throw new ConstructorCallExecutionFailedException( 128 "Access rights to the constructor denied: " + e); 129 } catch (IllegalArgumentException e) { 130 throw new ConstructorCallExecutionFailedException( 131 "Illegal constructor arguments: " + e); 132 } catch (InstantiationException 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 e) { 145 throw new ConstructorCallExecutionFailedException( 146 "Cannot build object because the initialization of its class failed: " + 147 e); 148 } 149 } 150 151 155 159 protected Class getReifiedClass() { 160 return reifiedConstructor.getDeclaringClass(); 161 } 162 163 private void writeObject(java.io.ObjectOutputStream out) 167 throws IOException { 168 out.writeObject(this.effectiveArguments); 171 Class declaringClass; 173 Class [] 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 in) 183 throws IOException , ClassNotFoundException { 184 Class declaringClass = null; 185 Class [] parameters; 186 try { 187 this.effectiveArguments = (Object []) in.readObject(); 188 } catch (IOException e) { 189 throw e; 192 } 193 declaringClass = (Class ) in.readObject(); 196 parameters = (Class []) in.readObject(); 197 198 try { 199 this.reifiedConstructor = declaringClass.getConstructor(parameters); 200 } catch (NoSuchMethodException 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 |