@@ -412,6 +412,63 @@ public static void findSupertypes(ITypeBinding binding, Set<String> supertypesCo
412
412
413
413
public static InjectionPoint [] findInjectionPoints (MethodDeclaration method , TextDocument doc ) throws BadLocationException {
414
414
List <InjectionPoint > result = new ArrayList <>();
415
+ findInjectionPoints (method , doc , result , false );
416
+
417
+ return result .size () > 0 ? result .toArray (new InjectionPoint [result .size ()]) : DefaultValues .EMPTY_INJECTION_POINTS ;
418
+ }
419
+
420
+ public static InjectionPoint [] findInjectionPoints (TypeDeclaration type , TextDocument doc ) throws BadLocationException {
421
+ List <InjectionPoint > result = new ArrayList <>();
422
+
423
+ findInjectionPoints (type .getMethods (), doc , result );
424
+ findInjectionPoints (type .getFields (), doc , result );
425
+
426
+ return result .size () > 0 ? result .toArray (new InjectionPoint [result .size ()]) : DefaultValues .EMPTY_INJECTION_POINTS ;
427
+ }
428
+
429
+ private static void findInjectionPoints (MethodDeclaration [] methods , TextDocument doc , List <InjectionPoint > result ) throws BadLocationException {
430
+ int constructorCount = 0 ;
431
+
432
+ // special rule that if there is a single constructor, it doesn't have to have an autowired or inject annotation on it
433
+ MethodDeclaration singleConstructor = null ;
434
+
435
+ for (MethodDeclaration method : methods ) {
436
+ if (method .isConstructor ()) {
437
+ constructorCount ++;
438
+ singleConstructor = method ;
439
+ }
440
+ }
441
+
442
+ if (constructorCount == 1 ) {
443
+ findInjectionPoints (singleConstructor , doc , result , false );
444
+ }
445
+
446
+ // look for all methods with annotations (whether constructors or regular methods)
447
+ for (MethodDeclaration method : methods ) {
448
+ findInjectionPoints (method , doc , result , true );
449
+ }
450
+ }
451
+
452
+ public static void findInjectionPoints (MethodDeclaration method , TextDocument doc , List <InjectionPoint > result , boolean checkForAnnotation ) throws BadLocationException {
453
+
454
+ Collection <Annotation > annotationsOnMethod = getAnnotations (method );
455
+
456
+ if (checkForAnnotation ) {
457
+ boolean isAutowired = false ;
458
+
459
+ for (Annotation annotation : annotationsOnMethod ) {
460
+ String qualifiedName = annotation .resolveTypeBinding ().getQualifiedName ();
461
+ if (Annotations .AUTOWIRED .equals (qualifiedName )
462
+ || Annotations .INJECT_JAVAX .equals (qualifiedName )
463
+ || Annotations .INJECT_JAKARTA .equals (qualifiedName )) {
464
+ isAutowired = true ;
465
+ }
466
+ }
467
+
468
+ if (!isAutowired ) {
469
+ return ;
470
+ }
471
+ }
415
472
416
473
List <?> parameters = method .parameters ();
417
474
for (Object object : parameters ) {
@@ -427,70 +484,65 @@ public static InjectionPoint[] findInjectionPoints(MethodDeclaration method, Tex
427
484
428
485
Location location = new Location (doc .getUri (), range );
429
486
430
- AnnotationMetadata [] annotations = getAnnotationsMetadata (getAnnotations (variable ), doc );
487
+ List <Annotation > allAnnotations = new ArrayList <>();
488
+ allAnnotations .addAll (annotationsOnMethod );
489
+ allAnnotations .addAll (getAnnotations (variable ));
490
+
491
+ AnnotationMetadata [] annotations = getAnnotationsMetadata (allAnnotations , doc );
431
492
432
493
result .add (new InjectionPoint (name , type , location , annotations ));
433
494
}
434
495
}
435
-
436
- return result .size () > 0 ? result .toArray (new InjectionPoint [result .size ()]) : DefaultValues .EMPTY_INJECTION_POINTS ;
437
496
}
438
-
439
- public static InjectionPoint [] findInjectionPoints (TypeDeclaration type , TextDocument doc ) throws BadLocationException {
440
- List <InjectionPoint > result = new ArrayList <>();
441
497
442
- MethodDeclaration [] methods = type .getMethods ();
443
- for (MethodDeclaration method : methods ) {
444
- if (method .isConstructor ()) {
445
- result .addAll (Arrays .asList (ASTUtils .findInjectionPoints (method , doc )));
446
- }
498
+ private static void findInjectionPoints (FieldDeclaration [] fields , TextDocument doc , List <InjectionPoint > result ) throws BadLocationException {
499
+ for (FieldDeclaration field : fields ) {
500
+ findInjectionPoints (field , doc , result );
447
501
}
502
+ }
448
503
449
- FieldDeclaration [] fields = type .getFields ();
450
- for (FieldDeclaration field : fields ) {
504
+ private static void findInjectionPoints (FieldDeclaration field , TextDocument doc , List <InjectionPoint > result ) throws BadLocationException {
505
+ boolean autowiredField = false ;
506
+
507
+ List <Annotation > fieldAnnotations = new ArrayList <>();
451
508
452
- boolean autowiredField = false ;
453
-
454
- List <Annotation > fieldAnnotations = new ArrayList <>();
455
-
456
- List <?> modifiers = field .modifiers ();
457
- for (Object modifier : modifiers ) {
458
- if (modifier instanceof Annotation ) {
459
- Annotation annotation = (Annotation ) modifier ;
460
- fieldAnnotations .add (annotation );
461
-
462
- String qualifiedName = annotation .resolveTypeBinding ().getQualifiedName ();
463
- if (Annotations .AUTOWIRED .equals (qualifiedName )
464
- || Annotations .INJECT_JAVAX .equals (qualifiedName )
465
- || Annotations .INJECT_JAKARTA .equals (qualifiedName )
466
- || Annotations .VALUE .equals (qualifiedName )) {
467
- autowiredField = true ;
468
- }
509
+ List <?> modifiers = field .modifiers ();
510
+ for (Object modifier : modifiers ) {
511
+ if (modifier instanceof Annotation ) {
512
+ Annotation annotation = (Annotation ) modifier ;
513
+ fieldAnnotations .add (annotation );
514
+
515
+ String qualifiedName = annotation .resolveTypeBinding ().getQualifiedName ();
516
+ if (Annotations .AUTOWIRED .equals (qualifiedName )
517
+ || Annotations .INJECT_JAVAX .equals (qualifiedName )
518
+ || Annotations .INJECT_JAKARTA .equals (qualifiedName )
519
+ || Annotations .VALUE .equals (qualifiedName )) {
520
+ autowiredField = true ;
469
521
}
470
522
}
523
+ }
471
524
472
- if (autowiredField ) {
473
- List <?> fragments = field .fragments ();
474
- for (Object fragment : fragments ) {
475
- if (fragment instanceof VariableDeclarationFragment ) {
476
- VariableDeclarationFragment varFragment = (VariableDeclarationFragment ) fragment ;
477
- String fieldName = varFragment .getName ().toString ();
525
+ if (!autowiredField ) {
526
+ return ;
527
+ }
528
+
529
+ List <?> fragments = field .fragments ();
530
+ for (Object fragment : fragments ) {
531
+ if (fragment instanceof VariableDeclarationFragment ) {
532
+ VariableDeclarationFragment varFragment = (VariableDeclarationFragment ) fragment ;
533
+ String fieldName = varFragment .getName ().toString ();
478
534
479
- DocumentRegion region = ASTUtils .nodeRegion (doc , varFragment .getName ());
480
- Range range = doc .toRange (region );
481
- Location fieldLocation = new Location (doc .getUri (), range );
535
+ DocumentRegion region = ASTUtils .nodeRegion (doc , varFragment .getName ());
536
+ Range range = doc .toRange (region );
537
+ Location fieldLocation = new Location (doc .getUri (), range );
482
538
483
- String fieldType = field .getType ().resolveBinding ().getQualifiedName ();
539
+ String fieldType = field .getType ().resolveBinding ().getQualifiedName ();
484
540
485
- AnnotationMetadata [] annotationsMetadata = getAnnotationsMetadata (fieldAnnotations , doc );
486
-
487
- result .add (new InjectionPoint (fieldName , fieldType , fieldLocation , annotationsMetadata ));
488
- }
489
- }
541
+ AnnotationMetadata [] annotationsMetadata = getAnnotationsMetadata (fieldAnnotations , doc );
542
+
543
+ result .add (new InjectionPoint (fieldName , fieldType , fieldLocation , annotationsMetadata ));
490
544
}
491
545
}
492
-
493
- return result .size () > 0 ? result .toArray (new InjectionPoint [result .size ()]) : DefaultValues .EMPTY_INJECTION_POINTS ;
494
546
}
495
547
496
548
public static AnnotationMetadata [] getAnnotationsMetadata (Collection <Annotation > annotations , TextDocument doc ) {
0 commit comments