问题
Is there a way to record or describe a Java refactoring in a way that it can be automatically applied to a depending code base?
My company works with two separate Java code bases, a platform and an add-on depending on the platform. (In fact, multiple add-ons for different customers.) The platform and the add-on are developed by different teams working on different release cycles: The add-on is updated some time after the platform is released. A refactoring applied to the platform is not automatically applied to the add-on, because the platform team does not even look at the add-on code. The add-on team uses the previously released platform code in read-only mode, and currently has to re-apply the platform refactorings manually (e.g. renaming classes and methods).
I wonder if and how the add-on team could apply the platform refactorings to the add-on automatically, instead of having to jump from compile error to compile error after a platform update.
Currently, Eclipse is used as IDE by both teams, but usage is shifting towards IntelliJ Idea.
回答1:
Yes, there is. What you want is a Program Transformation System (PTS).
A good PTS lets you define explicit transformations on the source code as a kind of "metaprogram", which if executed, achieves the desired effect. As an example, you might use the following DMS (my system, see bio) transformation to insert logging calls in every database interaction:
default domain Java~v7;
rule insert_db_log_call(i: identifier, m: modifiers, b: body, p: parameters): method->method =
" \m \i(\p) { \b } " -> " \m \i(\p) { log(\tostring\(\i\)); { \b } } "
if is_db_method_call(i).
If you define such a metaprogram carefully, and it works properly on your first code base, it should have the same effect on the second base. It may take some effort to express what you actually want to do; not all "refactorings" are as easily described as "rename a to b".
The example given is pretty simple; one can write much more complex metaprograms to achieve very complicated effects.
来源:https://stackoverflow.com/questions/26712133/automatic-refactoring-of-depending-code-after-the-fact