
com.github.thorbenkuck.di.aspects.ExecutionContext 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.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* TODO: Interface for the using aspect methods
*
* @param
*/
public class ExecutionContext {
@NotNull
private final AspectWrapper rootAspect;
@Nullable
private AspectWrapper currentAspectPointer;
@Nullable
private final T annotation;
@NotNull
private final Map arguments = new HashMap<>();
@Nullable
private final Function, Object> rootMethod;
@Nullable
private final ExecutionContext> then;
@Nullable
private Object lastReturnValue = null;
public ExecutionContext(
@NotNull AspectWrapper aspect,
@Nullable T annotation,
@NotNull Function, Object> rootMethod
) {
this.rootAspect = aspect;
this.annotation = annotation;
this.rootMethod = rootMethod;
this.then = null;
}
public ExecutionContext(
@NotNull final AspectWrapper aspect,
@Nullable final T annotation,
@NotNull final ExecutionContext> then
) {
this.rootAspect = aspect;
this.annotation = annotation;
this.then = then;
this.rootMethod = null;
}
void clear() {
arguments.clear();
if (then != null) {
then.clear();
}
}
@Nullable
Object run() {
this.currentAspectPointer = rootAspect;
return invokeCurrentAspect();
}
@Nullable
public Object proceed() {
if (currentAspectPointer == null) {
throw new IllegalStateException("No aspect pointer registered");
}
final AspectWrapper followup = currentAspectPointer.getNext();
if (followup != null) {
currentAspectPointer = followup;
return invokeCurrentAspect();
} else {
if (then != null) {
then.arguments.putAll(this.arguments);
then.lastReturnValue = lastReturnValue;
return then.run();
} else if (rootMethod != null) {
return rootMethod.apply(this);
} else {
throw new IllegalStateException("There is neither a root method, nor a next ExecutionContext set. This should never happen!");
}
}
}
@Nullable
private Object invokeCurrentAspect() {
if (currentAspectPointer == null) {
throw new IllegalStateException("No aspect pointer registered");
}
return currentAspectPointer.getRootAspect().process(this);
}
@NotNull
public Optional
© 2015 - 2025 Weber Informatics LLC | Privacy Policy