Skip to content

Commit

Permalink
[MSHARED-860] Deprecate unnecessary Java7Support (#30)
Browse files Browse the repository at this point in the history
* [MSHARED-860] Call java.nio.file.Files utility methods directly rather than via reflection

* [MSHARED-860] Move createSymbolicLink() utility to FileUtils

* [MSHARED-860] Deprecate Java7Support and call java.nio.file.Files methods directly
  • Loading branch information
roxspring committed May 16, 2020
1 parent bb2f85e commit 3294262
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 223 deletions.
Expand Up @@ -21,6 +21,7 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
Expand Down Expand Up @@ -902,13 +903,7 @@ public void addDefaultExcludes()
boolean isSymbolicLink( final File parent, final String name )
throws IOException
{
if ( Java7Support.isAtLeastJava7() )
{
return Java7Support.isSymLink( parent );
}
final File resolvedParent = new File( parent.getCanonicalPath() );
final File toTest = new File( resolvedParent, name );
return !toTest.getAbsolutePath().equals( toTest.getCanonicalPath() );
return Files.isSymbolicLink( parent.toPath() );
}

private void setupDefaultFilters()
Expand Down
59 changes: 27 additions & 32 deletions src/main/java/org/apache/maven/shared/utils/io/FileUtils.java
Expand Up @@ -41,6 +41,7 @@
import java.net.URL;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.security.SecureRandom;
import java.text.DecimalFormat;
import java.util.ArrayList;
Expand Down Expand Up @@ -764,10 +765,10 @@ public static void copyFile( @Nonnull final File source, @Nonnull final File des
final String message = "File " + source + " does not exist";
throw new IOException( message );
}
if ( Java7Support.isAtLeastJava7() && Java7Support.isSymLink( source ) )
if ( Files.isSymbolicLink( source.toPath() ) )
{
File target = Java7Support.readSymbolicLink( source );
Java7Support.createSymbolicLink( destination, target );
File target = Files.readSymbolicLink( source.toPath() ).toFile();
createSymbolicLink( destination, target );
return;
}

Expand Down Expand Up @@ -1101,17 +1102,7 @@ public static void forceDelete( @Nonnull final File file )
public static void delete( @Nonnull File file )
throws IOException
{
if ( Java7Support.isAtLeastJava7() )
{
Java7Support.delete( file );
}
else
{
if ( !file.delete() )
{
throw new IOException( "Could not delete " + file.getName() );
}
}
Files.delete( file.toPath() );
}

/**
Expand All @@ -1120,21 +1111,14 @@ public static void delete( @Nonnull File file )
*/
public static boolean deleteLegacyStyle( @Nonnull File file )
{
if ( Java7Support.isAtLeastJava7() )
try
{
try
{
Java7Support.delete( file );
return true;
}
catch ( IOException e )
{
return false;
}
Files.delete( file.toPath() );
return true;
}
else
catch ( IOException e )
{
return file.delete();
return false;
}
}

Expand Down Expand Up @@ -1934,11 +1918,7 @@ private static boolean isValidWindowsFileName( @Nonnull File f )
public static boolean isSymbolicLink( @Nonnull final File file )
throws IOException
{
if ( Java7Support.isAtLeastJava7() )
{
return Java7Support.isSymLink( file );
}
return isSymbolicLinkLegacy( file );
return Files.isSymbolicLink( file.toPath() );
}

/**
Expand All @@ -1953,7 +1933,7 @@ public static boolean isSymbolicLink( @Nonnull final File file )
public static boolean isSymbolicLinkForSure( @Nonnull final File file )
throws IOException
{
return Java7Support.isAtLeastJava7() && Java7Support.isSymLink( file );
return Files.isSymbolicLink( file.toPath() );
}

/**
Expand Down Expand Up @@ -1981,4 +1961,19 @@ static boolean isSymbolicLinkLegacy( @Nonnull final File file )
return !file.getAbsolutePath().equals( canonical.getPath() );
}

/**
* @param symlink The link name.
* @param target The target.
* @return The linked file.
* @throws IOException in case of an error.
*/
@Nonnull public static File createSymbolicLink( @Nonnull File symlink, @Nonnull File target )
throws IOException
{
if ( !Files.exists( symlink.toPath() ) )
{
return Files.createSymbolicLink( symlink.toPath(), target.toPath() ).toFile();
}
return symlink;
}
}
162 changes: 12 additions & 150 deletions src/main/java/org/apache/maven/shared/utils/io/Java7Support.java
Expand Up @@ -22,93 +22,27 @@
import javax.annotation.Nonnull;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Files;

/**
* Java7 feature detection
*
* @author Kristian Rosenvold
*
* @deprecated no longer needed, prefer to use {@link java.nio.file.Files} methods directly.
*/
@Deprecated
public class Java7Support
{

private static final boolean IS_JAVA7;

private static Method isSymbolicLink;

private static Method delete;

private static Method toPath;

private static Method exists;

private static Method toFile;

private static Method readSymlink;

private static Method createSymlink;

private static Object emptyLinkOpts;

private static Object emptyFileAttributes;

static
{
boolean isJava7x = true;
try
{
ClassLoader cl = Thread.currentThread().getContextClassLoader();
Class<?> files = cl.loadClass( "java.nio.file.Files" );
Class<?> path = cl.loadClass( "java.nio.file.Path" );
Class<?> fa = cl.loadClass( "java.nio.file.attribute.FileAttribute" );
Class<?> linkOption = cl.loadClass( "java.nio.file.LinkOption" );
isSymbolicLink = files.getMethod( "isSymbolicLink", path );
delete = files.getMethod( "delete", path );
readSymlink = files.getMethod( "readSymbolicLink", path );

emptyFileAttributes = Array.newInstance( fa, 0 );
final Object o = emptyFileAttributes;
createSymlink = files.getMethod( "createSymbolicLink", path, path, o.getClass() );
emptyLinkOpts = Array.newInstance( linkOption, 0 );
exists = files.getMethod( "exists", path, emptyLinkOpts.getClass() );
toPath = File.class.getMethod( "toPath" );
toFile = path.getMethod( "toFile" );
}
catch ( ClassNotFoundException e )
{
isJava7x = false;
}
catch ( NoSuchMethodException e )
{
isJava7x = false;
}
IS_JAVA7 = isJava7x;
}

/**
* @param file The file to check for being a symbolic link.
* @return true if the file is a symlink false otherwise.
*/
public static boolean isSymLink( @Nonnull File file )
{
try
{
Object path = toPath.invoke( file );
return (Boolean) isSymbolicLink.invoke( null, path );
}
catch ( IllegalAccessException e )
{
throw new RuntimeException( e );
}
catch ( InvocationTargetException e )
{
throw new RuntimeException( e );
}
return Files.isSymbolicLink( file.toPath() );
}


/**
* @param symlink The sym link.
* @return The file.
Expand All @@ -117,23 +51,9 @@ public static boolean isSymLink( @Nonnull File file )
@Nonnull public static File readSymbolicLink( @Nonnull File symlink )
throws IOException
{
try
{
Object path = toPath.invoke( symlink );
Object resultPath = readSymlink.invoke( null, path );
return (File) toFile.invoke( resultPath );
}
catch ( IllegalAccessException e )
{
throw new RuntimeException( e );
}
catch ( InvocationTargetException e )
{
throw new RuntimeException( e );
}
return Files.readSymbolicLink( symlink.toPath() ).toFile();
}


/**
* @param file The file to check.
* @return true if exist false otherwise.
Expand All @@ -142,21 +62,7 @@ public static boolean isSymLink( @Nonnull File file )
public static boolean exists( @Nonnull File file )
throws IOException
{
try
{
Object path = toPath.invoke( file );
final Object invoke = exists.invoke( null, path, emptyLinkOpts );
return (Boolean) invoke;
}
catch ( IllegalAccessException e )
{
throw new RuntimeException( e );
}
catch ( InvocationTargetException e )
{
throw (RuntimeException) e.getTargetException();
}

return Files.exists( file.toPath() );
}

/**
Expand All @@ -168,40 +74,9 @@ public static boolean exists( @Nonnull File file )
@Nonnull public static File createSymbolicLink( @Nonnull File symlink, @Nonnull File target )
throws IOException
{
try
{
if ( !exists( symlink ) )
{
Object link = toPath.invoke( symlink );
Object path = createSymlink.invoke( null, link, toPath.invoke( target ), emptyFileAttributes );
return (File) toFile.invoke( path );
}
return symlink;
}
catch ( IllegalAccessException e )
{
throw new RuntimeException( e );
}
catch ( InvocationTargetException e )
{
final Throwable targetException = e.getTargetException();
if ( targetException instanceof IOException )
{
throw (IOException) targetException;
}
else if ( targetException instanceof RuntimeException )
{
// java.lang.UnsupportedOperationException: Symbolic links not supported on this operating system
// java.lang.SecurityException: denies certain permissions see Javadoc
throw ( RuntimeException ) targetException;
}
else
{
throw new IOException( targetException.getClass() + ": " + targetException.getLocalizedMessage() );
}
}

return FileUtils.createSymbolicLink( symlink, target );
}

/**
* Performs a nio delete
* @param file the file to delete
Expand All @@ -210,35 +85,22 @@ else if ( targetException instanceof RuntimeException )
public static void delete( @Nonnull File file )
throws IOException
{
try
{
Object path = toPath.invoke( file );
delete.invoke( null, path );
}
catch ( IllegalAccessException e )
{
throw new RuntimeException( e );
}
catch ( InvocationTargetException e )
{
throw (IOException) e.getTargetException();
}
Files.delete( file.toPath() );
}

/**
* @return true in case of Java 7.
*/
public static boolean isJava7()
{
return IS_JAVA7;
return true;
}

/**
* @return true in case of Java7 or greater.
*/
public static boolean isAtLeastJava7()
{
return IS_JAVA7;
return true;
}

}
Expand Up @@ -153,7 +153,6 @@ public void followSymlinksFalse()
throws IOException
{
assumeFalse( Os.isFamily( Os.FAMILY_WINDOWS ) );
assumeTrue( Java7Support.isAtLeastJava7() );

File testDir = SymlinkTestSetup.createStandardSymlinkTestDir( new File( "target/test/symlinkTestCase" ) );

Expand Down Expand Up @@ -191,7 +190,6 @@ public void followSymlinks()
throws IOException
{
assumeFalse( Os.isFamily( Os.FAMILY_WINDOWS ) );
assumeTrue( Java7Support.isAtLeastJava7() );

DirectoryScanner ds = new DirectoryScanner();
File testDir = SymlinkTestSetup.createStandardSymlinkTestDir( new File( "target/test/symlinkTestCase" ) );
Expand Down

0 comments on commit 3294262

Please sign in to comment.