Open
Description
Affects PMD Version:
All
Rule:
ApexCrudViolation
Description:
Hello, I have encountered what I believe to be a false positive in the ApexCRUDViolation rule. Specifically the READ portion of that rule (or an isAccessible() check in Apex). I created a helper method so that I could dynamically do this check rather than hardcode every single field that I'm querying for. However, this still causes ApexCRUDViolation to fail its read check.
Code Sample demonstrating the issue:
Helper method:
public with sharing class Utilities {
public static void checkReadAccess(Map<String,Set<String>> fieldMap){
Set<String> keySet = fieldMap.keySet();
for(String key : keySet){
Schema.SObjectType objectType = Schema.getGlobalDescribe().get(key);
Schema.DescribeSObjectResult describeSObject = objectType.getDescribe();
if(!describeSObject.isAccessible()){
throw new System.NoAccessException();
}
Map<String,Schema.SObjectField> schemaFieldMap = describeSObject.fields.getMap();
for(String fieldName : fieldMap.get(key)){
Schema.DescribeFieldResult describeField = schemaFieldMap.get(fieldName).getDescribe();
if(!describeField.isAccessible()){
throw new System.NoAccessException();
}
}
}
}
And then to call it:
Set<String> fieldSet;
fieldSet = new Set<String>{'Id','Name'};
Map<String,Set<String>> fieldMap;
fieldMap = new Map<String,Set<String>>{'Account'=>fieldSet};
Utilities.checkReadAccess(fieldMap);
List<Account> templateList = [SELECT Id, Name FROM Account];
Running PMD through: Codacy