Stefan Bodewig wrote:
> 
> >>>>> "MKH" == Matthew Kuperus Heun <[EMAIL PROTECTED]> writes:
> 
>  >> destFile.setLastModified(sourceFile.lastModified());
>  >>
>  >> I'm affraid this isn't Java 1.1 compatible, is this a problem?
> 
> Take a look at the <touch> task (and unzip, untar ...).
> 
>  MKH> What is the general policy for ANT wrt jdk1.2?
> 
> Simple: Ant is JDK 1.1 compatible. If there is something you cannot do
> in 1.1 but is very useful (like setting the timestamp of a file) -
> call it via reflection and print a warning if necessary.

I've done it using reflection now. I created OldJava.java wich contains the
setFileLastModified method wich can be used at other places as well (like
Touch.java). Can somebody take a look at what I've done and add it to CVS if
approved?

Jaco
752,753d751
<             OldJava oldJava = new OldJava(this);
<             oldJava.setFileLastModified(destFile, sourceFile.lastModified());

/*
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 1999 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 *    any, must include the following acknowlegement:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowlegement may appear in the software itself,
 *    if and wherever such third-party acknowlegements normally appear.
 *
 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
 *    Foundation" must not be used to endorse or promote products derived
 *    from this software without prior written permission. For written
 *    permission, please contact [EMAIL PROTECTED]
 *
 * 5. Products derived from this software may not be called "Apache"
 *    nor may "Apache" appear in their names without prior written
 *    permission of the Apache Group.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */

package org.apache.tools.ant;

import java.io.*;
import java.lang.reflect.*;

/**
 * This class calls Java > 1.1 methods using reflection in a Java 1.1
 * compatible way.
 *
 * @author Jaco de Groot <a href="mailto:[EMAIL PROTECTED]">[EMAIL 
PROTECTED]</a>
 */
public class OldJava {
    private Project project;
    private static Method setLastModified = null;
    private static Object lockReflection = new Object();

    OldJava(Project project) {
        this.project = project;
    }

    /**
     * Calls File.setLastModified(long time)
     */
    void setFileLastModified(File file, long time) throws BuildException {
        if (project.getJavaVersion() == Project.JAVA_1_1) {
            return;
        }
        if (setLastModified == null) {
            synchronized (lockReflection) {
                if (setLastModified == null) {
                    try {
        setLastModified = java.io.File.class.getMethod("setLastModified", 
                                                       new Class[] {Long.TYPE});
                    } catch (NoSuchMethodException nse) {
        throw new BuildException("File.setlastModified not in JDK > 1.1?",
                                 nse);
                    }
                }
            }
        }
        Long[] times = new Long[1];
        if (time < 0) {
            times[0] = new Long(System.currentTimeMillis());
        } else {
            times[0] = new Long(time);
        }
        try {
            project.log("Setting modification time for " + file,
                        Project.MSG_VERBOSE);
            setLastModified.invoke(file, times);
        } catch (InvocationTargetException ite) {
            Throwable nested = ite.getTargetException();
            throw new BuildException("Exception setting the modification time "
                                     + "of " + file, nested);
        } catch (Throwable other) {
            throw new BuildException("Exception setting the modification time "
                                     + "of " + file, other);
        }
    }

}

Reply via email to