Skip to content

Commit 9dc2af8

Browse files
committed
minor cleanup for the patch -- not done yet, need to consider backwards compatibility
1 parent c06c030 commit 9dc2af8

File tree

10 files changed

+90
-77
lines changed

10 files changed

+90
-77
lines changed

avro/pom.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,16 @@ abstractions.
4949
<dependency>
5050
<groupId>org.projectlombok</groupId>
5151
<artifactId>lombok</artifactId>
52-
<version>1.16.12</version>
52+
<version>1.16.14</version>
5353
<scope>test</scope>
5454
</dependency>
5555
<!-- For validating more complex comparisons -->
56+
<!-- 27-Feb-2017, tatu: NOTE! Can NOT use 3.x as it requires Java 8
57+
-->
5658
<dependency>
5759
<groupId>org.assertj</groupId>
5860
<artifactId>assertj-core</artifactId>
59-
<version>3.6.2</version>
61+
<version>2.5.0</version>
6062
<scope>test</scope>
6163
</dependency>
6264

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,55 @@
11
package com.fasterxml.jackson.dataformat.avro;
22

3+
import com.fasterxml.jackson.core.Version;
4+
import com.fasterxml.jackson.databind.AnnotationIntrospector;
35
import com.fasterxml.jackson.databind.PropertyName;
46
import com.fasterxml.jackson.databind.introspect.Annotated;
5-
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
7+
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
8+
9+
import org.apache.avro.reflect.AvroDefault;
610
import org.apache.avro.reflect.AvroIgnore;
711
import org.apache.avro.reflect.AvroName;
8-
import org.codehaus.jackson.annotate.JsonIgnore;
9-
import org.codehaus.jackson.annotate.JsonProperty;
1012

1113
/**
1214
* Adds support for the following annotations from the Apache Avro implementation:
1315
* <ul>
14-
* <li>{@link AvroIgnore @AvroIgnore} - Alias for {@link JsonIgnore @JsonIgnore(true)}</li>
15-
* <li>{@link AvroName @AvroName("custom Name")} - Alias for {@link JsonProperty @JsonProperty("custom name")}</li>
16+
* <li>{@link AvroIgnore @AvroIgnore} - Alias for <code>JsonIgnore</code></li>
17+
* <li>{@link AvroName @AvroName("custom Name")} - Alias for <code>JsonProperty("custom name")</code></li>
1618
* </ul>
1719
*/
18-
public class AvroAnnotationIntrospector extends JacksonAnnotationIntrospector {
20+
public class AvroAnnotationIntrospector extends AnnotationIntrospector
21+
{
22+
private static final long serialVersionUID = 1L;
1923

2024
@Override
21-
protected boolean _isIgnorable(Annotated a) {
22-
return a.getAnnotation(AvroIgnore.class) != null || super._isIgnorable(a);
25+
public Version version() {
26+
return PackageVersion.VERSION;
27+
}
28+
29+
@Override
30+
public boolean hasIgnoreMarker(AnnotatedMember m) {
31+
return _findAnnotation(m, AvroIgnore.class) != null;
2332
}
2433

2534
@Override
2635
public PropertyName findNameForSerialization(Annotated a) {
27-
if (a.hasAnnotation(AvroName.class)) {
28-
return PropertyName.construct(a.getAnnotation(AvroName.class).value());
29-
}
30-
return super.findNameForSerialization(a);
36+
return _findName(a);
3137
}
3238

3339
@Override
3440
public PropertyName findNameForDeserialization(Annotated a) {
35-
if (a.hasAnnotation(AvroName.class)) {
36-
return PropertyName.construct(a.getAnnotation(AvroName.class).value());
37-
}
38-
return super.findNameForDeserialization(a);
41+
return _findName(a);
42+
}
43+
44+
@Override
45+
public String findPropertyDefaultValue(Annotated m) {
46+
AvroDefault ann = _findAnnotation(m, AvroDefault.class);
47+
return (ann == null) ? null : ann.value();
48+
}
49+
50+
protected PropertyName _findName(Annotated a)
51+
{
52+
AvroName ann = _findAnnotation(a, AvroName.class);
53+
return (ann == null) ? null : PropertyName.construct(ann.value());
3954
}
4055
}

avro/src/main/java/com/fasterxml/jackson/dataformat/avro/schema/IntegerVisitor.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.fasterxml.jackson.core.JsonParser;
66
import com.fasterxml.jackson.databind.JavaType;
77
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonIntegerFormatVisitor;
8-
import com.fasterxml.jackson.dataformat.avro.AvroSchema;
98

109
public class IntegerVisitor extends JsonIntegerFormatVisitor.Base
1110
implements SchemaBuilder

avro/src/main/java/com/fasterxml/jackson/dataformat/avro/schema/MapVisitor.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.fasterxml.jackson.dataformat.avro.schema;
22

33
import org.apache.avro.Schema;
4-
import org.apache.avro.Schema.Type;
54

65
import com.fasterxml.jackson.databind.JavaType;
76
import com.fasterxml.jackson.databind.JsonMappingException;

avro/src/main/java/com/fasterxml/jackson/dataformat/avro/schema/VisitorFormatWrapperImpl.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.fasterxml.jackson.databind.JavaType;
66
import com.fasterxml.jackson.databind.SerializerProvider;
77
import com.fasterxml.jackson.databind.jsonFormatVisitors.*;
8-
import com.fasterxml.jackson.dataformat.avro.AvroSchema;
98

109
public class VisitorFormatWrapperImpl
1110
implements JsonFormatVisitorWrapper

avro/src/test/java/com/fasterxml/jackson/dataformat/avro/AvroTestBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ protected void assertToken(JsonToken expToken, JsonParser jp)
271271
assertToken(expToken, jp.getCurrentToken());
272272
}
273273

274-
protected void verifyException(Throwable e, String... matches)
274+
public static void verifyException(Throwable e, String... matches)
275275
{
276276
String msg = e.getMessage();
277277
String lmsg = (msg == null) ? "" : msg.toLowerCase();

avro/src/test/java/com/fasterxml/jackson/dataformat/avro/interop/ApacheAvroInteropUtil.java

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
11
package com.fasterxml.jackson.dataformat.avro.interop;
22

3-
import com.fasterxml.jackson.core.JsonProcessingException;
4-
import com.fasterxml.jackson.databind.JavaType;
5-
import com.fasterxml.jackson.databind.JsonMappingException;
6-
import com.fasterxml.jackson.dataformat.avro.AvroMapper;
7-
import com.fasterxml.jackson.dataformat.avro.AvroSchema;
8-
import org.apache.avro.Schema;
9-
import org.apache.avro.io.Decoder;
10-
import org.apache.avro.io.DecoderFactory;
11-
import org.apache.avro.io.Encoder;
12-
import org.apache.avro.io.EncoderFactory;
13-
import org.apache.avro.reflect.ReflectData;
14-
153
import java.io.ByteArrayOutputStream;
164
import java.io.IOException;
175
import java.lang.reflect.ParameterizedType;
@@ -20,14 +8,27 @@
208
import java.util.HashMap;
219
import java.util.Map;
2210

11+
import org.apache.avro.Schema;
12+
import org.apache.avro.io.Decoder;
13+
import org.apache.avro.io.DecoderFactory;
14+
import org.apache.avro.io.Encoder;
15+
import org.apache.avro.io.EncoderFactory;
16+
import org.apache.avro.reflect.ReflectData;
17+
18+
import com.fasterxml.jackson.core.JsonProcessingException;
19+
import com.fasterxml.jackson.databind.JavaType;
20+
import com.fasterxml.jackson.databind.JsonMappingException;
21+
import com.fasterxml.jackson.dataformat.avro.AvroMapper;
22+
import com.fasterxml.jackson.dataformat.avro.AvroSchema;
23+
2324
/**
2425
* Utilities and helper functions to aid compatibility testing between Jackson and Apache Avro implementations
2526
*/
2627
public class ApacheAvroInteropUtil {
2728
/**
2829
* Functor of {@link #jacksonSerialize(Schema, Object)}
2930
*/
30-
public static final BiFunction<Schema, Object, byte[]> jacksonSerializer = new BiFunction<Schema, Object, byte[]>() {
31+
public static final BiFunction<Schema, Object, byte[]> jacksonSerializer = new BiFunction<Schema, Object, byte[]>() {
3132
@Override
3233
public byte[] apply(Schema schema, Object originalObject) {
3334
return jacksonSerialize(schema, originalObject);
@@ -79,14 +80,14 @@ public byte[] apply(Schema schema, Object originalObject) {
7980
return apacheSerialize(schema, originalObject);
8081
}
8182
};
82-
private static final AvroMapper MAPPER = new AvroMapper();
83+
private static final AvroMapper MAPPER = new AvroMapper();
8384
/*
8485
* Special subclass of ReflectData that knows how to resolve and bind generic types. This saves us much pain of
8586
* having to build these schemas by hand. Also, workarounds for several bugs in the Apache implementation are
8687
* implemented here.
8788
*/
88-
private static final ReflectData PATCHED_AVRO_REFLECT_DATA = new ReflectData() {
89-
@SuppressWarnings({"unchecked", "SuspiciousMethodCalls"})
89+
private static final ReflectData PATCHED_AVRO_REFLECT_DATA = new ReflectData() {
90+
@SuppressWarnings({"unchecked", "SuspiciousMethodCalls", "rawtypes"})
9091
@Override
9192
protected Schema createSchema(Type type, Map<String, Schema> names) {
9293
/*
@@ -96,21 +97,21 @@ protected Schema createSchema(Type type, Map<String, Schema> names) {
9697
* when building a schema from reflection data.
9798
*/
9899
if (type instanceof ParameterizedType) {
99-
TypeVariable[] genericParameters = ((Class) ((ParameterizedType) type).getRawType()).getTypeParameters();
100+
TypeVariable<?>[] genericParameters = ((Class<?>) ((ParameterizedType) type).getRawType()).getTypeParameters();
100101
if (genericParameters.length > 0) {
101102
Type[] boundParameters = ((ParameterizedType) type).getActualTypeArguments();
102103
for (int i = 0; i < boundParameters.length; i++) {
103104
((Map) names).put(genericParameters[i], createSchema(boundParameters[i], new HashMap<>(names)));
104105
}
105106
}
106107
}
107-
if (type instanceof Class && ((Class) type).getSuperclass() != null) {
108+
if (type instanceof Class<?> && ((Class<?>) type).getSuperclass() != null) {
108109
// Raw class may extend a generic superclass
109110
// extract all the type bindings and add them to the map so they can be returned by the next block
110111
// Interfaces shouldn't matter here because interfaces can't have fields and avro only looks at fields.
111-
TypeVariable[] genericParameters = ((Class) type).getSuperclass().getTypeParameters();
112+
TypeVariable<?>[] genericParameters = ((Class<?>) type).getSuperclass().getTypeParameters();
112113
if (genericParameters.length > 0) {
113-
Type[] boundParameters = ((ParameterizedType) ((Class) type).getGenericSuperclass()).getActualTypeArguments();
114+
Type[] boundParameters = ((ParameterizedType) ((Class<?>) type).getGenericSuperclass()).getActualTypeArguments();
114115
for (int i = 0; i < boundParameters.length; i++) {
115116
((Map) names).put(genericParameters[i], createSchema(boundParameters[i], new HashMap<>(names)));
116117
}

avro/src/test/java/com/fasterxml/jackson/dataformat/avro/interop/InteropTestBase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ public String toString() {
7171
if (typeBindings.length != 0) {
7272
builder.append('<');
7373
for (Type type : typeBindings) {
74-
if (type instanceof Class) {
75-
builder.append(((Class) type).getName());
74+
if (type instanceof Class<?>) {
75+
builder.append(((Class<?>) type).getName());
7676
} else {
7777
builder.append(type.toString());
7878
}

avro/src/test/java/com/fasterxml/jackson/dataformat/avro/interop/annotations/AvroIgnoreTest.java

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,26 @@
77
import static org.hamcrest.CoreMatchers.*;
88
import static org.junit.Assert.assertThat;
99

10-
public class AvroIgnoreTest extends InteropTestBase {
10+
public class AvroIgnoreTest extends InteropTestBase
11+
{
12+
static class RecordWithIgnoredField {
13+
public RecordWithIgnoredField() {}
14+
15+
@AvroIgnore
16+
public String ignoredField;
17+
public String notIgnoredField;
1118

19+
}
1220

1321
@Test
1422
public void testFieldIgnored() {
15-
1623
RecordWithIgnoredField r = new RecordWithIgnoredField();
1724
r.ignoredField = "fail";
1825
r.notIgnoredField = "success";
1926

2027
RecordWithIgnoredField processedR = roundTrip(r);
21-
2228
assertThat(processedR, is(not(nullValue())));
23-
2429
assertThat(processedR.ignoredField, is(nullValue()));
25-
2630
assertThat(processedR.notIgnoredField, is(equalTo("success")));
27-
28-
29-
}
30-
31-
public static class RecordWithIgnoredField {
32-
public RecordWithIgnoredField() {}
33-
34-
@AvroIgnore
35-
public String ignoredField;
36-
public String notIgnoredField;
37-
3831
}
39-
4032
}
Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,51 @@
11
package com.fasterxml.jackson.dataformat.avro.interop.annotations;
22

3+
import com.fasterxml.jackson.dataformat.avro.AvroTestBase;
34
import com.fasterxml.jackson.dataformat.avro.interop.InteropTestBase;
4-
import lombok.Data;
5+
56
import org.apache.avro.reflect.AvroName;
7+
68
import org.junit.Test;
79

810
import static org.assertj.core.api.Assertions.assertThat;
11+
import static org.junit.Assert.fail;
912

1013
/**
1114
* Tests the {@link AvroName @AvroName} annotation
1215
*/
13-
public class AvroNameTest extends InteropTestBase {
14-
15-
@Data
16+
public class AvroNameTest extends InteropTestBase
17+
{
1618
public static class RecordWithRenamed {
1719
@AvroName("newName")
18-
private String someField;
20+
public String someField;
21+
22+
@Override
23+
public boolean equals(Object o) {
24+
return ((RecordWithRenamed) o).someField.equals(someField);
25+
}
1926
}
2027

21-
@Data
2228
public static class RecordWithNameCollision {
2329
@AvroName("otherField")
24-
private String firstField;
30+
public String firstField;
2531

26-
private String otherField;
32+
public String otherField;
2733
}
2834

2935
@Test
3036
public void testRecordWithRenamedField() {
3137
RecordWithRenamed original = new RecordWithRenamed();
32-
original.setSomeField("blah");
33-
//
38+
original.someField = "blah";
3439
RecordWithRenamed result = roundTrip(original);
35-
//
36-
assertThat(result).isEqualTo(result);
40+
assertThat(result).isEqualTo(original);
3741
}
3842

39-
@Test(expected = Exception.class)
4043
public void testRecordWithNameCollision() {
41-
schemaFunctor.apply(RecordWithNameCollision.class);
42-
// Should throw because of name collision
44+
try {
45+
schemaFunctor.apply(RecordWithNameCollision.class);
46+
fail("Should not pass");
47+
} catch (IllegalArgumentException e) {
48+
AvroTestBase.verifyException(e, "foobar");
49+
}
4350
}
44-
4551
}

0 commit comments

Comments
 (0)