Re: [Codenarc-user] Writing CodeNarc rules
Brought to you by:
chrismair
From: <AGo...@cf...> - 2016-09-15 14:05:15
|
I am clearly going to have to get intimately familiar with AstUtil, but I don't think that solves my particular need. The standard I am trying to enforce is that a Grails action should consist of a try/catch block that catches java.lang.Exception (debating java.lang.Throwable). The idea here is that the controller should at minimum log the exception and provide some appropriate user interaction when an exception occurs and not throw it back to the Grails infrastructure where unfriendly things happen in the UI. Obviously most anything caught would implement Exception, but that is very different from Exception being caught. Thanks! From: "Hamlet D'Arcy" <ham...@gm...> To: AGo...@cf... Cc: cod...@li... Date: 09/15/2016 07:57 AM Subject: Re: [Codenarc-user] Writing CodeNarc rules Sounds like you're looking for AstUtils.classNodeImplementsType :) /** * This method tells you if a ClassNode implements or extends a certain class. * @param node * the node * @param target * the class * @return * true if the class node 'is a' target */ On Thu, Sep 15, 2016 at 12:14 AM, <AGo...@cf...> wrote: Thanks to the advice here I have made some good progress. I have a rule that is doing pretty much what I want, but I would like to add one more thing that is giving me trouble. I have the TryCatchStatement, and from there I can get the CatchStatement list. For each CatchStatement I can get the ClassNode for the exception type and from there I can check the ClassNode name to see if what is being caught is an Exception. I would like to go the extra step and confirm that the CatchStatement is catching a java.lang.Exception and not just a Throwable someone decided to name Exception. Is that possible? I could add another rule that makes sure none of the compiled code names a class "Exception", but it seems like I should be able to check the package of the class being caught. If someone writes their own java.lang.Exception class then I think we have gone beyond my scope, but there are definitely names that get reused across packages and this appears to be an important capability. Thanks! -Andrew Goodspeed From: Andy Goodspeed/IS/CFG To: cod...@li... Date: 09/06/2016 06:32 PM Subject: Re: [Codenarc-user] Writing CodeNarc rules Thanks Hamlet! That should be very helpful. Thanks also to Chris Mair who had some helpful suggestions. I will hope to get a chance to try them both out; still digging out after a week off. Once I get back to this I will report back. Thanks again! -Andrew Goodspeed From: "Hamlet D'Arcy" <ham...@gm...> To: AGo...@cf... Cc: cod...@li... Date: 09/01/2016 09:24 AM Subject: Re: [Codenarc-user] Writing CodeNarc rules Not sure if you ever got an answer to this... Here is the latest Javadoc for MethodNode: http://docs.groovy-lang.org/latest/html/api/org/codehaus/groovy/ast/MethodNode.html A MethodNode only ever has one Statement! so if your Method has only a TryCatchStatement then you know nothing follows it. If your method has-a BlockStatement then your method will be several statements long. The best way to explore is to write more unit tests and just look and see what is in the AST! On Thu, Aug 18, 2016 at 9:18 PM, <AGo...@cf...> wrote: Hello all. I am trying to write a CodeNarc rule and am struggling with just how to use the AST code visitor support (or anything else) to get were I want to go. Does anyone know of a resource that might be helpful and/or give me some firsthand assistance? I have looked at all of the obvious ones from the CodeNarc site as well as the distributed rules, google searches, etc. I am trying to write a rule that checks to make sure the body of particular methods consists entirely of a try/catch block where Exception is caught. I have gotten as far as verifying the first statement in the method is a try/catch statement using an AbstractMethodVisitor but am struggling a bit in figuring out how to make sure there is no statement following it in the method. This is where I am on my astVisitorClass. classMyTestAstVisitor extendsAbstractMethodVisitor { @Override voidvisitMethod(MethodNode methodNode) { if(Modifier.isPublic(methodNode.modifiers)) { if(AstUtil.isEmptyBlock(methodNode.code)) { addViolation(methodNode, "Action ${methodNode.name} in ${ currentClassName} is empty") } else{ deftryCatchStatement = methodNode.firstStatement if(!(tryCatchStatement instanceofTryCatchStatement)) { addViolation(methodNode, "First statement of action ${methodNode.name} in ${currentClassName} is ${tryCatchStatement.class. name}") } else{ // try/catch statement, but nothing more? } } } } } I am running with CodeNarc 0.22 and Grails 2.2.4/Groovy 2.0.8. Thanks for any insight. -Andrew Goodspeed NOTICE: This message and all attachments transmitted with it may contain sensitive and/or confidential information intended solely for the use of the addressee. If the reader of this message is not the intended recipient, you are hereby notified that any reading, dissemination, distribution, copying, or other use of this message or its attachments is strictly prohibited. If you have received this message in error, please notify the sender immediately and delete this message and all copies and backups thereof. If you choose to communicate with us by email, you should be aware that the security of incoming Internet email is not secure. We strongly encourage you to use encrypted email when sending sensitive and/or confidential information. By sending sensitive or confidential email messages that are not encrypted, you accept the risks of such lack of security and possible lack of confidentiality. If you elect to communicate from your workplace computer, you also should be aware that your employer and its agents have access to email communications between you and us. ------------------------------------------------------------------------------ _______________________________________________ Codenarc-user mailing list Cod...@li... https://lists.sourceforge.net/lists/listinfo/codenarc-user -- Hamlet D'Arcy ham...@gm... ------------------------------------------------------------------------------ _______________________________________________ Codenarc-user mailing list Cod...@li... https://lists.sourceforge.net/lists/listinfo/codenarc-user NOTICE: This message and all attachments transmitted with it may contain sensitive and/or confidential information intended solely for the use of the addressee. If the reader of this message is not the intended recipient, you are hereby notified that any reading, dissemination, distribution, copying, or other use of this message or its attachments is strictly prohibited. If you have received this message in error, please notify the sender immediately and delete this message and all copies and backups thereof. If you choose to communicate with us by email, you should be aware that the security of incoming Internet email is not secure. We strongly encourage you to use encrypted email when sending sensitive and/or confidential information. By sending sensitive or confidential email messages that are not encrypted, you accept the risks of such lack of security and possible lack of confidentiality. If you elect to communicate from your workplace computer, you also should be aware that your employer and its agents have access to email communications between you and us. ------------------------------------------------------------------------------ _______________________________________________ Codenarc-user mailing list Cod...@li... https://lists.sourceforge.net/lists/listinfo/codenarc-user -- Hamlet D'Arcy ham...@gm... ------------------------------------------------------------------------------ _______________________________________________ Codenarc-user mailing list Cod...@li... https://lists.sourceforge.net/lists/listinfo/codenarc-user NOTICE: This message and all attachments transmitted with it may contain sensitive and/or confidential information intended solely for the use of the addressee. If the reader of this message is not the intended recipient, you are hereby notified that any reading, dissemination, distribution, copying, or other use of this message or its attachments is strictly prohibited. If you have received this message in error, please notify the sender immediately and delete this message and all copies and backups thereof. If you choose to communicate with us by email, you should be aware that the security of incoming Internet email is not secure. We strongly encourage you to use encrypted email when sending sensitive and/or confidential information. By sending sensitive or confidential email messages that are not encrypted, you accept the risks of such lack of security and possible lack of confidentiality. If you elect to communicate from your workplace computer, you also should be aware that your employer and its agents have access to email communications between you and us. |