Silence the AvoidGlobalModifier warning in @RestResource Apex controller

Silence the AvoidGlobalModifier warning in @RestResource Apex controller



I have a simple Apex Controller annotated with @RestResource. According to the documentation, this must be declared global, but the static analysis complains due to the AvoidGlobalModifier PMD rule.


@RestResource


global


AvoidGlobalModifier



Is there some other annotation I need to use to avoid the warning when my code goes through static analysis?




2 Answers
2



You can just use the @SuppressWarning annotation. By convention, we include a comment to say why.


@SuppressWarning



For example, I have this in some of my code where the class represents data that I get back from SendGrid:


@SuppressWarnings('PMD.VariableNamingConventions') // We have to use the names chosen by SendGrid



So, I guess you would use something like @SuppressWarnings('PMD.AvoidGlobalModifier')


@SuppressWarnings('PMD.AvoidGlobalModifier')



I'm not entirely sure I've got the name right there, but you can get it from the message PMD gives you. I switch that rule off entirely via my xml ruleset because we use a lot of globals to communicate with a generic utils package we have.



Your use case needs you to expose a web service. A web service should be always global. I wont worrry much about that warning, as its informative more than obstructing your work. That being said you can change that rule.



The source code of that rule is present here:
https://github.com/pmd/pmd/blob/master/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/bestpractices/AvoidGlobalModifierRule.java



You can alter that rule's java file to not trigger if class contains RestResource or webservice keyword.


RestResource


webservice



Meanwhile, I have submitted it as an issue to PMD. Src: https://github.com/pmd/pmd/issues/1348



Edit : The above issue is fixed by PMD devs and the newest version you will download wil have it encorporated. No need to silence PMD rule now.
Src: https://github.com/pmd/pmd/pull/1353



Thanks for contributing an answer to Salesforce Stack Exchange!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.