org.bson.codecs.pojo.ConventionSetPrivateFieldImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mongo-java-driver Show documentation
Show all versions of mongo-java-driver Show documentation
The MongoDB Java Driver uber-artifact, containing mongodb-driver, mongodb-driver-core, and bson
/*
* Copyright 2008-present MongoDB, Inc.
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.bson.codecs.pojo;
import org.bson.codecs.configuration.CodecConfigurationException;
import static java.lang.String.format;
import static java.lang.reflect.Modifier.isPrivate;
final class ConventionSetPrivateFieldImpl implements Convention {
@Override
public void apply(final ClassModelBuilder> classModelBuilder) {
for (PropertyModelBuilder> propertyModelBuilder : classModelBuilder.getPropertyModelBuilders()) {
if (!(propertyModelBuilder.getPropertyAccessor() instanceof PropertyAccessorImpl)) {
throw new CodecConfigurationException(format("The SET_PRIVATE_FIELDS_CONVENTION is not compatible with "
+ "propertyModelBuilder instance that have custom implementations of org.bson.codecs.pojo.PropertyAccessor: %s",
propertyModelBuilder.getPropertyAccessor().getClass().getName()));
}
PropertyAccessorImpl> defaultAccessor = (PropertyAccessorImpl>) propertyModelBuilder.getPropertyAccessor();
PropertyMetadata> propertyMetaData = defaultAccessor.getPropertyMetadata();
if (!propertyMetaData.isDeserializable() && propertyMetaData.getField() != null
&& isPrivate(propertyMetaData.getField().getModifiers())) {
setPropertyAccessor(propertyModelBuilder);
}
}
}
@SuppressWarnings("unchecked")
private void setPropertyAccessor(final PropertyModelBuilder propertyModelBuilder) {
propertyModelBuilder.propertyAccessor(new PrivatePropertyAccessor(
(PropertyAccessorImpl) propertyModelBuilder.getPropertyAccessor()));
}
private static final class PrivatePropertyAccessor implements PropertyAccessor {
private final PropertyAccessorImpl wrapped;
private PrivatePropertyAccessor(final PropertyAccessorImpl wrapped) {
this.wrapped = wrapped;
try {
wrapped.getPropertyMetadata().getField().setAccessible(true);
} catch (Exception e) {
throw new CodecConfigurationException(format("Unable to make private field accessible '%s' in %s",
wrapped.getPropertyMetadata().getName(), wrapped.getPropertyMetadata().getDeclaringClassName()), e);
}
}
@Override
public T get(final S instance) {
return wrapped.get(instance);
}
@Override
public void set(final S instance, final T value) {
try {
wrapped.getPropertyMetadata().getField().set(instance, value);
} catch (Exception e) {
throw new CodecConfigurationException(format("Unable to set value for property '%s' in %s",
wrapped.getPropertyMetadata().getName(), wrapped.getPropertyMetadata().getDeclaringClassName()), e);
}
}
}
}