views:

89

answers:

3

Basically, I do lots of one-off code generation, large-scale refactorings, etc. etc. in Java.

My tool language of choice is Python, but I'll take whatever solutions you can offer.


Here is a simplified illustration of what I would like, in a pseudocode

Generating an implementation for an interface

search within my project:
  for each Interface as iName:
    write class(name=iName+"Impl", implements=iName)
    search within the body of iName:
      for each Method as mName:
        write method(name=mName, body="// TODO implement this...")

Basically, the tool I'm searching for would allow me to:

  • parse files according to their Java structure ("search for interfaces")
  • search for words contextualized by language elements and types ("variables of type SomeClass", "doStuff() method calls on SomeClass instances")
  • to run searches with structural context ("within the body of the current result")
  • easily replace or generate code (with helpers to generate, as above, or functions for replacing, "rename the interface to Foo", "insert the line Blah.Blah()", etc.)

The point is, I don't want to spend a lot of time writing these things, as they are usually throwaway. But sometimes I need something just a little smarter than what grep offers. It wouldn't be too hard to write up a simplistic version of this, but if I'm going to use something like this at all, I'd expect it to be robust.

Any suggestions of a tool/library that will help me accomplish this?


Edit to add some clarification

  1. Python is definitely not necessary; I'll take whatever is that. I merely suggest it incase there are choices.
  2. This is to be used in combination with IDE refactoring; sometimes it just doesn't do everything I want.
  3. In instances where I'm using for code generation (as above), it's for augmenting the output of other code generators. e.g. a library we use outputs a tonne of interfaces, and we need to make standard implementations of each one to mesh it to our codebase.
A: 

If you are coding in Java, I strongly recommend that you use NetBeans IDE. It has this kind of refactoring support builtin. Eclipse also supports this kind of thing (although I prefer NetBeans). Both projects are open source, so if you want to see how they perform this refactoring, you can look at their source code.

Michael Aaron Safyan
@Michael Aaron Safyan: you say they have *"this kind of refactoring support builtin"*... But do you really mean, like the OP's asking, that you can for example add *"Impl"* to every single concrete class that is not already ending with *Impl*? Do Eclipse and NetBeans really have this built in? For I don't think the OP is after manually doing that over a few tens of classes. I think he more has like a hundreds KLOC codebase and want this to be done in an entirely automated way...
Webinator
@WizardOfOdds - the chances are that no IDE will do *exactly* the refactorings that the OP wants in a single operation. HOWEVER, it is likely that one or more will do (say) 95% of the work ... and it will do it interactively, accurately, with undo/redo, with incremental recompilation, and so on. There is a good chance that the good things about an IDE will outweigh the missing (say) 5%.
Stephen C
@Stephen C: I disagree... From what I understand from the OP's question, where he mentions "large scale refactoring", I take it he wouldn't call *"manually finding every concrete class that doesn't end with Impl and then call the IDE's 'refactor name'"* doing "95% of the job". Our views probably differs on what the OP wants to do but I *did* refactor programmaticaly a 200 KLOC codebase and I can tell you that IntelliJ IDEA (which I love btw) simply wouldn't cut it for such a level of automation.
Webinator
@WizardOfOdds, NetBeans and Eclipse support a lot of different refactorings out of the box. If there is something that they do not currently do, then the OP can extend their source code to support the desired refactorings, hence my pointing out the fact that they are open source and that he can grab the code.
Michael Aaron Safyan
I'd say you are both right: The IDE does do 95% of what I want. Now what I want is a tool for cleaning up that final 5%.
Ipsquiggle
A: 

Java has its fair share of criticism these days but in the area of tooling - it isn't justified.

We are spoilt for choice; Eclipse, Netbeans, Intellij are the big three IDES. All of them offer excellent levels of searching and Refactoring. Eclipse has the edge on Netbeans I think and Intellij os often ahead of Eclipse

You can also use static analysis tools such as FindBugs, CheckTyle etc to find issues - i.e. excessively long methods and classes, overly complex code.

If you really want to leverage your Python skills - take a look at Jython. Its a Python interpreter written in Java.

Fortyrunner
@Fortyrunner: that is not at all how I understand the question and I'm really not sure that, say, IDEA's refactoring allows to do what the OP wants in an automated way. He said his **tool** language of choice is Python, because for what he wants to do, Python is probably much better than Java. It's not about "leveraging" his Python skills: he doesn't want to write a program that could be done either in Java or Python or... What he wants is to instrument Java source code. I don't see what FindBugs has to do with his question neither.
Webinator
FindBugs will help find issues that may require refactoring. Thats partly what we use it for.Not all refactoring should or could be done in an automated way - that's the extra bit that we add as developers and designers. I was suggesting that learning some of the tooling might help more than he thinks.
Fortyrunner
A: 

First, I am not aware of any tool or libraries implemented in Python that specifically designed for refactoring Java code, and a Google search did not give me any leads.

Second, I would posit that writing such a decent tool or library for refactoring Java in Python would be a large task. You would have to implement a Java compiler front-end (lexer/parser, AST builder and type analyser) in Python, then figure out how to integrate this with a program editor. I'm not surprised that nobody has done this ... given that mature alternatives already exist.

Thirdly, doing refactoring without a full analysis of the source code (but uses pattern matching for example) will be incapable of doing complex refactoring, and will is likely to make mistakes in edge cases that the implementor did not think of. I expect that is the level at which the OP is currently operating ...

Given that bleak outlook, what are the alternatives:

One alternative is to use one of the existing Java IDEs (e.g. NetBeans, Eclipse, IDEA. etc) as a refactoring tool. The OP won't be able to extend the capabilities of such a tool in Python code, but the chances are that he won't really need to. I expect that at least one of these IDEs does 95% of what he needs, and (if he is realistic) that should be good enough. Especially when you consider that IDEs have lots of incidental features that help make refactoring easier; e.g. structured editing, undo/redo, incremental compilation, intelligent code completion, intelligent searching, type and call hierarchy views, and so on.

(Aside ... if existing IDEs are not good enough (@WizardOfOdds - only the OP can make that call!!), it would make more sense to try to extend the refactoring capability of an existing IDE than start again in a different implementation language.)

Depending on what he is actually doing, model-driven code generation may be another alternative. For instance, if the refactoring is happening because he is frequently creating and recreating his object model(s), then an alternative is to code the models in some modeling language and generate his code from those models. My tool of choice when doing this kind of thing is Eclipse EMF and related technologies. The EMF technologies include generation of editors, XML serialization, persistence, queries, model to model transformation and so on. I have used EMF to implement and roll out projects with object models consisting of 50 to 100 distinct classes with complex relationships and validation requirements. EMF's support for merging source code edits when you regenerate from an updated model is a key feature.

Stephen C
Consider the python thing a very soft constraint: if it can't, it can't. As some of the other answers suggest, the refactoring tools in my IDE (Eclipse) don't seem to be quite what I'm looking for. (In fact, my most recent want of this tool was to repair a refactoring that went badly.) Also, I think model-driven generation is overkill for my needs; many of the scenarios I want this for are specifically one-offs.
Ipsquiggle
@Ipsquiggle - It appears that no tool currently exists that directly addresses your needs. If you want to do something about it, consider fixing / enhancing the refactoring component of one of the IDEs.
Stephen C
That does seem to be the case. It appears as if Eclipse actually will perform contextual *searches*, but gives no tools for manipulation after the fact. That, at least, is promising.
Ipsquiggle