Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* ObfuscatedBeanDeserializerModifier.java
* Copyright 2020 Rob Spoor
*
* 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 com.github.robtimus.obfuscation.jackson.databind;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.DeserializationConfig;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.deser.BeanDeserializerBuilder;
import com.fasterxml.jackson.databind.deser.BeanDeserializerModifier;
import com.fasterxml.jackson.databind.deser.SettableBeanProperty;
import com.fasterxml.jackson.databind.deser.ValueInstantiator;
import com.fasterxml.jackson.databind.util.ClassUtil;
import com.github.robtimus.obfuscation.Obfuscated;
import com.github.robtimus.obfuscation.Obfuscator;
import com.github.robtimus.obfuscation.annotation.CharacterRepresentationProvider;
import com.github.robtimus.obfuscation.annotation.ObjectFactory;
final class ObfuscatedBeanDeserializerModifier extends BeanDeserializerModifier {
private static final ObjectFactory CAN_OVERRIDE_ACCESS_MODIFIERS = ObfuscatedBeanDeserializerModifier::createInstanceWithCanFixAccess;
private static final ObjectFactory CANNOT_OVERRIDE_ACCESS_MODIFIERS = ObfuscatedBeanDeserializerModifier::createInstanceWithoutCanFixAccess;
private static final Function FACTORY_MAPPER = config -> config.canOverrideAccessModifiers()
? CAN_OVERRIDE_ACCESS_MODIFIERS
: CANNOT_OVERRIDE_ACCESS_MODIFIERS;
private final Function factoryMapper;
private final Obfuscator defaultObfuscator;
private final Map, Obfuscator> classObfuscators;
private final Map, Obfuscator> interfaceObfuscators;
private final Map, CharacterRepresentationProvider> classCharacterRepresentationProviders;
private final Map, CharacterRepresentationProvider> interfaceCharacterRepresentationProviders;
private final boolean requireObfuscatorAnnotation;
ObfuscatedBeanDeserializerModifier(ObjectFactory objectFactory,
Obfuscator defaultObfuscator,
Map, Obfuscator> classObfuscators,
Map, Obfuscator> interfaceObfuscators,
Map, CharacterRepresentationProvider> classCharacterRepresentationProviders,
Map, CharacterRepresentationProvider> interfaceCharacterRepresentationProviders,
boolean requireObfuscatorAnnotation) {
this.factoryMapper = objectFactory != null ? config -> objectFactory : FACTORY_MAPPER;
this.defaultObfuscator = defaultObfuscator;
this.classObfuscators = classObfuscators;
this.interfaceObfuscators = interfaceObfuscators;
this.classCharacterRepresentationProviders = classCharacterRepresentationProviders;
this.interfaceCharacterRepresentationProviders = interfaceCharacterRepresentationProviders;
this.requireObfuscatorAnnotation = requireObfuscatorAnnotation;
}
private static T createInstanceWithCanFixAccess(Class type) {
return ClassUtil.createInstance(type, true);
}
private static T createInstanceWithoutCanFixAccess(Class type) {
return ClassUtil.createInstance(type, false);
}
@Override
public BeanDeserializerBuilder updateBuilder(DeserializationConfig config, BeanDescription beanDesc, BeanDeserializerBuilder builder) {
BeanDeserializerBuilder updatedBuilder = super.updateBuilder(config, beanDesc, builder);
Map propertyReplacements = new LinkedHashMap<>();
ValueInstantiator valueInstantiator = updatedBuilder.getValueInstantiator();
SettableBeanProperty[] constructorArguments = valueInstantiator.getFromObjectArguments(config);
for (Iterator i = updatedBuilder.getProperties(); i.hasNext(); ) {
SettableBeanProperty property = i.next();
Class> rawPropertyType = property.getType().getRawClass();
// These if-statements check for exact interface declarations, so the obfuscating replacement will have a compatible type
if (rawPropertyType == Obfuscated.class) {
JsonDeserializer