forked from phoedos/pmd
Merge pull request #4092 from aaronhurst-google:enum-qualified-names
[apex] Implement ApexQualifiableNode for ASTUserEnum #4092
This commit is contained in:
@ -1053,7 +1053,8 @@
|
||||
"avatar_url": "https://avatars.githubusercontent.com/u/86377278?v=4",
|
||||
"profile": "https://github.com/aaronhurst-google",
|
||||
"contributions": [
|
||||
"bug"
|
||||
"bug",
|
||||
"code"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -738,7 +738,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
|
||||
<td align="center"><a href="https://www.linkedin.com/in/lazylead"><img src="https://avatars.githubusercontent.com/u/1651114?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Yurii Dubinka</b></sub></a><br /><a href="https://github.com/pmd/pmd/issues?q=author%3Adgroup" title="Bug reports">🐛</a></td>
|
||||
<td align="center"><a href="https://github.com/zolyfarkas"><img src="https://avatars.githubusercontent.com/u/144085?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Zoltan Farkas</b></sub></a><br /><a href="https://github.com/pmd/pmd/issues?q=author%3Azolyfarkas" title="Bug reports">🐛</a></td>
|
||||
<td align="center"><a href="https://github.com/Zustin"><img src="https://avatars.githubusercontent.com/u/87302257?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Zustin</b></sub></a><br /><a href="https://github.com/pmd/pmd/issues?q=author%3AZustin" title="Bug reports">🐛</a></td>
|
||||
<td align="center"><a href="https://github.com/aaronhurst-google"><img src="https://avatars.githubusercontent.com/u/86377278?v=4?s=100" width="100px;" alt=""/><br /><sub><b>aaronhurst-google</b></sub></a><br /><a href="https://github.com/pmd/pmd/issues?q=author%3Aaaronhurst-google" title="Bug reports">🐛</a></td>
|
||||
<td align="center"><a href="https://github.com/aaronhurst-google"><img src="https://avatars.githubusercontent.com/u/86377278?v=4?s=100" width="100px;" alt=""/><br /><sub><b>aaronhurst-google</b></sub></a><br /><a href="https://github.com/pmd/pmd/issues?q=author%3Aaaronhurst-google" title="Bug reports">🐛</a> <a href="https://github.com/pmd/pmd/commits?author=aaronhurst-google" title="Code">💻</a></td>
|
||||
<td align="center"><a href="https://github.com/alexmodis"><img src="https://avatars.githubusercontent.com/u/60091729?v=4?s=100" width="100px;" alt=""/><br /><sub><b>alexmodis</b></sub></a><br /><a href="https://github.com/pmd/pmd/issues?q=author%3Aalexmodis" title="Bug reports">🐛</a></td>
|
||||
<td align="center"><a href="http://andreoss.sdf.org/"><img src="https://avatars.githubusercontent.com/u/49783909?v=4?s=100" width="100px;" alt=""/><br /><sub><b>andreoss</b></sub></a><br /><a href="https://github.com/pmd/pmd/issues?q=author%3Aandreoss" title="Bug reports">🐛</a></td>
|
||||
</tr>
|
||||
|
@ -40,6 +40,7 @@ This is a {{ site.pmd.release_type }} release.
|
||||
|
||||
* [#4081](https://github.com/pmd/pmd/pull/4081): \[apex] Remove Jorje leaks outside `ast` package - [@eklimo](https://github.com/eklimo)
|
||||
* [#4083](https://github.com/pmd/pmd/pull/4083): \[java] UnnecessaryImport false positive for on-demand imports of nested classes (fix for #4082) - [@abyss638](https://github.com/abyss638)
|
||||
* [#4092](https://github.com/pmd/pmd/pull/4092): \[apex] Implement ApexQualifiableNode for ASTUserEnum - [@aaronhurst-google](https://github.com/aaronhurst-google)
|
||||
|
||||
{% endtocmaker %}
|
||||
|
||||
|
@ -8,7 +8,9 @@ import net.sourceforge.pmd.annotation.InternalApi;
|
||||
|
||||
import apex.jorje.semantic.ast.compilation.UserEnum;
|
||||
|
||||
public class ASTUserEnum extends ApexRootNode<UserEnum> {
|
||||
public class ASTUserEnum extends ApexRootNode<UserEnum> implements ApexQualifiableNode {
|
||||
|
||||
private ApexQualifiedName qname;
|
||||
|
||||
@Deprecated
|
||||
@InternalApi
|
||||
@ -30,4 +32,20 @@ public class ASTUserEnum extends ApexRootNode<UserEnum> {
|
||||
public ASTModifierNode getModifiers() {
|
||||
return getFirstChildOfType(ASTModifierNode.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ApexQualifiedName getQualifiedName() {
|
||||
if (qname == null) {
|
||||
|
||||
ASTUserClass parent = this.getFirstParentOfType(ASTUserClass.class);
|
||||
|
||||
if (parent != null) {
|
||||
qname = ApexQualifiedName.ofNestedEnum(parent.getQualifiedName(), this);
|
||||
} else {
|
||||
qname = ApexQualifiedName.ofOuterEnum(this);
|
||||
}
|
||||
}
|
||||
|
||||
return qname;
|
||||
}
|
||||
}
|
||||
|
@ -148,6 +148,20 @@ public final class ApexQualifiedName implements QualifiedName {
|
||||
}
|
||||
|
||||
|
||||
static ApexQualifiedName ofOuterEnum(ASTUserEnum astUserEnum) {
|
||||
String ns = astUserEnum.getNamespace();
|
||||
String[] classes = {astUserEnum.getImage()};
|
||||
return new ApexQualifiedName(StringUtils.isEmpty(ns) ? "c" : ns, classes, null);
|
||||
}
|
||||
|
||||
|
||||
static ApexQualifiedName ofNestedEnum(ApexQualifiedName parent, ASTUserEnum astUserEnum) {
|
||||
String[] classes = Arrays.copyOf(parent.classes, parent.classes.length + 1);
|
||||
classes[classes.length - 1] = astUserEnum.getImage();
|
||||
return new ApexQualifiedName(parent.nameSpace, classes, null);
|
||||
}
|
||||
|
||||
|
||||
private static String getOperationString(ASTMethod node) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(node.getImage()).append('(');
|
||||
@ -171,18 +185,28 @@ public final class ApexQualifiedName implements QualifiedName {
|
||||
|
||||
|
||||
static ApexQualifiedName ofMethod(ASTMethod node) {
|
||||
ASTUserClassOrInterface<?> parent = node.getFirstParentOfType(ASTUserClassOrInterface.class);
|
||||
if (parent == null) {
|
||||
ASTUserTrigger trigger = node.getFirstParentOfType(ASTUserTrigger.class);
|
||||
String ns = trigger.getNamespace();
|
||||
String targetObj = trigger.getTargetName();
|
||||
|
||||
return new ApexQualifiedName(StringUtils.isEmpty(ns) ? "c" : ns, new String[]{"trigger", targetObj}, trigger.getImage()); // uses a reserved word as a class name to prevent clashes
|
||||
|
||||
} else {
|
||||
ApexQualifiedName baseName = parent.getQualifiedName();
|
||||
// Check first, as enum must be innermost potential parent
|
||||
ASTUserEnum enumParent = node.getFirstParentOfType(ASTUserEnum.class);
|
||||
if (enumParent != null) {
|
||||
ApexQualifiedName baseName = enumParent.getQualifiedName();
|
||||
|
||||
return new ApexQualifiedName(baseName.nameSpace, baseName.classes, getOperationString(node));
|
||||
}
|
||||
|
||||
ASTUserClassOrInterface<?> classParent = node.getFirstParentOfType(ASTUserClassOrInterface.class);
|
||||
if (classParent != null) {
|
||||
ApexQualifiedName baseName = classParent.getQualifiedName();
|
||||
|
||||
return new ApexQualifiedName(baseName.nameSpace, baseName.classes, getOperationString(node));
|
||||
}
|
||||
|
||||
ASTUserTrigger triggerParent = node.getFirstParentOfType(ASTUserTrigger.class);
|
||||
if (triggerParent != null) {
|
||||
String ns = triggerParent.getNamespace();
|
||||
String targetObj = triggerParent.getTargetName();
|
||||
|
||||
return new ApexQualifiedName(StringUtils.isEmpty(ns) ? "c" : ns, new String[]{"trigger", targetObj}, triggerParent.getImage()); // uses a reserved word as a class name to prevent clashes
|
||||
}
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ -94,4 +95,32 @@ public class ApexQualifiedNameTest extends ApexParserTestBase {
|
||||
assertEquals("c__trigger.Account#myAccountTrigger", m.getQualifiedName().toString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testUnqualifiedEnum() {
|
||||
ApexNode<?> root = parse("public enum primaryColor { RED, YELLOW, BLUE }");
|
||||
|
||||
ApexQualifiedName enumQName = ASTUserEnum.class.cast(root).getQualifiedName();
|
||||
List<ASTMethod> methods = root.findDescendantsOfType(ASTMethod.class);
|
||||
|
||||
assertEquals("c__primaryColor", enumQName.toString());
|
||||
for (ASTMethod m : methods) {
|
||||
assertTrue(m.getQualifiedName().toString().startsWith("c__primaryColor#"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQualifiedEnum() {
|
||||
ApexNode<?> root = parse("public class Outer { public enum Inner { OK } }");
|
||||
|
||||
ASTUserEnum enumNode = root.getFirstDescendantOfType(ASTUserEnum.class);
|
||||
ApexQualifiedName enumQName = enumNode.getQualifiedName();
|
||||
List<ASTMethod> methods = enumNode.findDescendantsOfType(ASTMethod.class);
|
||||
|
||||
assertEquals("c__Outer.Inner", enumQName.toString());
|
||||
for (ASTMethod m : methods) {
|
||||
assertTrue(m.getQualifiedName().toString().startsWith("c__Outer.Inner#"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user