alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.java.fastjavac;

import java.io.File;
import java.io.ObjectInputStream;
import java.io.IOException;
import java.util.ResourceBundle;

import org.openide.ErrorManager;
import org.openide.loaders.DataObject;
import org.openide.compiler.Compiler;
import org.openide.compiler.CompilerType;
import org.openide.compiler.CompilerJob;
import org.openide.compiler.ExternalCompiler;
import org.openide.compiler.ExternalCompiler.ErrorExpression;
import org.openide.compiler.ExternalCompilerGroup;
import org.openide.execution.NbProcessDescriptor;
import org.openide.util.HelpCtx;
import org.openide.util.NbBundle;
import org.openide.util.Utilities;
import org.openide.filesystems.FileObject;

import org.netbeans.modules.java.JavaExternalCompilerType;
import org.netbeans.modules.java.JExternalCompilerGroup;

/**
*
* @author Ales Novak
*/
public class FastJavacCompilerType extends JavaExternalCompilerType {
    /** generated Serialized Version UID */
    private static final long serialVersionUID = -3875747959787225041L;
    
    /** Fully qualified name of the binary according to the host OS */
    private static String wiredFullName;
    
    /** base name of the binary, dependent on the host OS */
    private static String wiredName;
    
    private transient int version = 0;

    public FastJavacCompilerType() {
        super();
    }

    /** overriden */
    protected NbProcessDescriptor createDescriptor() {
        return new NbProcessDescriptor(
            NEW_PROCESS_NAME,
            //"-msgfile {" + JExternalCompilerGroup.JFormat.TAG_MSGFILE + "} " + // NOI18N
            "{" + JExternalCompilerGroup.JFormat.TAG_DEBUGINFO + "} " + // NOI18N
            "{" + JExternalCompilerGroup.JFormat.TAG_DEPRECATION + "} " + // NOI18N
            "{" + JExternalCompilerGroup.JFormat.TAG_OPTIMIZE + "} " + // NOI18N
            "{" + JExternalCompilerGroup.JFormat.TAG_OUTDIR + "} " + // NOI18N
            "{" + JExternalCompilerGroup.JFormat.TAG_ENCODING + "} " + // NOI18N
            "{" + JExternalCompilerGroup.JFormat.TAG_SOURCE + "} " + // NOI18N
            "{" + JExternalCompilerGroup.JFormat.TAG_OPTION_BOOTCLASSPATH + "} " + // NOI18N
            "{" + JExternalCompilerGroup.JFormat.TAG_BOOTCLASSPATH + "} " + // NOI18N
            "-jdk {" + JExternalCompilerGroup.JFormat.TAG_QUOTE + "}{" + // NOI18N
                ExternalCompilerGroup.Format.TAG_JDKHOME + "}{" + // NOI18N
                JExternalCompilerGroup.JFormat.TAG_QUOTE + "} " + // NOI18N
            "-classpath {" + ExternalCompilerGroup.Format.TAG_REPOSITORY + "}{" + ExternalCompilerGroup.Format.TAG_PATHSEPARATOR + "}" + // NOI18N
            "{" + ExternalCompilerGroup.Format.TAG_CLASSPATH + "} " + // NOI18N
            "{" + ExternalCompilerGroup.Format.TAG_FILES + "}", // NOI18N
            getAdditionalHint() + Util.getString("MSG_FastCompilerHint")
        );
    }
    
    private static final String NEW_PROCESS_NAME = 
            "{" + JExternalCompilerGroup.JFormat.TAG_FASTJAVAC_BINARY_FULL + "}"; // NOI18N
    
    private static final String REPLACE_PROCESS_NAME = 
            "{" + JExternalCompilerGroup.JFormat.TAG_IDEHOME + "}{" + ExternalCompilerGroup.Format.TAG_SEPARATOR + "}bin{" + // NOI18N
            ExternalCompilerGroup.Format.TAG_SEPARATOR + "}fastjavac{" + ExternalCompilerGroup.Format.TAG_SEPARATOR + "}{" + // NOI18N
		JExternalCompilerGroup.JFormat.TAG_FASTJAVAC_BINARY + "}"; // NOI18N

    public NbProcessDescriptor getExternalCompiler() {
        updateConfig();
        return super.getExternalCompiler();
    }
    
    /**
     * This method tries to update name hardwired to the IDE's home directory
     * to a more flexible form which allows to override FastJavac for the specific
     * user directory.
     */
    private void updateConfig() {
        if (externalCompiler == null || version > 0)
            return;
        String procName = externalCompiler.getProcessName();
        if (REPLACE_PROCESS_NAME.equals(procName)) {
            externalCompiler = new NbProcessDescriptor(NEW_PROCESS_NAME,
                externalCompiler.getArguments());
        }
        version = 1;
    }
    
    /** human presentable name */
    public String displayName() {
        // Default only matters for regular external, which is
        // instantiated using Class.newInstance:
        return Util.getString("CTL_FastCompilerType");
        // In the case of the FastJavac, the .ser is given the token
        // name; when initially loaded from manifest, readObject
        // gives it a localized name. Thereafter the localized name
        // is serialized in the project.
    }

    public HelpCtx getHelpCtx () {
        return new HelpCtx(FastJavacCompilerType.class);
    }

    
    private static boolean isX86() {
	String arch = System.getProperty("os.arch"); // NOI18N
	return (arch.indexOf("i386") >= 0 || // NOI18N
	    arch.indexOf("x86") >= 0); // NOI18N
    }
    
    private static boolean isX86Linux() {
        return isX86() && ((Utilities.getOperatingSystem() & Utilities.OS_LINUX) != 0);
    }
    public static boolean isFastJavacPlatform() {
        return getWiredFullName() != null;
    }

    static java.lang.reflect.Method timestamp;
    static java.lang.reflect.Method uptodate;

    protected Compiler createCompiler(Class type, FileObject fo) {
        Compiler c = super.createCompiler(type, fo);
        if (timestamp == null) {
            try {
                timestamp = Compiler.class.getDeclaredMethod("getTimeStamp", new Class[0]);
                timestamp.setAccessible(true);
                uptodate = Compiler.class.getDeclaredMethod("isUpToDate", new Class[0]);
                uptodate.setAccessible(true);
            } catch (Exception ex) {
                // should never happen
                ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
            }
        }
        return new C(c);
    }

    private static boolean isWin() {
        return ((Utilities.getOperatingSystem() & Utilities.OS_WINDOWS_MASK) != 0);
    }
    
    private static String findFastJavac(String rootName) {
	String name = getWiredName();

	if (name == null)
	    // unknown architecture ?!?
	    return null;
	    
        File root = new java.io.File(rootName);
        File dir = new File(root, "modules" + File.separator + "bin" + File.separator + "fastjavac"); // NOI18N
        File f1 = new File(dir, getWiredName());
        if (f1.exists()) {
            try {
                return f1.getCanonicalPath();
            } catch (IOException ex) {
                // next try
            }
        } 
        f1 = new File(dir, "fastjavac"); // NOI18N
        if (f1.exists()) {
            try {
                return f1.getCanonicalPath();
            } catch (IOException ex) {
                // next try
            }
        }
        return null;
    }
    
    static String getWiredFullName() {
        if (wiredFullName != null) {
            if (wiredFullName.length() == 0)
                return null;
            return wiredFullName;
        }
        String user = System.getProperty("netbeans.user"); // NOI18N
        String nb = System.getProperty(JExternalCompilerGroup.JFormat.TAG_NBHOME);
        String fjpath = null;
        
        if (!user.equals(nb)) {
            fjpath = findFastJavac(user);
        }
        
        if (fjpath == null) {
            fjpath = findFastJavac(nb);
        }
        if (fjpath != null) {
            wiredFullName = fjpath;
            return fjpath;
        }
        wiredFullName = ""; // NOI18N
        return null;
    }
    
    /** @return one of fastjavac.sun, fastjavac.sun.intel, fastjavac.exe
     * fastjavac.linux, null
     */
    static String getWiredName() {
        if (wiredName != null)
            return wiredName;
        int OS = Utilities.getOperatingSystem();
        if ((OS & Utilities.OS_SOLARIS) != 0) {
            if (System.getProperty("os.arch").indexOf("sparc") >= 0) { // NOI18N
                wiredName = "fastjavac.sun"; // NOI18N
            } else {
                wiredName = "fastjavac.sun.intel"; // NOI18N
            }
        } else if ((OS & Utilities.OS_WINDOWS_MASK) != 0) {
            wiredName = "fastjavac.exe";  // NOI18N
        } else if (isX86Linux()) {
            wiredName = "fastjavac.linux"; // NOI18N
        }
        
        return wiredName;
    }

    /** @return additional hints for java's external compiler types */
    final static String getAdditionalHint() {
        java.util.ResourceBundle bundle = NbBundle.getBundle("org.openide.compiler.Bundle"); // NOI18N
        String hint0 = bundle.getString ("MSG_ExternalCompilerHint");
	return hint0;
    }

    static class C extends Compiler {
        Compiler    del;

        C(Compiler del) {
            this.del = del;
        }

        public boolean isUpToDate() {
            try {
                return ((Boolean)uptodate.invoke(del, new Object[0])).booleanValue();
            } catch (Exception ex) {
                ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
                return false;
            }
        }

        public Class compilerGroupClass() {
            return FastJavacCG.class;
        }

        public Object compilerGroupKey() {
            return del.compilerGroupKey();
        }

        public int hashCode() {
            return del.hashCode();
        }

        public boolean equals(Object o) {
            return (o instanceof C) && ((C)o).del.equals(del);
        }

        protected java.util.Date getTimeStamp() {
            try {
                return (java.util.Date)timestamp.invoke(del, new Object[0]);
            } catch (Exception ex) {
                ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
                return null;
            }
        }

        public void dependsOn(java.util.Collection compilables) {
            del.dependsOn(compilables);
        }
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.