[
https://issues.apache.org/jira/browse/AVRO-1658?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16693754#comment-16693754
]
ASF GitHub Bot commented on AVRO-1658:
--------------------------------------
dkulp closed pull request #189: AVRO-1658: Java: Add reflection annotation
@AvroDoc.
URL: https://github.com/apache/avro/pull/189
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/lang/java/avro/src/main/java/org/apache/avro/reflect/AvroDoc.java
b/lang/java/avro/src/main/java/org/apache/avro/reflect/AvroDoc.java
new file mode 100644
index 000000000..7b46a476b
--- /dev/null
+++ b/lang/java/avro/src/main/java/org/apache/avro/reflect/AvroDoc.java
@@ -0,0 +1,17 @@
+package org.apache.avro.reflect;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Sets the avrodoc for this java field.
+ * When reading into this class, a reflectdatumreader
+ * looks for a schema field with the avrodoc.
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ElementType.TYPE, ElementType.FIELD})
+public @interface AvroDoc {
+ String value();
+}
diff --git
a/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java
b/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java
index eb9f5fb0f..29f2acf71 100644
--- a/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java
+++ b/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java
@@ -572,6 +572,8 @@ protected Schema createSchema(Type type, Map<String,Schema>
names) {
String fullName = c.getName();
Schema schema = names.get(fullName);
if (schema == null) {
+ AvroDoc annotatedDoc = c.getAnnotation(AvroDoc.class); // Docstring
+ String doc = (annotatedDoc != null) ? annotatedDoc.value() : null;
String name = c.getSimpleName();
String space = c.getPackage() == null ? "" : c.getPackage().getName();
if (c.getEnclosingClass() != null) // nested class
@@ -588,18 +590,18 @@ protected Schema createSchema(Type type,
Map<String,Schema> names) {
Enum[] constants = (Enum[])c.getEnumConstants();
for (int i = 0; i < constants.length; i++)
symbols.add(constants[i].name());
- schema = Schema.createEnum(name, null /* doc */, space, symbols);
+ schema = Schema.createEnum(name, doc, space, symbols);
consumeAvroAliasAnnotation(c, schema);
} else if (GenericFixed.class.isAssignableFrom(c)) { // fixed
int size = c.getAnnotation(FixedSize.class).value();
- schema = Schema.createFixed(name, null /* doc */, space, size);
+ schema = Schema.createFixed(name, doc, space, size);
consumeAvroAliasAnnotation(c, schema);
} else if (IndexedRecord.class.isAssignableFrom(c)) { // specific
return super.createSchema(type, names);
} else { // record
List<Schema.Field> fields = new ArrayList<Schema.Field>();
boolean error = Throwable.class.isAssignableFrom(c);
- schema = Schema.createRecord(name, null /* doc */, space, error);
+ schema = Schema.createRecord(name, doc, space, error);
consumeAvroAliasAnnotation(c, schema);
names.put(c.getName(), schema);
for (Field field : getCachedFields(c))
@@ -611,6 +613,8 @@ protected Schema createSchema(Type type, Map<String,Schema>
names) {
JsonNode defaultValue = (defaultAnnotation == null)
? null
: Schema.parseJson(defaultAnnotation.value());
+ annotatedDoc = field.getAnnotation(AvroDoc.class); //
Docstring
+ doc = (annotatedDoc != null) ? annotatedDoc.value() : null;
if (defaultValue == null
&& fieldSchema.getType() == Schema.Type.UNION) {
@@ -624,7 +628,7 @@ protected Schema createSchema(Type type, Map<String,Schema>
names) {
? annotatedName.value()
: field.getName();
Schema.Field recordField
- = new Schema.Field(fieldName, fieldSchema, null, defaultValue);
+ = new Schema.Field(fieldName, fieldSchema, doc, defaultValue);
AvroMeta meta = field.getAnnotation(AvroMeta.class);
// add metadata
if (meta != null)
diff --git
a/lang/java/avro/src/test/java/org/apache/avro/reflect/TestReflect.java
b/lang/java/avro/src/test/java/org/apache/avro/reflect/TestReflect.java
index afe7f08ae..ddca2f9bb 100644
--- a/lang/java/avro/src/test/java/org/apache/avro/reflect/TestReflect.java
+++ b/lang/java/avro/src/test/java/org/apache/avro/reflect/TestReflect.java
@@ -1101,4 +1101,34 @@ public void testNullableByteArrayNotNullValue() throws
Exception {
public void testNullableByteArrayNullValue() throws Exception {
checkReadWrite(new NullableBytesTest());
}
+
+ private enum DocTestEnum {
+ ENUM_1,
+ ENUM_2
+ }
+
+ @AvroDoc("DocTest class docs")
+ private static class DocTest {
+ @AvroDoc("Some Documentation")
+ int foo;
+
+ @AvroDoc("Some other Documentation")
+ DocTestEnum enums;
+
+ @AvroDoc("And again")
+ DefaultTest defaultTest;
+ }
+
+ @Test
+ public void testAvroDoc() {
+ check(DocTest.class,
+
"{\"type\":\"record\",\"name\":\"DocTest\",\"namespace\":\"org.apache.avro.reflect.TestReflect$\","
+ + "\"doc\":\"DocTest class docs\","
+ +
"\"fields\":[{\"name\":\"foo\",\"type\":\"int\",\"doc\":\"Some
Documentation\"},"
+ +
"{\"name\":\"enums\",\"type\":{\"type\":\"enum\",\"name\":\"DocTestEnum\","
+ + "\"symbols\":[\"ENUM_1\",\"ENUM_2\"]},\"doc\":\"Some
other Documentation\"},"
+ +
"{\"name\":\"defaultTest\",\"type\":{\"type\":\"record\",\"name\":\"DefaultTest\","
+ +
"\"fields\":[{\"name\":\"foo\",\"type\":\"int\",\"default\":1}]},\"doc\":\"And
again\"}]}");
+ }
+
}
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> Add avroDoc on reflect
> ----------------------
>
> Key: AVRO-1658
> URL: https://issues.apache.org/jira/browse/AVRO-1658
> Project: Apache Avro
> Issue Type: New Feature
> Components: java
> Affects Versions: 1.7.7
> Reporter: Zhaonan Sun
> Assignee: Zhaonan Sun
> Priority: Major
> Labels: reflection
> Fix For: 1.9.0
>
> Attachments:
> 0001-AVRO-1658-Java-Add-reflection-annotation-AvroDoc.patch,
> 0001-AVRO-1658-Java-Add-reflection-annotation-AvroDoc.patch,
> 0001-AVRO-1658-Java-Add-reflection-annotation-AvroDoc.patch
>
>
> Looks like @AvroMeta can't add reserved fields, like @AvroMeta("doc", "some
> doc") will have exceptions.
> I would be greate if we have a @AvroDoc("some documentations") in
> org.apache.avro.reflect
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)