Example with bytecode patching
authorJaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:50:51 +0200
changeset 105611653dec2f
parent 9 45a6b338a903
child 11 617540e9e582
Example with bytecode patching
samples/differentreturntype/build.xml
samples/differentreturntype/nbproject/project.xml
samples/differentreturntype/src-api1.0/api/API.java
samples/differentreturntype/src-api2.0/api/API.java
samples/differentreturntype/src-apimerge/api/API.java
samples/differentreturntype/src-impl/impl/Impl.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/differentreturntype/build.xml	Sat Jun 14 09:50:51 2008 +0200
     1.3 @@ -0,0 +1,106 @@
     1.4 +<?xml version="1.0" encoding="UTF-8"?>
     1.5 +<project name="differentreturntype" default="run" basedir=".">
     1.6 +    <target name="clean">
     1.7 +        <delete dir="build"/>
     1.8 +    </target>
     1.9 +    
    1.10 +    <!-- This is the target that does the whole trick - e,g. it compiles
    1.11 +        the Java source and then replaces tokens inside of the .class file
    1.12 +     -->
    1.13 +    <target name="-build-and-rename">
    1.14 +        <mkdir dir="build/apimerge/classes"/>
    1.15 +        <javac 
    1.16 +            srcdir="src-apimerge" 
    1.17 +            destdir="build/apimerge/classes" 
    1.18 +            source="1.4" target="1.4"
    1.19 +            classpath="${cp}"
    1.20 +        />
    1.21 +        
    1.22 +        <!-- this is the replace. As the replace is done textually,
    1.23 +            we need to use some reasonable encoding that treats all byte values
    1.24 +            as characters. E.g. it is not possible to use UTF-8 as it does not
    1.25 +            like the standard Java header 0xCAFEBABE. Western Europe encoding is fine
    1.26 +        -->
    1.27 +        <replace dir="build/apimerge/classes" casesensitive="true" encoding="iso-8859-1" summary="true">
    1.28 +            <include name="**/*.class"/>
    1.29 +            <replacetoken>g3tIcon</replacetoken>
    1.30 +            <replacevalue>getIcon</replacevalue>
    1.31 +        </replace>
    1.32 +    </target>
    1.33 +    
    1.34 +    <target name="build" depends="clean">
    1.35 +        <antcall target="-build-one">
    1.36 +            <param name="version" value="api1.0"/>
    1.37 +        </antcall>
    1.38 +        <antcall target="-build-one">
    1.39 +            <param name="version" value="api2.0"/>
    1.40 +        </antcall>
    1.41 +        <antcall target="-build-and-rename"/>
    1.42 +        <antcall target="-build-one">
    1.43 +            <param name="version" value="impl"/>
    1.44 +            <param name="target" value="impl-with-api1.0"/>
    1.45 +            <param name="cp" location="build/api1.0/classes"/>
    1.46 +        </antcall>
    1.47 +        <antcall target="-build-one">
    1.48 +            <param name="version" value="impl"/>
    1.49 +            <param name="target" value="impl-with-api2.0"/>
    1.50 +            <param name="cp" location="build/api2.0/classes"/>
    1.51 +        </antcall>
    1.52 +    </target>
    1.53 +    
    1.54 +    <target name="run" depends="build">
    1.55 +        <echo level="info" message="Running the Implementation Compiled against Version 1.0 with Version 1.0. This should succeeds."/>
    1.56 +        <antcall target="-run-one">
    1.57 +            <param name="version" value="api1.0"/>
    1.58 +        </antcall>
    1.59 +        <echo level="info" message="Running the Implementation Compiled against Version 2.0 with Version 2.0. This should succeeds."/>
    1.60 +        <antcall target="-run-one">
    1.61 +            <param name="version" value="api2.0"/>
    1.62 +        </antcall>
    1.63 +        <echo level="info" message="Running the Implementation Compiled against Version 1.0 with Version 2.0. This should fail."/>
    1.64 +        <antcall target="-run-one">
    1.65 +            <param name="version" value="api2.0"/>
    1.66 +            <param name="target" value="api1.0"/>
    1.67 +        </antcall>
    1.68 +        <echo level="info" message="Running the Implementation Compiled against Version 2.0 with Version 1.0. This should fail."/>
    1.69 +        <antcall target="-run-one">
    1.70 +            <param name="version" value="api1.0"/>
    1.71 +            <param name="target" value="api2.0"/>
    1.72 +        </antcall>
    1.73 +        <echo level="info" message="Final success1: Running the Implementation Compiled against Version 1.0 with Merged Version."/>
    1.74 +        <antcall target="-run-one">
    1.75 +            <param name="version" value="apimerge"/>
    1.76 +            <param name="target" value="api1.0"/>
    1.77 +        </antcall>
    1.78 +        <echo level="info" message="Final success2: Running the Implementation Compiled against Version 2.0 with Merged Version."/>
    1.79 +        <antcall target="-run-one">
    1.80 +            <param name="version" value="apimerge"/>
    1.81 +            <param name="target" value="api2.0"/>
    1.82 +        </antcall>
    1.83 +    </target>
    1.84 +
    1.85 +    
    1.86 +    <!-- support methods -->
    1.87 +    
    1.88 +    <target name="-run-one">
    1.89 +        <fail message="You need to specify API version number" unless="version"/>
    1.90 +        <property name="target" value="${version}"/>
    1.91 +        <java classpath="build/${version}/classes:build/impl-with-${target}/classes" classname="impl.Impl"
    1.92 +            failonerror="false"
    1.93 +        />
    1.94 +    </target>
    1.95 +    
    1.96 +    <target name="-build-one">
    1.97 +        <fail message="You need to specify version number" unless="version"/>
    1.98 +        
    1.99 +        <property name="cp" value=""/>
   1.100 +        <property name="target" value="${version}"/>
   1.101 +        <mkdir dir="build/${target}/classes"/>
   1.102 +        <javac 
   1.103 +            srcdir="src-${version}" 
   1.104 +            destdir="build/${target}/classes" 
   1.105 +            source="1.4" target="1.4"
   1.106 +            classpath="${cp}"
   1.107 +        />
   1.108 +    </target>
   1.109 +</project>
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/samples/differentreturntype/nbproject/project.xml	Sat Jun 14 09:50:51 2008 +0200
     2.3 @@ -0,0 +1,96 @@
     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>differentreturntype</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>differentreturntype</name>
    2.14 +            <properties/>
    2.15 +            <folders>
    2.16 +                <source-folder>
    2.17 +                    <label>src-api1.0</label>
    2.18 +                    <type>java</type>
    2.19 +                    <location>src-api1.0</location>
    2.20 +                    <encoding>UTF-8</encoding>
    2.21 +                </source-folder>
    2.22 +                <source-folder>
    2.23 +                    <label>impl</label>
    2.24 +                    <type>java</type>
    2.25 +                    <location>impl</location>
    2.26 +                    <encoding>UTF-8</encoding>
    2.27 +                </source-folder>
    2.28 +                <source-folder>
    2.29 +                    <label>differentreturntype</label>
    2.30 +                    <location>.</location>
    2.31 +                    <encoding>UTF-8</encoding>
    2.32 +                </source-folder>
    2.33 +            </folders>
    2.34 +            <ide-actions>
    2.35 +                <action name="build">
    2.36 +                    <target>build</target>
    2.37 +                </action>
    2.38 +                <action name="clean">
    2.39 +                    <target>clean</target>
    2.40 +                </action>
    2.41 +                <action name="run">
    2.42 +                    <target>run</target>
    2.43 +                </action>
    2.44 +                <action name="rebuild">
    2.45 +                    <target>clean</target>
    2.46 +                    <target>build</target>
    2.47 +                </action>
    2.48 +            </ide-actions>
    2.49 +            <view>
    2.50 +                <items>
    2.51 +                    <source-folder style="packages">
    2.52 +                        <label>API Version 1.0</label>
    2.53 +                        <location>src-api1.0</location>
    2.54 +                    </source-folder>
    2.55 +                    <source-folder style="packages">
    2.56 +                        <label>API Version 2.0</label>
    2.57 +                        <location>src-api2.0</location>
    2.58 +                    </source-folder>
    2.59 +                    <source-folder style="packages">
    2.60 +                        <label>API Merge of 1.0 and 2.0</label>
    2.61 +                        <location>src-apimerge</location>
    2.62 +                    </source-folder>
    2.63 +                    <source-folder style="packages">
    2.64 +                        <label>impl</label>
    2.65 +                        <location>src-impl</location>
    2.66 +                    </source-folder>
    2.67 +                    <source-file>
    2.68 +                        <location>build.xml</location>
    2.69 +                    </source-file>
    2.70 +                </items>
    2.71 +                <context-menu>
    2.72 +                    <ide-action name="build"/>
    2.73 +                    <ide-action name="rebuild"/>
    2.74 +                    <ide-action name="clean"/>
    2.75 +                    <ide-action name="run"/>
    2.76 +                </context-menu>
    2.77 +            </view>
    2.78 +        </general-data>
    2.79 +        <java-data xmlns="http://www.netbeans.org/ns/freeform-project-java/1">
    2.80 +            <compilation-unit>
    2.81 +                <package-root>src-api1.0</package-root>
    2.82 +                <source-level>1.4</source-level>
    2.83 +            </compilation-unit>
    2.84 +            <compilation-unit>
    2.85 +                <package-root>src-api2.0</package-root>
    2.86 +                <source-level>1.4</source-level>
    2.87 +            </compilation-unit>
    2.88 +            <compilation-unit>
    2.89 +                <package-root>src-apimerge</package-root>
    2.90 +                <source-level>1.4</source-level>
    2.91 +            </compilation-unit>
    2.92 +            <compilation-unit>
    2.93 +                <package-root>src-impl</package-root>
    2.94 +                <classpath mode="compile">src-api1.0</classpath>
    2.95 +                <source-level>1.4</source-level>
    2.96 +            </compilation-unit>
    2.97 +        </java-data>
    2.98 +    </configuration>
    2.99 +</project>
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/samples/differentreturntype/src-api1.0/api/API.java	Sat Jun 14 09:50:51 2008 +0200
     3.3 @@ -0,0 +1,11 @@
     3.4 +package api;
     3.5 +
     3.6 +import javax.swing.Icon;
     3.7 +
     3.8 +public abstract class API {
     3.9 +// BEGIN: theory.binary.differentreturntype.api
    3.10 +    public static Icon getIcon() {
    3.11 +// END: theory.binary.differentreturntype.api
    3.12 +        return null;
    3.13 +    }
    3.14 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/samples/differentreturntype/src-api2.0/api/API.java	Sat Jun 14 09:50:51 2008 +0200
     4.3 @@ -0,0 +1,11 @@
     4.4 +package api;
     4.5 +
     4.6 +import javax.swing.ImageIcon;
     4.7 +
     4.8 +public abstract class API {
     4.9 +// BEGIN: theory.binary.differentreturntype.api2
    4.10 +    public static ImageIcon getIcon() {
    4.11 +// END: theory.binary.differentreturntype.api2
    4.12 +        return null;
    4.13 +    }
    4.14 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/samples/differentreturntype/src-apimerge/api/API.java	Sat Jun 14 09:50:51 2008 +0200
     5.3 @@ -0,0 +1,11 @@
     5.4 +package api;
     5.5 +
     5.6 +import javax.swing.Icon;
     5.7 +import javax.swing.ImageIcon;
     5.8 +
     5.9 +public abstract class API {
    5.10 +// BEGIN: theory.binary.differentreturntype.apimerge
    5.11 +    public static ImageIcon getIcon() { return null; }
    5.12 +    public static Icon g3tIcon() { return null; }
    5.13 +// END: theory.binary.differentreturntype.apimerge
    5.14 +}
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/samples/differentreturntype/src-impl/impl/Impl.java	Sat Jun 14 09:50:51 2008 +0200
     6.3 @@ -0,0 +1,10 @@
     6.4 +package impl;
     6.5 +import api.API;
     6.6 +import javax.swing.Icon;
     6.7 +
     6.8 +public class Impl extends API {
     6.9 +    public static void main(String[] args) {
    6.10 +        Icon icon = API.getIcon();
    6.11 +        System.err.println("OK: " + icon);
    6.12 +    }
    6.13 +}