io.github.honoriuss.blossom.BlossomAspect Maven / Gradle / Ivy
package io.github.honoriuss.blossom;
import io.github.honoriuss.blossom.annotations.AppContext;
import io.github.honoriuss.blossom.annotations.TrackParameters;
import io.github.honoriuss.blossom.interfaces.ITrackingHandler;
import io.github.honoriuss.blossom.interfaces.ITrackingObjectMapper;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.Arrays;
@Aspect
@Component
class BlossomAspect { //TODO null checks
private final ITrackingHandler trackingHandler;
private final ITrackingObjectMapper trackingObjectMapper;
private final BlossomAspectHelper blossomAspectHelper;
public BlossomAspect(ITrackingHandler trackingHandler,
ITrackingObjectMapper trackingObjectMapper,
BlossomAspectHelper blossomAspectHelper) {
this.trackingHandler = trackingHandler;
this.trackingObjectMapper = trackingObjectMapper;
this.blossomAspectHelper = blossomAspectHelper;
}
@Before(value = "@annotation(trackParameters)")
void track(JoinPoint joinPoint, TrackParameters trackParameters) {
trackingHandler.handleTracking(blossomAspectHelper.createTrackingObject(joinPoint, trackParameters));
}
@AfterReturning(value = "@annotation(io.github.honoriuss.blossom.annotations.TrackResult)", returning = "result")
void track(Object result) {
trackingHandler.handleTracking(trackingObjectMapper.mapResult(result));
}
}
@Service
class BlossomAspectHelper {
private final Logger logger = LoggerFactory.getLogger(BlossomAspectHelper.class);
private final ITrackingObjectMapper trackingObjectMapper;
BlossomAspectHelper(ITrackingHandler trackingHandler,
ITrackingObjectMapper trackingObjectMapper) {
this.trackingObjectMapper = trackingObjectMapper;
compareGenericParams(trackingHandler, trackingObjectMapper); //TODO
logger.info("using ITrackingHandler: {} and ITrackingObjectMapper: {}", trackingHandler.getClass(), trackingObjectMapper.getClass());
}
protected void addOptionalArgument(ArrayList