Skip to content

Commit

Permalink
Merge pull request #81 from garena/master
Browse files Browse the repository at this point in the history
Add Additional statements for exporting individual Enum Values See #74
  • Loading branch information
JakeWharton committed Jan 15, 2014
2 parents 4ed5f27 + 45dd2bc commit 8c072e8
Show file tree
Hide file tree
Showing 13 changed files with 173 additions and 85 deletions.
26 changes: 22 additions & 4 deletions wire-compiler/src/main/java/com/squareup/wire/MessageWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,30 @@ public void emitType(Type type, String currentType, Map<String, ?> optionsMap, b
writer.endType();
} else if (type instanceof EnumType) {
EnumType enumType = (EnumType) type;
writer.beginType(enumType.getName(), "enum", EnumSet.of(PUBLIC));
for (EnumType.Value value : enumType.getValues()) {
writer.beginType(enumType.getName(), "enum", EnumSet.of(PUBLIC), null, "ProtoEnum");
List<EnumType.Value> values = enumType.getValues();
for (int i = 0, count = values.size(); i < count; i++) {
EnumType.Value value = values.get(i);
MessageWriter.emitDocumentation(writer, value.getDocumentation());
writer.emitAnnotation(ProtoEnum.class, value.getTag());
writer.emitEnumValue(value.getName());
writer.emitEnumValue(value.getName() + "(" + value.getTag() + ")", (i == count - 1));
}

// Output Private tag field
writer.emitEmptyLine();
writer.emitField("int", "value", EnumSet.of(PRIVATE, FINAL));
writer.emitEmptyLine();

// Private Constructor
writer.beginConstructor(EnumSet.of(PRIVATE), "int", "value");
writer.emitStatement("this.value = value");
writer.endConstructor();
writer.emitEmptyLine();

// Public Getter
writer.emitAnnotation(Override.class);
writer.beginMethod("int", "getValue", EnumSet.of(PUBLIC));
writer.emitStatement("return value");
writer.endMethod();
writer.endType();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1211,9 +1211,20 @@ public AllTypes build() {
}
}

public enum NestedEnum {
@ProtoEnum(1)
A,
public enum NestedEnum
implements ProtoEnum {
A(1);

private final int value;

private NestedEnum(int value) {
this.value = value;
}

@Override
public int getValue() {
return value;
}
}

public static final class NestedMessage extends Message {
Expand Down
19 changes: 4 additions & 15 deletions wire-runtime/src/main/java/com/squareup/wire/EnumAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,21 @@
*/
package com.squareup.wire;

import java.lang.reflect.Field;
import java.util.LinkedHashMap;
import java.util.Map;

/**
* Converts values of an enum to and from integers using {@link ProtoEnum}
* annotations.
* Converts values of an enum to and from integers.
*/
final class EnumAdapter<E extends Enum> {
private final Map<Integer, E> fromInt = new LinkedHashMap<Integer, E>();
private final Map<E, Integer> toInt = new LinkedHashMap<E, Integer>();

EnumAdapter(Class<E> type) {
// Record values for each constant annotated with '@ProtoEnum'.
for (E value : type.getEnumConstants()) {
try {
Field f = type.getField(value.name());
if (f.isAnnotationPresent(ProtoEnum.class)) {
ProtoEnum annotation = f.getAnnotation(ProtoEnum.class);
int tag = annotation.value();
fromInt.put(tag, value);
toInt.put(value, tag);
}
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
}
int tag = ((ProtoEnum) value).getValue();
fromInt.put(tag, value);
toInt.put(value, tag);
}
}

Expand Down
6 changes: 3 additions & 3 deletions wire-runtime/src/main/java/com/squareup/wire/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ protected static <T> List<T> immutableCopyOf(List<T> source) {

/**
* Returns the integer value tagged associated with the given enum instance.
* If the enum value is not annotated with a {@link ProtoEnum} annotation, an exception
* If the enum instance is not initialized with an integer tag value, an exception
* will be thrown.
*
* @param <E> the enum class type
Expand All @@ -189,8 +189,8 @@ public static <E extends Enum> int intFromEnum(E value) {

/**
* Returns the enumerated value tagged with the given integer value for the
* given enum class. If no enum value in the given class is annotated with a {@link ProtoEnum}
* annotation having the given value, null is returned.
* given enum class. If no enum value in the given class is initialized
* with the given integer tag value, an exception will be thrown.
*
* @param <E> the enum class type
*/
Expand Down
18 changes: 6 additions & 12 deletions wire-runtime/src/main/java/com/squareup/wire/ProtoEnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,13 @@
*/
package com.squareup.wire;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


/**
* Annotates generated {@link Enum} values with metadata for serialization and
* Interface for generated {@link Enum} values to help serialization and
* deserialization.
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ProtoEnum {
/** The value of the enum constant, as declared in the .proto source file. */
int value();
public interface ProtoEnum {
/**
* The tag value of an enum constant.
*/
int getValue();
}
23 changes: 16 additions & 7 deletions wire-runtime/src/test/java/com/google/protobuf/FieldOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,24 @@ public FieldOptions build() {
}
}

public enum CType {
public enum CType
implements ProtoEnum {
/**
* Default mode.
*/
@ProtoEnum(0)
STRING,
@ProtoEnum(1)
CORD,
@ProtoEnum(2)
STRING_PIECE,
STRING(0),
CORD(1),
STRING_PIECE(2);

private final int value;

private CType(int value) {
this.value = value;
}

@Override
public int getValue() {
return value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1212,9 +1212,20 @@ public AllTypes build() {
}
}

public enum NestedEnum {
@ProtoEnum(1)
A,
public enum NestedEnum
implements ProtoEnum {
A(1);

private final int value;

private NestedEnum(int value) {
this.value = value;
}

@Override
public int getValue() {
return value;
}
}

public static final class NestedMessage extends Message {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,21 @@ public Nested build() {
}
}

public enum FooBarBazEnum {
@ProtoEnum(1)
FOO,
@ProtoEnum(2)
BAR,
@ProtoEnum(3)
BAZ,
public enum FooBarBazEnum
implements ProtoEnum {
FOO(1),
BAR(2),
BAZ(3);

private final int value;

private FooBarBazEnum(int value) {
this.value = value;
}

@Override
public int getValue() {
return value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,21 @@ public final class FooBar extends ExtendableMessage<FooBar> {
}
}

public enum FooBarBazEnum {
@ProtoEnum(1)
FOO,
@ProtoEnum(2)
BAR,
@ProtoEnum(3)
BAZ,
public enum FooBarBazEnum
implements ProtoEnum {
FOO(1),
BAR(2),
BAZ(3);

private final int value;

private FooBarBazEnum(int value) {
this.value = value;
}

@Override
public int getValue() {
return value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,19 @@

import com.squareup.wire.ProtoEnum;

public enum ForeignEnum {
@ProtoEnum(0)
BAV,
@ProtoEnum(1)
BAX,
public enum ForeignEnum
implements ProtoEnum {
BAV(0),
BAX(1);

private final int value;

private ForeignEnum(int value) {
this.value = value;
}

@Override
public int getValue() {
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,25 @@ public Person build() {
}
}

public enum PhoneType {
@ProtoEnum(0)
MOBILE,
@ProtoEnum(1)
HOME,
public enum PhoneType
implements ProtoEnum {
MOBILE(0),
HOME(1),
/**
* Could be phone or fax.
*/
@ProtoEnum(2)
WORK,
WORK(2);

private final int value;

private PhoneType(int value) {
this.value = value;
}

@Override
public int getValue() {
return value;
}
}

public static final class PhoneNumber extends Message {
Expand Down
20 changes: 15 additions & 5 deletions wire-runtime/src/test/java/com/squareup/wire/protos/roots/G.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,19 @@

import com.squareup.wire.ProtoEnum;

public enum G {
@ProtoEnum(1)
FOO,
@ProtoEnum(2)
BAR,
public enum G
implements ProtoEnum {
FOO(1),
BAR(2);

private final int value;

private G(int value) {
this.value = value;
}

@Override
public int getValue() {
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -353,12 +353,21 @@ public NestedMessage build() {
}
}

public enum NestedEnum {
@ProtoEnum(1)
FOO,
@ProtoEnum(2)
BAR,
@ProtoEnum(3)
BAZ,
public enum NestedEnum
implements ProtoEnum {
FOO(1),
BAR(2),
BAZ(3);

private final int value;

private NestedEnum(int value) {
this.value = value;
}

@Override
public int getValue() {
return value;
}
}
}

0 comments on commit 8c072e8

Please sign in to comment.