-
Notifications
You must be signed in to change notification settings - Fork 181
Safer type.forName #149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Safer type.forName #149
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -127,11 +127,20 @@ public with sharing virtual class FinalizerHandler { | |
) { | ||
Object dynamicInstance; | ||
String className = finalizerMetadata.Apex_Class_Name__c; | ||
String namespace = finalizerMetadata.Apex_Class_Namespace__c; | ||
if (FinalizerHandler.isBypassed(className)) { | ||
return; | ||
} | ||
|
||
try { | ||
dynamicInstance = Type.forName(className).newInstance(); | ||
try { | ||
dynamicInstance = Type.forName(namespace, className).newInstance(); | ||
} catch (Exception e) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we catch an explicit type of exception here? |
||
// fallback to package namespace to not break existing implementations that are distributed in a namespace | ||
String[] parts = String.valueOf(FinalizerHandler.class).split('\\.', 2); | ||
namespace = parts.size() == 2 ? parts[0] : ''; | ||
dynamicInstance = Type.forName(namespace, className).newInstance(); | ||
} | ||
} catch (System.NullPointerException e) { | ||
handleFinalizerException(INVALID_CLASS_ERROR_FINALIZER, className); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,6 +86,7 @@ public inherited sharing class MetadataTriggerHandler extends TriggerBase implem | |
private static final String QUERY_TEMPLATE = String.join( | ||
new List<String>{ | ||
'SELECT Apex_Class_Name__c,', | ||
'Apex_Class_Namespace__c,', | ||
'Order__c,', | ||
'Flow_Name__c,', | ||
'Bypass_Permission__c,', | ||
|
@@ -385,8 +386,15 @@ public inherited sharing class MetadataTriggerHandler extends TriggerBase implem | |
for (Trigger_Action__mdt triggerMetadata : actionMetadata) { | ||
Object triggerAction; | ||
try { | ||
triggerAction = Type.forName(triggerMetadata.Apex_Class_Name__c) | ||
.newInstance(); | ||
String namespace = triggerMetadata.Apex_Class_Namespace__c; | ||
try { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Double nested try blocks? |
||
triggerAction = Type.forName(namespace, triggerMetadata.Apex_Class_Name__c).newInstance(); | ||
} catch (Exception e) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we catch an explicit type of exception here? |
||
// fallback to package namespace to not break existing implementations that are distributed in a namespace | ||
String[] parts = String.valueOf(MetadataTriggerHandler.class).split('\\.', 2); | ||
namespace = parts.size() == 2 ? parts[0] : ''; | ||
triggerAction = Type.forName(namespace, triggerMetadata.Apex_Class_Name__c).newInstance(); | ||
} | ||
if (triggerMetadata.Flow_Name__c != null) { | ||
((TriggerActionFlow) triggerAction) | ||
.flowName = triggerMetadata.Flow_Name__c; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
<fullName>Apex_Class_Namespace__c</fullName> | ||
<description>Enter the apex class namespace</description> | ||
<externalId>false</externalId> | ||
<fieldManageability>SubscriberControlled</fieldManageability> | ||
<inlineHelpText>Enter the apex class namespace</inlineHelpText> | ||
<label>Apex Class Namespace</label> | ||
<length>15</length> | ||
<required>false</required> | ||
<type>Text</type> | ||
<unique>false</unique> | ||
</CustomField> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
<fullName>Apex_Class_Namespace__c</fullName> | ||
<description>Enter the apex class namespace</description> | ||
<externalId>false</externalId> | ||
<fieldManageability>SubscriberControlled</fieldManageability> | ||
<inlineHelpText>Enter the apex class namespace</inlineHelpText> | ||
<label>Apex Class Namespace</label> | ||
<length>15</length> | ||
<required>false</required> | ||
<type>Text</type> | ||
<unique>false</unique> | ||
</CustomField> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Double nested try blocks?