Skip to content

Instantly share code, notes, and snippets.

@zachelrath
Created October 29, 2012 21:32
Show Gist options
  • Save zachelrath/3976677 to your computer and use it in GitHub Desktop.
Save zachelrath/3976677 to your computer and use it in GitHub Desktop.
Dynamic Method invocation in Apex
public interface Executable {
void execute(String method);
}
public class DoAwesomeStuff implements Executable {
public override void execute(String method) {
if (method == 'method1') method1();
else if (method == 'method2') method2();
}
private void method1(){
/* method 1 body */
}
private void method2(){
/* method 2 body */
}
}
public class RidiculousStuff implements Executable {
public override void execute(String method) {
if (method == 'foo') foo();
else if (method == 'bar') bar();
}
private void foo(){
/* do foo */
}
private void bar(){
/* do bar */
}
}
public class Main {
public Main() {
Map<String,String> params = ApexPages.currentPage().getParameters();
System.Type t = System.Type.forName(params.get('class'));
if (t == null) return;
Executable x = (Executable) t.newInstance();
x.execute(params.get('method'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment