2

I'm writing a custom check to see if some classes conform to our architecture , one of the rules states that if a class has a certain prefix to it's name , it must inherit a certain type of servlet directly or indirectly.


the available checks don't cover this case and the indirect inheritance is proving to be a challange to test because i can't find a way to get the Ast tree of the first inherited class so patterns like this:

  • dummyClass1 extends dumDummy
  • dumDummy extends realyImportantClass

are going unchecked. The question is:

  1. How can i access the Ast tree of the inherited class?
  2. The first idea was to get the .class to the first inherited class and check it , can it be done?
  3. Can Checkstyle get the Ast tree from a .class file?

Thanks in advance , please warn me if the question was unclear , i'll be happy to provide as much information as possible

Lucas Moreira
  • 498
  • 1
  • 4
  • 14
  • Duplicate of '[How to write a Checkstyle custom check involving indirect inheritance?](http://stackoverflow.com/q/7773022/1005481)'. – barfuin Jun 02 '13 at 12:32

1 Answers1

2

You could do the following to check:

Class<?> dummyClass = DummyClass1.class;
Class<?> reallyImportantClass = ReallyImportantClass.class;
if(reallyImportantClass.isAssignableFrom(dummyClass)){
  return true;
}else{
  return false;
}

See: http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#isAssignableFrom(java.lang.Class)

jsurls
  • 365
  • 2
  • 11
  • that was one of the sugested methods , but this check runs on the Svn server machine throuhg a pre-commit hook so the machine doesn't have the same enviroment (classpath and such) so using this would not be an option , the check has to be done through checkstyle – Lucas Moreira May 29 '13 at 15:37
  • timing , if the unit test fails the build our continuous integration tool (Jenkins) will send emails to all of the teams involved in the project in the next build, it's too much fuss for an architecture violation that may happen in one or two files a at time per commit, the pre commit hook gives us a chance to warn the developer in time so he can make the changes locally. I also want to see how good checkstyle is :) – Lucas Moreira May 29 '13 at 15:48
  • You could also write a custom check via java if it had to be done through checkstyle: http://checkstyle.sourceforge.net/writingchecks.html – jsurls May 29 '13 at 15:53
  • that's what we are trying to do ,i have wrote several checks, but i can't get the Ast tree of the inherited class into checkstyle – Lucas Moreira May 29 '13 at 15:54
  • The more I look at it, I think checkstyle is the wrong place for this kind of check. Checkstyle seems to shine more for stylistic checks and is difficult to check for architecture guidelines. You'd have to do this via a unit test. I'm assuming you aren't using maven and probably don't mandate that developers run any unit tests via ant before checking in, otherwise unit testing would be ideal to catch this **before** a check in. Jenkins then really acts as a guard against any developer who skipped any steps or simply didn't know about the design decisions to inherit certain classes. – jsurls May 29 '13 at 16:04