1 /* 2 * ProGuard -- shrinking, optimization, obfuscation, and preverification 3 * of Java bytecode. 4 * 5 * Copyright (c) 2002-2009 Eric Lafortune (eric (at) graphics.cornell.edu) 6 * 7 * This program is free software; you can redistribute it and/or modify it 8 * under the terms of the GNU General Public License as published by the Free 9 * Software Foundation; either version 2 of the License, or (at your option) 10 * any later version. 11 * 12 * This program is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 15 * more details. 16 * 17 * You should have received a copy of the GNU General Public License along 18 * with this program; if not, write to the Free Software Foundation, Inc., 19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 */ 21 package proguard; 22 23 import java.io.File; 24 25 /** 26 * This class checks whether the output is up to date. 27 * 28 * @author Eric Lafortune 29 */ 30 public class UpToDateChecker 31 { 32 private final Configuration configuration; 33 34 35 /** 36 * Creates a new UpToDateChecker with the given configuration. 37 */ 38 public UpToDateChecker(Configuration configuration) 39 { 40 this.configuration = configuration; 41 } 42 43 44 /** 45 * Returns whether the output is up to date, based on the modification times 46 * of the input jars, output jars, and library jars (or directories). 47 */ 48 public boolean check() 49 { 50 long inputLastModified = configuration.lastModified; 51 long outputLastModified = Long.MAX_VALUE; 52 53 ClassPath programJars = configuration.programJars; 54 ClassPath libraryJars = configuration.libraryJars; 55 56 // Check the dates of the program jars, if any. 57 if (programJars != null) 58 { 59 for (int index = 0; index < programJars.size(); index++) 60 { 61 // Break early, if possible. 62 if (inputLastModified >= outputLastModified) 63 { 64 break; 65 } 66 67 // Update the input and output modification times. 68 ClassPathEntry classPathEntry = programJars.get(index); 69 if (classPathEntry.isOutput()) 70 { 71 long lastModified = lastModified(classPathEntry.getFile(), true); 72 if (outputLastModified > lastModified) 73 { 74 outputLastModified = lastModified; 75 } 76 } 77 else 78 { 79 long lastModified = lastModified(classPathEntry.getFile(), false); 80 if (inputLastModified < lastModified) 81 { 82 inputLastModified = lastModified; 83 } 84 } 85 } 86 } 87 88 // Check the dates of the library jars, if any. 89 if (libraryJars != null) 90 { 91 for (int index = 0; index < libraryJars.size(); index++) 92 { 93 // Break early, if possible. 94 if (inputLastModified >= outputLastModified) 95 { 96 break; 97 } 98 99 // Update the input modification time. 100 ClassPathEntry classPathEntry = libraryJars.get(index); 101 long lastModified = lastModified(classPathEntry.getFile(), false); 102 if (inputLastModified < lastModified) 103 { 104 inputLastModified = lastModified; 105 } 106 } 107 } 108 109 boolean outputUpToDate = inputLastModified < outputLastModified; 110 if (outputUpToDate) 111 { 112 System.out.println("The output is up to date"); 113 } 114 115 return outputUpToDate; 116 } 117 118 119 /** 120 * Returns the minimum or maximum modification time of the given file or 121 * of the files in the given directory (recursively). 122 */ 123 private long lastModified(File file, boolean minimum) 124 { 125 // Is it a directory? 126 if (file.isDirectory()) 127 { 128 // Ignore the directory's modification time; just recurse on its files. 129 File[] files = file.listFiles(); 130 131 // Still, an empty output directory is probably a sign that it is 132 // not up to date. 133 long lastModified = files.length != 0 && minimum ? 134 Long.MAX_VALUE : 0L; 135 136 for (int index = 0; index < files.length; index++) 137 { 138 long fileLastModified = lastModified(files[index], minimum); 139 if ((lastModified < fileLastModified) ^ minimum) 140 { 141 lastModified = fileLastModified; 142 } 143 } 144 145 return lastModified; 146 } 147 else 148 { 149 // Return the file's modification time. 150 return file.lastModified(); 151 } 152 } 153 } 154