Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have created an Apex InterfaceFactory that the user can use to get an instance of a specified implementation of an interface. For this I use the System.Type class. The actual name of the class can be set in a custom setting. I wanted to be able to pass some parameters to the constructor of the dynamically instantiated class. In short; this is the code I'm talking about:

public static MyInterface GetMyInterfaceImplementationByClassName(String settingName){
    String implementationClassName;
    Type t;
    MyInterface implementation = null;

    //get the actual class name from the custom setting
    Interface_Implementations__c settings = Interface_Implementations__c.getInstance(settingName);

    //just make sure there actually is a className found
    if (settings != null && settings.Class__c != null) {
        implementationClassName = settings.Class__c;
        //get the type object for the class name retrieved from the custom setting
        t = Type.forName(implementationClassName);
        //this line is where my question comes in; how can I pass parameters here
        implementation = (MyInterface)t.newInstance();
    }

    return implementation;
}
share|improve this question
    
Sadly, this cannot currently be done in Apex. –  user293 Jan 7 '13 at 16:03
add comment

2 Answers

up vote 2 down vote accepted

Sadly, this cannot currently be done in Apex. It would be great to be able to say pass parameters to the newInstance method but it is not currently possible.

Link to documentation here:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_type.htm

Which doesn't explicitly say it isn't possible, but given there are no documentation i think that implies it isn't (and certainly, better men than me have told me it isn't :).

You could maybe try another approach and have a common setters on your base interface and inject parameters accordingly?

EDIT: Some more dark arts reflection tricks can be found on this link (see solution provided by @zachelrath):

Managed Package Integration without Extensions or Dependencies

share|improve this answer
    
I was thinking of using the getter-and-setter-appraoch, but constructor parameters would be so much nicer... –  Lex Jan 7 '13 at 16:24
    
@Lex tell me about it –  user293 Jan 7 '13 at 16:25
    
Getters and setters are your only manner of doing so at this point, SFDC will likely expose it at a future date. In Java it is done like so: Class.getDeclaredConstructors(types list).newInstance(args list); –  Mark Pond Jan 7 '13 at 16:47
add comment

I have simulated this many times by making my dynamically instantiated classes implement an interface that declares something like init(String p1, String p2) and then store those initialization params in the same place that the class is stored.

It's barely much more work than if the reflection API included passing parameters to constructors.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.