Skip to content
Danila Rassokhin edited this page Feb 7, 2023 · 5 revisions

Welcome to the aide wiki!

Aide is a set of useful utils for fast reflection, extended optionals and conditionals. It can help you with development of some service or your own framework.

Reflection

Aide reflection contains utils for reflection such as fast method invocation with lambda wrapping, annotation processing and other useful methods.

Reflective method calls with Aide are simple:

// Get LambdaWrapperHolder isntance with default LambdaWrapper interface loaded
LambdaWrapperHolder lambdaWrapperHolder = LambdaWrapperHolder.DEFAULT;
// Find static method
Method staticMethod = ReflectionUtil.getMethod(TestClass.class, "staticMethod", String.class);
// Wrap static method
// LambdaWrapper - default wrapper interface from Aide
// Void - caller class. In case of static method caller is not needed, so Void
// Integer - return type
MethodHolder<LambdaWrapper, Void, Integer> staticHolder = lambdaWrapperHolder.wrapSafe(staticMethod);
// Invoke static method without caller
int staticResult = staticHolder.invokeStatic("Hello");

Optional

Aide optional contains extended optional classes for String, Boolean types, IfTrue and When conditionals, Object utils.

Extended optionals provides new methods for some types:

BooleanOptional.of(Modifier.isPublic(executable.getModifiers()))
    .ifFalseThrow(() -> ReflectionException.format("Wrapping is supported for PUBLIC methods only!"));

With conditionals you can make your code more functional. Thats how Aide reflection uses them:

AbstractSignature signature = IfTrueConditional.create()
    .ifTrue(exact).then(() -> ExactMethodSignature.from(method))
    .ifTrue(!exact).then(() -> MethodSignature.from(method))
    .orElseThrow(() -> ReflectionException.format("%s undefined!", exact));

Or WhenConditional:

WhenConditional.create()
    .when(someObj, Objects::nonNull).then(MyClass::nonNull)
    .when(someObj, Objects::isNull).then(MyClass::isNull)
    .orDoNothing();

SwitchConditional too:

Status status = Status.BAD_REQUEST;
String message = SwitchConditional.<Status, String>on(status)
  .caseOn(Status.BAD_REQUEST::equals).thenGet("Error: Bad request")
  .caseOn(Status.INTERNAL_ERROR::equals).thenGet("Error: Internal error")
  .orElse("");
    
assert message.equals("Error: Bad request");
    
SwitchConditional.on(status)
  // false = no break;, so all branches below will be executed
  .caseOn(Status.BAD_REQUEST::equals, false).thenDo(this::action)
  .caseOn(Status.INTERNAL_ERROR::equals).thenDo(this::action)
  .orElseDo(() -> System.out.println("No action"));
Clone this wiki locally