是否有应用重构脚本只是它的副作用的方法是什么? [英] Is there a way to apply a refactoring script just for its side-effects?

查看:198
本文介绍了是否有应用重构脚本只是它的副作用的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了改变引入工厂方法代替构造函数(原因并不重要,现在,在当前的情况下,它是改良型推断居多)。

I've got a library that was changed to introduce a factory method to replace a constructor (the reasons aren't important right now, in the current case it was for improved type inference mostly).

假设有的它一个Eclipse重构脚本(所以我pretty多有变化的一个很好的计算机可读的描述),是有一些方法来的适用的该脚本来只的使用的那个库(即它只有到库的编译版本的引用)?

Assuming that there is an Eclipse refactoring script for it (so I pretty much have a nice computer-readable description of the change), is there some way to apply that script to a project that only uses that library (i.e. it only has a reference to a compiled version of the library)?

例如,我开始与这个简单的类:

For example, I start with this simple class:

public class MyContainer<T> {
    private final T content;

    public MyContainer(final T content) {
        this.content = content;
    }

    public T getContent() {
        return content;
    }
}

现在我介绍一个工厂(未做构造私有的,但现在不是真正相关),工厂方法是这样的:

Now I introduce a factory (without making the constructor private, but that isn't really relevant right now), the factory method looks like this:

public static <T> MyContainer<T> holding(T content) {
    return new MyContainer<T>(content);
}

当我现在导出迁移脚本,它看起来是这样的:

When I now export a migration script it looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<session version="1.0">
<refactoring comment="Introduce factory &apos;holding&apos; in &apos;scratch.MyContainer&apos; for constructor &apos;scratch.MyContainer.MyContainer()&apos;&#x0A;- Original project: &apos;scratch&apos;&#x0A;- Original element: &apos;scratch.MyContainer.MyContainer()&apos;&#x0A;- Factory name: &apos;holding&apos;&#x0A;- Owner class: &apos;scratch.MyContainer&apos;"
             description="Introduce factory for &apos;MyContainer&apos;"
             element1="/src&lt;scratch{MyContainer.java[MyContainer"
             flags="589830" id="org.eclipse.jdt.ui.introduce.factory"
             input="/src&lt;scratch{MyContainer.java"
             name="holding" project="scratch" protect="false"
             selection="89 0" version="1.0"/>
</session>

此的 的应持有足够的信息来应用重构到另一个项目,它通过一个jar文件的类的引用(一个的电流的版本)。

This should hold enough information to apply the refactoring to another project that references (a current version) of the class via a jar file.

但尝试应用重构给了我这个错误:

But trying to apply the refactoring gives me this error:

该refatoring介绍工厂(org.eclipse.jdt.ui.introduce.factory)无法执行,因为它的输入'scratch.MyContainer.java'不存在。

The refatoring 'Introduce Factory' (org.eclipse.jdt.ui.introduce.factory) cannot be performed, since its input 'scratch.MyContainer.java' does not exist.

请注意,它的明确的提到了的.java ,所以它找一个源文件,而不是内容与应用重构二进制类型。

Note that it explicitly mentions .java, so it's looking for a source file and isn't content with applying the refactoring to the binary type.

注:的我的的要修补的二进制库。我的只是的希望原始构造的的来电的转换为调用工厂方法来代替。

Note: I don't want to patch the binary library. I just want the callers of the original constructor to be converted to calling the factory method instead.

注2:的搜索和替换我的具体案例将不会有所帮助。原因是,我的两个的构造方法,一个服用对象和其他需要字符串。他们应该被映射两个不同的的工厂方法,所以我需要在搜索和替换功能,在参数的类型信息看为好。

Note 2: Search-and-replace won't help in my specific case. The reason is that I have two constructors, one taking an Object and the other takes a String. They should be mapped on two different factory methods, so I'd need the search-and-replace function to look at the type information of the argument as well.

注3:的甚至一种方式来手动指定替代对象 X 与此构造与调用该方法,而不是实例化将是AP preciated。

Note 3: even a way to manually specify "replace instantiations of object X with this constructor with calls to this method instead" would be appreciated.

更新:的有在Eclipse的Bugzilla一个Bugzilla条目约的添加替换调用重构。该功能的实现的可能的能够解决我的问题,如果调用包括通过给定的构造函数实例化对象。不幸的是它看到在过去的几年中没有活性(虽然它被计划为3.2)。

Update: There is a bugzilla entry on the Eclipse bugzilla that is about adding a replace invocation refactoring. An implementation of that feature might be able to solve my problem, if "invocation" includes object instantiation via a given constructor. Unfortunately it saw no activity in the last few years (although it was planned for 3.2).

推荐答案

我认为你将不得不编写自己的重构插件。在<一个href=\"http://dev.eclipse.org/viewcvs/viewvc.cgi/org.eclipse.jdt.ui.tests.refactoring/test%20cases/org/eclipse/jdt/ui/tests/refactoring/RefactoringTest.java?view=markup\"相对=nofollow>测试了解org.eclipse.jdt.ui.tests.refactoring是一个有趣的地方开始。

I think you will have to write your own refactoring plugin. The tests for org.eclipse.jdt.ui.tests.refactoring are an interesting place to start.

一些有趣的话题写着:

  • http://www.eclipse.org/articles/article.php?file=Article-Unleashing-the-Power-of-Refactoring/index.html
  • http://www.eclipse.org/articles/Article-LTK/ltk.html

这篇关于是否有应用重构脚本只是它的副作用的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆