An open API service indexing awesome lists of open source software.

https://github.com/agentgill/reflection


https://github.com/agentgill/reflection

Last synced: 2 months ago
JSON representation

Awesome Lists containing this project

README

        

# Apex Reflection Convention

Apex lacks native reflection, but it's still semi-possible with a shared convention. This is interesting for ISV and cross namespace applications. We can avoid extension packages and compile-time bindings on invoked code.

### Goals

- Be discoverable
- Avoid package dependencies

### Introducing StubProvider

Developers can achieve cheap reflection in Apex by using the StubProvider system interface as a shared entry point.

```c#
Object handleMethodCall(
Object stub,
String method,
Type returnType,
List argTypes,
List argNames,
List argValues
)
```

The class to be executed is instrumented with `handleMethodCall` which delegates to methods made available.

### Discovering Methods

```c#
Type reflector = Type.forName('my_ns.Extension')
StubProvider impl = (StubProvider)reflector.newInstance();
List> supportedMethods = impl.handleMethodCall(impl, 'reflect', null, null, null, null);
```

### Invoking Methods

```c#
String result = impl.handleMethodCall(impl, 'exampleOne', null, null, null, null);
```

### Defining Methods

```c#
global class Extension implements StubProvider {

/**
* Actual method
*/
String exampleOne(String arg1)
{
return arg1 + arg1;
}

/**
* Actual method
*/
Decimal exampleTwo(Decimal arg2)
{
return arg2 * arg2;
}

/**
* Advertises supported methods
*/
List> reflect()
{
return new List>
{
new List{
'exampleOne', // method
String.class, // return type
new List{String.class}, // arg types
new List{'arg1'} // arg names
},
new List{
'exampleTwo', // method
String.class, // return type
new List{Decimal.class}, // arg types
new List{'arg2'} // arg names
}
}
}

/**
* Dispatches actual methods
*/
global Object handleMethodCall(Object stub, String method, Type returnType, List argTypes, List argNames, List argValues)
{
if (method == 'reflect') return this.reflect();
if (method == 'exampleOne') return this.exampleOne((String)argValues[0]);
if (method == 'exampleTwo') return this.exampleTwo((Decimal)argValues[0]);
throw new StringException('method not implemented');
}

}
```