From 91d0ebf4620b9cda44ce6f02232fe8a003d82efd Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Thu, 28 Mar 2019 21:04:49 +0100 Subject: [PATCH] [apex] Expose parameter names of NewKeyValueObjectExpression --- .../lang/apex/ast/ASTLiteralExpression.java | 35 ++++++++++++++++++ .../ast/ASTNewKeyValueObjectExpression.java | 8 +++++ .../pmd/lang/apex/rule/security/Helper.java | 30 +++++----------- .../ASTNewKeyValueObjectExpressionTest.java | 36 +++++++++++++++++++ 4 files changed, 87 insertions(+), 22 deletions(-) create mode 100644 pmd-apex/src/test/java/net/sourceforge/pmd/lang/apex/ast/ASTNewKeyValueObjectExpressionTest.java diff --git a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/ASTLiteralExpression.java b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/ASTLiteralExpression.java index ab76f960f0..1ea93d98f1 100644 --- a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/ASTLiteralExpression.java +++ b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/ASTLiteralExpression.java @@ -4,8 +4,13 @@ package net.sourceforge.pmd.lang.apex.ast; +import java.lang.reflect.Field; +import java.util.Optional; + +import apex.jorje.data.Identifier; import apex.jorje.data.ast.LiteralType; import apex.jorje.semantic.ast.expression.LiteralExpression; +import apex.jorje.semantic.ast.expression.NewKeyValueObjectExpression.NameValueParameter; public class ASTLiteralExpression extends AbstractApexNode { @@ -59,4 +64,34 @@ public class ASTLiteralExpression extends AbstractApexNode { } return null; } + + public String getName() { + if (jjtGetParent() instanceof ASTNewKeyValueObjectExpression) { + ASTNewKeyValueObjectExpression parent = (ASTNewKeyValueObjectExpression) jjtGetParent(); + try { + Field exprField = NameValueParameter.class.getDeclaredField("expression"); + exprField.setAccessible(true); + Optional parameter = parent.node.getParameters().stream().filter(p -> { + try { + return exprField.get(p) == this.node; + } catch (IllegalArgumentException | IllegalAccessException e) { + return false; + } + }).findFirst(); + + Field nameField = NameValueParameter.class.getDeclaredField("name"); + nameField.setAccessible(true); + return parameter.map(p -> { + try { + return (Identifier) nameField.get(p); + } catch (IllegalArgumentException | IllegalAccessException e) { + return null; + } + }).map(Identifier::getValue).orElse(null); + } catch (NoSuchFieldException | SecurityException e1) { + return null; + } + } + return null; + } } diff --git a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/ASTNewKeyValueObjectExpression.java b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/ASTNewKeyValueObjectExpression.java index 60269d4510..9ffa7b764e 100644 --- a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/ASTNewKeyValueObjectExpression.java +++ b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/ASTNewKeyValueObjectExpression.java @@ -16,4 +16,12 @@ public class ASTNewKeyValueObjectExpression extends AbstractApexNode node = parse("public class Foo { \n" + + " public void foo(String newName, String tempID) { \n" + + " if (Contact.sObjectType.getDescribe().isCreateable() && Contact.sObjectType.getDescribe().isUpdateable()) {\n" + + " upsert new Contact(FirstName = 'First', LastName = 'Last', Phone = '414-414-4414');\n" + + " }\n" + " } \n" + "}"); + + ASTNewKeyValueObjectExpression keyValueExpr = node.getFirstDescendantOfType(ASTNewKeyValueObjectExpression.class); + Assert.assertEquals(3, keyValueExpr.getParameterCount()); + + List literals = keyValueExpr.findDescendantsOfType(ASTLiteralExpression.class); + Assert.assertEquals(3, literals.size()); + Assert.assertEquals("FirstName", literals.get(0).getName()); + Assert.assertEquals("LastName", literals.get(1).getName()); + Assert.assertEquals("Phone", literals.get(2).getName()); + } + +}