Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule to match at least one constructors in exceptions that match ctor(cause, pattern, args) or ctor(pattern, args) #3

Closed
dgroup opened this issue Jan 6, 2022 · 5 comments · Fixed by #19
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@dgroup
Copy link
Owner

dgroup commented Jan 6, 2022

class MyException extends Exception {
    public MyException(String pattern, Object ...args) {
         super(String.format(pattern, args));
   }

   public MyException(Throwable cause, String pattern, Object ...args) {
         super(String.format(pattern, args), cause);
   }
}
@dykov
Copy link
Collaborator

dykov commented Jan 6, 2022

//ClassOrInterfaceDeclaration
[pmd-java:typeIs('java.lang.Exception')] (: extends Exception class :)
[
not(
./ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/ConstructorDeclaration/FormalParameters[
    (: Ctor(Throwable, String, Object...) :)
    (./FormalParameter[1]/Type/ReferenceType/ClassOrInterfaceType[pmd-java:typeIs('java.lang.Throwable')]
    and
    ./FormalParameter[2]/Type/ReferenceType/ClassOrInterfaceType[pmd-java:typeIs('java.lang.String')]
    and
    ./FormalParameter[3][@Varargs=true()]/Type/ReferenceType/ClassOrInterfaceType[pmd-java:typeIsExactly('java.lang.Object')])

    or

    (: Ctor(String, Object...) :)
    (./FormalParameter[1]/Type/ReferenceType/ClassOrInterfaceType[pmd-java:typeIs('java.lang.String')]
    and
    ./FormalParameter[2][@Varargs=true()]/Type/ReferenceType/ClassOrInterfaceType[pmd-java:typeIsExactly('java.lang.Object')])] (: typeIs('java.lang.Object') can accept any class :)
)
]

@dykov dykov self-assigned this Jan 7, 2022
@dykov dykov added the enhancement New feature or request label Jan 7, 2022
@dykov
Copy link
Collaborator

dykov commented Jan 8, 2022

@dgroup , should the rule throw violation for the following case?

class MyException extends Exception {
    public MyException(Exception cause, String pattern, Object... args) {
    }
}

Here we have Exception as a first constructor param, not Throwable

@dykov
Copy link
Collaborator

dykov commented Jan 8, 2022

@dgroup , also should these constructors have strictly public modifier?

@dykov dykov linked a pull request Jan 8, 2022 that will close this issue
3 tasks
@dgroup
Copy link
Owner Author

dgroup commented Jan 8, 2022

Not sure about public, let's do not check it so far

@rultor rultor closed this as completed in #19 Jan 8, 2022
@dykov
Copy link
Collaborator

dykov commented Jan 8, 2022

@dgroup, so i suggest to check for non-private modifier?

@dgroup dgroup added this to the 0.1.0 milestone Jan 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants