Skip to content

Commit 8c23cd3

Browse files
Update sources to satisfy a new Error Prone check. (#2887)
* Update sources to satisfy a new Error Prone check. The latest Error Prone release includes a new check `EffectivelyPrivate`, which flags class members that are public or protected when the enclosing class isn't. This is only a warning in Error Prone, but because we treat warnings as errors it currently breaks the build. Nearly all cases were in private nested classes in tests. For the most part the members didn't need to be public. In a couple of places they were accessed through reflection in the test, and it was enough to change `getField` to `getDeclaredField` (etc). (`getField` only retrieves public fields.) I don't believe any of these tests depended on the public members for the correctness of what they were testing. Although `getField` works when the target field is public, regardless of the visibility of the containing class, actually reading or writing a field requires both the field and its containing class to be public unless `Field.setAccessible(true)` is called, and likewise for constructors and methods.
1 parent 5eab3ed commit 8c23cd3

41 files changed

Lines changed: 106 additions & 107 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

extras/src/test/java/com/google/gson/interceptors/InterceptorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ private static final class User {
157157
String email;
158158
Address address;
159159

160-
public User(String name, String password) {
160+
User(String name, String password) {
161161
this.name = name;
162162
this.password = password;
163163
}

gson/src/main/java/com/google/gson/internal/GsonTypes.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ private static final class ParameterizedTypeImpl implements ParameterizedType, S
520520
@SuppressWarnings("serial")
521521
private final Type[] typeArguments;
522522

523-
public ParameterizedTypeImpl(Type ownerType, Class<?> rawType, Type... typeArguments) {
523+
ParameterizedTypeImpl(Type ownerType, Class<?> rawType, Type... typeArguments) {
524524
requireNonNull(rawType);
525525

526526
if (ownerType == null && requiresOwnerType(rawType)) {
@@ -592,7 +592,7 @@ private static final class GenericArrayTypeImpl implements GenericArrayType, Ser
592592
@SuppressWarnings("serial")
593593
private final Type componentType;
594594

595-
public GenericArrayTypeImpl(Type componentType) {
595+
GenericArrayTypeImpl(Type componentType) {
596596
requireNonNull(componentType);
597597
this.componentType = canonicalize(componentType);
598598
}
@@ -633,7 +633,7 @@ private static final class WildcardTypeImpl implements WildcardType, Serializabl
633633
@SuppressWarnings("serial")
634634
private final Type lowerBound;
635635

636-
public WildcardTypeImpl(Type[] upperBounds, Type[] lowerBounds) {
636+
WildcardTypeImpl(Type[] upperBounds, Type[] lowerBounds) {
637637
if (lowerBounds.length > 1) {
638638
throw new IllegalArgumentException("At most one lower bound is supported");
639639
}

gson/src/main/java/com/google/gson/internal/ReflectionAccessFilterHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public static boolean canAccess(AccessibleObject accessibleObject, Object object
7878
}
7979

8080
private abstract static class AccessChecker {
81-
public static final AccessChecker INSTANCE;
81+
static final AccessChecker INSTANCE;
8282

8383
static {
8484
AccessChecker accessChecker = null;
@@ -116,6 +116,6 @@ public boolean canAccess(AccessibleObject accessibleObject, Object object) {
116116
INSTANCE = accessChecker;
117117
}
118118

119-
public abstract boolean canAccess(AccessibleObject accessibleObject, Object object);
119+
abstract boolean canAccess(AccessibleObject accessibleObject, Object object);
120120
}
121121
}

gson/src/main/java/com/google/gson/internal/bind/CollectionTypeAdapterFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ private static final class Adapter<E> extends TypeAdapter<Collection<E>> {
6565
private final TypeAdapter<E> elementTypeAdapter;
6666
private final ObjectConstructor<? extends Collection<E>> constructor;
6767

68-
public Adapter(
68+
Adapter(
6969
TypeAdapter<E> elementTypeAdapter, ObjectConstructor<? extends Collection<E>> constructor) {
7070
this.elementTypeAdapter = elementTypeAdapter;
7171
this.constructor = constructor;

gson/src/main/java/com/google/gson/internal/bind/MapTypeAdapterFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ private final class Adapter<K, V> extends TypeAdapter<Map<K, V>> {
163163
private final TypeAdapter<V> valueTypeAdapter;
164164
private final ObjectConstructor<? extends Map<K, V>> constructor;
165165

166-
public Adapter(
166+
Adapter(
167167
TypeAdapter<K> keyTypeAdapter,
168168
TypeAdapter<V> valueTypeAdapter,
169169
ObjectConstructor<? extends Map<K, V>> constructor) {

gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapterFactory.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -286,16 +286,14 @@ void readIntoField(JsonReader reader, Object target)
286286
}
287287

288288
private static class FieldsData {
289-
public static final FieldsData EMPTY =
290-
new FieldsData(Collections.emptyMap(), Collections.emptyList());
289+
static final FieldsData EMPTY = new FieldsData(Collections.emptyMap(), Collections.emptyList());
291290

292291
/** Maps from JSON member name to field */
293-
public final Map<String, BoundField> deserializedFields;
292+
final Map<String, BoundField> deserializedFields;
294293

295-
public final List<BoundField> serializedFields;
294+
final List<BoundField> serializedFields;
296295

297-
public FieldsData(
298-
Map<String, BoundField> deserializedFields, List<BoundField> serializedFields) {
296+
FieldsData(Map<String, BoundField> deserializedFields, List<BoundField> serializedFields) {
299297
this.deserializedFields = deserializedFields;
300298
this.serializedFields = serializedFields;
301299
}

gson/src/main/java/com/google/gson/internal/reflect/ReflectionHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ private abstract static class RecordHelper {
227227

228228
abstract <T> Constructor<T> getCanonicalRecordConstructor(Class<T> raw);
229229

230-
public abstract Method getAccessor(Class<?> raw, Field field);
230+
abstract Method getAccessor(Class<?> raw, Field field);
231231
}
232232

233233
private static class RecordSupportedHelper extends RecordHelper {

gson/src/test/java/com/google/gson/ExposeAnnotationExclusionStrategyTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,22 +83,22 @@ public void testDifferentSerializeAndDeserializeField() throws Exception {
8383
}
8484

8585
private static Field createFieldAttributes(String fieldName) throws Exception {
86-
return MockObject.class.getField(fieldName);
86+
return MockObject.class.getDeclaredField(fieldName);
8787
}
8888

8989
@SuppressWarnings("unused")
9090
private static class MockObject {
91-
@Expose public final int exposedField = 0;
91+
@Expose final int exposedField = 0;
9292

9393
@Expose(serialize = true, deserialize = true)
94-
public final int explicitlyExposedField = 0;
94+
final int explicitlyExposedField = 0;
9595

9696
@Expose(serialize = false, deserialize = false)
97-
public final int explicitlyHiddenField = 0;
97+
final int explicitlyHiddenField = 0;
9898

9999
@Expose(serialize = true, deserialize = false)
100-
public final int explicitlyDifferentModeField = 0;
100+
final int explicitlyDifferentModeField = 0;
101101

102-
public final int hiddenField = 0;
102+
final int hiddenField = 0;
103103
}
104104
}

gson/src/test/java/com/google/gson/FieldAttributesTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public void testDeclaredTypeAndClass() {
7575
}
7676

7777
private static class Foo {
78-
@SuppressWarnings("unused")
78+
@SuppressWarnings({"unused", "EffectivelyPrivate"})
7979
public transient List<String> bar;
8080
}
8181
}

gson/src/test/java/com/google/gson/GsonBuilderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public void testDisableJdkUnsafe() {
202202
}
203203

204204
private static class ClassWithoutNoArgsConstructor {
205-
@SuppressWarnings("unused")
205+
@SuppressWarnings({"unused", "EffectivelyPrivate"})
206206
public ClassWithoutNoArgsConstructor(String s) {}
207207
}
208208

0 commit comments

Comments
 (0)