https://github.com/agentgill/reflection
https://github.com/agentgill/reflection
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/agentgill/reflection
- Owner: agentgill
- Created: 2019-03-26T17:30:06.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2018-07-27T13:35:44.000Z (almost 7 years ago)
- Last Synced: 2025-01-18T13:38:35.837Z (4 months ago)
- Size: 1.95 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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');
}
}
```