2121import java .util .List ;
2222import java .util .Map ;
2323import java .util .Objects ;
24- import java .util .function .Function ;
25- import java .util .function .Predicate ;
2624import java .util .stream .Collectors ;
2725import javax .annotation .CheckForNull ;
2826import javax .annotation .Nullable ;
3937
4038final class JSymbolMetadata implements SymbolMetadata {
4139
42- private static final NullabilityData [] NO_ANNOTATION_NULLABILITY =
43- forEachLevel (level -> new JNullabilityData (NullabilityType .NO_ANNOTATION , level , null , null , false ));
40+ public static NullabilityData noNullabilityAnnotationAt (NullabilityLevel level ) {
41+ return JNullabilityData .noNullabilityAnnotationAt (level );
42+ }
4443
45- private static final NullabilityData [] UNKNOWN_NULLABILITY =
46- forEachLevel (level -> new JNullabilityData (NullabilityType .UNKNOWN , level , null , null , false ));
44+ public static NullabilityData unknownNullabilityAt (NullabilityLevel level ) {
45+ return JNullabilityData .unknownNullabilityAt (level );
46+ }
4747
4848 private final JSema sema ;
4949 private final Symbol symbol ;
@@ -118,18 +118,6 @@ private static JSymbolMetadata ofTypeArg(JSema sema, Symbol owner, ITypeBinding
118118 return new JSymbolMetadata (sema , owner , symbolAnnotations , parameterMetaDatas , true );
119119 }
120120
121- private static NullabilityData [] forEachLevel (Function <NullabilityLevel , NullabilityData > initializer ) {
122- return Arrays .stream (NullabilityLevel .values ()).map (initializer ).toArray (NullabilityData []::new );
123- }
124-
125- public static NullabilityData noNullabilityAnnotationAt (NullabilityLevel level ) {
126- return NO_ANNOTATION_NULLABILITY [level .ordinal ()];
127- }
128-
129- public static NullabilityData unknownNullabilityAt (NullabilityLevel level ) {
130- return UNKNOWN_NULLABILITY [level .ordinal ()];
131- }
132-
133121 @ Override
134122 public List <AnnotationInstance > annotations () {
135123 if (annotations == null ) {
@@ -231,7 +219,7 @@ private NullabilityData resolveNullability(NullabilityTarget target) {
231219
232220 // Not annotated or meta annotated, check upper level in the ownership hierarchy...
233221 if (symbol .isPackageSymbol ()) {
234- return NO_ANNOTATION_NULLABILITY [ currentLevel . ordinal ()] ;
222+ return noNullabilityAnnotationAt ( currentLevel ) ;
235223 }
236224 Symbol owner = getEffectiveOwner (symbol , currentLevel );
237225 if (owner == null ) return unknownNullabilityAt (currentLevel );
@@ -249,7 +237,7 @@ private static NullabilityData getNullabilityDataFromInheritance(Symbol.MethodSy
249237 return nullabilityData ;
250238 }
251239 }
252- return NO_ANNOTATION_NULLABILITY [ level . ordinal ()] ;
240+ return noNullabilityAnnotationAt ( level ) ;
253241 }
254242
255243 @ CheckForNull
@@ -302,82 +290,6 @@ private static NullabilityTarget getTarget(Symbol symbol) {
302290 return null ;
303291 }
304292
305- static final class JNullabilityData implements NullabilityData {
306-
307- private final NullabilityType type ;
308- private final NullabilityLevel level ;
309-
310- @ Nullable
311- private final AnnotationInstance annotation ;
312- @ Nullable
313- private final AnnotationTree annotationTree ;
314-
315- private final boolean metaAnnotation ;
316-
317- public JNullabilityData (NullabilityType type , NullabilityLevel level , @ Nullable AnnotationInstance annotation ,
318- @ Nullable AnnotationTree annotationTree , boolean metaAnnotation ) {
319- this .type = type ;
320- this .level = level ;
321- this .annotation = annotation ;
322- this .annotationTree = annotationTree ;
323- this .metaAnnotation = metaAnnotation ;
324- }
325-
326- @ Override
327- public NullabilityType type () {
328- return type ;
329- }
330-
331- @ Override
332- public NullabilityLevel level () {
333- return level ;
334- }
335-
336- @ Override
337- public boolean metaAnnotation () {
338- return metaAnnotation ;
339- }
340-
341- @ Override
342- public boolean isNonNull (NullabilityLevel minLevel , boolean ignoreMetaAnnotation , boolean defaultValue ) {
343- return testNullabilityType (minLevel , ignoreMetaAnnotation , defaultValue , t -> t == NullabilityType .NON_NULL );
344- }
345-
346- @ Override
347- public boolean isNullable (NullabilityLevel minLevel , boolean ignoreMetaAnnotation , boolean defaultValue ) {
348- return testNullabilityType (minLevel , ignoreMetaAnnotation , defaultValue ,
349- t -> t == NullabilityType .STRONG_NULLABLE || t == NullabilityType .WEAK_NULLABLE );
350- }
351-
352- @ Override
353- public boolean isStrongNullable (NullabilityLevel minLevel , boolean ignoreMetaAnnotation , boolean defaultValue ) {
354- return testNullabilityType (minLevel , ignoreMetaAnnotation , defaultValue , t -> t == NullabilityType .STRONG_NULLABLE );
355- }
356-
357- private boolean testNullabilityType (NullabilityLevel minLevel , boolean ignoreMetaAnnotation , boolean defaultValue , Predicate <NullabilityType > typePredicate ) {
358- if (type == NullabilityType .NO_ANNOTATION ) {
359- return false ;
360- } else if (type == NullabilityType .UNKNOWN || (ignoreMetaAnnotation && metaAnnotation )) {
361- return defaultValue ;
362- } else if (typePredicate .test (type )) {
363- return minLevel .ordinal () <= level .ordinal ();
364- }
365- return false ;
366- }
367-
368- @ Nullable
369- @ Override
370- public AnnotationInstance annotation () {
371- return annotation ;
372- }
373-
374- @ Nullable
375- @ Override
376- public Tree declaration () {
377- return annotationTree ;
378- }
379- }
380-
381293 static final class JAnnotationInstance implements AnnotationInstance {
382294 private final JSema sema ;
383295 private final IAnnotationBinding annotationBinding ;
@@ -401,27 +313,26 @@ public Symbol symbol() {
401313 public List <AnnotationValue > values () {
402314 if (values == null ) {
403315 values = Arrays .stream (annotationBinding .getDeclaredMemberValuePairs ())
404- .map (p -> new AnnotationValueImpl (p .getName (), convertAnnotationValue (p .getValue ())))
316+ .map (p -> new AnnotationValueImpl (p .getName (), convertAnnotationValue (sema , p .getValue ())))
405317 .collect (Collectors .toList ());
406318 }
407319 return values ;
408320 }
321+ }
409322
410- private Object convertAnnotationValue (Object value ) {
411- return switch (value ) {
412- case IVariableBinding iVariableBinding -> sema .variableSymbol (iVariableBinding );
413- case ITypeBinding iTypeBinding -> sema .typeSymbol (iTypeBinding );
414- case IAnnotationBinding iAnnotationBinding -> sema .annotation (iAnnotationBinding );
415- case Object [] a -> {
416- Object [] result = new Object [a .length ];
417- for (int i = 0 ; i < a .length ; i ++) {
418- result [i ] = convertAnnotationValue (a [i ]);
419- }
420- yield result ;
323+ static Object convertAnnotationValue (JSema sema , Object value ) {
324+ return switch (value ) {
325+ case IVariableBinding iVariableBinding -> sema .variableSymbol (iVariableBinding );
326+ case ITypeBinding iTypeBinding -> sema .typeSymbol (iTypeBinding );
327+ case IAnnotationBinding iAnnotationBinding -> sema .annotation (iAnnotationBinding );
328+ case Object [] a -> {
329+ Object [] result = new Object [a .length ];
330+ for (int i = 0 ; i < a .length ; i ++) {
331+ result [i ] = convertAnnotationValue (sema , a [i ]);
421332 }
422- case null , default -> value ;
423- };
424- }
333+ yield result ;
334+ }
335+ case null , default -> value ;
336+ };
425337 }
426-
427338}
0 commit comments