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

Annotation Guide

Annotation

Feature Overview

This feature adds support for custom annotation on classes.

Prerequisite

None

Sweet Apex Example

@MyAnnotation(name='Test')
public class AnnotationDemo {
    public @interface MyAnnotation {
        public String name();

        public Integer number() default 10;
    }
}
​x
 
1
@MyAnnotation(name='Test')
2
public class AnnotationDemo {
3
    public @interface MyAnnotation {
4
        public String name();
5
​
6
        public Integer number() default 10;
7
    }
8
}

Transpiled Apex

public class AnnotationDemo {
    public class MyAnnotation {
        private String m_name;
        private Integer m_number = 10;
        public MyAnnotation name(String m_name) {
            this.m_name = m_name;
            return this;
        }
        public String name() {
            return this.m_name;
        }
        public MyAnnotation number(Integer m_number) {
            this.m_number = m_number;
            return this;
        }
        public Integer number() {
            return this.m_number;
        }
    }
}
20
 
1
public class AnnotationDemo {
2
    public class MyAnnotation {
3
        private String m_name;
4
        private Integer m_number = 10;
5
        public MyAnnotation name(String m_name) {
6
            this.m_name = m_name;
7
            return this;
8
        }
9
        public String name() {
10
            return this.m_name;
11
        }
12
        public MyAnnotation number(Integer m_number) {
13
            this.m_number = m_number;
14
            return this;
15
        }
16
        public Integer number() {
17
            return this.m_number;
18
        }
19
    }
20
}

Sweet Annotations

public class SweetAnnotations implements Sweet.Annotations {
    private final Map<String, List<Object>> annotations = new Map<String, List<Object>>();

    public List<Object> getAnnotations(String name) {
        List<Object> aList = annotations.get(name);
        return aList == null ? new List<Object>() : aList;
    }

    public Object getAnnotation(String name) {
        List<Object> aList = getAnnotations(name);
        return aList.isEmpty() ? null : aList.get(0);
    }

    private void registerAnnotation(String targetName, Object annotation) {
        List<Object> aList = annotations.get(targetName);
        if(aList == null) {
            aList = new List<Object>();
        }
        aList.add(annotation);
        annotations.put(targetName, aList);
    }

    {
        registerAnnotation(AnnotationDemo.class.getName(), new AnnotationDemo.MyAnnotation().name('Test'));
    }
}
26
 
1
public class SweetAnnotations implements Sweet.Annotations {
2
    private final Map<String, List<Object>> annotations = new Map<String, List<Object>>();
3
​
4
    public List<Object> getAnnotations(String name) {
5
        List<Object> aList = annotations.get(name);
6
        return aList == null ? new List<Object>() : aList;
7
    }
8
​
9
    public Object getAnnotation(String name) {
10
        List<Object> aList = getAnnotations(name);
11
        return aList.isEmpty() ? null : aList.get(0);
12
    }
13
​
14
    private void registerAnnotation(String targetName, Object annotation) {
15
        List<Object> aList = annotations.get(targetName);
16
        if(aList == null) {
17
            aList = new List<Object>();
18
        }
19
        aList.add(annotation);
20
        annotations.put(targetName, aList);
21
    }
22
​
23
    {
24
        registerAnnotation(AnnotationDemo.class.getName(), new AnnotationDemo.MyAnnotation().name('Test'));
25
    }
26
}

Usage

Define custom annotations.

public @interface MyAnnotation {
    public String name();

    public Integer number() default 10;
}
5
 
1
public @interface MyAnnotation {
2
    public String name();
3
​
4
    public Integer number() default 10;
5
}

Apply custom annotations on classes.

@MyAnnotation(name='Test')
public class AnnotationDemo {
}
3
 
1
@MyAnnotation(name='Test')
2
public class AnnotationDemo {
3
}

Retrieve annotation information from the instance of the class.

AnnotationDemo demo = new AnnotationDemo();
AnnotationDemo.MyAnnotation myAnn = (AnnotationDemo.MyAnnotation)Sweet.getAnnotation(demo);
System.debug(myAnn.name());
3
 
1
AnnotationDemo demo = new AnnotationDemo();
2
AnnotationDemo.MyAnnotation myAnn = (AnnotationDemo.MyAnnotation)Sweet.getAnnotation(demo);
3
System.debug(myAnn.name());

Contribute on Github! Edit this section.

  • Feature Overview3 sec read
  • Prerequisite1 sec read
  • Sweet Apex Example8 sec read
  • Transpiled Apex22 sec read
  • Sweet Annotations37 sec read
  • Usage18 sec read
    Copy