|
11 | 11 | import java.util.ArrayList; |
12 | 12 | import java.util.Collections; |
13 | 13 | import java.util.List; |
| 14 | +import java.util.regex.Matcher; |
| 15 | +import java.util.regex.Pattern; |
14 | 16 |
|
15 | 17 | public class Util { |
| 18 | + // whitespace not in quotes |
| 19 | + private static final Pattern WHITE_SPACE_REGEX = |
| 20 | + Pattern.compile("\\s+(?=([^\"]*\"[^\"]*\")*[^\"]*$)"); |
| 21 | + // comma not in quotes |
| 22 | + private static final Pattern COMMA_PATTERN = |
| 23 | + Pattern.compile(", (?=(?:[^\\\"]*\\\"[^\\\"]*\\\")*[^\\\"]*$)"); |
16 | 24 |
|
17 | 25 | /** |
18 | 26 | * Parse the raw type potentially handling generic parameters. |
@@ -40,12 +48,29 @@ public static String typeDef(TypeMirror typeMirror) { |
40 | 48 | } |
41 | 49 | } |
42 | 50 |
|
43 | | - public static String trimAnnotations(String type) { |
44 | | - int pos = type.indexOf("@"); |
| 51 | + /** Trim off annotations from the raw type if present. */ |
| 52 | + public static String trimAnnotations(String input) { |
| 53 | + |
| 54 | + input = COMMA_PATTERN.matcher(input).replaceAll(","); |
| 55 | + |
| 56 | + return cutAnnotations(input); |
| 57 | + } |
| 58 | + |
| 59 | + private static String cutAnnotations(String input) { |
| 60 | + final int pos = input.indexOf("@"); |
45 | 61 | if (pos == -1) { |
46 | | - return type; |
| 62 | + return input; |
47 | 63 | } |
48 | | - return type.substring(0, pos) + type.substring(type.lastIndexOf(' ') + 1); |
| 64 | + |
| 65 | + final Matcher matcher = WHITE_SPACE_REGEX.matcher(input); |
| 66 | + |
| 67 | + int currentIndex = 0; |
| 68 | + if (matcher.find()) { |
| 69 | + currentIndex = matcher.start(); |
| 70 | + } |
| 71 | + final var result = input.substring(0, pos) + input.substring(currentIndex + 1); |
| 72 | + |
| 73 | + return cutAnnotations(result); |
49 | 74 | } |
50 | 75 |
|
51 | 76 | static String trimPath(String value) { |
|
0 commit comments