|
66 | 66 | import org.springframework.jndi.support.SimpleJndiBeanFactory;
|
67 | 67 | import org.springframework.util.Assert;
|
68 | 68 | import org.springframework.util.ClassUtils;
|
| 69 | +import org.springframework.util.ReflectionUtils; |
69 | 70 | import org.springframework.util.StringUtils;
|
70 | 71 |
|
71 | 72 | /**
|
@@ -339,75 +340,85 @@ private InjectionMetadata findResourceMetadata(String beanName, final Class<?> c
|
339 | 340 | return metadata;
|
340 | 341 | }
|
341 | 342 |
|
342 |
| - private InjectionMetadata buildResourceMetadata(Class<?> clazz) { |
| 343 | + private InjectionMetadata buildResourceMetadata(final Class<?> clazz) { |
343 | 344 | LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<InjectionMetadata.InjectedElement>();
|
344 | 345 | Class<?> targetClass = clazz;
|
345 | 346 |
|
346 | 347 | do {
|
347 |
| - LinkedList<InjectionMetadata.InjectedElement> currElements = new LinkedList<InjectionMetadata.InjectedElement>(); |
348 |
| - for (Field field : targetClass.getDeclaredFields()) { |
349 |
| - if (webServiceRefClass != null && field.isAnnotationPresent(webServiceRefClass)) { |
350 |
| - if (Modifier.isStatic(field.getModifiers())) { |
351 |
| - throw new IllegalStateException("@WebServiceRef annotation is not supported on static fields"); |
352 |
| - } |
353 |
| - currElements.add(new WebServiceRefElement(field, field, null)); |
354 |
| - } |
355 |
| - else if (ejbRefClass != null && field.isAnnotationPresent(ejbRefClass)) { |
356 |
| - if (Modifier.isStatic(field.getModifiers())) { |
357 |
| - throw new IllegalStateException("@EJB annotation is not supported on static fields"); |
358 |
| - } |
359 |
| - currElements.add(new EjbRefElement(field, field, null)); |
360 |
| - } |
361 |
| - else if (field.isAnnotationPresent(Resource.class)) { |
362 |
| - if (Modifier.isStatic(field.getModifiers())) { |
363 |
| - throw new IllegalStateException("@Resource annotation is not supported on static fields"); |
364 |
| - } |
365 |
| - if (!ignoredResourceTypes.contains(field.getType().getName())) { |
366 |
| - currElements.add(new ResourceElement(field, field, null)); |
367 |
| - } |
368 |
| - } |
369 |
| - } |
370 |
| - for (Method method : targetClass.getDeclaredMethods()) { |
371 |
| - Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method); |
372 |
| - if (!BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod)) { |
373 |
| - continue; |
374 |
| - } |
375 |
| - if (method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) { |
376 |
| - if (webServiceRefClass != null && bridgedMethod.isAnnotationPresent(webServiceRefClass)) { |
377 |
| - if (Modifier.isStatic(method.getModifiers())) { |
378 |
| - throw new IllegalStateException("@WebServiceRef annotation is not supported on static methods"); |
| 348 | + final LinkedList<InjectionMetadata.InjectedElement> currElements = |
| 349 | + new LinkedList<InjectionMetadata.InjectedElement>(); |
| 350 | + |
| 351 | + ReflectionUtils.doWithLocalFields(targetClass, new ReflectionUtils.FieldCallback() { |
| 352 | + @Override |
| 353 | + public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { |
| 354 | + if (webServiceRefClass != null && field.isAnnotationPresent(webServiceRefClass)) { |
| 355 | + if (Modifier.isStatic(field.getModifiers())) { |
| 356 | + throw new IllegalStateException("@WebServiceRef annotation is not supported on static fields"); |
379 | 357 | }
|
380 |
| - if (method.getParameterTypes().length != 1) { |
381 |
| - throw new IllegalStateException("@WebServiceRef annotation requires a single-arg method: " + method); |
| 358 | + currElements.add(new WebServiceRefElement(field, field, null)); |
| 359 | + } |
| 360 | + else if (ejbRefClass != null && field.isAnnotationPresent(ejbRefClass)) { |
| 361 | + if (Modifier.isStatic(field.getModifiers())) { |
| 362 | + throw new IllegalStateException("@EJB annotation is not supported on static fields"); |
382 | 363 | }
|
383 |
| - PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz); |
384 |
| - currElements.add(new WebServiceRefElement(method, bridgedMethod, pd)); |
| 364 | + currElements.add(new EjbRefElement(field, field, null)); |
385 | 365 | }
|
386 |
| - else if (ejbRefClass != null && bridgedMethod.isAnnotationPresent(ejbRefClass)) { |
387 |
| - if (Modifier.isStatic(method.getModifiers())) { |
388 |
| - throw new IllegalStateException("@EJB annotation is not supported on static methods"); |
| 366 | + else if (field.isAnnotationPresent(Resource.class)) { |
| 367 | + if (Modifier.isStatic(field.getModifiers())) { |
| 368 | + throw new IllegalStateException("@Resource annotation is not supported on static fields"); |
389 | 369 | }
|
390 |
| - if (method.getParameterTypes().length != 1) { |
391 |
| - throw new IllegalStateException("@EJB annotation requires a single-arg method: " + method); |
| 370 | + if (!ignoredResourceTypes.contains(field.getType().getName())) { |
| 371 | + currElements.add(new ResourceElement(field, field, null)); |
392 | 372 | }
|
393 |
| - PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz); |
394 |
| - currElements.add(new EjbRefElement(method, bridgedMethod, pd)); |
395 | 373 | }
|
396 |
| - else if (bridgedMethod.isAnnotationPresent(Resource.class)) { |
397 |
| - if (Modifier.isStatic(method.getModifiers())) { |
398 |
| - throw new IllegalStateException("@Resource annotation is not supported on static methods"); |
399 |
| - } |
400 |
| - Class<?>[] paramTypes = method.getParameterTypes(); |
401 |
| - if (paramTypes.length != 1) { |
402 |
| - throw new IllegalStateException("@Resource annotation requires a single-arg method: " + method); |
| 374 | + } |
| 375 | + }); |
| 376 | + |
| 377 | + ReflectionUtils.doWithLocalMethods(targetClass, new ReflectionUtils.MethodCallback() { |
| 378 | + @Override |
| 379 | + public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { |
| 380 | + Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method); |
| 381 | + if (!BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod)) { |
| 382 | + return; |
| 383 | + } |
| 384 | + if (method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) { |
| 385 | + if (webServiceRefClass != null && bridgedMethod.isAnnotationPresent(webServiceRefClass)) { |
| 386 | + if (Modifier.isStatic(method.getModifiers())) { |
| 387 | + throw new IllegalStateException("@WebServiceRef annotation is not supported on static methods"); |
| 388 | + } |
| 389 | + if (method.getParameterTypes().length != 1) { |
| 390 | + throw new IllegalStateException("@WebServiceRef annotation requires a single-arg method: " + method); |
| 391 | + } |
| 392 | + PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz); |
| 393 | + currElements.add(new WebServiceRefElement(method, bridgedMethod, pd)); |
403 | 394 | }
|
404 |
| - if (!ignoredResourceTypes.contains(paramTypes[0].getName())) { |
| 395 | + else if (ejbRefClass != null && bridgedMethod.isAnnotationPresent(ejbRefClass)) { |
| 396 | + if (Modifier.isStatic(method.getModifiers())) { |
| 397 | + throw new IllegalStateException("@EJB annotation is not supported on static methods"); |
| 398 | + } |
| 399 | + if (method.getParameterTypes().length != 1) { |
| 400 | + throw new IllegalStateException("@EJB annotation requires a single-arg method: " + method); |
| 401 | + } |
405 | 402 | PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz);
|
406 |
| - currElements.add(new ResourceElement(method, bridgedMethod, pd)); |
| 403 | + currElements.add(new EjbRefElement(method, bridgedMethod, pd)); |
| 404 | + } |
| 405 | + else if (bridgedMethod.isAnnotationPresent(Resource.class)) { |
| 406 | + if (Modifier.isStatic(method.getModifiers())) { |
| 407 | + throw new IllegalStateException("@Resource annotation is not supported on static methods"); |
| 408 | + } |
| 409 | + Class<?>[] paramTypes = method.getParameterTypes(); |
| 410 | + if (paramTypes.length != 1) { |
| 411 | + throw new IllegalStateException("@Resource annotation requires a single-arg method: " + method); |
| 412 | + } |
| 413 | + if (!ignoredResourceTypes.contains(paramTypes[0].getName())) { |
| 414 | + PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz); |
| 415 | + currElements.add(new ResourceElement(method, bridgedMethod, pd)); |
| 416 | + } |
407 | 417 | }
|
408 | 418 | }
|
409 | 419 | }
|
410 |
| - } |
| 420 | + }); |
| 421 | + |
411 | 422 | elements.addAll(0, currElements);
|
412 | 423 | targetClass = targetClass.getSuperclass();
|
413 | 424 | }
|
|
0 commit comments