use of net.sourceforge.pmd.lang.apex.ast.ASTUserClass in project pmd by pmd.
the class ApexCRUDViolationRule method checkForAccessibility.
private void checkForAccessibility(final ASTSoqlExpression node, Object data) {
final boolean isCount = node.getNode().getCanonicalQuery().startsWith("SELECT COUNT()");
final Set<String> typesFromSOQL = getTypesFromSOQLQuery(node);
final Set<ASTMethodCallExpression> prevCalls = getPreviousMethodCalls(node);
for (ASTMethodCallExpression prevCall : prevCalls) {
collectCRUDMethodLevelChecks(prevCall);
}
boolean isGetter = false;
String returnType = null;
final ASTMethod wrappingMethod = node.getFirstParentOfType(ASTMethod.class);
final ASTUserClass wrappingClass = node.getFirstParentOfType(ASTUserClass.class);
if (isCount || wrappingClass != null && Helper.isTestMethodOrClass(wrappingClass) || wrappingMethod != null && Helper.isTestMethodOrClass(wrappingMethod)) {
return;
}
if (wrappingMethod != null) {
isGetter = isMethodAGetter(wrappingMethod);
returnType = getReturnType(wrappingMethod);
}
final ASTVariableDeclaration variableDecl = node.getFirstParentOfType(ASTVariableDeclaration.class);
if (variableDecl != null) {
String type = variableDecl.getNode().getLocalInfo().getType().getApexName();
type = getSimpleType(type);
StringBuilder typeCheck = new StringBuilder().append(variableDecl.getNode().getDefiningType().getApexName()).append(":").append(type);
if (!isGetter) {
if (typesFromSOQL.isEmpty()) {
validateCRUDCheckPresent(node, data, ANY, typeCheck.toString());
} else {
for (String typeFromSOQL : typesFromSOQL) {
validateCRUDCheckPresent(node, data, ANY, typeFromSOQL);
}
}
}
}
final ASTAssignmentExpression assignment = node.getFirstParentOfType(ASTAssignmentExpression.class);
if (assignment != null) {
final ASTVariableExpression variable = assignment.getFirstChildOfType(ASTVariableExpression.class);
if (variable != null) {
String variableWithClass = Helper.getFQVariableName(variable);
if (varToTypeMapping.containsKey(variableWithClass)) {
String type = varToTypeMapping.get(variableWithClass);
if (!isGetter) {
if (typesFromSOQL.isEmpty()) {
validateCRUDCheckPresent(node, data, ANY, type);
} else {
for (String typeFromSOQL : typesFromSOQL) {
validateCRUDCheckPresent(node, data, ANY, typeFromSOQL);
}
}
}
}
}
}
final ASTReturnStatement returnStatement = node.getFirstParentOfType(ASTReturnStatement.class);
if (returnStatement != null) {
if (!isGetter) {
if (typesFromSOQL.isEmpty()) {
validateCRUDCheckPresent(node, data, ANY, returnType);
} else {
for (String typeFromSOQL : typesFromSOQL) {
validateCRUDCheckPresent(node, data, ANY, typeFromSOQL);
}
}
}
}
}
use of net.sourceforge.pmd.lang.apex.ast.ASTUserClass in project pmd by pmd.
the class ApexCRUDViolationRule method checkForCRUD.
private void checkForCRUD(final AbstractApexNode<?> node, final Object data, final String crudMethod) {
final Set<ASTMethodCallExpression> prevCalls = getPreviousMethodCalls(node);
for (ASTMethodCallExpression prevCall : prevCalls) {
collectCRUDMethodLevelChecks(prevCall);
}
final ASTMethod wrappingMethod = node.getFirstParentOfType(ASTMethod.class);
final ASTUserClass wrappingClass = node.getFirstParentOfType(ASTUserClass.class);
if (wrappingClass != null && Helper.isTestMethodOrClass(wrappingClass) || wrappingMethod != null && Helper.isTestMethodOrClass(wrappingMethod)) {
return;
}
final ASTNewKeyValueObjectExpression newObj = node.getFirstChildOfType(ASTNewKeyValueObjectExpression.class);
if (newObj != null) {
final String type = Helper.getFQVariableName(newObj);
validateCRUDCheckPresent(node, data, crudMethod, type);
}
final ASTVariableExpression variable = node.getFirstChildOfType(ASTVariableExpression.class);
if (variable != null) {
final String type = varToTypeMapping.get(Helper.getFQVariableName(variable));
if (type != null) {
StringBuilder typeCheck = new StringBuilder().append(node.getNode().getDefiningType().getApexName()).append(":").append(type);
validateCRUDCheckPresent(node, data, crudMethod, typeCheck.toString());
}
}
}
use of net.sourceforge.pmd.lang.apex.ast.ASTUserClass in project pmd by pmd.
the class ApexProjectMirrorTest method visitWith.
private List<Integer> visitWith(ApexNode<Compilation> acu, final boolean force) {
final ApexProjectMemoizer toplevel = ApexMetrics.getFacade().getLanguageSpecificProjectMemoizer();
final List<Integer> result = new ArrayList<>();
acu.jjtAccept(new ApexParserVisitorAdapter() {
@Override
public Object visit(ASTMethod node, Object data) {
MetricMemoizer<ASTMethod> op = toplevel.getOperationMemoizer(node.getQualifiedName());
result.add((int) ApexMetricsComputer.INSTANCE.computeForOperation(opMetricKey, node, force, MetricOptions.emptyOptions(), op));
return super.visit(node, data);
}
@Override
public Object visit(ASTUserClass node, Object data) {
MetricMemoizer<ASTUserClassOrInterface<?>> clazz = toplevel.getClassMemoizer(node.getQualifiedName());
result.add((int) ApexMetricsComputer.INSTANCE.computeForType(classMetricKey, node, force, MetricOptions.emptyOptions(), clazz));
return super.visit(node, data);
}
}, null);
return result;
}
use of net.sourceforge.pmd.lang.apex.ast.ASTUserClass in project pmd by pmd.
the class TooManyFieldsRule method visit.
@Override
public Object visit(ASTUserClass node, Object data) {
int maxFields = getProperty(MAX_FIELDS_DESCRIPTOR);
stats = new HashMap<>(5);
nodes = new HashMap<>(5);
List<ASTField> l = node.findDescendantsOfType(ASTField.class);
for (ASTField fd : l) {
if (fd.getNode().getModifierInfo().all(FINAL, STATIC)) {
continue;
}
ASTUserClass clazz = fd.getFirstParentOfType(ASTUserClass.class);
if (clazz != null) {
bumpCounterFor(clazz);
}
}
for (Map.Entry<String, Integer> entry : stats.entrySet()) {
int val = entry.getValue();
Node n = nodes.get(entry.getKey());
if (val > maxFields) {
addViolation(data, n);
}
}
return data;
}
Aggregations