从Java 8到Java 7语法的Delambdafy Java代码工具? [英] Tool to Delambdafy Java code from Java 8 to Java 7 syntax?

查看:157
本文介绍了从Java 8到Java 7语法的Delambdafy Java代码工具?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道将使用lambdas和方法引用的Java 8代码(源代码级)转换为使用匿名内部类的Java 7代码的任何工具?
我知道 Retrolambda ,但它适用于字节码级别,而不是源级别。

Does anyone know of any tool to convert Java 8 code (at the source level) that uses lambdas and method references into Java 7 code that uses anonymous inner classes? I know about Retrolambda, but that works at the bytecode level, not the source level.

目前,我有一个可以作为IntelliJ插件工作的版本。
我扩展了当前的IntelliJ代码,一次性转换包中的所有lambda,而不是单独选择每个lambda并转换为匿名内部类
这种方法的问题在于它无法作为一个独立的工具工作,比如一个maven插件,因为它需要IntelliJ上下文才能工作。

For now, I have a version working that works as an IntelliJ plugin. I extended the current IntelliJ code to convert all lambdas in a package at one go, instead of selecting each lambda individually and converting to anonymous inner class. The problem with this approach though is that it cannot work as a standalone tool, say a maven plugin as it needs an IntelliJ context to work.

编辑:请注意,重点是将lamda /方法引用转换为匿名内部类。我不担心Java 8中的API更改会被Java 7编译器捕获并报告为编译错误。

Note that the focus is on converting lamda/method references to anonymous inner classes. I am not concerned about API changes in Java 8 which would be caught by a Java 7 compiler and reported as compilation errors.

推荐答案

您可以尝试。这是一个人的概念验证,可以做一些接近你要求的事情:

You can try this. It's one person's proof-of-concept to do something close to what you are asking:


它可以转换功能接口的变量声明使用
a lambda初始化程序进入匿名内部类。

It can convert a variable declaration for a functional interface with a lambda initializer into a anonymous inner class.

例如:

Callable<String> callable = () -> "abc" ;

// Converted to:

Callable<String> callable = new Callable<String>() {
    public String call() throws java.lang.Exception { 
        return "abc"; 
    }
};

我不知道它是否可以转换匿名lambdas(即未声明为变量的那些)或方法引用(例如 Person :: compareByAge )或闭包(局部变量捕获),但这些可能是您可以在需要时扩展其代码的功能。

I don't know if it can convert anonymous lambdas (i.e. those not declared as a variable) or Method References (e.g. Person::compareByAge), or closures (local variable capture), but those might be features you can extend his code to do if you need them.

作者指出距离功能完成还有很长的路要走,但它可以使用......

The author notes that "It's a long way from being feature complete, but it's usable..."

这篇关于从Java 8到Java 7语法的Delambdafy Java代码工具?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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