Skip to content
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

[BEAM-790] Validate PipelineOptions Default annotation. #1159

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -1120,7 +1120,7 @@ public Iterable<String> apply(final @Nonnull Method method) {
public String apply(@Nonnull Annotation annotation) {
return String.format(
"[%s on %s]",
ReflectHelpers.toStringForPrint(annotation),
ReflectHelpers.ANNOTATION_CONCISE_STRING.apply(annotation),
ReflectHelpers.CLASS_AND_METHOD_FORMATTER.apply(method));
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,22 @@ public String apply(@Nonnull Class<?> input) {
}
};

/**
* A {@link Function} that returns a concise string for a {@link Annotation}.
*/
public static final Function<Annotation, String> ANNOTATION_CONCISE_STRING =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ANNOTATION_CONCISE_STRING -> ANNOTATION_FORMATTER

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

new Function<Annotation, String>() {
@Override
public String apply(@Nonnull Annotation annotation) {
String annotationName = annotation.annotationType().getName();
String annotationNameWithoutPackage =
annotationName.substring(annotationName.lastIndexOf('.') + 1).replace('$', '.');
String annotationToString = annotation.toString();
String values = annotationToString.substring(annotationToString.indexOf('('));
return String.format("%s%s", annotationNameWithoutPackage, values);
}
};

/** A {@link Function} that formats types. */
public static final Function<Type, String> TYPE_SIMPLE_DESCRIPTION =
new Function<Type, String>() {
Expand Down Expand Up @@ -187,19 +203,4 @@ public static Iterable<Method> getClosureOfMethodsOnInterface(Class<?> iface) {
}
return builder.build();
}

/**
* Returns a concise string for printing a {@link Annotation}.
*
* @param annotation The {@link Annotation} to print.
* @return a concise string representation that doesn't contain the package name.
*/
public static String toStringForPrint(Annotation annotation) {
String annotationName = annotation.annotationType().getName();
String annotationNameWithoutPackage =
annotationName.substring(annotationName.lastIndexOf('.') + 1).replace('$', '.');
String annotationToString = annotation.toString();
String values = annotationToString.substring(annotationToString.indexOf('('));
return String.format("%s%s", annotationNameWithoutPackage, values);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@ private interface Options extends PipelineOptions {
public void testToStringForPrint() throws Exception {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testToStringForPrint -> testAnnotationFormatter

Add a test for a default value with a . and ( and $ in it so that we remember why we have string parsing logic setup the way we do.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

assertEquals(
"Default.Integer(value=1)",
ReflectHelpers.toStringForPrint(
ReflectHelpers.ANNOTATION_CONCISE_STRING.apply(
Options.class.getMethod("getInteger").getAnnotations()[0]));

assertEquals(
"JsonIgnore(value=true)",
ReflectHelpers.toStringForPrint(
ReflectHelpers.ANNOTATION_CONCISE_STRING.apply(
Options.class.getMethod("getObject").getAnnotations()[0]));
}

Expand Down