Skip to content

Commit

Permalink
feat(android): add @NonNull annotation to serialization package method
Browse files Browse the repository at this point in the history
  • Loading branch information
medns authored and siguangli committed May 25, 2021
1 parent 242d009 commit 7009ed2
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.tencent.mtt.hippy.serialization;

import androidx.annotation.NonNull;

import com.tencent.mtt.hippy.serialization.utils.IntegerPolyfill;
import com.tencent.mtt.hippy.serialization.nio.writer.BinaryWriter;

Expand Down Expand Up @@ -287,7 +289,7 @@ public void writeDouble(double value) {
* is the easiest/best/most correct way to iterate through the characters of a string in
* Java?</a>
*/
protected void writeString(String value) {
protected void writeString(@NonNull String value) {
int length = value.length();
if (length > SSO_SMALL_STRING_MAX_LENGTH) {
if (stringWriteBuffer == null || stringWriteBuffer.length < length) {
Expand Down Expand Up @@ -334,7 +336,7 @@ protected void writeString(String value) {
// endregion
}

protected void writeBigIntContents(BigInteger bigInteger) {
protected void writeBigIntContents(@NonNull BigInteger bigInteger) {
boolean negative = bigInteger.signum() == -1;
if (negative) {
bigInteger = bigInteger.negate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.tencent.mtt.hippy.serialization.compatible;

import androidx.annotation.NonNull;

import com.tencent.mtt.hippy.common.ConstantValue;
import com.tencent.mtt.hippy.common.HippyArray;
import com.tencent.mtt.hippy.common.HippyMap;
Expand Down Expand Up @@ -70,7 +72,7 @@ public boolean writeValue(Object object) {
return true;
}

private void writeJSObject(HippyMap value) {
private void writeJSObject(@NonNull HippyMap value) {
writeTag(SerializationTag.BEGIN_JS_OBJECT);
Set<String> keys = value.keySet();
for (String key : keys) {
Expand All @@ -85,7 +87,7 @@ private void writeJSObject(HippyMap value) {
writer.putVarint(keys.size());
}

private void writeJSArray(HippyArray value) {
private void writeJSArray(@NonNull HippyArray value) {
long length = value.size();
writeTag(SerializationTag.BEGIN_DENSE_JS_ARRAY);
writer.putVarint(length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ public class DataCloneException extends RuntimeException {

@SuppressWarnings("unused")
public DataCloneException(Object received) {
super(String.format("[%s] could not be cloned", received.toString()));
super(String.format("[%s] could not be cloned", received != null ? received.toString() : "null"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.tencent.mtt.hippy.serialization.nio.reader;

import androidx.annotation.NonNull;

import java.nio.ByteBuffer;

@SuppressWarnings({"unused"})
Expand Down Expand Up @@ -93,7 +95,7 @@ public long readInt64() {
}

@Override
public SafeDirectReader reset(ByteBuffer byteBuffer) {
public SafeDirectReader reset(@NonNull ByteBuffer byteBuffer) {
buffer = byteBuffer;
base = byteBuffer.position();
count = byteBuffer.limit() - byteBuffer.position();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.tencent.mtt.hippy.serialization.nio.reader;

import androidx.annotation.NonNull;

import java.nio.ByteBuffer;

@SuppressWarnings({"unused"})
Expand Down Expand Up @@ -80,6 +82,7 @@ public double getDouble() {
return Double.longBitsToDouble(readInt64());
}

@SuppressWarnings("SpellCheckingInspection")
@Override
public long getVarint() {
long value = 0;
Expand Down Expand Up @@ -113,7 +116,7 @@ public long readInt64() {
}

@Override
public SafeHeapReader reset(ByteBuffer byteBuffer) {
public SafeHeapReader reset(@NonNull ByteBuffer byteBuffer) {
buffer = byteBuffer.array();
base = byteBuffer.arrayOffset() + byteBuffer.position();
count = byteBuffer.limit() - byteBuffer.position();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public void putDouble(double d) {
value.putDouble(d);
}

@SuppressWarnings("SpellCheckingInspection")
@Override
public int putVarint(long l) {
if (value.position() + 10 > value.capacity()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public void putDouble(double d) {
putInt64(Double.doubleToRawLongBits(d));
}

@SuppressWarnings("SpellCheckingInspection")
@Override
public int putVarint(long l) {
if (count + 10 > value.length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.tencent.mtt.hippy.serialization.recommend;

import androidx.annotation.NonNull;

import com.tencent.mtt.hippy.serialization.exception.DataCloneOutOfRangeException;
import com.tencent.mtt.hippy.serialization.exception.DataCloneOutOfValueException;
import com.tencent.mtt.hippy.exception.UnexpectedException;
Expand Down Expand Up @@ -190,7 +192,7 @@ protected JSObject readJSObject() {
return object;
}

private int readJSProperties(JSObject object, SerializationTag endTag) {
private int readJSProperties(@NonNull JSObject object, SerializationTag endTag) {
final StringLocation keyLocation, valueLocation;
switch (endTag) {
case END_DENSE_JS_ARRAY: {
Expand Down Expand Up @@ -454,7 +456,7 @@ protected Object readHostObject() {
* @param transferId transfer id
* @param arrayBuffer JSArrayBuffer
*/
public void transferArrayBuffer(int transferId, JSArrayBuffer arrayBuffer) {
public void transferArrayBuffer(int transferId, @NonNull JSArrayBuffer arrayBuffer) {
if (arrayBufferTransferMap == null) {
arrayBufferTransferMap = new HashMap<>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import android.util.Pair;

import androidx.annotation.NonNull;

import com.tencent.mtt.hippy.exception.UnreachableCodeException;
import com.tencent.mtt.hippy.runtime.builtins.JSRegExp;
import com.tencent.mtt.hippy.runtime.builtins.JSValue;
Expand Down Expand Up @@ -218,35 +220,35 @@ public boolean writeValue(Object object) {
return true;
}

private void writeDate(Date date) {
private void writeDate(@NonNull Date date) {
writer.putDouble(date.getTime());
}

private void writeJSBoolean(JSBooleanObject value) {
private void writeJSBoolean(@NonNull JSBooleanObject value) {
writeTag(value.isTrue() ? SerializationTag.TRUE_OBJECT : SerializationTag.FALSE_OBJECT);
}

private void writeJSBigIntContents(JSBigintObject value) {
private void writeJSBigIntContents(@NonNull JSBigintObject value) {
writeBigIntContents(value.getValue());
}

private void writeJSNumber(JSNumberObject value) {
private void writeJSNumber(@NonNull JSNumberObject value) {
writeTag(SerializationTag.NUMBER_OBJECT);
writeDouble(value.getValue().doubleValue());
}

private void writeJSString(JSStringObject value) {
private void writeJSString(@NonNull JSStringObject value) {
writeTag(SerializationTag.STRING_OBJECT);
writeString(value.getValue().toString());
}

private void writeJSRegExp(JSRegExp value) {
private void writeJSRegExp(@NonNull JSRegExp value) {
writeTag(SerializationTag.REGEXP);
writeString(value.getSource());
writer.putVarint(value.getFlags());
}

private void writeJSArrayBuffer(JSArrayBuffer value) {
private void writeJSArrayBuffer(@NonNull JSArrayBuffer value) {
if (arrayBufferTransferMap == null) {
arrayBufferTransferMap = new IdentityHashMap<>();
}
Expand All @@ -266,7 +268,7 @@ private void writeJSArrayBuffer(JSArrayBuffer value) {
}
}

private void writeJSSharedArrayBuffer(JSSharedArrayBuffer value) {
private void writeJSSharedArrayBuffer(@NonNull JSSharedArrayBuffer value) {
if (delegate == null) {
throw new DataCloneException(value);
}
Expand All @@ -282,14 +284,14 @@ private void writeJSObject(JSObject value) {
writer.putVarint(value.size());
}

private void writeJSObjectProperties(Set<Pair<String, Object>> props) {
private void writeJSObjectProperties(@NonNull Set<Pair<String, Object>> props) {
for (Pair<String, Object> prop : props) {
writeString(prop.first);
writeValue(prop.second);
}
}

private void writeJSMap(JSMap value) {
private void writeJSMap(@NonNull JSMap value) {
writeTag(SerializationTag.BEGIN_JS_MAP);
Iterator<Map.Entry<Object, Object>> entries = value.getInternalMap().entrySet().iterator();
int count = 0;
Expand All @@ -303,7 +305,7 @@ private void writeJSMap(JSMap value) {
writer.putVarint(2 * count);
}

private void writeJSSet(JSSet value) {
private void writeJSSet(@NonNull JSSet value) {
writeTag(SerializationTag.BEGIN_JS_SET);
Iterator<Object> entries = value.getInternalSet().iterator();
int count = 0;
Expand All @@ -315,7 +317,7 @@ private void writeJSSet(JSSet value) {
writer.putVarint(count);
}

private void writeJSArray(JSAbstractArray value) {
private void writeJSArray(@NonNull JSAbstractArray value) {
int length = value.size();
if (value.isDenseArray()) {
writeTag(SerializationTag.BEGIN_DENSE_JS_ARRAY);
Expand All @@ -341,7 +343,7 @@ private void writeJSArray(JSAbstractArray value) {
writer.putVarint(length);
}

private void writeJSArrayBufferView(JSDataView<?> value) {
private void writeJSArrayBufferView(@NonNull JSDataView<?> value) {
if (treatArrayBufferViewsAsHostObjects) {
if (!writeHostObject(value)) {
throw new DataCloneException(value);
Expand Down Expand Up @@ -400,7 +402,7 @@ private void writeJSArrayBufferView(JSDataView<?> value) {
}
}

private void writeJSError(JSError error) {
private void writeJSError(@NonNull JSError error) {
writeTag(SerializationTag.ERROR);
writeErrorTypeTag(error);

Expand All @@ -419,7 +421,7 @@ private void writeJSError(JSError error) {
writeTag(ErrorTag.END);
}

private void writeErrorTypeTag(JSError error) {
private void writeErrorTypeTag(@NonNull JSError error) {
JSError.ErrorType errorType = error.getType();
ErrorTag tag;
switch (errorType) {
Expand Down Expand Up @@ -469,7 +471,7 @@ private boolean writeHostObject(Object object) {
* @param transferId transfer id
* @param arrayBuffer JSArrayBuffer
*/
public void transferArrayBuffer(int transferId, JSArrayBuffer arrayBuffer) {
public void transferArrayBuffer(int transferId, @NonNull JSArrayBuffer arrayBuffer) {
if (arrayBufferTransferMap == null) {
arrayBufferTransferMap = new IdentityHashMap<>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.tencent.mtt.hippy.serialization.string;

import androidx.annotation.NonNull;

import com.tencent.mtt.hippy.serialization.StringLocation;

import java.io.UnsupportedEncodingException;
Expand All @@ -27,8 +29,8 @@
public class DirectStringTable implements StringTable {

@Override
public String lookup(ByteBuffer byteBuffer, String encoding, StringLocation location,
Object relatedKey) throws UnsupportedEncodingException {
public String lookup(ByteBuffer byteBuffer, @NonNull String encoding, StringLocation location,
Object relatedKey) throws UnsupportedEncodingException {
if (location == StringLocation.VOID) {
return "";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.tencent.mtt.hippy.serialization.string;

import androidx.annotation.NonNull;;
import android.util.LruCache;

import com.tencent.mtt.hippy.exception.UnreachableCodeException;
Expand Down Expand Up @@ -105,7 +106,7 @@ private static int STRING_HASH(byte[] value, int offset, int length) {
* @param string an string
* @return {@code true} if it's equal, {@code false} otherwise
*/
private boolean equals(byte[] sequence, int offset, int length, String encoding, String string) {
private boolean equals(byte[] sequence, int offset, int length, @NonNull String encoding, @NonNull String string) {
final int bytesPerCharacter = encoding.equals("UTF-16LE") ? 2 : 1;
final int count = string.length();
// fast negative check
Expand All @@ -128,7 +129,7 @@ private boolean equals(byte[] sequence, int offset, int length, String encoding,
// endregion

// region lookup
private String lookupKey(byte[] sequence, int offset, int length, String encoding)
private String lookupKey(byte[] sequence, int offset, int length, @NonNull String encoding)
throws UnsupportedEncodingException {
// only calculate one or two byte encoding
if (length >= MAX_KEY_CALC_LENGTH || encoding.equals("UTF-8")) {
Expand All @@ -146,7 +147,7 @@ private String lookupKey(byte[] sequence, int offset, int length, String encodin
return internalized;
}

private String lookupValue(byte[] sequence, int offset, int length, String encoding,
private String lookupValue(byte[] sequence, int offset, int length, @NonNull String encoding,
Object relatedKey) throws UnsupportedEncodingException {
if (relatedKey instanceof String) {
char[] valuePrefix = cacheablesProperty.get(relatedKey);
Expand Down Expand Up @@ -180,7 +181,7 @@ private String lookupValue(byte[] sequence, int offset, int length, String encod
}

@Override
public String lookup(ByteBuffer byteBuffer, String encoding, StringLocation location,
public String lookup(@NonNull ByteBuffer byteBuffer, @NonNull String encoding, StringLocation location,
Object relatedKey) throws UnsupportedEncodingException {
final byte[] sequence = byteBuffer.array();
final int offset = byteBuffer.arrayOffset() + byteBuffer.position();
Expand Down

0 comments on commit 7009ed2

Please sign in to comment.