117 * @throws IllegalArgumentException if implClass implements illegal |
117 * @throws IllegalArgumentException if implClass implements illegal |
118 * remote interfaces |
118 * remote interfaces |
119 * @throws StubNotFoundException if problem locating/creating stub or |
119 * @throws StubNotFoundException if problem locating/creating stub or |
120 * creating the dynamic proxy instance |
120 * creating the dynamic proxy instance |
121 **/ |
121 **/ |
122 public static Remote createProxy(Class implClass, |
122 public static Remote createProxy(Class<?> implClass, |
123 RemoteRef clientRef, |
123 RemoteRef clientRef, |
124 boolean forceStubUse) |
124 boolean forceStubUse) |
125 throws StubNotFoundException |
125 throws StubNotFoundException |
126 { |
126 { |
127 Class remoteClass; |
127 Class<?> remoteClass; |
128 |
128 |
129 try { |
129 try { |
130 remoteClass = getRemoteClass(implClass); |
130 remoteClass = getRemoteClass(implClass); |
131 } catch (ClassNotFoundException ex ) { |
131 } catch (ClassNotFoundException ex ) { |
132 throw new StubNotFoundException( |
132 throw new StubNotFoundException( |
160 * Returns true if a stub class for the given impl class can be loaded, |
160 * Returns true if a stub class for the given impl class can be loaded, |
161 * otherwise returns false. |
161 * otherwise returns false. |
162 * |
162 * |
163 * @param remoteClass the class to obtain remote interfaces from |
163 * @param remoteClass the class to obtain remote interfaces from |
164 */ |
164 */ |
165 private static boolean stubClassExists(Class remoteClass) { |
165 private static boolean stubClassExists(Class<?> remoteClass) { |
166 if (!withoutStubs.containsKey(remoteClass)) { |
166 if (!withoutStubs.containsKey(remoteClass)) { |
167 try { |
167 try { |
168 Class.forName(remoteClass.getName() + "_Stub", |
168 Class.forName(remoteClass.getName() + "_Stub", |
169 false, |
169 false, |
170 remoteClass.getClassLoader()); |
170 remoteClass.getClassLoader()); |
180 /* |
180 /* |
181 * Returns the class/superclass that implements the remote interface. |
181 * Returns the class/superclass that implements the remote interface. |
182 * @throws ClassNotFoundException if no class is found to have a |
182 * @throws ClassNotFoundException if no class is found to have a |
183 * remote interface |
183 * remote interface |
184 */ |
184 */ |
185 private static Class getRemoteClass(Class cl) |
185 private static Class<?> getRemoteClass(Class<?> cl) |
186 throws ClassNotFoundException |
186 throws ClassNotFoundException |
187 { |
187 { |
188 while (cl != null) { |
188 while (cl != null) { |
189 Class[] interfaces = cl.getInterfaces(); |
189 Class<?>[] interfaces = cl.getInterfaces(); |
190 for (int i = interfaces.length -1; i >= 0; i--) { |
190 for (int i = interfaces.length -1; i >= 0; i--) { |
191 if (Remote.class.isAssignableFrom(interfaces[i])) |
191 if (Remote.class.isAssignableFrom(interfaces[i])) |
192 return cl; // this class implements remote object |
192 return cl; // this class implements remote object |
193 } |
193 } |
194 cl = cl.getSuperclass(); |
194 cl = cl.getSuperclass(); |
204 * @param remoteClass the class to obtain remote interfaces from |
204 * @param remoteClass the class to obtain remote interfaces from |
205 * @throws IllegalArgumentException if remoteClass implements |
205 * @throws IllegalArgumentException if remoteClass implements |
206 * any illegal remote interfaces |
206 * any illegal remote interfaces |
207 * @throws NullPointerException if remoteClass is null |
207 * @throws NullPointerException if remoteClass is null |
208 */ |
208 */ |
209 private static Class[] getRemoteInterfaces(Class remoteClass) { |
209 private static Class<?>[] getRemoteInterfaces(Class<?> remoteClass) { |
210 ArrayList<Class<?>> list = new ArrayList<Class<?>>(); |
210 ArrayList<Class<?>> list = new ArrayList<>(); |
211 getRemoteInterfaces(list, remoteClass); |
211 getRemoteInterfaces(list, remoteClass); |
212 return list.toArray(new Class<?>[list.size()]); |
212 return list.toArray(new Class<?>[list.size()]); |
213 } |
213 } |
214 |
214 |
215 /** |
215 /** |
218 * |
218 * |
219 * @throws IllegalArgumentException if the specified class implements |
219 * @throws IllegalArgumentException if the specified class implements |
220 * any illegal remote interfaces |
220 * any illegal remote interfaces |
221 * @throws NullPointerException if the specified class or list is null |
221 * @throws NullPointerException if the specified class or list is null |
222 */ |
222 */ |
223 private static void getRemoteInterfaces(ArrayList<Class<?>> list, Class cl) { |
223 private static void getRemoteInterfaces(ArrayList<Class<?>> list, Class<?> cl) { |
224 Class superclass = cl.getSuperclass(); |
224 Class<?> superclass = cl.getSuperclass(); |
225 if (superclass != null) { |
225 if (superclass != null) { |
226 getRemoteInterfaces(list, superclass); |
226 getRemoteInterfaces(list, superclass); |
227 } |
227 } |
228 |
228 |
229 Class[] interfaces = cl.getInterfaces(); |
229 Class<?>[] interfaces = cl.getInterfaces(); |
230 for (int i = 0; i < interfaces.length; i++) { |
230 for (int i = 0; i < interfaces.length; i++) { |
231 Class intf = interfaces[i]; |
231 Class<?> intf = interfaces[i]; |
232 /* |
232 /* |
233 * If it is a remote interface (if it extends from |
233 * If it is a remote interface (if it extends from |
234 * java.rmi.Remote) and is not already in the list, |
234 * java.rmi.Remote) and is not already in the list, |
235 * then add the interface to the list. |
235 * then add the interface to the list. |
236 */ |
236 */ |
270 * implements a remote interface. The stub class name is the name of |
270 * implements a remote interface. The stub class name is the name of |
271 * the specified remoteClass with the suffix "_Stub". The loading of |
271 * the specified remoteClass with the suffix "_Stub". The loading of |
272 * the stub class is initiated from class loader of the specified class |
272 * the stub class is initiated from class loader of the specified class |
273 * (which may be the bootstrap class loader). |
273 * (which may be the bootstrap class loader). |
274 **/ |
274 **/ |
275 private static RemoteStub createStub(Class remoteClass, RemoteRef ref) |
275 private static RemoteStub createStub(Class<?> remoteClass, RemoteRef ref) |
276 throws StubNotFoundException |
276 throws StubNotFoundException |
277 { |
277 { |
278 String stubname = remoteClass.getName() + "_Stub"; |
278 String stubname = remoteClass.getName() + "_Stub"; |
279 |
279 |
280 /* Make sure to use the local stub loader for the stub classes. |
280 /* Make sure to use the local stub loader for the stub classes. |
283 * pickle methods |
283 * pickle methods |
284 */ |
284 */ |
285 try { |
285 try { |
286 Class<?> stubcl = |
286 Class<?> stubcl = |
287 Class.forName(stubname, false, remoteClass.getClassLoader()); |
287 Class.forName(stubname, false, remoteClass.getClassLoader()); |
288 Constructor cons = stubcl.getConstructor(stubConsParamTypes); |
288 Constructor<?> cons = stubcl.getConstructor(stubConsParamTypes); |
289 return (RemoteStub) cons.newInstance(new Object[] { ref }); |
289 return (RemoteStub) cons.newInstance(new Object[] { ref }); |
290 |
290 |
291 } catch (ClassNotFoundException e) { |
291 } catch (ClassNotFoundException e) { |
292 throw new StubNotFoundException( |
292 throw new StubNotFoundException( |
293 "Stub class not found: " + stubname, e); |
293 "Stub class not found: " + stubname, e); |
313 * Locate and return the Skeleton for the specified remote object |
313 * Locate and return the Skeleton for the specified remote object |
314 */ |
314 */ |
315 static Skeleton createSkeleton(Remote object) |
315 static Skeleton createSkeleton(Remote object) |
316 throws SkeletonNotFoundException |
316 throws SkeletonNotFoundException |
317 { |
317 { |
318 Class cl; |
318 Class<?> cl; |
319 try { |
319 try { |
320 cl = getRemoteClass(object.getClass()); |
320 cl = getRemoteClass(object.getClass()); |
321 } catch (ClassNotFoundException ex ) { |
321 } catch (ClassNotFoundException ex ) { |
322 throw new SkeletonNotFoundException( |
322 throw new SkeletonNotFoundException( |
323 "object does not implement a remote interface: " + |
323 "object does not implement a remote interface: " + |
325 } |
325 } |
326 |
326 |
327 // now try to load the skeleton based ont he name of the class |
327 // now try to load the skeleton based ont he name of the class |
328 String skelname = cl.getName() + "_Skel"; |
328 String skelname = cl.getName() + "_Skel"; |
329 try { |
329 try { |
330 Class skelcl = Class.forName(skelname, false, cl.getClassLoader()); |
330 Class<?> skelcl = Class.forName(skelname, false, cl.getClassLoader()); |
331 |
331 |
332 return (Skeleton)skelcl.newInstance(); |
332 return (Skeleton)skelcl.newInstance(); |
333 } catch (ClassNotFoundException ex) { |
333 } catch (ClassNotFoundException ex) { |
334 throw new SkeletonNotFoundException("Skeleton class not found: " + |
334 throw new SkeletonNotFoundException("Skeleton class not found: " + |
335 skelname, ex); |
335 skelname, ex); |
389 * the definition of a "method descriptor". |
389 * the definition of a "method descriptor". |
390 */ |
390 */ |
391 private static String getMethodNameAndDescriptor(Method m) { |
391 private static String getMethodNameAndDescriptor(Method m) { |
392 StringBuffer desc = new StringBuffer(m.getName()); |
392 StringBuffer desc = new StringBuffer(m.getName()); |
393 desc.append('('); |
393 desc.append('('); |
394 Class[] paramTypes = m.getParameterTypes(); |
394 Class<?>[] paramTypes = m.getParameterTypes(); |
395 for (int i = 0; i < paramTypes.length; i++) { |
395 for (int i = 0; i < paramTypes.length; i++) { |
396 desc.append(getTypeDescriptor(paramTypes[i])); |
396 desc.append(getTypeDescriptor(paramTypes[i])); |
397 } |
397 } |
398 desc.append(')'); |
398 desc.append(')'); |
399 Class returnType = m.getReturnType(); |
399 Class<?> returnType = m.getReturnType(); |
400 if (returnType == void.class) { // optimization: handle void here |
400 if (returnType == void.class) { // optimization: handle void here |
401 desc.append('V'); |
401 desc.append('V'); |
402 } else { |
402 } else { |
403 desc.append(getTypeDescriptor(returnType)); |
403 desc.append(getTypeDescriptor(returnType)); |
404 } |
404 } |
407 |
407 |
408 /** |
408 /** |
409 * Get the descriptor of a particular type, as appropriate for either |
409 * Get the descriptor of a particular type, as appropriate for either |
410 * a parameter or return type in a method descriptor. |
410 * a parameter or return type in a method descriptor. |
411 */ |
411 */ |
412 private static String getTypeDescriptor(Class type) { |
412 private static String getTypeDescriptor(Class<?> type) { |
413 if (type.isPrimitive()) { |
413 if (type.isPrimitive()) { |
414 if (type == int.class) { |
414 if (type == int.class) { |
415 return "I"; |
415 return "I"; |
416 } else if (type == boolean.class) { |
416 } else if (type == boolean.class) { |
417 return "Z"; |
417 return "Z"; |
452 * top-level types, so for a nested type, the returned name will |
452 * top-level types, so for a nested type, the returned name will |
453 * still be qualified with the simple name of its enclosing |
453 * still be qualified with the simple name of its enclosing |
454 * top-level type (and perhaps other enclosing types), the |
454 * top-level type (and perhaps other enclosing types), the |
455 * separator will be '$', etc. |
455 * separator will be '$', etc. |
456 **/ |
456 **/ |
457 public static String getUnqualifiedName(Class c) { |
457 public static String getUnqualifiedName(Class<?> c) { |
458 String binaryName = c.getName(); |
458 String binaryName = c.getName(); |
459 return binaryName.substring(binaryName.lastIndexOf('.') + 1); |
459 return binaryName.substring(binaryName.lastIndexOf('.') + 1); |
460 } |
460 } |
461 } |
461 } |