private void handleInitalizers( List<? extends ExpressionTree> initializers, AnnotatedArrayType type) { List<Integer> array = new ArrayList<>(); array.add(initializers.size()); type.replaceAnnotation(createArrayLenAnnotation(array)); boolean singleDem = type.getComponentType().getKind() != TypeKind.ARRAY; if (singleDem) { return; } List<List<Integer>> summarylengths = new ArrayList<>(); for (ExpressionTree init : initializers) { AnnotatedArrayType subArrayType = (AnnotatedArrayType) getAnnotatedType(init); AnnotatedTypeMirror componentType = subArrayType; int count = 0; while (componentType.getKind() == TypeKind.ARRAY) { AnnotationMirror arrayLen = componentType.getAnnotation(ArrayLen.class); List<Integer> currentLengths; if (arrayLen != null) { currentLengths = getArrayLength(arrayLen); } else { currentLengths = (new ArrayList<Integer>()); } if (count == summarylengths.size()) { summarylengths.add(new ArrayList<Integer>()); } summarylengths.get(count).addAll(currentLengths); count++; componentType = ((AnnotatedArrayType) componentType).getComponentType(); } } AnnotatedTypeMirror componentType = type.getComponentType(); int i = 0; while (componentType.getKind() == TypeKind.ARRAY) { componentType.addAnnotation(createArrayLenAnnotation(summarylengths.get(i))); componentType = ((AnnotatedArrayType) componentType).getComponentType(); i++; } }
@Override public Void visitNewArray(NewArrayTree node, Void p) { AnnotatedArrayType type = atypeFactory.getAnnotatedType(node); AnnotatedTypeMirror componentType = type.getComponentType(); if (componentType.hasEffectiveAnnotation(NONNULL) && !isNewArrayAllZeroDims(node) && !isNewArrayInToArray(node) && !TypesUtils.isPrimitive(componentType.getUnderlyingType()) && checker.getLintOption("forbidnonnullarraycomponents", false)) { checker.report( Result.failure("new.array.type.invalid", componentType.getAnnotations(), type.toString()), node); } return super.visitNewArray(node, p); }
/** * Recursive method to handle array initializations. Recursively descends the initializer to * find each dimension's size and create the appropriate annotation for it. * * @param dimensions a list of ExpressionTrees where each ExpressionTree is a specifier of the * size of that dimension (should be an IntVal). * @param type the AnnotatedTypeMirror of the array */ private void handleDimensions( List<? extends ExpressionTree> dimensions, AnnotatedArrayType type) { if (dimensions.size() > 1) { handleDimensions( dimensions.subList(1, dimensions.size()), (AnnotatedArrayType) type.getComponentType()); } AnnotationMirror dimType = getAnnotatedType(dimensions.get(0)).getAnnotationInHierarchy(UNKNOWNVAL); if (!AnnotationUtils.areSameIgnoringValues(dimType, UNKNOWNVAL)) { List<Long> longLengths = getIntValues(dimType); HashSet<Integer> lengths = new HashSet<Integer>(longLengths.size()); for (Long l : longLengths) { lengths.add(l.intValue()); } AnnotationMirror newQual = createArrayLenAnnotation(new ArrayList<>(lengths)); type.replaceAnnotation(newQual); } }
@Override public R visitArray(AnnotatedArrayType type, P p) { R r = scan(type.getComponentType(), p); return r; }