|
| 1 | +/*BEGIN_COPYRIGHT_BLOCK |
| 2 | + * |
| 3 | + * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu) |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions are met: |
| 8 | + * * Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * * Redistributions in binary form must reproduce the above copyright |
| 11 | + * notice, this list of conditions and the following disclaimer in the |
| 12 | + * documentation and/or other materials provided with the distribution. |
| 13 | + * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the |
| 14 | + * names of its contributors may be used to endorse or promote products |
| 15 | + * derived from this software without specific prior written permission. |
| 16 | + * |
| 17 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 20 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 21 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 22 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 23 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 24 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 25 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 26 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | + * |
| 29 | + * This software is Open Source Initiative approved Open Source Software. |
| 30 | + * Open Source Initative Approved is a trademark of the Open Source Initiative. |
| 31 | + * |
| 32 | + * This file is part of DrJava. Download the current version of this project |
| 33 | + * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/ |
| 34 | + * |
| 35 | + * END_COPYRIGHT_BLOCK*/ |
| 36 | + |
| 37 | +package edu.rice.cs.drjava.model.compiler; |
| 38 | + |
| 39 | +import java.util.List; |
| 40 | +import java.util.ArrayList; |
| 41 | +import java.util.LinkedList; |
| 42 | +import java.util.Iterator; |
| 43 | +import java.io.File; |
| 44 | +import java.io.IOException; |
| 45 | +import java.io.FilenameFilter; |
| 46 | +import java.io.FileFilter; |
| 47 | +import java.io.InputStream; |
| 48 | +import java.io.FileOutputStream; |
| 49 | +import edu.rice.cs.plt.io.IOUtil; |
| 50 | +import edu.rice.cs.drjava.config.OptionConstants; |
| 51 | +import edu.rice.cs.drjava.DrJava; |
| 52 | +import edu.rice.cs.drjava.model.DJError; |
| 53 | +import edu.rice.cs.util.ArgumentTokenizer; |
| 54 | +import edu.rice.cs.plt.reflect.JavaVersion; |
| 55 | + |
| 56 | +/** An abstract parent for all javac-based compiler interfaces that may need to filter .exe files from the |
| 57 | + * classpath, i.e. javac from JDKs 1.6.0_04 or newer. |
| 58 | + * @version $Id$ |
| 59 | + */ |
| 60 | +public abstract class Javac160FilteringCompiler extends JavacCompiler { |
| 61 | + protected final boolean _filterExe; |
| 62 | + protected final File _tempJUnit; |
| 63 | + protected static final String PREFIX = "drjava-junit"; |
| 64 | + protected static final String SUFFIX = ".jar"; |
| 65 | + |
| 66 | + protected Javac160FilteringCompiler(JavaVersion.FullVersion version, |
| 67 | + String location, |
| 68 | + List<? extends File> defaultBootClassPath) { |
| 69 | + super(version, location, defaultBootClassPath); |
| 70 | + |
| 71 | + _filterExe = version.compareTo(JavaVersion.parseFullVersion("1.6.0_04")) >= 0; |
| 72 | + File tempJUnit = null; |
| 73 | + if (_filterExe) { |
| 74 | + // if we need to filter out exe files from the classpath, we also need to |
| 75 | + // extract junit.jar and create a temporary file |
| 76 | + try { |
| 77 | + // edu.rice.cs.util.Log LOG = new edu.rice.cs.util.Log("jdk160.txt",true); |
| 78 | + // LOG.log("Filtering exe files from classpath."); |
| 79 | + InputStream is = Javac160FilteringCompiler.class.getResourceAsStream("/junit.jar"); |
| 80 | + if (is!=null) { |
| 81 | + // LOG.log("\tjunit.jar found"); |
| 82 | + tempJUnit = edu.rice.cs.plt.io.IOUtil.createAndMarkTempFile(PREFIX,SUFFIX); |
| 83 | + FileOutputStream fos = new FileOutputStream(tempJUnit); |
| 84 | + int size = edu.rice.cs.plt.io.IOUtil.copyInputStream(is,fos); |
| 85 | + // LOG.log("\t"+size+" bytes written to "+tempJUnit.getAbsolutePath()); |
| 86 | + } |
| 87 | + else { |
| 88 | + // LOG.log("\tjunit.jar not found"); |
| 89 | + if (tempJUnit!=null) { |
| 90 | + tempJUnit.delete(); |
| 91 | + tempJUnit = null; |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + catch(IOException ioe) { |
| 96 | + if (tempJUnit!=null) { |
| 97 | + tempJUnit.delete(); |
| 98 | + tempJUnit = null; |
| 99 | + } |
| 100 | + } |
| 101 | + // sometimes this file may be left behind, so create a shutdown hook |
| 102 | + // that deletes temporary files matching our pattern |
| 103 | + Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { |
| 104 | + public void run() { |
| 105 | + try { |
| 106 | + File temp = File.createTempFile(PREFIX, SUFFIX); |
| 107 | + IOUtil.attemptDelete(temp); |
| 108 | + File[] toDelete = temp.getParentFile().listFiles(new FilenameFilter() { |
| 109 | + public boolean accept(File dir, String name) { |
| 110 | + if ((!name.startsWith(PREFIX)) || (!name.endsWith(SUFFIX))) return false; |
| 111 | + String rest = name.substring(PREFIX.length(), name.length()-SUFFIX.length()); |
| 112 | + try { |
| 113 | + Integer i = new Integer(rest); |
| 114 | + // we could create an integer from the rest, this is one of our temporary files |
| 115 | + return true; |
| 116 | + } |
| 117 | + catch(NumberFormatException e) { /* couldn't convert, ignore this file */ } |
| 118 | + return false; |
| 119 | + } |
| 120 | + }); |
| 121 | + for(File f: toDelete) { |
| 122 | + f.delete(); |
| 123 | + } |
| 124 | + } |
| 125 | + catch(IOException ioe) { /* could not delete temporary files, ignore */ } |
| 126 | + } |
| 127 | + })); |
| 128 | + } |
| 129 | + _tempJUnit = tempJUnit; |
| 130 | + } |
| 131 | + |
| 132 | + protected java.util.List<File> getFilteredClassPath(java.util.List<? extends File> classPath) { |
| 133 | + java.util.List<File> filteredClassPath = null; |
| 134 | + if (classPath!=null) { |
| 135 | + filteredClassPath = new LinkedList<File>(classPath); |
| 136 | + |
| 137 | + if (_filterExe) { |
| 138 | + FileFilter filter = IOUtil.extensionFilePredicate("exe"); |
| 139 | + Iterator<? extends File> i = filteredClassPath.iterator(); |
| 140 | + while (i.hasNext()) { |
| 141 | + if (filter.accept(i.next())) { i.remove(); } |
| 142 | + } |
| 143 | + if (_tempJUnit!=null) { filteredClassPath.add(_tempJUnit); } |
| 144 | + } |
| 145 | + } |
| 146 | + return filteredClassPath; |
| 147 | + } |
| 148 | +} |
0 commit comments