从 Java 类中提取接口的自动化方法

发布于 2024-10-10 23:21:22 字数 729 浏览 4 评论 0原文

我有一个定义 API 的具体类的集合,我想从 API 的实际实现中提取这些类的接口(即:本质上是类型层次结构和公共方法)。

因此,例如,如果 API 中的公共类之一是

 public class Foo extends Bar {
     /* some fields which I don't care about */

     public void method() {
     /* implementation here */
     } 

     public void otherMethod() {
     /* implementation goes here */
     }

    /* some non public methods which I don't care about */

 }

我想闯入一个接口和一个实现

是否

public interface FooInterface extends BarInterface {
    public void method();
    public void otherMethod()
}

 public class Foo implements FooInterface {
  /* etc etc */
 }

,即

有一个工具我可以用来以自动方式进行这种分离,或者我是否必须滚动我的自己的程序来完成这项工作?它需要是一个需要最少交互的工具。

I've got a collection of concrete Classes which define an API, and I would like to extract the Interface of these classes (ie: essentially the type hierarchy and public methods) from the actual implementation of the API.

So for example if one of the public classes in the API is

 public class Foo extends Bar {
     /* some fields which I don't care about */

     public void method() {
     /* implementation here */
     } 

     public void otherMethod() {
     /* implementation goes here */
     }

    /* some non public methods which I don't care about */

 }

I would like to break into an interface and an implementation

ie

public interface FooInterface extends BarInterface {
    public void method();
    public void otherMethod()
}

 public class Foo implements FooInterface {
  /* etc etc */
 }

and

Is there a tool I can use to do this separation in automated way, or am I going to have to roll my own program to do the job? It needs to be a tool that requires minimal interaction.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一场春暖 2024-10-17 23:21:22

我找到了解决方案!

Eclipse 支持重构脚本,脚本采用 xml 格式,但不太人性化,但我已经生成了脚本,使 eclipse 执行所有重构。

有关 eclipse 脚本重构的详细信息,查看此处并使用 eclipse 为您生成一些示例,以便您知道脚本应该是什么样子

I found the solution!

Eclipse, has support for refactoring scripts, the scripts are in xml yet not very human friendly but I have generated the scripts anyway that makes eclipse perform all the refactoring.

For details of eclipse script refactoring, look here and use eclipse to generate a few examples for you so you know what the script should look like

陌上芳菲 2024-10-17 23:21:22

许多 IDE(例如 IntelliJ)都有一个“Extract Interface”命令,可以为您执行重构:http://www.jetbrains.com/idea/features/refactoring.html#Extract_Interface

Many IDEs, like IntelliJ, have an "Extract Interface" command which performs a refactoring for you: http://www.jetbrains.com/idea/features/refactoring.html#Extract_Interface

一世旳自豪 2024-10-17 23:21:22

javap,JDK中的一个工具,将给你大部分你想要的东西。但是,请做好准备,因为它无法正确处理某些事情 - 抱歉,我忘记了什么,也许是注释,内部类,类似的东西。

如果在您自己的 Java 应用程序中嵌入 javap 会有所帮助,您可以执行以下操作:

import sun.tools.javap.Main;
...
    void javap(String className) throws IOException
    {
        bos = new ByteArrayOutputStream();
        ourOut = new PrintStream(bos);

        PrintStream oldOut = System.out;
        System.setOut(ourOut);

        String[] params = new String[] {"-public", className};
        Main.main(params);

        System.setOut(oldOut);

        String str = bos.toString();
     }

这需要类路径中的 Sun JDK tools.jar。

javap, a tool in the JDK, will get you most of what you want. However, be prepared for it to not handle some things correctly - sorry, I forget what, maybe annotations, inner classes, something like that.

If embedding javap in you own Java app would help, you can do something like this:

import sun.tools.javap.Main;
...
    void javap(String className) throws IOException
    {
        bos = new ByteArrayOutputStream();
        ourOut = new PrintStream(bos);

        PrintStream oldOut = System.out;
        System.setOut(ourOut);

        String[] params = new String[] {"-public", className};
        Main.main(params);

        System.setOut(oldOut);

        String str = bos.toString();
     }

This requires the Sun JDK tools.jar in the classpath.

ら栖息 2024-10-17 23:21:22

我很确定您必须使用自己的工具来完成这项工作;它不是许多开发人员会使用的东西,而那些使用它的人也不会经常使用它。提取接口,要正确完成,确实需要智能(真正的)来指导它。一个盲目提取所有公共和信息的工具。受保护的元素,并盲目地构建一个接口层次结构来反映实现层次结构,这远非明智之举,而且最终产品也不会很漂亮。开发(人工智能)智能来做出关于包含什么和导出什么的正确决策——这将是一个有用的工具,但我承认对我来说难以想象的困难。当前 IDE 提供的引导帮助可能是最好的折衷方案。开心点;这比手动完成每一步要简单得多!

I'm pretty sure you'll have to roll your own tool to do this job; it's not something that many developers would use, and those who did use it wouldn't use it often. Extract Interface, to be done right, really needs intelligence (the real kind) guiding it. A tool that blindly extracted all public & protected elements, and blindly built an interface hierarchy to mirror an implementation hierarchy, is far from smart, and the end product wouldn't be pretty. Developing the (artificial) intelligence to make good decisions about what to include and what to lead out - that would be a useful tool, but I confess unimaginably difficult for me. What current IDEs offer - guided assistance - is probably the best compromise. Cheer up; it's massively less tedious than doing every step by hand!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文