Compilation in Java 7 - a method that uses Java 8 types

I need to write code to handle dates. We are slowly moving from Java 7 to Java 8, so my library code should work with both. For this reason, it must have compiled Java 7.

However, projects already ported to Java 8 started using JSR-310 types.

Consider the following statements

if (obj instanceof Date){
    //Serialize using SimpleDateFormat
} else if (obj instanceof org.joda.time.LocalDateTime){
    //Serialize using Joda
} else if (obj instanceof java.time.LocalDateTime){
    //Serialize using JSR 310
}

I do not use Lambdas or other language materials provided by Java 8. In any case, I use the Java 8 compiler. I want to create Java-compatible code (no UnsupportedClassVersionError), which breaks when the third one, if the branch gets into Java 7 ( NoClassDefFoundError).

Currently, the above code does not compile at all if I use the Java 8 javac compiler for the target Java 7.

I am running Ant in Java 8

<javac includeantruntime="false" srcdir="src" destdir="build/compiled" target="1.7" source="1.7"

import java.time.LocalDateTime , . , Java 8 Java 7 , , , LocalDateTime Javapath class class class class, Java 7.

, , (if (obj.getClass().getCanonicalName().equals("java.time.LocalDateTime") , ), ThreeTen -Backport, JSR-310 Java 7, threeten Java 8, Ivy.

? ( ), .

+4
3

, . , , , -, -.

, -: , , . : "" java7 . , "" java8. , java7 , - "" java8. - , git cherry-pick, , .

+2

Java7 Java8- - - Java8 , , Java8 -. , Java7 Java7 .

. JVM , , .

, . , - JVM, .

, - , , .

, JVM. , , : Java7 ( Java8) Java8.

+1

: . Java 8.

: , .   2 . . :

MyDateHandler.handle();

, java. maven . , Java 7 MyDateHandler. Java 8 Java 8.

0

Source: https://habr.com/ru/post/1687715/


All Articles