From 2d54f61a4602a74fcef2baae8741a3cafe5a6fea Mon Sep 17 00:00:00 2001 From: Evan McClain Date: Thu, 3 Oct 2024 15:28:39 -0400 Subject: [PATCH] AVRO-4072: Add AvroOrder for ReflectData --- .../org/apache/avro/reflect/AvroOrder.java | 34 +++++++++++++++++++ .../org/apache/avro/reflect/ReflectData.java | 6 +++- .../org/apache/avro/reflect/TestReflect.java | 18 ++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 lang/java/avro/src/main/java/org/apache/avro/reflect/AvroOrder.java diff --git a/lang/java/avro/src/main/java/org/apache/avro/reflect/AvroOrder.java b/lang/java/avro/src/main/java/org/apache/avro/reflect/AvroOrder.java new file mode 100644 index 00000000000..da00e55b33e --- /dev/null +++ b/lang/java/avro/src/main/java/org/apache/avro/reflect/AvroOrder.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +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; + +import org.apache.avro.Schema; + +/** + * Sets the field order for this java field. + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.FIELD }) +public @interface AvroOrder { + Schema.Field.Order 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 4ead6b888f4..ef385c99d8a 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 @@ -744,7 +744,11 @@ protected Schema createSchema(Type type, Map names) { if (STRING_OUTER_PARENT_REFERENCE.equals(fieldName)) { throw new AvroTypeException("Class " + fullName + " must be a static inner class"); } - Schema.Field recordField = new Schema.Field(fieldName, fieldSchema, doc, defaultValue); + + AvroOrder fieldOrder = field.getAnnotation(AvroOrder.class); + Schema.Field recordField = (null == fieldOrder) + ? new Schema.Field(fieldName, fieldSchema, doc, defaultValue) + : new Schema.Field(fieldName, fieldSchema, doc, defaultValue, fieldOrder.value()); AvroMeta[] metadata = field.getAnnotationsByType(AvroMeta.class); // add metadata for (AvroMeta meta : metadata) { 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 e3065d59b87..d3506454c4f 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 @@ -1349,4 +1349,22 @@ public void testAvroDoc() { + "{\"name\":\"foo\",\"type\":\"int\",\"doc\":\"Some Documentation\"}" + "]}"); } + private static class OrderTest { + @AvroOrder(Field.Order.IGNORE) + int foo; + @AvroOrder(Field.Order.DESCENDING) + String bar; + String fooBar; + } + + @Test + public void testAvroOrder() { + Schema schema = ReflectData.get().getSchema(OrderTest.class); + + assertEquals(schema.getField("foo").order(), Field.Order.IGNORE); + assertEquals(schema.getField("bar").order(), Field.Order.DESCENDING); + // NOTE: The default field order is ASCENDING + assertEquals(schema.getField("fooBar").order(), Field.Order.ASCENDING); + } + }