1 /* 2 * Copyright (C) 2019 The Dagger Authors. 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 17 package dagger.internal.codegen.serialization; 18 19 import static com.google.common.io.BaseEncoding.base64; 20 21 import com.google.common.io.BaseEncoding; 22 import com.google.protobuf.InvalidProtocolBufferException; 23 import com.google.protobuf.Message; 24 import com.squareup.javapoet.CodeBlock; 25 import javax.lang.model.element.AnnotationValue; 26 import javax.lang.model.element.AnnotationValueVisitor; 27 import javax.lang.model.util.SimpleAnnotationValueVisitor8; 28 29 /** 30 * Serializes and deserializes {@link Message}s using {@link BaseEncoding#base64()} for use in 31 * annotation values. 32 */ 33 public final class ProtoSerialization { 34 /** Returns a {@link CodeBlock} of {@code message} serialized as a String. */ toAnnotationValue(Message message)35 public static CodeBlock toAnnotationValue(Message message) { 36 return CodeBlock.of("$S", base64().encode(message.toByteArray())); 37 } 38 39 /** 40 * Returns a {@link Message T} from the deserialized the String {@code value}. 41 * 42 * @throws IllegalArgumentException if {@code value} represents an {@link AnnotationValue} who's 43 * type is not {@link String} 44 */ fromAnnotationValue( AnnotationValue value, T defaultInstance)45 public static <T extends Message> T fromAnnotationValue( 46 AnnotationValue value, T defaultInstance) { 47 byte[] bytes = base64().decode(value.accept(STRING_VALUE, null)); 48 Message message; 49 try { 50 message = defaultInstance.getParserForType().parseFrom(bytes); 51 } catch (InvalidProtocolBufferException e) { 52 throw new InconsistentSerializedProtoException(e); 53 } 54 @SuppressWarnings("unchecked") // guaranteed by proto API 55 T t = (T) message; 56 return t; 57 } 58 59 private static final AnnotationValueVisitor<String, Void> STRING_VALUE = 60 new SimpleAnnotationValueVisitor8<String, Void>() { 61 @Override 62 public String visitString(String s, Void ignored) { 63 return s; 64 } 65 66 @Override 67 protected String defaultAction(Object o, Void ignored) { 68 throw new IllegalArgumentException(o + " is not a String"); 69 } 70 }; 71 72 /** 73 * An exception thrown when the proto that's serialized in a compiled subcomponent implementation 74 * is from a different version than the current compiler's. 75 */ 76 public static final class InconsistentSerializedProtoException extends RuntimeException { InconsistentSerializedProtoException(Throwable cause)77 InconsistentSerializedProtoException(Throwable cause) { 78 super(cause); 79 } 80 } 81 } 82