Showing that covariance is source compatible, but not binary compatible in Java
authorJaroslav Tulach <jtulach@netbeans.org>
Tue, 18 Oct 2011 06:47:43 +0200
changeset 37868bba7c8a1b3
parent 377 d65e561f94d7
child 379 b632733724a8
Showing that covariance is source compatible, but not binary compatible in Java
samples/covariance/build.xml
samples/covariance/nbproject/project.xml
samples/covariance/src-api1.0/api/Covariance.java
samples/covariance/src-api2.0/api/Covariance.java
samples/covariance/src-impl/test/CovarianceTest.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/covariance/build.xml	Tue Oct 18 06:47:43 2011 +0200
     1.3 @@ -0,0 +1,88 @@
     1.4 +<?xml version="1.0" encoding="UTF-8"?>
     1.5 +<project name="co-contra-variance" default="run" basedir=".">
     1.6 +    <target name="clean">
     1.7 +        <delete dir="build"/>
     1.8 +    </target>
     1.9 +    <target name="test" depends="run"/>
    1.10 +    
    1.11 +    <target name="compile" depends="build"/>
    1.12 +    <target name="build">
    1.13 +        <antcall target="-build-one">
    1.14 +            <param name="version" value="api1.0"/>
    1.15 +        </antcall>
    1.16 +        <antcall target="-build-one">
    1.17 +            <param name="version" value="api2.0"/>
    1.18 +        </antcall>
    1.19 +        <echo level="info" message="Compiling the test against Version 1.0 of the API. This should succeeds."/>
    1.20 +        <antcall target="-build-one">
    1.21 +            <param name="version" value="impl"/>
    1.22 +            <param name="output" value="impl-with-1.0"/>
    1.23 +            <param name="cp" location="build/api1.0/classes"/>
    1.24 +        </antcall>
    1.25 +        <echo level="info" message="Compiling the test against Version 2.0 of the API. This should succeeds."/>
    1.26 +        <antcall target="-build-one">
    1.27 +            <param name="version" value="impl"/>
    1.28 +            <param name="output" value="impl-with-2.0"/>
    1.29 +            <param name="cp" location="build/api2.0/classes"/>
    1.30 +        </antcall>
    1.31 +    </target>
    1.32 +    
    1.33 +    <target name="run" depends="build">
    1.34 +        <echo level="info" message="Running the test compiled with version 1.0 of the API against version 1.0 of the API. This should succeeds."/>
    1.35 +        <antcall target="-run-one">
    1.36 +            <param name="version" value="api1.0"/>
    1.37 +            <param name="testversion" value="impl-with-1.0"/>
    1.38 +            <param name="result" value="0"/>
    1.39 +        </antcall>
    1.40 +        <echo level="info" message="Running the test compiled with version 2.0 of the API against version 2.0 of the API. This should succeeds."/>
    1.41 +        <antcall target="-run-one">
    1.42 +            <param name="version" value="api2.0"/>
    1.43 +            <param name="testversion" value="impl-with-2.0"/>
    1.44 +            <param name="result" value="0"/>
    1.45 +        </antcall>
    1.46 +        <echo level="info" message="Mixing should not work: Running the test compiled with version 1.0 of the API against version 2.0 of the API. This should fail."/>
    1.47 +        <antcall target="-run-one">
    1.48 +            <param name="version" value="api2.0"/>
    1.49 +            <param name="testversion" value="impl-with-1.0"/>
    1.50 +            <param name="result" value="1"/>
    1.51 +        </antcall>
    1.52 +        <echo level="info" message="Mixing does not work: Running the test compiled with version 2.0 of the API against version 1.0 of the API. This should fail."/>
    1.53 +        <antcall target="-run-one">
    1.54 +            <param name="version" value="api1.0"/>
    1.55 +            <param name="testversion" value="impl-with-2.0"/>
    1.56 +            <param name="result" value="1"/>
    1.57 +        </antcall>
    1.58 +    </target>
    1.59 +
    1.60 +    
    1.61 +    <!-- support methods -->
    1.62 +    
    1.63 +    <target name="-run-one">
    1.64 +        <fail message="You need to specify API version number" unless="version"/>
    1.65 +        <java classpath="build/${version}/classes:build/${testversion}/classes" classname="test.CovarianceTest"
    1.66 +            resultproperty="result.real" failonerror="false" fork="true"
    1.67 +        />
    1.68 +        <fail message="Unexpected failure for ${version}: ${result.real}">
    1.69 +            <condition>
    1.70 +                <not>
    1.71 +                    <equals arg1="${result}" arg2="${result.real}"/>
    1.72 +                </not>
    1.73 +            </condition>
    1.74 +        </fail>
    1.75 +    </target>
    1.76 +    
    1.77 +    <target name="-build-one">
    1.78 +        <fail message="You need to specify version number" unless="version"/>
    1.79 +        
    1.80 +        <property name="output" value="${version}"/>
    1.81 +        <mkdir dir="build/${output}/classes"/>
    1.82 +        <property name="cp" value=""/>
    1.83 +        <javac 
    1.84 +            srcdir="src-${version}" 
    1.85 +            destdir="build/${output}/classes" 
    1.86 +            source="1.6" target="1.6"
    1.87 +            classpath="${cp}"
    1.88 +            includeantruntime="false"
    1.89 +        />
    1.90 +    </target>
    1.91 +</project>
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/samples/covariance/nbproject/project.xml	Tue Oct 18 06:47:43 2011 +0200
     2.3 @@ -0,0 +1,99 @@
     2.4 +<?xml version="1.0" encoding="UTF-8"?>
     2.5 +<project xmlns="http://www.netbeans.org/ns/project/1" xmlns:ns4="null">
     2.6 +    <type>org.netbeans.modules.ant.freeform</type>
     2.7 +    <configuration>
     2.8 +        <general-data xmlns="http://www.netbeans.org/ns/freeform-project/1">
     2.9 +            <name>Covariance</name>
    2.10 +        </general-data>
    2.11 +        <general-data xmlns="http://www.netbeans.org/ns/freeform-project/2">
    2.12 +            <!-- Do not use Project Properties customizer when editing this file manually. -->
    2.13 +            <name>Covariance</name>
    2.14 +            <properties/>
    2.15 +            <folders>
    2.16 +                <source-folder>
    2.17 +                    <label>covariance</label>
    2.18 +                    <location>.</location>
    2.19 +                    <encoding>UTF-8</encoding>
    2.20 +                </source-folder>
    2.21 +                <source-folder>
    2.22 +                    <label>src-api1.0</label>
    2.23 +                    <type>java</type>
    2.24 +                    <location>src-api1.0</location>
    2.25 +                    <encoding>UTF-8</encoding>
    2.26 +                </source-folder>
    2.27 +                <source-folder>
    2.28 +                    <label>src-api2.0</label>
    2.29 +                    <type>java</type>
    2.30 +                    <location>src-api2.0</location>
    2.31 +                    <encoding>UTF-8</encoding>
    2.32 +                </source-folder>
    2.33 +                <source-folder>
    2.34 +                    <label>test</label>
    2.35 +                    <type>java</type>
    2.36 +                    <location>impl</location>
    2.37 +                    <encoding>UTF-8</encoding>
    2.38 +                </source-folder>
    2.39 +            </folders>
    2.40 +            <ide-actions>
    2.41 +                <action name="build">
    2.42 +                    <target>build</target>
    2.43 +                </action>
    2.44 +                <action name="clean">
    2.45 +                    <target>clean</target>
    2.46 +                </action>
    2.47 +                <action name="run">
    2.48 +                    <target>run</target>
    2.49 +                </action>
    2.50 +                <action name="rebuild">
    2.51 +                    <target>clean</target>
    2.52 +                    <target>build</target>
    2.53 +                </action>
    2.54 +                <action name="test">
    2.55 +                    <target>test</target>
    2.56 +                </action>
    2.57 +            </ide-actions>
    2.58 +            <view>
    2.59 +                <items>
    2.60 +                    <source-folder style="packages">
    2.61 +                        <label>src-api1.0</label>
    2.62 +                        <location>src-api1.0</location>
    2.63 +                    </source-folder>
    2.64 +                    <source-folder style="packages">
    2.65 +                        <label>src-api2.0</label>
    2.66 +                        <location>src-api2.0</location>
    2.67 +                    </source-folder>
    2.68 +                    <source-folder style="packages">
    2.69 +                        <label>test</label>
    2.70 +                        <location>src-impl</location>
    2.71 +                    </source-folder>
    2.72 +                    <source-file>
    2.73 +                        <location>build.xml</location>
    2.74 +                    </source-file>
    2.75 +                </items>
    2.76 +                <context-menu>
    2.77 +                    <ide-action name="build"/>
    2.78 +                    <ide-action name="rebuild"/>
    2.79 +                    <ide-action name="clean"/>
    2.80 +                    <ide-action name="run"/>
    2.81 +                    <ide-action name="test"/>
    2.82 +                </context-menu>
    2.83 +            </view>
    2.84 +            <subprojects/>
    2.85 +        </general-data>
    2.86 +        <java-data xmlns="http://www.netbeans.org/ns/freeform-project-java/3">
    2.87 +            <compilation-unit>
    2.88 +                <package-root>src-api1.0</package-root>
    2.89 +                <source-level>1.6</source-level>
    2.90 +            </compilation-unit>
    2.91 +            <compilation-unit>
    2.92 +                <package-root>src-api2.0</package-root>
    2.93 +                <source-level>1.6</source-level>
    2.94 +            </compilation-unit>
    2.95 +            <compilation-unit>
    2.96 +                <package-root>src-impl</package-root>
    2.97 +                <classpath mode="compile">src-api1.0</classpath>
    2.98 +                <source-level>1.6</source-level>
    2.99 +            </compilation-unit>
   2.100 +        </java-data>
   2.101 +    </configuration>
   2.102 +</project>
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/samples/covariance/src-api1.0/api/Covariance.java	Tue Oct 18 06:47:43 2011 +0200
     3.3 @@ -0,0 +1,12 @@
     3.4 +package api;
     3.5 +
     3.6 +public class Covariance {
     3.7 +    private Covariance() {
     3.8 +    }
     3.9 +
    3.10 +    // BEGIN: variance.covariance.v1
    3.11 +    public static Number max(int n1, int n2) {
    3.12 +        return Math.max(n1, n2);
    3.13 +    }
    3.14 +    // END: variance.covariance.v1
    3.15 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/samples/covariance/src-api2.0/api/Covariance.java	Tue Oct 18 06:47:43 2011 +0200
     4.3 @@ -0,0 +1,12 @@
     4.4 +package api;
     4.5 +
     4.6 +public class Covariance {
     4.7 +    private Covariance() {
     4.8 +    }
     4.9 +
    4.10 +    // BEGIN: variance.covariance.v2
    4.11 +    public static Integer max(int n1, int n2) {
    4.12 +        return Math.max(n1, n2);
    4.13 +    }
    4.14 +    // END: variance.covariance.v2
    4.15 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/samples/covariance/src-impl/test/CovarianceTest.java	Tue Oct 18 06:47:43 2011 +0200
     5.3 @@ -0,0 +1,13 @@
     5.4 +package test;
     5.5 +
     5.6 +import api.Covariance;
     5.7 +
     5.8 +public class CovarianceTest {
     5.9 +    // BEGIN: variance.covariance.test
    5.10 +    public static void main(String[] args) {
    5.11 +        Number n = Covariance.max(10, 20);
    5.12 +        System.err.println("value: " + n + " type: " + n.getClass());
    5.13 +        assert n.intValue() == 20 : "The max should be 20, but was: " + n;
    5.14 +    }
    5.15 +    // END: variance.covariance.test
    5.16 +}