Salesforce Asked on October 4, 2021
There is a critical update, Restrict Reflective Access to Non-Global Constructors in Packages
When this critical update is enabled, regardless of API version, you can initiate only Apex classes that have a no-arguments constructor that is visible to the code running Type.newInstance.
How: This critical update enables strict enforcement of Apex type visibility rules for the Type.newInstance method. It also removes the legacy compatibility behavior supporting these rules in API version 28.0 and earlier. Apex classes contained in second-generation packages can instantiate public classes from other packages in the same namespace only if they are annotated @NamespaceAccessible.
Also, regardless of API version, construction of abstract classes or interfaces is no longer allowed even when the type is visible and can be referenced using Type.forName. Such instantiation fails with this message: "ApexType does not have a no-arg constructor" where ApexType is the type you want to create using the Type.newInstance method.
It sounds as if I should no longer be able to use Type.forName
and Type.newInstance
to instantiate public (non-global) classes that are inside managed packages.
However, if I activate the critical update and create an apex class with the following code and with the Salesforce.com API version set to 27.0, the code will still find and instantiate the type successfully. Though as was the case even before the critical update was enabled, Type.forName
will return null if the API version is set to a newer version.
public class MyClass {
public static void foo() {
System.debug(Type.forName('MYNAMESPACE', 'MyPublicManagedClass').newInstance());
}
}
This also works from inside a managed package to access both unmanaged public classes and managed public classes belonging to another package, where both managed packages are 1st generation.
Is this working as expected?
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP