Skip to content

Commit 38a9946

Browse files
shangm2arhimondr
authored andcommitted
Add support for adding custom codecs with pom file
1 parent 12c6572 commit 38a9946

File tree

12 files changed

+184
-12
lines changed

12 files changed

+184
-12
lines changed

drift-codec-utils/src/main/java/com/facebook/drift/codec/utils/DataSizeToBytesThriftCodec.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.facebook.drift.codec.utils;
1717

18+
import com.facebook.drift.codec.CodecThriftType;
1819
import com.facebook.drift.codec.ThriftCodec;
1920
import com.facebook.drift.codec.internal.coercion.FromThrift;
2021
import com.facebook.drift.codec.internal.coercion.ToThrift;
@@ -32,16 +33,24 @@
3233
public class DataSizeToBytesThriftCodec
3334
implements ThriftCodec<DataSize>
3435
{
36+
private static final ThriftType THRIFT_TYPE = new ThriftType(ThriftType.DOUBLE, DataSize.class);
37+
3538
@Inject
3639
public DataSizeToBytesThriftCodec(ThriftCatalog thriftCatalog)
3740
{
3841
thriftCatalog.addDefaultCoercions(getClass());
3942
}
4043

44+
@CodecThriftType
45+
public static ThriftType getThriftType()
46+
{
47+
return THRIFT_TYPE;
48+
}
49+
4150
@Override
4251
public ThriftType getType()
4352
{
44-
return new ThriftType(ThriftType.DOUBLE, DataSize.class);
53+
return THRIFT_TYPE;
4554
}
4655

4756
@Override

drift-codec-utils/src/main/java/com/facebook/drift/codec/utils/DurationToMillisThriftCodec.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.facebook.drift.codec.utils;
1717

18+
import com.facebook.drift.codec.CodecThriftType;
1819
import com.facebook.drift.codec.ThriftCodec;
1920
import com.facebook.drift.codec.internal.coercion.FromThrift;
2021
import com.facebook.drift.codec.internal.coercion.ToThrift;
@@ -32,16 +33,24 @@
3233
public class DurationToMillisThriftCodec
3334
implements ThriftCodec<Duration>
3435
{
36+
private static final ThriftType THRIFT_TYPE = new ThriftType(ThriftType.DOUBLE, Duration.class);
37+
3538
@Inject
3639
public DurationToMillisThriftCodec(ThriftCatalog thriftCatalog)
3740
{
3841
thriftCatalog.addDefaultCoercions(getClass());
3942
}
4043

44+
@CodecThriftType
45+
public static ThriftType getThriftType()
46+
{
47+
return THRIFT_TYPE;
48+
}
49+
4150
@Override
4251
public ThriftType getType()
4352
{
44-
return new ThriftType(ThriftType.DOUBLE, Duration.class);
53+
return THRIFT_TYPE;
4554
}
4655

4756
@Override

drift-codec-utils/src/main/java/com/facebook/drift/codec/utils/JodaDateTimeToEpochMillisThriftCodec.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.facebook.drift.codec.utils;
1717

18+
import com.facebook.drift.codec.CodecThriftType;
1819
import com.facebook.drift.codec.ThriftCodec;
1920
import com.facebook.drift.codec.internal.coercion.FromThrift;
2021
import com.facebook.drift.codec.internal.coercion.ToThrift;
@@ -31,16 +32,24 @@
3132
public class JodaDateTimeToEpochMillisThriftCodec
3233
implements ThriftCodec<DateTime>
3334
{
35+
private static final ThriftType THRIFT_TYPE = new ThriftType(ThriftType.I64, DateTime.class);
36+
3437
@Inject
3538
public JodaDateTimeToEpochMillisThriftCodec(ThriftCatalog thriftCatalog)
3639
{
3740
thriftCatalog.addDefaultCoercions(getClass());
3841
}
3942

43+
@CodecThriftType
44+
public static ThriftType getThriftType()
45+
{
46+
return THRIFT_TYPE;
47+
}
48+
4049
@Override
4150
public ThriftType getType()
4251
{
43-
return new ThriftType(ThriftType.I64, DateTime.class);
52+
return THRIFT_TYPE;
4453
}
4554

4655
@Override

drift-codec-utils/src/main/java/com/facebook/drift/codec/utils/LocaleToLanguageTagCodec.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.facebook.drift.codec.utils;
1717

18+
import com.facebook.drift.codec.CodecThriftType;
1819
import com.facebook.drift.codec.ThriftCodec;
1920
import com.facebook.drift.codec.internal.coercion.FromThrift;
2021
import com.facebook.drift.codec.internal.coercion.ToThrift;
@@ -32,16 +33,24 @@
3233
public class LocaleToLanguageTagCodec
3334
implements ThriftCodec<Locale>
3435
{
36+
private static final ThriftType THRIFT_TYPE = new ThriftType(ThriftType.STRING, Locale.class);
37+
3538
@Inject
3639
public LocaleToLanguageTagCodec(ThriftCatalog thriftCatalog)
3740
{
3841
thriftCatalog.addDefaultCoercions(getClass());
3942
}
4043

44+
@CodecThriftType
45+
public static ThriftType getThriftType()
46+
{
47+
return THRIFT_TYPE;
48+
}
49+
4150
@Override
4251
public ThriftType getType()
4352
{
44-
return new ThriftType(ThriftType.STRING, Locale.class);
53+
return THRIFT_TYPE;
4554
}
4655

4756
@Override
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (C) 2012 Facebook, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.facebook.drift.codec;
17+
18+
import java.lang.annotation.Retention;
19+
import java.lang.annotation.Target;
20+
21+
import static java.lang.annotation.ElementType.METHOD;
22+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
23+
24+
@Retention(RUNTIME)
25+
@Target(METHOD)
26+
public @interface CodecThriftType
27+
{
28+
}

drift-codec/src/main/java/com/facebook/drift/codec/internal/builtin/OptionalDoubleThriftCodec.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.facebook.drift.codec.internal.builtin;
1717

18+
import com.facebook.drift.codec.CodecThriftType;
1819
import com.facebook.drift.codec.ThriftCodec;
1920
import com.facebook.drift.codec.metadata.ThriftType;
2021
import com.facebook.drift.protocol.TProtocolReader;
@@ -30,10 +31,18 @@
3031
public class OptionalDoubleThriftCodec
3132
implements ThriftCodec<OptionalDouble>
3233
{
34+
private static final ThriftType THRIFT_TYPE = new ThriftType(ThriftType.DOUBLE, OptionalDouble.class, OptionalDouble.empty());
35+
36+
@CodecThriftType
37+
public static ThriftType getThriftType()
38+
{
39+
return THRIFT_TYPE;
40+
}
41+
3342
@Override
3443
public ThriftType getType()
3544
{
36-
return new ThriftType(ThriftType.DOUBLE, OptionalDouble.class, OptionalDouble.empty());
45+
return THRIFT_TYPE;
3746
}
3847

3948
@Override

drift-codec/src/main/java/com/facebook/drift/codec/internal/builtin/OptionalIntThriftCodec.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.facebook.drift.codec.internal.builtin;
1717

18+
import com.facebook.drift.codec.CodecThriftType;
1819
import com.facebook.drift.codec.ThriftCodec;
1920
import com.facebook.drift.codec.metadata.ThriftType;
2021
import com.facebook.drift.protocol.TProtocolReader;
@@ -30,10 +31,18 @@
3031
public class OptionalIntThriftCodec
3132
implements ThriftCodec<OptionalInt>
3233
{
34+
private static final ThriftType THRIFT_TYPE = new ThriftType(ThriftType.I32, OptionalInt.class, OptionalInt.empty());
35+
3336
@Override
3437
public ThriftType getType()
3538
{
36-
return new ThriftType(ThriftType.I32, OptionalInt.class, OptionalInt.empty());
39+
return THRIFT_TYPE;
40+
}
41+
42+
@CodecThriftType
43+
public static ThriftType getThriftType()
44+
{
45+
return THRIFT_TYPE;
3746
}
3847

3948
@Override

drift-codec/src/main/java/com/facebook/drift/codec/internal/builtin/OptionalLongThriftCodec.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.facebook.drift.codec.internal.builtin;
1717

18+
import com.facebook.drift.codec.CodecThriftType;
1819
import com.facebook.drift.codec.ThriftCodec;
1920
import com.facebook.drift.codec.metadata.ThriftType;
2021
import com.facebook.drift.protocol.TProtocolReader;
@@ -30,10 +31,18 @@
3031
public class OptionalLongThriftCodec
3132
implements ThriftCodec<OptionalLong>
3233
{
34+
private static final ThriftType THRIFT_TYPE = new ThriftType(ThriftType.I64, OptionalLong.class, OptionalLong.empty());
35+
36+
@CodecThriftType
37+
public static ThriftType getThriftType()
38+
{
39+
return THRIFT_TYPE;
40+
}
41+
3342
@Override
3443
public ThriftType getType()
3544
{
36-
return new ThriftType(ThriftType.I64, OptionalLong.class, OptionalLong.empty());
45+
return THRIFT_TYPE;
3746
}
3847

3948
@Override

drift-codec/src/main/java/com/facebook/drift/codec/metadata/AbstractThriftMetadataBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ protected final Requiredness extractFieldRequiredness(short fieldId, String fiel
704704
}
705705

706706
/**
707-
* Verifies that the the fields all have a supported Java type and that all fields map to the
707+
* Verifies that the fields all have a supported Java type and that all fields map to the
708708
* exact same ThriftType.
709709
*/
710710
protected final void verifyFieldType(short id, String name, Collection<FieldMetadata> fields, ThriftCatalog catalog)

drift-idl-generator/src/main/java/com/facebook/drift/idl/generator/ThriftIdlGenerator.java

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package com.facebook.drift.idl.generator;
1717

1818
import com.facebook.drift.annotations.ThriftService;
19+
import com.facebook.drift.codec.CodecThriftType;
20+
import com.facebook.drift.codec.ThriftCodec;
1921
import com.facebook.drift.codec.ThriftCodecManager;
2022
import com.facebook.drift.codec.ThriftProtocolType;
2123
import com.facebook.drift.codec.metadata.FieldKind;
@@ -36,6 +38,9 @@
3638
import com.google.common.collect.ImmutableSet;
3739
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
3840

41+
import java.lang.reflect.InvocationTargetException;
42+
import java.lang.reflect.Method;
43+
import java.lang.reflect.Modifier;
3944
import java.lang.reflect.Type;
4045
import java.net.URI;
4146
import java.util.ArrayList;
@@ -129,11 +134,65 @@ public ThriftIdlGenerator(ThriftIdlGeneratorConfig config, ClassLoader classLoad
129134

130135
this.namespaces = config.getNamespaces();
131136
this.recursive = config.isRecursive();
137+
138+
for (String customCodecClassName : config.getCustomCodecs()) {
139+
loadCustomCodec(customCodecClassName);
140+
}
141+
}
142+
143+
private void loadCustomCodec(String customCodecClassName)
144+
{
145+
Class<?> codecClass = load(customCodecClassName);
146+
if (codecClass == null) {
147+
throw new ThriftIdlGeneratorException("Failed to load codec class: " + customCodecClassName);
148+
}
149+
if (!ThriftCodec.class.isAssignableFrom(codecClass)) {
150+
throw new ThriftIdlGeneratorException("Class " + customCodecClassName + " does not implement ThriftCodec");
151+
}
152+
153+
// Look for a static method annotated with @CodecThriftType
154+
ThriftType thriftType = null;
155+
for (Method method : codecClass.getDeclaredMethods()) {
156+
if (method.isAnnotationPresent(CodecThriftType.class)) {
157+
if (!Modifier.isPublic(method.getModifiers())) {
158+
throw new ThriftIdlGeneratorException("Method annotated with @CodecThriftType must be public: " + customCodecClassName + "#" + method.getName());
159+
}
160+
161+
if (!Modifier.isStatic(method.getModifiers())) {
162+
throw new ThriftIdlGeneratorException("Method annotated with @CodecThriftType must be static: " + customCodecClassName + "#" + method.getName());
163+
}
164+
165+
if (method.getParameterCount() != 0) {
166+
throw new ThriftIdlGeneratorException("Method annotated with @CodecThriftType must have no parameters: " + customCodecClassName + "#" + method.getName());
167+
}
168+
169+
if (!ThriftType.class.isAssignableFrom(method.getReturnType())) {
170+
throw new ThriftIdlGeneratorException("Method annotated with @CodecThriftType must return ThriftType: " + customCodecClassName + "#" + method.getName());
171+
}
172+
173+
try {
174+
thriftType = (ThriftType) method.invoke(null);
175+
if (thriftType == null) {
176+
throw new ThriftIdlGeneratorException("Method annotated with @CodecThriftType returns null: " + customCodecClassName + "#" + method.getName());
177+
}
178+
break;
179+
}
180+
catch (IllegalAccessException | InvocationTargetException e) {
181+
throw new ThriftIdlGeneratorException("Method annotated with @CodecThriftType can not be invoked: " + customCodecClassName + "#" + method.getName());
182+
}
183+
}
184+
}
185+
if (thriftType == null) {
186+
throw new ThriftIdlGeneratorException("Codec must have a public static method annotated with @CodecThriftType and return ThriftType: " + customCodecClassName);
187+
}
188+
189+
addCustomType(thriftType);
190+
catalog.addThriftType(thriftType);
132191
}
133192

134-
public void addCustomType(ThriftType... type)
193+
public void addCustomType(ThriftType... types)
135194
{
136-
for (ThriftType t : type) {
195+
for (ThriftType t : types) {
137196
customTypes.add(t);
138197
knownTypes.add(t);
139198
}

0 commit comments

Comments
 (0)