53
53
import com .sun .source .tree .UnaryTree ;
54
54
import com .sun .source .tree .VariableTree ;
55
55
import com .sun .source .tree .WhileLoopTree ;
56
+ import com .sun .source .tree .YieldTree ;
56
57
import com .sun .source .util .TreePath ;
57
58
import java .io .IOException ;
58
59
import java .util .ArrayList ;
@@ -133,7 +134,11 @@ public static List<? extends TypeMirror> resolveType(Set<ElementKind> types, Com
133
134
return Collections .singletonList (info .getTypes ().getNoType (TypeKind .VOID ));
134
135
135
136
case RETURN :
136
- return computeReturn (types , info , currentPath , unresolved , offset );
137
+ return computeReturn (types , info , currentPath , unresolved );
138
+ case YIELD :
139
+ return computeYield (types , info , currentPath , unresolved , offset );
140
+ case CASE :
141
+ return computeCase (types , info , currentPath , unresolved , offset );
137
142
case TYPE_PARAMETER :
138
143
return computeTypeParameter (types , info , currentPath , unresolved , offset );
139
144
case PARAMETERIZED_TYPE :
@@ -208,7 +213,7 @@ public static List<? extends TypeMirror> resolveType(Set<ElementKind> types, Com
208
213
return computeImport (types , info , currentPath , unresolved , offset );
209
214
210
215
case LAMBDA_EXPRESSION :
211
- return computeLambdaRetrun (types , info , currentPath , unresolved , offset );
216
+ return computeLambdaReturn (types , info , currentPath , unresolved , offset );
212
217
213
218
case BLOCK :
214
219
case BREAK :
@@ -234,8 +239,7 @@ public static List<? extends TypeMirror> resolveType(Set<ElementKind> types, Com
234
239
case NULL_LITERAL :
235
240
//ignored:
236
241
return null ;
237
-
238
- case CASE :
242
+
239
243
case ANNOTATION :
240
244
case UNBOUNDED_WILDCARD :
241
245
case EXTENDS_WILDCARD :
@@ -401,6 +405,13 @@ private static List<? extends TypeMirror> computeAssignment(Set<ElementKind> typ
401
405
types .clear ();
402
406
types .add (ElementKind .RESOURCE_VARIABLE );
403
407
}
408
+ if (parent .getParentPath () != null &&
409
+ parent .getParentPath ().getLeaf ().getKind () == Kind .EXPRESSION_STATEMENT &&
410
+ parent .getParentPath ().getParentPath () != null &&
411
+ parent .getParentPath ().getParentPath ().getLeaf ().getKind () == Kind .FOR_LOOP &&
412
+ ((ForLoopTree ) parent .getParentPath ().getParentPath ().getLeaf ()).getInitializer ().contains (parent .getParentPath ().getLeaf ())) {
413
+ types .add (ElementKind .OTHER );
414
+ }
404
415
}
405
416
406
417
if (at .getExpression () == error ) {
@@ -545,17 +556,32 @@ private static List<? extends TypeMirror> computeAssert(Set<ElementKind> types,
545
556
return null ;
546
557
}
547
558
548
- private static List <? extends TypeMirror > computeLambdaRetrun (Set <ElementKind > types , CompilationInfo info , TreePath parent , Tree error , int offset ) {
559
+ private static List <? extends TypeMirror > computeLambdaReturn (Set <ElementKind > types , CompilationInfo info , TreePath parent , Tree error , int offset ) {
549
560
LambdaExpressionTree let = (LambdaExpressionTree )parent .getLeaf ();
550
561
if (let .getBody () != error ) {
551
562
return null ;
552
563
}
553
- TreePath parentParent = parent .getParentPath ();
554
- TypeMirror m = info .getTrees ().getTypeMirror (parentParent );
555
- if (org .netbeans .modules .java .hints .errors .Utilities .isValidType (m )) {
556
- return Collections .singletonList (m );
564
+
565
+ List <TypeMirror > result = new ArrayList <>();
566
+ for (TypeMirror target : resolveType (types , info , parent .getParentPath (), let , offset , null , null )) {
567
+ if (!org .netbeans .modules .java .hints .errors .Utilities .isValidType (target ) ||
568
+ target .getKind () != TypeKind .DECLARED ) {
569
+ continue ;
570
+ }
571
+
572
+ DeclaredType declaredTarget = (DeclaredType ) target ;
573
+ ExecutableElement functionalMethod =
574
+ info .getElementUtilities ()
575
+ .getDescriptorElement ((TypeElement ) (declaredTarget ).asElement ());
576
+
577
+ if (functionalMethod == null ) {
578
+ continue ;
579
+ }
580
+
581
+ result .add (((ExecutableType ) info .getTypes ().asMemberOf (declaredTarget , functionalMethod )).getReturnType ());
557
582
}
558
- return resolveType (types , info , parentParent , let , offset , null , null );
583
+
584
+ return result ;
559
585
}
560
586
561
587
private static List <? extends TypeMirror > computeParenthesis (Set <ElementKind > types , CompilationInfo info , TreePath parent , Tree error , int offset ) {
@@ -672,7 +698,7 @@ private static List<? extends TypeMirror> computeUnary(Set<ElementKind> types, C
672
698
return null ;
673
699
}
674
700
675
- private static List <? extends TypeMirror > computeReturn (Set <ElementKind > types , CompilationInfo info , TreePath parent , Tree error , int offset ) {
701
+ private static List <? extends TypeMirror > computeReturn (Set <ElementKind > types , CompilationInfo info , TreePath parent , Tree error ) {
676
702
ReturnTree rt = (ReturnTree ) parent .getLeaf ();
677
703
678
704
if (rt .getExpression () == error ) {
@@ -698,6 +724,45 @@ private static List<? extends TypeMirror> computeReturn(Set<ElementKind> types,
698
724
return null ;
699
725
}
700
726
727
+ private static List <? extends TypeMirror > computeYield (Set <ElementKind > types , CompilationInfo info , TreePath parent , Tree error , int offset ) {
728
+ YieldTree yieldStatement = (YieldTree ) parent .getLeaf ();
729
+
730
+ if (yieldStatement .getValue () == error ) {
731
+ Tree yieldTargetTree = info .getTreeUtilities ().getBreakContinueTargetTree (parent );
732
+ TreePath switchExpression = parent ;
733
+
734
+ while (switchExpression != null && switchExpression .getLeaf () != yieldTargetTree ) {
735
+ switchExpression = switchExpression .getParentPath ();
736
+ }
737
+
738
+ if (switchExpression == null ) {
739
+ return null ;
740
+ }
741
+
742
+ types .add (ElementKind .PARAMETER );
743
+ types .add (ElementKind .LOCAL_VARIABLE );
744
+ types .add (ElementKind .FIELD );
745
+
746
+ return resolveType (types , info , switchExpression .getParentPath (), switchExpression .getLeaf (), offset , null , null );
747
+ }
748
+
749
+ return null ;
750
+ }
751
+
752
+ private static List <? extends TypeMirror > computeCase (Set <ElementKind > types , CompilationInfo info , TreePath parent , Tree error , int offset ) {
753
+ TreePath switchCandidate = parent .getParentPath ();
754
+
755
+ if (switchCandidate .getLeaf ().getKind () == Kind .SWITCH_EXPRESSION ) {
756
+ types .add (ElementKind .PARAMETER );
757
+ types .add (ElementKind .LOCAL_VARIABLE );
758
+ types .add (ElementKind .FIELD );
759
+
760
+ return resolveType (types , info , switchCandidate .getParentPath (), switchCandidate .getLeaf (), offset , null , null );
761
+ }
762
+
763
+ return null ;
764
+ }
765
+
701
766
private static List <? extends TypeMirror > computeTypeParameter (Set <ElementKind > types , CompilationInfo info , TreePath parent , Tree error , int offset ) {
702
767
TypeParameterTree tpt = (TypeParameterTree ) parent .getLeaf ();
703
768
0 commit comments