6

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.

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

1

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

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')

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.

5

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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