use java 7 syntax to compile to java 5

2019-07-02 01:49发布

问题:

is there any way to use java 7 syntax and produce bytecode that works on 1.5? as far as i know, options -target and -source can't be different. checking if no new API was used also would be nice but is not crucial

回答1:

No, it is not. As far as I know, different -source and -target work, but only if the source is lower or equal the target (in order to provide backwards compatibility). There may be source converters that convert your java 7 code into older versions.



回答2:

There's a project called Retroweaver which allows you to write Java 5 syntax (including generics, etc.) and convert it to code that runs on Java 1.4 and older.

As far as I know there isn't anything like that to make code with Java 7 syntax work on older versions.



回答3:

No, if you use Java7 syntax, you can't compile that code with Java 5.

-source and -target works only if the syntax is compatible with -target version.



回答4:

No, it isn't possible. E.g. consider the new try-with-resources construct: this might set suppressed throwables, but this API is only available in Java SE 7+:

http://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html#getSuppressed%28%29

This means older Java version wouldn't be able to run that code anyway.