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

Reflection Guide

Reflection

Feature Overview

This feature generates reflection code for a class.

Prerequisite

None

Sweet Apex Example

@reflect
public class ReflectDemo {
    private static String version = '1.0';
    private String name;
    private Integer count;

    public ReflectDemo() {

    }

    public String getName() {
        return this.name;
    }

    public Integer getCount() {
        return this.count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    public static void test() {
        ReflectDemo demo = new ReflectDemo();
        Sweet.Reflection reflection = Sweet.reflect(demo);
    }
}

Transpiled Apex

public class ReflectDemo implements Sweet.Reflectable {
    private static String version = '1.0';
    private String name;
    private Integer count;
    public ReflectDemo() {

    }
    public String getName() {
        return this.name;
    }
    public Integer getCount() {
        return this.count;
    }
    public void setCount(Integer count) {
        this.count = count;
    }
    public static void test() {
        ReflectDemo demo = new ReflectDemo();
        Sweet.Reflection reflection = Sweet.reflect(demo);
    }

    public List reflect_getFieldNames() {
        return new List{ 'name', 'count' };
    }

    public Object reflect_getFieldValue(String name) {
        if(name == 'name') {
            return this.name;
        } else {
            if(name == 'count') {
                return this.count;
            } else {
                throw new Sweet.SweetException('Field ' + name + ' does not exist');
            }
        }
    }

    public void reflect_setFieldValue(String name, Object value) {
        if(name == 'name') {
            this.name = (String)value;
        } else {
            if(name == 'count') {
                this.count = (Integer)value;
            } else {
                throw new Sweet.SweetException('Field ' + name + ' does not exist');
            }
        }
    }

    public List reflect_getMethodNames() {
        return new List{ 'getName', 'getCount', 'setCount' };
    }

    public Object reflect_invokeMethod(String name, List args) {
        if(name == 'getName') {
            return this.getName();
        } else {
            if(name == 'getCount') {
                return this.getCount();
            } else {
                if(name == 'setCount') {
                    this.setCount((Integer)args.get(0));
                } else {
                    throw new Sweet.SweetException('Method ' + name + ' does not exist');
                }
            }
        }
        return null;
    }
}

Usage

@reflect can only generate reflection methods on non-static fields and methods(excluding constructors).

Here is how you can use the reflection.

Sweet.Reflection r = Sweet.reflect(target);
Object value = r.getFieldValue('name');

Contribute on Github! Edit this section.