aboutsummaryrefslogtreecommitdiffstats
path: root/java
diff options
context:
space:
mode:
authorMax Cai <maxtroy@google.com>2013-10-09 13:36:23 +0100
committerMax Cai <maxtroy@google.com>2013-12-10 16:46:22 +0000
commit5cc242074f189837b38e7768b57ccfb0bca258df (patch)
tree186c81cc5282eb6ae8056ad478692e3a24188e33 /java
parentcea499acf68b35921b956785c26c0e6f18c241c1 (diff)
downloadexternal_protobuf-5cc242074f189837b38e7768b57ccfb0bca258df.zip
external_protobuf-5cc242074f189837b38e7768b57ccfb0bca258df.tar.gz
external_protobuf-5cc242074f189837b38e7768b57ccfb0bca258df.tar.bz2
Avoid class initializers to help ProGuard.
Class initializers prevent ProGuard from inlining any methods because it thinks the class initializer may have side effects. This is true for static methods, but instance methods can still be inlined, because to have an instance you will have touched the class and any class initializers would have run. But ProGuard only starts inlining instance methods of classes with class initializers from v4.11b6, and Android uses v4.4 now. This change tries to avoid the class initializers as much as possible, by delaying the initialization of the empty array and some fields' saved defaults until when they're needed. However, if the message hosts any extensions, they must be public static final and therefore introducing the class initializer. In that case we won't bother with lazy initialization. Change-Id: I00d8296f6eb0023112b93ee135cdb28dbd52b0b8
Diffstat (limited to 'java')
-rw-r--r--java/src/main/java/com/google/protobuf/nano/InternalNano.java12
-rw-r--r--java/src/main/java/com/google/protobuf/nano/MessageNanoPrinter.java35
2 files changed, 29 insertions, 18 deletions
diff --git a/java/src/main/java/com/google/protobuf/nano/InternalNano.java b/java/src/main/java/com/google/protobuf/nano/InternalNano.java
index ce4a206..90ca11d 100644
--- a/java/src/main/java/com/google/protobuf/nano/InternalNano.java
+++ b/java/src/main/java/com/google/protobuf/nano/InternalNano.java
@@ -46,6 +46,18 @@ public final class InternalNano {
private InternalNano() {}
/**
+ * An object to provide synchronization when lazily initializing static fields
+ * of {@link MessageNano} subclasses.
+ * <p>
+ * To enable earlier versions of ProGuard to inline short methods from a
+ * generated MessageNano subclass to the call sites, that class must not have
+ * a class initializer, which will be created if there is any static variable
+ * initializers. To lazily initialize the static variables in a thread-safe
+ * manner, the initialization code will synchronize on this object.
+ */
+ public static final Object LAZY_INIT_LOCK = new Object();
+
+ /**
* Helper called by generated code to construct default values for string
* fields.
* <p>
diff --git a/java/src/main/java/com/google/protobuf/nano/MessageNanoPrinter.java b/java/src/main/java/com/google/protobuf/nano/MessageNanoPrinter.java
index d135a51..c4e123c 100644
--- a/java/src/main/java/com/google/protobuf/nano/MessageNanoPrinter.java
+++ b/java/src/main/java/com/google/protobuf/nano/MessageNanoPrinter.java
@@ -62,7 +62,7 @@ public final class MessageNanoPrinter {
StringBuffer buf = new StringBuffer();
try {
- print(null, message.getClass(), message, new StringBuffer(), buf);
+ print(null, message, new StringBuffer(), buf);
} catch (IllegalAccessException e) {
return "Error printing proto: " + e.getMessage();
}
@@ -70,33 +70,32 @@ public final class MessageNanoPrinter {
}
/**
- * Function that will print the given message/class into the StringBuffer.
+ * Function that will print the given message/field into the StringBuffer.
* Meant to be called recursively.
*
* @param identifier the identifier to use, or {@code null} if this is the root message to
* print.
- * @param clazz the class of {@code message}.
- * @param message the value to print. May in fact be a primitive value or byte array and not a
+ * @param object the value to print. May in fact be a primitive value or byte array and not a
* message.
* @param indentBuf the indentation each line should begin with.
* @param buf the output buffer.
*/
- private static void print(String identifier, Class<?> clazz, Object message,
+ private static void print(String identifier, Object object,
StringBuffer indentBuf, StringBuffer buf) throws IllegalAccessException {
- if (message == null) {
+ if (object == null) {
// This can happen if...
// - we're about to print a message, String, or byte[], but it not present;
// - we're about to print a primitive, but "reftype" optional style is enabled, and
// the field is unset.
// In both cases the appropriate behavior is to output nothing.
- } else if (MessageNano.class.isAssignableFrom(clazz)) { // Nano proto message
+ } else if (object instanceof MessageNano) { // Nano proto message
int origIndentBufLength = indentBuf.length();
if (identifier != null) {
buf.append(indentBuf).append(deCamelCaseify(identifier)).append(" <\n");
indentBuf.append(INDENT);
}
- for (Field field : clazz.getFields()) {
+ for (Field field : object.getClass().getFields()) {
// Proto fields are public, non-static variables that do not begin or end with '_'
int modifiers = field.getModifiers();
String fieldName = field.getName();
@@ -106,24 +105,24 @@ public final class MessageNanoPrinter {
continue;
}
- Class <?> fieldType = field.getType();
- Object value = field.get(message);
+ Class<?> fieldType = field.getType();
+ Object value = field.get(object);
if (fieldType.isArray()) {
Class<?> arrayType = fieldType.getComponentType();
// bytes is special since it's not repeated, but is represented by an array
if (arrayType == byte.class) {
- print(fieldName, fieldType, value, indentBuf, buf);
+ print(fieldName, value, indentBuf, buf);
} else {
int len = value == null ? 0 : Array.getLength(value);
for (int i = 0; i < len; i++) {
Object elem = Array.get(value, i);
- print(fieldName, arrayType, elem, indentBuf, buf);
+ print(fieldName, elem, indentBuf, buf);
}
}
} else {
- print(fieldName, fieldType, value, indentBuf, buf);
+ print(fieldName, value, indentBuf, buf);
}
}
if (identifier != null) {
@@ -134,13 +133,13 @@ public final class MessageNanoPrinter {
// Non-null primitive value
identifier = deCamelCaseify(identifier);
buf.append(indentBuf).append(identifier).append(": ");
- if (message instanceof String) {
- String stringMessage = sanitizeString((String) message);
+ if (object instanceof String) {
+ String stringMessage = sanitizeString((String) object);
buf.append("\"").append(stringMessage).append("\"");
- } else if (message instanceof byte[]) {
- appendQuotedBytes((byte[]) message, buf);
+ } else if (object instanceof byte[]) {
+ appendQuotedBytes((byte[]) object, buf);
} else {
- buf.append(message);
+ buf.append(object);
}
buf.append("\n");
}