
com.github.thorbenkuck.di.aspects.AspectExecutionContext Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of wire-di-runtime-environment Show documentation
Show all versions of wire-di-runtime-environment Show documentation
Easy and simple di using annotation processors
The newest version!
package com.github.thorbenkuck.di.aspects;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
public class AspectExecutionContext {
@NotNull
private final AtomicReference> executionContextAtomicReference = new AtomicReference<>();
@NotNull
private final Map arguments = new HashMap<>();
@NotNull
private final AspectRepository aspectRepository;
@NotNull
private final Function, Object> realMethod;
public AspectExecutionContext(
@NotNull final AspectRepository aspectRepository,
@NotNull final Function, Object> realMethod
) {
this.aspectRepository = aspectRepository;
this.realMethod = realMethod;
}
@NotNull
public AspectExecutionContext announceInterestForAspect(
@NotNull final Class annotationType,
@Nullable final T annotation
) {
aspectRepository.access(annotationType).ifPresent(aspect -> {
final ExecutionContext> methodContext = executionContextAtomicReference.get();
final ExecutionContext> currentContext;
if (methodContext == null) {
currentContext = new ExecutionContext(aspect, annotation, realMethod);
} else {
currentContext = new ExecutionContext<>(aspect, annotation, methodContext);
}
executionContextAtomicReference.set(currentContext);
});
return this;
}
public void declareArgument(@NotNull final String name, @Nullable final T t) {
arguments.put(name, t);
}
public boolean noAspectsPresent() {
return executionContextAtomicReference.get() == null;
}
@Nullable
public Object run(final boolean mayBeNull) {
final ExecutionContext> executionContext = executionContextAtomicReference.get();
if(executionContext == null) {
throw new IllegalStateException("No ExecutionContext set");
}
executionContext.setArguments(arguments);
try {
final Object result = executionContext.run();
if (!mayBeNull && result == null) {
throw new NullPointerException("Expect the result not to be null, but yet it is");
}
return result;
} finally {
arguments.clear();
executionContext.clear();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy