Coverage Report - org.apache.maven.shared.dependency.analyzer.ClassFileVisitorUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ClassFileVisitorUtils
82%
39/47
88%
16/18
4
 
 1  
 package org.apache.maven.shared.dependency.analyzer;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *  http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import org.codehaus.plexus.util.DirectoryScanner;
 23  
 
 24  
 import java.io.File;
 25  
 import java.io.FileInputStream;
 26  
 import java.io.IOException;
 27  
 import java.io.InputStream;
 28  
 import java.net.URI;
 29  
 import java.net.URISyntaxException;
 30  
 import java.net.URL;
 31  
 import java.util.jar.JarEntry;
 32  
 import java.util.jar.JarInputStream;
 33  
 
 34  
 
 35  
 /**
 36  
  * Utility to visit classes in a library given either as a jar file or an exploded directory.
 37  
  * 
 38  
  * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
 39  
  * @version $Id: ClassFileVisitorUtils.java 1400598 2012-10-21 08:50:43Z hboutemy $
 40  
  */
 41  
 public final class ClassFileVisitorUtils
 42  
 {
 43  
     // constants --------------------------------------------------------------
 44  
 
 45  2
     private static final String[] CLASS_INCLUDES = { "**/*.class" };
 46  
 
 47  
     // constructors -----------------------------------------------------------
 48  
 
 49  
     private ClassFileVisitorUtils()
 50  0
     {
 51  
         // private constructor for utility class
 52  0
     }
 53  
 
 54  
     // public methods ---------------------------------------------------------
 55  
 
 56  
     public static void accept( URL url, ClassFileVisitor visitor )
 57  
         throws IOException
 58  
     {
 59  46
         if ( url.getPath().endsWith( ".jar" ) )
 60  
         {
 61  18
             acceptJar( url, visitor );
 62  
         }
 63  28
         else if ( url.getProtocol().equalsIgnoreCase( "file" ) )
 64  
         {
 65  
             try
 66  
             {
 67  26
                 File file = new File( new URI( url.toString() ) );
 68  
 
 69  26
                 if ( file.isDirectory() )
 70  
                 {
 71  12
                     acceptDirectory( file, visitor );
 72  
                 }
 73  14
                 else if ( file.exists() )
 74  
                 {
 75  2
                     throw new IllegalArgumentException( "Cannot accept visitor on URL: " + url );
 76  
                 }
 77  
             }
 78  0
             catch ( URISyntaxException exception )
 79  
             {
 80  0
                 IllegalArgumentException e = new IllegalArgumentException( "Cannot accept visitor on URL: " + url );
 81  0
                 e.initCause( exception );
 82  0
                 throw e;
 83  24
             }
 84  
         }
 85  
         else
 86  
         {
 87  2
             throw new IllegalArgumentException( "Cannot accept visitor on URL: " + url );
 88  
         }
 89  40
     }
 90  
 
 91  
     // private methods --------------------------------------------------------
 92  
 
 93  
     private static void acceptJar( URL url, ClassFileVisitor visitor )
 94  
         throws IOException
 95  
     {
 96  18
         JarInputStream in = new JarInputStream( url.openStream() );
 97  
 
 98  18
         JarEntry entry = null;
 99  
 
 100  1138
         while ( ( entry = in.getNextJarEntry() ) != null )
 101  
         {
 102  1120
             String name = entry.getName();
 103  
 
 104  1120
             if ( name.endsWith( ".class" ) )
 105  
             {
 106  962
                 visitClass( name, in, visitor );
 107  
             }
 108  1120
         }
 109  
 
 110  16
         in.close();
 111  16
     }
 112  
 
 113  
     private static void acceptDirectory( File directory, ClassFileVisitor visitor )
 114  
         throws IOException
 115  
     {
 116  12
         if ( !directory.isDirectory() )
 117  
         {
 118  0
             throw new IllegalArgumentException( "File is not a directory" );
 119  
         }
 120  
 
 121  12
         DirectoryScanner scanner = new DirectoryScanner();
 122  
 
 123  12
         scanner.setBasedir( directory );
 124  12
         scanner.setIncludes( CLASS_INCLUDES );
 125  
 
 126  12
         scanner.scan();
 127  
 
 128  12
         String[] paths = scanner.getIncludedFiles();
 129  
 
 130  24
         for ( String path : paths )
 131  
         {
 132  12
             path = path.replace( File.separatorChar, '/' );
 133  
 
 134  12
             File file = new File( directory, path );
 135  12
             FileInputStream in = new FileInputStream( file );
 136  
 
 137  12
             visitClass( path, in, visitor );
 138  
 
 139  12
             in.close();
 140  
         }
 141  12
     }
 142  
 
 143  
     private static void visitClass( String path, InputStream in, ClassFileVisitor visitor )
 144  
     {
 145  974
         if ( !path.endsWith( ".class" ) )
 146  
         {
 147  0
             throw new IllegalArgumentException( "Path is not a class" );
 148  
         }
 149  
 
 150  974
         String className = path.substring( 0, path.length() - 6 );
 151  
 
 152  974
         className = className.replace( '/', '.' );
 153  
 
 154  974
         visitor.visitClass( className, in );
 155  974
     }
 156  
 }