Skip to content

Update Trim Annotation Logic #232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,16 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Util {
// whitespace not in quotes
private static final Pattern WHITE_SPACE_REGEX =
Pattern.compile("\\s+(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
// comma not in quotes
private static final Pattern COMMA_PATTERN =
Pattern.compile(", (?=(?:[^\\\"]*\\\"[^\\\"]*\\\")*[^\\\"]*$)");

/**
* Parse the raw type potentially handling generic parameters.
Expand Down Expand Up @@ -40,12 +48,29 @@ public static String typeDef(TypeMirror typeMirror) {
}
}

public static String trimAnnotations(String type) {
int pos = type.indexOf("@");
/** Trim off annotations from the raw type if present. */
public static String trimAnnotations(String input) {

input = COMMA_PATTERN.matcher(input).replaceAll(",");

return cutAnnotations(input);
}

private static String cutAnnotations(String input) {
final int pos = input.indexOf("@");
if (pos == -1) {
return type;
return input;
}
return type.substring(0, pos) + type.substring(type.lastIndexOf(' ') + 1);

final Matcher matcher = WHITE_SPACE_REGEX.matcher(input);

int currentIndex = 0;
if (matcher.find()) {
currentIndex = matcher.start();
}
final var result = input.substring(0, pos) + input.substring(currentIndex + 1);

return cutAnnotations(result);
}

static String trimPath(String value) {
Expand Down