I have this ... set of classes that I use to see if a created class matches a given stru开发者_如何学运维cture. What I'm doing is to read a properties file and then compare it with a class.
ie.
className: demo.HelloWorld
classFile: demo/HelloWorld.class
methods: public static main(java/lang.String[]) : void
attributes: name : java.lang.String
And so on.
Before I continue working on something like this, I would like to know if anyone knows of a library or framework to do the same.
To do this well, you'd need to have a Java parser that was able to parse the "structures" (Java fragments) and likely able to look up the types involved. (You could likely do this badly with some scripting langauge, but it would be extremely fragile and prone to producing false negatives, which would eat the very time you hope this tool might save you).
Our DMS Software Reengineering Toolkit with its Java Front End is a framework that has these capabilities. DMS is designed to enable one to build arbitrary custom tools for analyzing or transforming source code. DMS provides parsers (which one is defined by the chosen front end), AST builders, symbol table construction for languages such a C, C++, Java and COBOL, tools for inspecting trees both procedurally and using surface syntax patterns, which allow to you to directly express fragments of code (as determined by the front end parse) for code generation or pattern matching purposes.
For the task you want to do, you'd have DMS parse the software of interest, and do name and type resolution (build up the symbol tables). Then you would want to use DMS's pattern parser to read your "specification file" as a set of method signatures, etc, and using the name resolver to locate/lookup identifiers (e.g, "demo.Helloworld), find the corresponding classes, and then match the parsed pattern against the method signatures. And mismatches would report an inconsistency, which is what I think you are looking for.
I have a nice library built with a lot reflection tools but i can't release it because of licensing issues (teaser!) check this out tho or this here. Once you build some basic reflection tools things start to come together pretty quickly.
See http://innig.net/macker/
Although its not exactly what you want and is more rule based.
I hava used some time ago a nice library called clapper : http://software.clapper.org/javautil/#documentation
In your case - find "classutil" package - it provides simple and useful class discovery and filters, maybe this will be useful.
精彩评论