Skip to content

Commit 4b9d755

Browse files
authored
Merge pull request #232 from SentryMan/trim
Update Trim Annotation Logic
2 parents 8fe71bd + b73c790 commit 4b9d755

File tree

1 file changed

+29
-4
lines changed
  • http-generator-core/src/main/java/io/avaje/http/generator/core

1 file changed

+29
-4
lines changed

http-generator-core/src/main/java/io/avaje/http/generator/core/Util.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,16 @@
1111
import java.util.ArrayList;
1212
import java.util.Collections;
1313
import java.util.List;
14+
import java.util.regex.Matcher;
15+
import java.util.regex.Pattern;
1416

1517
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(", (?=(?:[^\\\"]*\\\"[^\\\"]*\\\")*[^\\\"]*$)");
1624

1725
/**
1826
* Parse the raw type potentially handling generic parameters.
@@ -40,12 +48,29 @@ public static String typeDef(TypeMirror typeMirror) {
4048
}
4149
}
4250

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("@");
4561
if (pos == -1) {
46-
return type;
62+
return input;
4763
}
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);
4974
}
5075

5176
static String trimPath(String value) {

0 commit comments

Comments
 (0)