Package org.apache.tools.ant.taskdefs.optional.javah

Source Code of org.apache.tools.ant.taskdefs.optional.javah.SunJavah

/*
* Copyright 2005 The Apache Software Foundation
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*
*/
package org.apache.tools.ant.taskdefs.optional.javah;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.taskdefs.ExecuteJava;
import org.apache.tools.ant.taskdefs.optional.Javah;
import org.apache.tools.ant.types.Commandline;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.util.JavaEnvUtils;

/**
* Adapter to com.sun.tools.javah.oldjavah.Main or com.sun.tools.javah.Main.
*
* @since Ant 1.6.3
*/
public class SunJavah implements JavahAdapter {

    public static final String IMPLEMENTATION_NAME = "sun";

    /**
     * Performs the actual compilation.
     *
     * @since Ant 1.6.3
     */
    public boolean compile(Javah javah) throws BuildException {
        Commandline cmd = setupJavahCommand(javah);
        ExecuteJava ej = new ExecuteJava();

        try {
            try {
                // first search for the "old" javah class in 1.4.2 tools.jar
                Class.forName("com.sun.tools.javah.oldjavah.Main");
                cmd.setExecutable("com.sun.tools.javah.oldjavah.Main");
            } catch (ClassNotFoundException cnfe) {
                // assume older than 1.4.2 tools.jar
                Class.forName("com.sun.tools.javah.Main");
                cmd.setExecutable("com.sun.tools.javah.Main");
            }
        } catch (ClassNotFoundException ex) {
            throw new BuildException("Can't load javah", ex,
                                     javah.getLocation());
        }
        ej.setJavaCommand(cmd);
        return ej.fork(javah) == 0;
    }

    private Commandline setupJavahCommand(Javah javah) {
        Commandline cmd = new Commandline();

        if (javah.getDestdir() != null) {
            cmd.createArgument().setValue("-d");
            cmd.createArgument().setFile(javah.getDestdir());
        }

        if (javah.getOutputfile() != null) {
            cmd.createArgument().setValue("-o");
            cmd.createArgument().setFile(javah.getOutputfile());
        }

        if (javah.getClasspath() != null) {
            cmd.createArgument().setValue("-classpath");
            cmd.createArgument().setPath(javah.getClasspath());
        }

        // JDK1.1 is rather simpler than JDK1.2
        if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1)) {
            if (javah.getVerbose()) {
                cmd.createArgument().setValue("-v");
            }
        } else {
            if (javah.getVerbose()) {
                cmd.createArgument().setValue("-verbose");
            }
            if (javah.getOld()) {
                cmd.createArgument().setValue("-old");
            }
            if (javah.getForce()) {
                cmd.createArgument().setValue("-force");
            }
            if (javah.getStubs() && !javah.getOld()) {
                throw new BuildException("stubs only available in old mode.",
                                         javah.getLocation());
            }
        }

        if (javah.getStubs()) {
            cmd.createArgument().setValue("-stubs");
        }
        Path bcp = new Path(javah.getProject());
        if (javah.getBootclasspath() != null) {
            bcp.append(javah.getBootclasspath());
        }
        if (bcp.size() > 0) {
            cmd.createArgument().setValue("-bootclasspath");
            cmd.createArgument().setPath(bcp);
        }

        cmd.addArguments(javah.getCurrentArgs());

        javah.logAndAddFiles(cmd);
        return cmd;
    }

}
TOP

Related Classes of org.apache.tools.ant.taskdefs.optional.javah.SunJavah

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.