blob: 412b02aafeb51489b7a743a25a1ecbb0f2d006f6 [file] [log] [blame]
package org.apache.maven.shared.dependency.analyzer;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.codehaus.plexus.util.DirectoryScanner;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
/**
* Utility to visit classes in a library given either as a jar file or an exploded directory.
*
* @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
*/
public final class ClassFileVisitorUtils
{
private ClassFileVisitorUtils()
{
// private constructor for utility class
}
/**
* <p>accept.</p>
*
* @param url a {@link java.net.URL} object.
* @param visitor a {@link org.apache.maven.shared.dependency.analyzer.ClassFileVisitor} object.
* @throws java.io.IOException if any.
*/
public static void accept( URL url, ClassFileVisitor visitor )
throws IOException
{
if ( url.getPath().endsWith( ".jar" ) )
{
acceptJar( url, visitor );
}
else if ( url.getProtocol().equalsIgnoreCase( "file" ) )
{
try
{
File file = new File( new URI( url.toString() ) );
if ( file.isDirectory() )
{
acceptDirectory( file, visitor );
}
else if ( file.exists() )
{
throw new IllegalArgumentException( "Cannot accept visitor on URL: " + url );
}
}
catch ( URISyntaxException exception )
{
throw new IllegalArgumentException( "Cannot accept visitor on URL: " + url,
exception );
}
}
else
{
throw new IllegalArgumentException( "Cannot accept visitor on URL: " + url );
}
}
// private methods --------------------------------------------------------
private static void acceptJar( URL url, ClassFileVisitor visitor )
throws IOException
{
try ( JarInputStream in = new JarInputStream( url.openStream() ) )
{
JarEntry entry;
while ( ( entry = in.getNextJarEntry() ) != null )
{
String name = entry.getName();
// ignore files like package-info.class and module-info.class
if ( name.endsWith( ".class" ) && name.indexOf( '-' ) == -1 )
{
visitClass( name, in, visitor );
}
}
}
}
private static void acceptDirectory( File directory, ClassFileVisitor visitor )
throws IOException
{
if ( !directory.isDirectory() )
{
throw new IllegalArgumentException( "File is not a directory" );
}
DirectoryScanner scanner = new DirectoryScanner();
scanner.setBasedir( directory );
scanner.setIncludes( new String[] { "**/*.class" } );
scanner.scan();
String[] paths = scanner.getIncludedFiles();
for ( String path : paths )
{
path = path.replace( File.separatorChar, '/' );
File file = new File( directory, path );
try ( InputStream in = new FileInputStream( file ) )
{
visitClass( path, in, visitor );
}
}
}
private static void visitClass( String path, InputStream in, ClassFileVisitor visitor )
{
if ( !path.endsWith( ".class" ) )
{
throw new IllegalArgumentException( "Path is not a class" );
}
String className = path.substring( 0, path.length() - 6 );
className = className.replace( '/', '.' );
visitor.visitClass( className, in );
}
}