Skip to content

Commit

Permalink
@AutoCodec: proper handling of checked exceptions thrown by construct…
Browse files Browse the repository at this point in the history
…ors.

PiperOrigin-RevId: 180697983
  • Loading branch information
aoeui authored and Copybara-Service committed Jan 3, 2018
1 parent d2acedc commit e981d20
Showing 1 changed file with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.devtools.build.lib.skyframe.serialization.ObjectCodec;
import com.google.devtools.build.lib.skyframe.serialization.SerializationException;
import com.google.devtools.build.lib.skyframe.serialization.strings.StringCodecs;
import com.google.protobuf.CodedInputStream;
import com.google.protobuf.CodedOutputStream;
Expand Down Expand Up @@ -160,7 +161,7 @@ private void buildClassWithConstructorStrategy(
MethodSpec.Builder deserializeBuilder =
AutoCodecUtil.initializeDeserializeMethodBuilder(encodedType);
buildDeserializeBody(deserializeBuilder, constructorParameters);
addReturnNew(deserializeBuilder, encodedType, constructorParameters);
addReturnNew(deserializeBuilder, encodedType, constructor, constructorParameters);
codecClassBuilder.addMethod(deserializeBuilder.build());
}

Expand Down Expand Up @@ -289,11 +290,28 @@ private void buildDeserializeBody(
* <p>Used by the {@link AutoCodec.Strategy.CONSTRUCTOR} strategy.
*/
private static void addReturnNew(
MethodSpec.Builder builder, TypeElement type, List<? extends VariableElement> parameters) {
MethodSpec.Builder builder,
TypeElement type,
ExecutableElement constructor,
List<? extends VariableElement> parameters) {
List<? extends TypeMirror> allThrown = constructor.getThrownTypes();
if (!allThrown.isEmpty()) {
builder.beginControlFlow("try");
}
builder.addStatement(
"return new $T($L)",
TypeName.get(type.asType()),
parameters.stream().map(p -> p.getSimpleName() + "_").collect(Collectors.joining(", ")));
if (!allThrown.isEmpty()) {
for (TypeMirror thrown : allThrown) {
builder.nextControlFlow("catch ($T e)", TypeName.get(thrown));
builder.addStatement(
"throw new $T(\"$L constructor threw an exception\", e)",
SerializationException.class,
type.getQualifiedName());
}
builder.endControlFlow();
}
}

/**
Expand Down Expand Up @@ -399,7 +417,8 @@ private static MethodSpec buildPolymorphicSerializeMethod(TypeElement encodedTyp
NoSuchMethodException.class,
IllegalAccessException.class,
InvocationTargetException.class);
builder.addStatement("throw new SerializationException(input.getClass().getName(), e)");
builder.addStatement(
"throw new $T(input.getClass().getName(), e)", SerializationException.class);
builder.endControlFlow();
builder.nextControlFlow("else");
builder.addStatement("codedOut.writeBoolNoTag(false)");
Expand Down Expand Up @@ -430,7 +449,7 @@ private static MethodSpec buildPolymorphicDeserializeMethod(TypeElement encodedT
NoSuchMethodException.class,
IllegalAccessException.class,
InvocationTargetException.class);
builder.addStatement("throw new SerializationException(className, e)");
builder.addStatement("throw new $T(className, e)", SerializationException.class);
builder.endControlFlow();
builder.endControlFlow();
builder.addStatement("return deserialized");
Expand Down

0 comments on commit e981d20

Please sign in to comment.