1

As part of implementing custom coding guideline checker, we need to make sure we do not have any try-catch block in our class or "new" keyword is not used to create objects. Is there any way to achieve this ?

Cootri
  • 3,806
  • 20
  • 30
  • 1
    you don't want the 'new' keyword being used to create new instances ...; so, how else do you plan on doing so? – Stultuske Mar 31 '16 at 13:27
  • maybe he wants to use only DI for some classes or packages – Cootri Mar 31 '16 at 13:28
  • by registering them as beans and letting spring container initialize them. – user6140183 Mar 31 '16 at 13:30
  • 1
    grep your source code? – khelwood Mar 31 '16 at 13:30
  • @user6140183 which is good if you know up forehand all the values for it's variables, but if they're supposed to be dynamic ... what's the point of that? You'll waste time on pointless configuration. – Stultuske Mar 31 '16 at 13:32
  • 2
    This question is a bit too broad. Surely there are ways not to have a `try-catch` in your code: just don't handle exceptions at all. What I'm trying to say, you cannot specify guidelines in an arbitrary way; you need to choose an architecture first, and then decide which guidelines would make sense. – lrnzcig Mar 31 '16 at 13:32
  • 1
    Parse the code and look for those constructs? I did a quick search for "Java parser" and found one. You could also analyze the bytecode. – yshavit Mar 31 '16 at 13:33
  • Just wondering: I can see the reason to avoid "new"; but what is wrong with try-catch? – GhostCat Mar 31 '16 at 13:33
  • 1
    @khelwood `String s = "you could try that, but you'd get false positives"; ` – yshavit Mar 31 '16 at 13:34
  • You going to test for [`Class.newInstance()`](https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#newInstance--) as well? – Elliott Frisch Mar 31 '16 at 13:34
  • 1
    @yshavit Depends what pattern you search for. – khelwood Mar 31 '16 at 13:35
  • Jagermeister, try-catch is mostly about checked exceptions. Maybe he wants to ban them? – Cootri Mar 31 '16 at 13:35
  • It is planned to have one base class which will deal with the exceptions and all the child classes will need to throw the exception instead of dealing with it themselves. – user6140183 Mar 31 '16 at 13:38
  • @khelwood grep uses a regular language, but Java is a context-free language. You _may_ be able to use extended regex for this, if you're willing to put up with an abomination of a pattern. Do I have to [refer you to Zalgo](http://stackoverflow.com/a/1732454/1076640)? ;) – yshavit Mar 31 '16 at 13:42
  • @lrnzcig If I get it correctly, he doesn't mean "not write them", but to have an algorithm check code to see if it has been written. – Stultuske Mar 31 '16 at 13:42
  • @stultuske yes and along with some other rules, if instances of violations are found then fail the maven build. Idea is to integrate coding guideline checker with maven plugin – user6140183 Mar 31 '16 at 13:49
  • fwiw,I think this is a bad idea. I had assumed at first this was for an introductory course, where arbitrary limits can enforce the lesson of the week. Object creation and exception handling are fundamental, basic Java concepts. If you can't find developers you trust with these ideas, frankly you've got a bigger problem on your hands than a stylep checker can solve. – yshavit Mar 31 '16 at 13:54
  • You need rules about what you want to check for, you need a parser for the source code, and you probably need deeper semantic analysis on top of that. Without a more precise characterization of exactly what you want to search for; otherwise (as pointed out in another comment) you can simply grep for "try" or "new" and complain if encountered. – Ira Baxter Mar 31 '16 at 19:03

1 Answers1

0

You should use some statistical analysis tool like SonarQube and write your own rule for it (or find one in the Web).

If you need to check code rules compliance during build then you should use something like PMD maven plugin. Anyway, your problem is about statistical code analysis and you have to find suitable and customizable tool - writing your own is tricky :)

Cootri
  • 3,806
  • 20
  • 30
  • Idea is to integrate this coding guideline checker as part of maven plugin and fail the build if any violations are found. – user6140183 Mar 31 '16 at 13:40
  • 1
    then you should use something like https://maven.apache.org/plugins/maven-pmd-plugin/ Anyway, your starting point is to google something like "statistical analysis maven" – Cootri Mar 31 '16 at 13:44