Skip to content

Conversation

@ashley-taylor
Copy link
Contributor

This PR adds the ability to use the CustomEncode annotation directly on a class.

It required some changes to when this annotation would be scanned. Currently, it is only used on fields.
Getting this to work for the first Record written required additional changes to support the feature.

I added a new map on ReflectData as I feel this is cleaner from an encapsulation point of view. Alternatively, it could be added to SpecifiedData by changing the classCache map to an object that stores the class and this CustomEncoding. This may be slightly faster, as there would be one less map lookup. However, it would probably be negligible as this new lookup is only performed once per row, whereas other maps can be queried per field.

This change added tests and can be verified as follows:

  • *Added tests that use the annotation in the new place. Checking it works at the top of the structure. Also tested as a field and precedence if competing encoders

Documentation

  • Does this pull request introduce a new feature? yes
  • If yes, how is the feature documented? CustomEncode will appear in IDE IntelliSense in this location now.

@github-actions github-actions bot added the Java Pull Requests for Java binding label Jul 5, 2025
Comment on lines 1054 to 1079
private CustomEncodingWrapper populateEncoderCache(Schema schema) {
var enc = ReflectionUtil.getAvroEncode(getClass(schema));
if (enc != null) {
try {
return new CustomEncodingWrapper(enc.using().getDeclaredConstructor().newInstance());
} catch (Exception e) {
throw new AvroRuntimeException("Could not instantiate custom Encoding");
}
}
return new CustomEncodingWrapper(null);
}

private class CustomEncodingWrapper {

private final CustomEncoding customEncoding;

private CustomEncodingWrapper(CustomEncoding customEncoding) {
this.customEncoding = customEncoding;
}

public CustomEncoding get() {
return customEncoding;
}

}

Copy link
Contributor

Choose a reason for hiding this comment

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

I like this implementation! As you already mentioned in the PR, it's also suited for one of the superclasses. However, as the current use case for CustomEncoding is only from @AvroEncode, I think it's good as it is now.

@ashley-taylor
Copy link
Contributor Author

@opwvhk thanks for the review. Have addressed the feedback. Question on process: Do you like conversations to be left for you to resolve?

@opwvhk opwvhk merged commit 30c31a5 into apache:main Jul 19, 2025
9 checks passed
@ashley-taylor ashley-taylor deleted the AVRO-4165-ability-to-specify-AvroEncode-on-a-class branch July 19, 2025 10:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Java Pull Requests for Java binding

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants