io.github.honoriuss.blossom.BlossomAspect Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tracking-blossom Show documentation
Show all versions of tracking-blossom Show documentation
Track your app nice and convenient
package io.github.honoriuss.blossom;
import io.github.honoriuss.blossom.annotations.AdditionalTrackingInfo;
import io.github.honoriuss.blossom.annotations.AppContext;
import io.github.honoriuss.blossom.annotations.TrackParameters;
import io.github.honoriuss.blossom.interfaces.ITrackingAppContextHandler;
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.aspectj.lang.reflect.MethodSignature;
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) {
var trackingObj = blossomAspectHelper.createTrackingObject(joinPoint, trackParameters);
var method = ((MethodSignature) joinPoint.getSignature()).getMethod();
if (method.isAnnotationPresent(AdditionalTrackingInfo.class)) {
trackingHandler.handleTracking(trackingObj, method.getAnnotation(AdditionalTrackingInfo.class));
} else {
trackingHandler.handleTracking(trackingObj);
}
}
@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;
private final ITrackingAppContextHandler trackingAppContextHandler;
BlossomAspectHelper(ITrackingHandler trackingHandler,
ITrackingObjectMapper trackingObjectMapper, ITrackingAppContextHandler trackingAppContextHandler) {
this.trackingObjectMapper = trackingObjectMapper;
this.trackingAppContextHandler = trackingAppContextHandler;
compareGenericParams(trackingHandler, trackingObjectMapper); //TODO
logger.info("using ITrackingHandler: {} and ITrackingObjectMapper: {}", trackingHandler.getClass(), trackingObjectMapper.getClass());
}
protected void addOptionalArgument(ArrayList