Upgrade to 6.0.0 categories
This commit is contained in:
@ -1,26 +1,43 @@
|
||||
package net.sourceforge.pmd.lang.apex.rule.style;
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
import java.util.*;
|
||||
package net.sourceforge.pmd.lang.apex.rule.errorprone;
|
||||
|
||||
import net.sourceforge.pmd.lang.apex.ast.*;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import net.sourceforge.pmd.lang.apex.ast.ASTField;
|
||||
import net.sourceforge.pmd.lang.apex.ast.ASTMethod;
|
||||
import net.sourceforge.pmd.lang.apex.ast.ASTProperty;
|
||||
import net.sourceforge.pmd.lang.apex.ast.ASTUserClass;
|
||||
import net.sourceforge.pmd.lang.apex.ast.ASTUserEnum;
|
||||
import net.sourceforge.pmd.lang.apex.ast.ASTUserInterface;
|
||||
import net.sourceforge.pmd.lang.apex.ast.AbstractApexNode;
|
||||
import net.sourceforge.pmd.lang.apex.rule.AbstractApexRule;
|
||||
|
||||
import apex.jorje.semantic.ast.compilation.*;
|
||||
import apex.jorje.semantic.ast.member.*;
|
||||
import apex.jorje.semantic.ast.compilation.UserClass;
|
||||
import apex.jorje.semantic.ast.compilation.UserEnum;
|
||||
import apex.jorje.semantic.ast.compilation.UserInterface;
|
||||
import apex.jorje.semantic.ast.member.Field;
|
||||
import apex.jorje.semantic.ast.member.Method;
|
||||
import apex.jorje.semantic.ast.member.Property;
|
||||
import apex.jorje.semantic.ast.modifier.Annotation;
|
||||
import apex.jorje.semantic.ast.modifier.ModifierNode;
|
||||
import apex.jorje.semantic.symbol.type.*;
|
||||
import apex.jorje.semantic.symbol.type.AnnotationTypeInfos;
|
||||
import apex.jorje.semantic.symbol.type.StandardAnnotationTypeInfo;
|
||||
|
||||
/**
|
||||
* Apex supported non existent annotations for legacy reasons.
|
||||
* In the future, use of such non-existent annotations could result in broken apex code that will not copile.
|
||||
* In the future, use of such non-existent annotations could result in broken apex code that will not compile.
|
||||
* This will prevent users of garbage annotations from being able to use legitimate annotations added to apex in the future.
|
||||
* A full list of supported annotations can be found at https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation.htm
|
||||
*
|
||||
* @author a.subramanian
|
||||
*/
|
||||
public class AvoidNonExistentAnnotationsRule extends AbstractApexRule {
|
||||
|
||||
private static final Set<StandardAnnotationTypeInfo> supportedApexAnnotations = getSupportedApexAnnotations();
|
||||
private static final Set<StandardAnnotationTypeInfo> SUPPORTED_APEX_ANNOTATIONS = getSupportedApexAnnotations();
|
||||
|
||||
@Override
|
||||
public Object visit(final ASTUserClass node, final Object data) {
|
||||
@ -52,6 +69,7 @@ public class AvoidNonExistentAnnotationsRule extends AbstractApexRule {
|
||||
@Override
|
||||
public Object visit(final ASTProperty node, final Object data) {
|
||||
final Property property = node.getNode();
|
||||
// may have nested methods, don't visit children
|
||||
return checkForNonExistentAnnotation(node, property.getModifiersNode(), data);
|
||||
}
|
||||
|
||||
@ -61,9 +79,9 @@ public class AvoidNonExistentAnnotationsRule extends AbstractApexRule {
|
||||
return checkForNonExistentAnnotation(node, field.getModifiers(), data);
|
||||
}
|
||||
|
||||
private Object checkForNonExistentAnnotation(final AbstractApexNode node, final ModifierNode modifierNode, final Object data) {
|
||||
private Object checkForNonExistentAnnotation(final AbstractApexNode<?> node, final ModifierNode modifierNode, final Object data) {
|
||||
for (final Annotation annotation : modifierNode.getModifiers().getAnnotations()) {
|
||||
if (!supportedApexAnnotations.contains(annotation.getType())) {
|
||||
if (!SUPPORTED_APEX_ANNOTATIONS.contains(annotation.getType())) {
|
||||
addViolationWithMessage(data, node, "Use of non existent annotations will lead to broken Apex code which will not compile in the future.");
|
||||
}
|
||||
}
|
@ -109,5 +109,4 @@ trigger Accounts on Account (before insert, before update, before delete, after
|
||||
]]>
|
||||
</example>
|
||||
</rule>
|
||||
|
||||
</ruleset>
|
||||
|
@ -267,4 +267,25 @@ public class MyClass {
|
||||
</example>
|
||||
</rule>
|
||||
|
||||
<rule name="AvoidNonExistentAnnotations"
|
||||
since="6.5.0"
|
||||
message="Use of non existent annotations will lead to broken Apex code which will not compile in the future."
|
||||
class="net.sourceforge.pmd.lang.apex.rule.errorprone.AvoidNonExistentAnnotationsRule"
|
||||
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_apex_errorprone.html#avoidnonexistentannotations">
|
||||
<description>
|
||||
Apex supported non existent annotations for legacy reasons.
|
||||
In the future, use of such non-existent annotations could result in broken apex code that will not compile.
|
||||
This will prevent users of garbage annotations from being able to use legitimate annotations added to Apex in the future.
|
||||
A full list of supported annotations can be found at https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation.htm
|
||||
</description>
|
||||
<priority>3</priority>
|
||||
<example>
|
||||
<![CDATA[@NonExistentAnnotation public class ClassWithNonexistentAnnotation {
|
||||
@NonExistentAnnotation public void methodWithNonExistentAnnotation() {
|
||||
// ...
|
||||
}
|
||||
}
|
||||
]]>
|
||||
</example>
|
||||
</rule>
|
||||
</ruleset>
|
||||
|
@ -158,8 +158,7 @@
|
||||
<property name="cc_block_highlighting" value="false" />
|
||||
</properties>
|
||||
</rule>
|
||||
<rule ref="rulesets/apex/style.xml/AvoidNonExistentAnnotations" message="Use of non existent annotations will lead to broken Apex code which will not compile in the future.">
|
||||
<priority>3</priority>
|
||||
<rule ref="category/apex/errorprone.xml/AvoidNonExistentAnnotations">
|
||||
<properties>
|
||||
<!-- relevant for Code Climate output only -->
|
||||
<property name="cc_categories" value="Style" />
|
||||
|
@ -19,24 +19,4 @@ The Style Ruleset contains rules regarding preferred usage of names and identifi
|
||||
<rule ref="category/apex/bestpractices.xml/AvoidGlobalModifier" deprecated="true" />
|
||||
<rule ref="category/apex/bestpractices.xml/AvoidLogicInTrigger" deprecated="true" />
|
||||
|
||||
<rule name="AvoidNonExistentAnnotations"
|
||||
since="5.5.5"
|
||||
message="Use of non existent annotations will lead to broken Apex code which will not compile in the future."
|
||||
class="net.sourceforge.pmd.lang.apex.rule.style.AvoidNonExistentAnnotationsRule"
|
||||
externalInfoUrl="${pmd.website.baseurl}/rules/apex/style.html#AvoidNonExistentAnnotations">
|
||||
<description>
|
||||
Apex supported non existent annotations for legacy reasons.
|
||||
In the future, use of such non-existent annotations could result in broken apex code that will not copile.
|
||||
This will prevent users of garbage annotations from being able to use legitimate annotations added to apex in the future.
|
||||
</description>
|
||||
<priority>3</priority>
|
||||
<example>
|
||||
<![CDATA[@NonExistentAnnotation public class ClassWithNonexistentAnnotation {
|
||||
@NonExistentAnnotation public void methodWithNonExistentAnnotation() {
|
||||
// ...
|
||||
}
|
||||
}
|
||||
]]>
|
||||
</example>
|
||||
</rule>
|
||||
</ruleset>
|
||||
|
@ -10,13 +10,11 @@ public class ErrorProneRulesTest extends SimpleAggregatorTst {
|
||||
|
||||
private static final String RULESET = "category/apex/errorprone.xml";
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void setUp() {
|
||||
addRule(RULESET, "AvoidDirectAccessTriggerMap");
|
||||
addRule(RULESET, "AvoidHardcodingId");
|
||||
addRule(RULESET, "AvoidNonExistentAnnotations");
|
||||
addRule(RULESET, "EmptyCatchBlock");
|
||||
addRule(RULESET, "EmptyIfStmt");
|
||||
addRule(RULESET, "EmptyStatementBlock");
|
||||
|
@ -1,24 +0,0 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.apex.rule.style;
|
||||
|
||||
import net.sourceforge.pmd.testframework.SimpleAggregatorTst;
|
||||
|
||||
public class StyleRulesTest extends SimpleAggregatorTst {
|
||||
|
||||
private static final String RULESET = "apex-style";
|
||||
|
||||
@Override
|
||||
public void setUp() {
|
||||
addRule(RULESET, "AvoidGlobalModifier");
|
||||
addRule(RULESET, "AvoidLogicInTrigger");
|
||||
addRule(RULESET, "AvoidNonExistentAnnotations");
|
||||
addRule(RULESET, "ClassNamingConventions");
|
||||
addRule(RULESET, "MethodNamingConventions");
|
||||
addRule(RULESET, "VariableNamingConventions");
|
||||
addRule(RULESET, "MethodWithSameNameAsEnclosingClass");
|
||||
|
||||
}
|
||||
}
|
@ -1,6 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<test-data>
|
||||
<test-data
|
||||
xmlns="http://pmd.sourceforge.net/rule-tests"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests http://pmd.sourceforge.net/rule-tests_1_0_0.xsd">
|
||||
|
||||
<test-code>
|
||||
<description>Class with nonexistent annotation</description>
|
||||
@ -49,6 +52,20 @@ public class Foo {
|
||||
<code><![CDATA[
|
||||
public class Foo {
|
||||
@NonExistentStupidity public Integer myField;
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description>Valid annotations are not flagged</description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
@IsTest
|
||||
public class Foo {
|
||||
@Future
|
||||
@Deprecated
|
||||
public Integer bar() {
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
Reference in New Issue
Block a user