|
1 | 1 | package org.checkerframework.framework.ajava; |
2 | 2 |
|
| 3 | +import com.github.javaparser.JavaToken; |
| 4 | +import com.github.javaparser.JavaToken.Kind; |
3 | 5 | import com.github.javaparser.Position; |
| 6 | +import com.github.javaparser.TokenRange; |
4 | 7 | import com.github.javaparser.ast.CompilationUnit; |
5 | 8 | import com.github.javaparser.ast.ImportDeclaration; |
6 | 9 | import com.github.javaparser.ast.Node; |
|
11 | 14 | import com.github.javaparser.ast.expr.AnnotationExpr; |
12 | 15 | import com.github.javaparser.ast.nodeTypes.NodeWithAnnotations; |
13 | 16 | import com.github.javaparser.ast.type.ArrayType; |
| 17 | +import com.github.javaparser.ast.type.ArrayType.ArrayBracketPair; |
14 | 18 | import com.github.javaparser.ast.type.ClassOrInterfaceType; |
15 | 19 | import com.github.javaparser.ast.type.Type; |
16 | 20 | import com.github.javaparser.printer.DefaultPrettyPrinter; |
@@ -238,23 +242,29 @@ public void defaultAction(Node src, Node dest) { |
238 | 242 | @Override |
239 | 243 | public void visit(ArrayType src, Node other) { |
240 | 244 | ArrayType dest = (ArrayType) other; |
241 | | - // The second component of this pair contains a list of ArrayBracketPairs from left to |
242 | | - // right. For example, if src contains String[][], then the list will contain the |
243 | | - // types String[] and String[][]. To insert array annotations in the correct location, |
244 | | - // we insert them directly to the right of the end of the previous element. |
245 | | - Pair<Type, List<ArrayType.ArrayBracketPair>> srcArrayTypes = ArrayType.unwrapArrayTypes(src); |
246 | | - Pair<Type, List<ArrayType.ArrayBracketPair>> destArrayTypes = |
247 | | - ArrayType.unwrapArrayTypes(dest); |
248 | | - // The first annotations go directly after the element type. |
249 | | - Position firstPosition = destArrayTypes.a.getEnd().get(); |
250 | | - addAnnotations(firstPosition, srcArrayTypes.b.get(0).getAnnotations(), 1, false); |
251 | | - for (int i = 1; i < srcArrayTypes.b.size(); i++) { |
252 | | - Position position = destArrayTypes.b.get(i - 1).getTokenRange().get().toRange().get().end; |
253 | | - addAnnotations(position, srcArrayTypes.b.get(i).getAnnotations(), 1, true); |
| 245 | + Pair<Type, List<ArrayBracketPair>> destArrayTypes = ArrayType.unwrapArrayTypes(dest); |
| 246 | + TokenRange innerMostCom = destArrayTypes.a.getTokenRange().get(); |
| 247 | + |
| 248 | + List<Position> positions = new ArrayList<>(); |
| 249 | + for (JavaToken token : dest.getTokenRange().get().withBegin(innerMostCom.getEnd())) { |
| 250 | + if (token.getKind() == Kind.LBRACKET.getKind()) { |
| 251 | + positions.add(token.getRange().get().begin); |
| 252 | + } |
| 253 | + } |
| 254 | + |
| 255 | + // At the end of the loop, these two variables will contain the innermost array type. |
| 256 | + ArrayType srcArray = src; |
| 257 | + ArrayType destArray = dest; |
| 258 | + for (Position position : positions) { |
| 259 | + addAnnotations(position, srcArray.getAnnotations(), 0, true); |
| 260 | + if (srcArray.getComponentType().isArrayType()) { |
| 261 | + srcArray = (ArrayType) srcArray.getComponentType(); |
| 262 | + destArray = (ArrayType) destArray.getComponentType(); |
| 263 | + } |
254 | 264 | } |
255 | 265 |
|
256 | | - // Visit the component type. |
257 | | - srcArrayTypes.a.accept(this, destArrayTypes.a); |
| 266 | + // Visit the innermost component type. |
| 267 | + srcArray.getComponentType().accept(this, destArray.getComponentType()); |
258 | 268 | } |
259 | 269 |
|
260 | 270 | @Override |
|
0 commit comments