Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Logger, replace existing logging #193

Merged
merged 4 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 7 additions & 4 deletions src/main/java/io/beanmapper/config/OverrideField.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.function.Supplier;

import io.beanmapper.utils.BeanMapperLogger;

public class OverrideField<T> {

private final Supplier<T> supplier;
Expand Down Expand Up @@ -31,9 +33,10 @@ public T get() {
if (this.block) {
return null;
}
return this.value == null ?
this.supplier.get() :
this.value;
if (this.value == null) {
this.value = BeanMapperLogger.logTimed("Retrieving nested configuration field.", this.supplier);
}
return value;
}

}
}
13 changes: 8 additions & 5 deletions src/main/java/io/beanmapper/config/StrictMappingProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.beanmapper.core.unproxy.BeanUnproxy;
import io.beanmapper.core.unproxy.SkippingBeanUnproxy;
import io.beanmapper.utils.BeanMapperLogger;

public class StrictMappingProperties {

Expand Down Expand Up @@ -79,18 +80,20 @@ public void setApplyStrictMappingConvention(boolean applyStrictMappingConvention
}

public BeanPair createBeanPair(Class<?> sourceClass, Class<?> targetClass) {
sourceClass = beanUnproxy.unproxy(sourceClass);
targetClass = beanUnproxy.unproxy(targetClass);
BeanPair beanPair = new BeanPair(sourceClass, targetClass);
BeanPair beanPair = BeanMapperLogger.logTimed("Creating BeanPair, and unproxying source %s-class and target %s-class.".formatted(sourceClass.getCanonicalName(), targetClass.getCanonicalName()), () -> {
Class<?> unproxiedSource = beanUnproxy.unproxy(sourceClass);
Class<?> unproxiedTarget = beanUnproxy.unproxy(targetClass);
return new BeanPair(unproxiedSource, unproxiedTarget);
});
if (!isApplyStrictMappingConvention()) {
return beanPair;
}
if (strictSourceSuffix != null &&
sourceClass.getCanonicalName().endsWith(strictSourceSuffix)) {
beanPair.getSourceClass().getCanonicalName().endsWith(strictSourceSuffix)) {
beanPair = beanPair.withStrictSource();
}
if (strictTargetSuffix != null &&
targetClass.getCanonicalName().endsWith(strictTargetSuffix)) {
beanPair.getTargetClass().getCanonicalName().endsWith(strictTargetSuffix)) {
beanPair = beanPair.withStrictTarget();
}
return beanPair;
Expand Down
12 changes: 4 additions & 8 deletions src/main/java/io/beanmapper/core/BeanMatchStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,11 @@
import io.beanmapper.exceptions.BeanMissingPathException;
import io.beanmapper.exceptions.BeanNoSuchPropertyException;
import io.beanmapper.exceptions.FieldShadowingException;
import io.beanmapper.utils.BeanMapperLogger;
import io.beanmapper.utils.Trinary;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BeanMatchStore {

private final Logger logger = LoggerFactory.getLogger(getClass());

private final CollectionHandlerStore collectionHandlerStore;

private final BeanUnproxy beanUnproxy;
Expand Down Expand Up @@ -287,11 +283,11 @@ private BeanPropertyWrapper dealWithBeanProperty(BeanPropertyMatchupDirection ma
new BeanPropertyCreator(matchupDirection.getInverse(), otherType, wrapper.getName())
.determineNodesForPath());
} catch (BeanNoSuchPropertyException err) {
if (logger.isDebugEnabled()) {
logger.debug("""

BeanMapperLogger.log("""
BeanNoSuchPropertyException thrown by BeanMatchStore#dealWithBeanProperty(BeanPropertyMatchupDirection, Map<String, BeanProperty>, Class, PropertyAccessor), for {}.
{}""", wrapper.getName(), err.getMessage());
}

}
}
return wrapper;
Expand Down
10 changes: 3 additions & 7 deletions src/main/java/io/beanmapper/core/BeanPropertyMatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@
import io.beanmapper.exceptions.BeanMappingException;
import io.beanmapper.exceptions.BeanNoLogicSecuredCheckSetException;
import io.beanmapper.exceptions.BeanNoRoleSecuredCheckSetException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.beanmapper.utils.BeanMapperLogger;

public class BeanPropertyMatch {

protected final Logger logger = LoggerFactory.getLogger(getClass());

private final BeanMatch beanMatch;
private final Object source;
private final Object target;
Expand Down Expand Up @@ -81,7 +77,7 @@ private boolean checkForLogicSecured(
if (enforcedSecuredProperties) {
throw new BeanNoLogicSecuredCheckSetException(message);
}
logger.warn(message);
BeanMapperLogger.warn(message);
return true;
}
return logicSecuredCheck.isAllowed(source, target);
Expand All @@ -96,7 +92,7 @@ private void checkIfSecuredFieldHandlerNotSet(BeanProperty beanProperty, boolean
if (enforcedSecuredProperties) {
throw new BeanNoRoleSecuredCheckSetException(message);
}
logger.warn(message);
BeanMapperLogger.warn(message);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
import java.util.Collections;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.beanmapper.utils.BeanMapperLogger;

public class BeanStrictMappingRequirementsException extends RuntimeException {

protected final transient Logger logger = LoggerFactory.getLogger(getClass());

private final List<BeanMatchValidationMessage> validationMessages;

public BeanStrictMappingRequirementsException(BeanMatchValidationMessage validationMessage) {
Expand All @@ -26,7 +23,7 @@ private void logErrors(List<BeanMatchValidationMessage> validationMessages) {
if (validationMessage.isLogged()) {
continue;
}
logger.error("""
BeanMapperLogger.error("""
Missing matching properties for source [{}] {} > target [{}] {} for fields:
""",
validationMessage.getSourceClass().getCanonicalName(),
Expand All @@ -35,7 +32,7 @@ private void logErrors(List<BeanMatchValidationMessage> validationMessages) {
(validationMessage.isTargetStrict() ? "*" : ""));

for (BeanProperty field : validationMessage.getFields()) {
logger.error("""
BeanMapperLogger.error("""
> {}.{}
""",
validationMessage.getStrictClass().getSimpleName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.beanmapper.config.CollectionFlusher;
import io.beanmapper.core.constructor.DefaultBeanInitializer;
import io.beanmapper.exceptions.BeanCollectionUnassignableTargetCollectionTypeException;
import io.beanmapper.utils.BeanMapperLogger;
import io.beanmapper.utils.Classes;

public abstract class AbstractCollectionHandler<C> implements CollectionHandler<C> {
Expand Down Expand Up @@ -32,14 +33,16 @@ public Object mapItem(
BeanMapper beanMapper,
Class<?> collectionElementClass,
Object source) {

return beanMapper
.wrap()
return BeanMapperLogger.logTimed("Recursively calling BeanMapper#map(Object), to map collection elements of type %s, to %s."
.formatted(source != null
? source.getClass().getCanonicalName()
: "null", collectionElementClass.getCanonicalName()),
() -> beanMapper.wrap()
.setTargetClass(collectionElementClass)
.setCollectionClass(null)
.setConverterChoosable(true)
.build()
.map(source);
.map(source));
}

@Override
Expand Down Expand Up @@ -90,4 +93,4 @@ public int getGenericParameterIndex() {
return 0;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,17 @@
import io.beanmapper.BeanMapper;
import io.beanmapper.config.BeanMapperBuilder;
import io.beanmapper.strategy.ConstructorArguments;
import io.beanmapper.utils.BeanMapperLogger;
import io.beanmapper.utils.DefaultValues;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DefaultBeanInitializer implements BeanInitializer {

private final Logger logger = LoggerFactory.getLogger(getClass());

/**
* {@inheritDoc}
*/
@Override
public <T> T instantiate(Class<T> beanClass, ConstructorArguments arguments) {
BeanMapperLogger.log("Creating a new instance of type %s, using reflection.".formatted(beanClass));
try {
if (arguments == null) {
return beanClass.getConstructor().newInstance();
Expand All @@ -34,7 +31,7 @@ public <T> T instantiate(Class<T> beanClass, ConstructorArguments arguments) {
var constructorParameterTypes = Arrays.stream(constructor.getParameters()).map(Parameter::getParameterizedType).toArray(Type[]::new);
return beanClass.getConstructor(arguments.getTypes()).newInstance(mapParameterizedArguments(constructorParameterTypes, arguments.getValues()));
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
logger.error("Could not instantiate bean of class %s. Returning the default value associated with the given type. %s".formatted(beanClass.getName(),
BeanMapperLogger.error("Could not instantiate bean of class %s. Returning the default value associated with the given type. %s".formatted(beanClass.getName(),
e.getMessage()));
return DefaultValues.defaultValueFor(beanClass);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.beanmapper.core.BeanPropertyMatch;
import io.beanmapper.core.collections.CollectionHandler;
import io.beanmapper.core.converter.BeanConverter;
import io.beanmapper.utils.BeanMapperLogger;

public class CollectionConverter implements BeanConverter {

Expand All @@ -24,7 +25,11 @@ public <R, U> U convert(
return targetClass.cast(source);
}

return beanMapper.wrap()
return BeanMapperLogger.logTimed("Calling BeanMapper#map(Object) recursively, to convert object of type %s, to type %s."
.formatted(source != null
? source.getClass().getCanonicalName()
: "null", targetClass.getCanonicalName()),
() -> beanMapper.wrap()
.setCollectionClass(collectionHandler.getType())
.setCollectionUsage(beanPropertyMatch.getCollectionInstructions().getBeanCollectionUsage())
.setPreferredCollectionClass(beanPropertyMatch.getCollectionInstructions().getPreferredCollectionClass().getAnnotationClass())
Expand All @@ -33,7 +38,7 @@ public <R, U> U convert(
.setTarget(beanPropertyMatch.getTargetObject())
.setUseNullValue()
.build()
.map(source);
.map(source));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
import io.beanmapper.BeanMapper;
import io.beanmapper.core.BeanPropertyMatch;
import io.beanmapper.core.converter.BeanConverter;
import io.beanmapper.utils.BeanMapperLogger;
import io.beanmapper.utils.Classes;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This converter facilitates the conversion of an arbitrary amount of Optional wrappers, however, support for complex
* datastructures, such as Maps, Sets, List, etc. is limited to a single layer. As such, if the user requires support
Expand All @@ -25,8 +23,6 @@
*/
public class OptionalToObjectConverter implements BeanConverter {

private final Logger logger = LoggerFactory.getLogger(this.getClass());

/**
* {@inheritDoc}
*/
Expand All @@ -35,13 +31,11 @@ public <S, T> T convert(BeanMapper beanMapper, S source, Class<T> targetClass, B
Object obj = ((Optional<?>) source).orElse(null);

if (targetClass.equals(Optional.class)) {
if (logger.isDebugEnabled()) {
// Not always possible to get the actual source class, so just report the name of the field. Debug log will show the call stack.
logger.debug("Converting Optional to Optional. Perhaps the target does not need to be an Optional?\nSource-field: {}\nTarget: {}.{}",
beanPropertyMatch.getSourceFieldName(),
beanPropertyMatch.getTarget().getClass(),
beanPropertyMatch.getTargetFieldName());
}
// Not always possible to get the actual source class, so just report the name of the field. Debug log will show the call stack.
BeanMapperLogger.log("Converting Optional to Optional. Perhaps the target does not need to be an Optional?\nSource-field: {}\nTarget: {}.{}",
beanPropertyMatch.getSourceFieldName(),
beanPropertyMatch.getTarget().getClass(),
beanPropertyMatch.getTargetFieldName());
return targetClass.cast(convertToOptional(beanMapper, (Optional<?>) source, beanPropertyMatch));
} else if (obj == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.util.HashSet;
import java.util.Set;

import io.beanmapper.utils.BeanMapperLogger;

/**
* Unproxy that allows you to configure classes to skip.
*
Expand All @@ -31,6 +33,7 @@ public SkippingBeanUnproxy(BeanUnproxy delegate) {
*/
@Override
public Class<?> unproxy(Class<?> beanClass) {
BeanMapperLogger.log("Unproxying class %s.".formatted(beanClass != null ? beanClass.getCanonicalName() : "null"));
if (isSkippedProxyClass(beanClass)) {
return beanClass;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package io.beanmapper.exceptions;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.beanmapper.utils.BeanMapperLogger;

public class BeanNoLogicSecuredCheckSetException extends IllegalArgumentException {

protected final transient Logger logger = LoggerFactory.getLogger(getClass());

public BeanNoLogicSecuredCheckSetException(String message) {
super(message);
logger.error(message);
BeanMapperLogger.error(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package io.beanmapper.exceptions;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.beanmapper.utils.BeanMapperLogger;

public class BeanNoRoleSecuredCheckSetException extends IllegalArgumentException {

protected final transient Logger logger = LoggerFactory.getLogger(getClass());

public BeanNoRoleSecuredCheckSetException(String message) {
super(message);
logger.error(message);
BeanMapperLogger.error(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@
*/
package io.beanmapper.exceptions;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.beanmapper.utils.BeanMapperLogger;

/**
* Exception thrown when a property could not be found.
*/
public class BeanNoSuchPropertyException extends IllegalArgumentException {

protected final transient Logger logger = LoggerFactory.getLogger(getClass());

public BeanNoSuchPropertyException(String message) {
super(message);
logger.error(message);
marcus-talbot42 marked this conversation as resolved.
Show resolved Hide resolved
BeanMapperLogger.error(message);
}

}
Loading
Loading