Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<compiler-plugin-version>3.8.0</compiler-plugin-version>
<okhttp3-version>4.9.1</okhttp3-version>
<guava-version>30.1.1-jre</guava-version>
<gson-version>2.8.9</gson-version>
<gson-version>2.9.0</gson-version>
<commons-codec-version>1.15</commons-codec-version>
<commons-io-version>2.7</commons-io-version>
<commons-lang3-version>3.8.1</commons-lang3-version>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* (C) Copyright IBM Corp. 2019, 2020.
* (C) Copyright IBM Corp. 2019, 2022.
* Copyright (C) 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
Expand All @@ -15,6 +15,7 @@
package com.ibm.cloud.sdk.core.util;

import java.io.IOException;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
Expand All @@ -34,7 +35,6 @@
import com.google.gson.annotations.SerializedName;
import com.google.gson.internal.$Gson$Types;
import com.google.gson.internal.Primitives;
import com.google.gson.internal.reflect.ReflectionAccessor;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
Expand Down Expand Up @@ -74,7 +74,6 @@
*/
public class DynamicModelTypeAdapterFactory implements TypeAdapterFactory {
private static final Logger LOGGER = Logger.getLogger(DynamicModelTypeAdapterFactory.class.getName());
private ReflectionAccessor accessor = ReflectionAccessor.getInstance();

public DynamicModelTypeAdapterFactory() {
}
Expand Down Expand Up @@ -119,9 +118,7 @@ protected Constructor<?> getDefaultCtor(Class<?> clazz) {
for (int i = 0; i < allCtors.length; i++) {
Constructor<?> ctor = allCtors[i];
if (ctor.getParameterTypes().length == 0) {
if (!ctor.isAccessible()) {
accessor.makeAccessible(ctor);
}
suppressAccessChecks(ctor);
return ctor;
}
}
Expand Down Expand Up @@ -157,7 +154,7 @@ protected Map<String, BoundField> getBoundFields(Gson context, TypeToken<?> type
boolean serialize = true;
boolean deserialize = true;

accessor.makeAccessible(field);
suppressAccessChecks(field);

Type fieldType = $Gson$Types.resolve(type.getType(), raw, field.getGenericType());
BoundField previous = null;
Expand Down Expand Up @@ -186,6 +183,28 @@ protected Map<String, BoundField> getBoundFields(Gson context, TypeToken<?> type
return result;
}

/**
* This method will try to suppress the default java language access control checks on the
* specified AccessibleObject (ctor, field, method, etc.).
* If we're unsuccessful at suppressing the checks, we'll ignore the error and potentially
* encounter an error later on when the AccessibleObject is used.
*
* @param ao the AccessibleObject instance (a Constructor, Field, Method, etc.)
*/
protected void suppressAccessChecks(AccessibleObject ao) {
// If access control checks are currently NOT being suppressed,
// then try to suppress them.
if (!ao.isAccessible()) {
try {
ao.setAccessible(true);
} catch (Throwable t) {
// If we couldn't suppress the access control checks, then we'll just ignore this
// for now and wait until we run into an actual problem later when trying to use the
// reflected object (ctor, field, etc.).
}
}
}

/**
* Returns a list of field names for the specified Field. Note that we will return field names ONLY if the field has
* the SerializedName annotation set on it.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* (C) Copyright IBM Corp. 2015, 2019.
* (C) Copyright IBM Corp. 2015, 2022.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
Expand All @@ -23,7 +23,7 @@
import com.ibm.cloud.sdk.core.test.model.generated.AnimalCat;

/**
* A few simple tests that exercise the GenericModel methods.
* A few simple tests that exercise the DynamicModel methods.
*/
public class DynamicModelTest {
private boolean displayOutput = false;
Expand Down