Sweet.apex

  • Docs
  • Tutorials
Docs Menu
  • Sweet.apex Core
    • Transpilation
    • Grammar
    • Command
    • Config
  • Features
    • Action
    • Apex Doc
    • Array Creation
    • Aspect
    • Cast
    • Default Value
    • Enum
    • File
    • Function
    • Identity
    • Injection
    • Lambda
    • Log
    • Mod
    • Not Null
    • Operator
    • Optional
    • Reflection
    • Rethrow
    • Switch
    • Template String
    • Template
    • Script
    • Tagged String
    • Annotation
    • Nullable
    • Var
    • Val
    • Map Access
    • Constructor
    • Transaction
    • Destructure
    • Import Static
    • Pipeline
    • Varargs
    • Patch
    • Import As
  • Plugin Development
    • Feature
    • Test Case

Aspect Guide

Aspect

Feature Overview

This feature applies aspects onto methods.

Aspects represent cross-cutting concerns like logging, validation and so on. Using AOP(Aspect Oriented Programming), our code will look clean and the core business logic code is more condensed.

Prerequisite

You need to have some basic understanding of Aspect Oriented Programming.

Sweet Apex Example

public class AspectDemo {
    @afterMethod('AspectDemo.version')
    public static Integer afterVersion(Sweet.MethodInfo method, List args, Object result) {
        return (Integer)result + 1;
    }

    public static Integer version(Integer base) {
        return base + 1;
    }
}

Transpiled Apex

public class AspectDemo {
    public static Integer afterVersion(Sweet.MethodInfo method, List args, Object result) {
        return (Integer)result + 1;
    }
    public static Integer version(Integer base) {
        Integer ret = aspect_version(base);
        ret = (Integer)AspectDemo.afterVersion(new Sweet.MethodInfo('version', AspectDemo.class, null, new List{ Integer.class }), new List{ base }, ret);
        return ret;
    }
    private static Integer aspect_version(Integer base) {
        return base + 1;
    }
}

Usage

This feature includes two annotations, @beforeMethod and @afterMethod. They can only be applied to public/global static methods that accept the following arguments.

  • @beforeMethod

Method name is not important but it should be expecting (Object, List).

The first argument will be the method info for this aspect.

The second argument will be the list of arguments passed to the target method.

  • @afterMethod

Method name is not important but it should be expecting (Object, List, Object).

The first argument will be the method info for this aspect.

The second argument will be the list of arguments passed to the target method.

The third argument will be the result of the target method.

An aspect here is any method marked with the annotation @beforeMethod or @afterMethod. This feature will scan all the source files to find all the aspects before trying to apply them to the target methods. So you can make any method to be an aspect and expect it to be applied to other source files.

Contribute on Github! Edit this section.