Skip to content

Commit

Permalink
more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jubafourali committed Apr 5, 2024
1 parent cdf0cf9 commit 2cc2a43
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 59 deletions.
35 changes: 35 additions & 0 deletions gson/src/main/java/com/google/gson/internal/CurrentWrite.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.google.gson.internal;

public class CurrentWrite implements CharSequence{
private char[] chars;
private String cachedString;

void setChars(char[] chars) {
this.chars = chars;
this.cachedString = null;
}

@Override
public int length() {
return chars.length;
}

@Override
public char charAt(int i) {
return chars[i];
}

@Override
public CharSequence subSequence(int start, int end) {
return new String(chars, start, end - start);
}

// Must return string representation to satisfy toString() contract
@Override
public String toString() {
if (cachedString == null) {
cachedString = new String(chars);
}
return cachedString;
}
}
37 changes: 1 addition & 36 deletions gson/src/main/java/com/google/gson/internal/Streams.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,40 +120,5 @@ public Writer append(CharSequence csq, int start, int end) throws IOException {
appendable.append(csq, start, end);
return this;
}

/** A mutable char sequence pointing at a single char[]. */
private static class CurrentWrite implements CharSequence {
private char[] chars;
private String cachedString;

void setChars(char[] chars) {
this.chars = chars;
this.cachedString = null;
}

@Override
public int length() {
return chars.length;
}

@Override
public char charAt(int i) {
return chars[i];
}

@Override
public CharSequence subSequence(int start, int end) {
return new String(chars, start, end - start);
}

// Must return string representation to satisfy toString() contract
@Override
public String toString() {
if (cachedString == null) {
cachedString = new String(chars);
}
return cachedString;
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.google.gson.internal.bind;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

class BoundFieldCreationContext{
final Gson context;
final Field field;
final Method accessor;
final String serializedName;
final TypeToken<?> fieldType;

BoundFieldCreationContext(
final Gson context,
final Field field,
final Method accessor,
final String serializedName,
final TypeToken<?> fieldType){

this.context = context;
this.field = field;
this.accessor = accessor;
this.serializedName = serializedName;
this.fieldType = fieldType;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,30 +175,26 @@ private static <M extends AccessibleObject & Member> void checkAccessible(
}

private BoundField createBoundField(
final Gson context,
final Field field,
final Method accessor,
final String serializedName,
final TypeToken<?> fieldType,
final BoundFieldCreationContext boundFieldCreationContext,
final boolean serialize,
final boolean blockInaccessible) {

final boolean isPrimitive = Primitives.isPrimitive(fieldType.getRawType());
final boolean isPrimitive = Primitives.isPrimitive(boundFieldCreationContext.fieldType.getRawType());

int modifiers = field.getModifiers();
int modifiers = boundFieldCreationContext.field.getModifiers();
final boolean isStaticFinalField = Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers);

JsonAdapter annotation = field.getAnnotation(JsonAdapter.class);
JsonAdapter annotation = boundFieldCreationContext.field.getAnnotation(JsonAdapter.class);
TypeAdapter<?> mapped = null;
if (annotation != null) {
// This is not safe; requires that user has specified correct adapter class for @JsonAdapter
mapped =
jsonAdapterFactory.getTypeAdapter(
constructorConstructor, context, fieldType, annotation, false);
constructorConstructor, boundFieldCreationContext.context, boundFieldCreationContext.fieldType, annotation, false);
}
final boolean jsonAdapterPresent = mapped != null;
if (mapped == null) {
mapped = context.getAdapter(fieldType);
mapped = boundFieldCreationContext.context.getAdapter(boundFieldCreationContext.fieldType);
}

@SuppressWarnings("unchecked")
Expand All @@ -208,31 +204,31 @@ private BoundField createBoundField(
writeTypeAdapter =
jsonAdapterPresent
? typeAdapter
: new TypeAdapterRuntimeTypeWrapper<>(context, typeAdapter, fieldType.getType());
: new TypeAdapterRuntimeTypeWrapper<>(boundFieldCreationContext.context, typeAdapter, boundFieldCreationContext.fieldType.getType());
} else {
// Will never actually be used, but we set it to avoid confusing nullness-analysis tools
writeTypeAdapter = typeAdapter;
}
return new BoundField(serializedName, field) {
return new BoundField(boundFieldCreationContext.serializedName, boundFieldCreationContext.field) {
@Override
void write(JsonWriter writer, Object source) throws IOException, IllegalAccessException {
if (blockInaccessible) {
if (accessor == null) {
if (boundFieldCreationContext.accessor == null) {
checkAccessible(source, field);
} else {
// Note: This check might actually be redundant because access check for canonical
// constructor should have failed already
checkAccessible(source, accessor);
checkAccessible(source, boundFieldCreationContext.accessor);
}
}

Object fieldValue;
if (accessor != null) {
if (boundFieldCreationContext.accessor != null) {
try {
fieldValue = accessor.invoke(source);
fieldValue = boundFieldCreationContext.accessor.invoke(source);
} catch (InvocationTargetException e) {
String accessorDescription =
ReflectionHelper.getAccessibleObjectDescription(accessor, false);
ReflectionHelper.getAccessibleObjectDescription(boundFieldCreationContext.accessor, false);
throw new JsonIOException(
"Accessor " + accessorDescription + " threw exception", e.getCause());
}
Expand Down Expand Up @@ -391,13 +387,10 @@ private FieldsData getBoundFields(
Type fieldType = $Gson$Types.resolve(type.getType(), raw, field.getGenericType());
List<String> fieldNames = getFieldNames(field);
String serializedName = fieldNames.get(0);
BoundFieldCreationContext boundFieldCreationContext = new BoundFieldCreationContext(context, field, accessor, serializedName, TypeToken.get(fieldType));
BoundField boundField =
createBoundField(
context,
field,
accessor,
serializedName,
TypeToken.get(fieldType),
boundFieldCreationContext,
serialize,
blockInaccessible);

Expand Down Expand Up @@ -666,4 +659,4 @@ T finalize(Object[] accumulator) {
}
}
}
}
}

0 comments on commit 2cc2a43

Please sign in to comment.