[apex] Expose parameter names of NewKeyValueObjectExpression

This commit is contained in:
Andreas Dangel
2019-03-28 21:04:49 +01:00
parent 680b164e10
commit 91d0ebf462
4 changed files with 87 additions and 22 deletions

View File

@ -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<LiteralExpression> {
@ -59,4 +64,34 @@ public class ASTLiteralExpression extends AbstractApexNode<LiteralExpression> {
}
return null;
}
public String getName() {
if (jjtGetParent() instanceof ASTNewKeyValueObjectExpression) {
ASTNewKeyValueObjectExpression parent = (ASTNewKeyValueObjectExpression) jjtGetParent();
try {
Field exprField = NameValueParameter.class.getDeclaredField("expression");
exprField.setAccessible(true);
Optional<NameValueParameter> 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;
}
}

View File

@ -16,4 +16,12 @@ public class ASTNewKeyValueObjectExpression extends AbstractApexNode<NewKeyValue
public Object jjtAccept(ApexParserVisitor visitor, Object data) {
return visitor.visit(this, data);
}
public String getType() {
return node.getTypeRef().getNames().get(0).getValue();
}
public int getParameterCount() {
return node.getParameters().size();
}
}

View File

@ -31,11 +31,9 @@ import net.sourceforge.pmd.lang.apex.ast.ApexNode;
import apex.jorje.data.Identifier;
import apex.jorje.data.ast.TypeRef;
import apex.jorje.semantic.ast.expression.MethodCallExpression;
import apex.jorje.semantic.ast.expression.NewKeyValueObjectExpression;
import apex.jorje.semantic.ast.expression.VariableExpression;
import apex.jorje.semantic.ast.member.Field;
import apex.jorje.semantic.ast.member.Parameter;
import apex.jorje.semantic.ast.statement.FieldDeclaration;
import apex.jorje.semantic.ast.statement.VariableDeclaration;
/**
@ -154,7 +152,8 @@ public final class Helper {
static String getFQVariableName(final ASTField variable) {
Field n = variable.getNode();
StringBuilder sb = new StringBuilder().append(n.getDefiningType().getApexName()).append(":")
StringBuilder sb = new StringBuilder()
.append(n.getDefiningType().getApexName()).append(":")
.append(n.getFieldInfo().getName());
return sb.toString();
}
@ -167,29 +166,16 @@ public final class Helper {
}
static String getFQVariableName(final ASTFieldDeclaration variable) {
FieldDeclaration n = variable.getNode();
String name = "";
try {
java.lang.reflect.Field f = n.getClass().getDeclaredField("name");
f.setAccessible(true);
Identifier nameField = (Identifier) f.get(n);
name = nameField.getValue();
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
throw new RuntimeException(e);
}
StringBuilder sb = new StringBuilder().append(n.getDefiningType().getApexName()).append(":").append(name);
StringBuilder sb = new StringBuilder()
.append(variable.getNode().getDefiningType().getApexName()).append(":")
.append(variable.getImage());
return sb.toString();
}
static String getFQVariableName(final ASTNewKeyValueObjectExpression variable) {
NewKeyValueObjectExpression n = variable.getNode();
TypeRef typeRef = n.getTypeRef();
String objType = typeRef.getNames().get(0).getValue();
StringBuilder sb = new StringBuilder().append(n.getDefiningType().getApexName()).append(":").append(objType);
StringBuilder sb = new StringBuilder()
.append(variable.getNode().getDefiningType().getApexName()).append(":")
.append(variable.getType());
return sb.toString();
}

View File

@ -0,0 +1,36 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.apex.ast;
import static net.sourceforge.pmd.lang.apex.ast.ApexParserTestHelpers.parse;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import apex.jorje.semantic.ast.compilation.Compilation;
public class ASTNewKeyValueObjectExpressionTest {
@Test
public void testParameterName() {
ApexNode<Compilation> 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<ASTLiteralExpression> 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());
}
}