Coverage Report - org.apache.maven.plugins.shade.filter.MinijarFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
MinijarFilter
0%
0/43
0%
0/10
2
 
 1  
 package org.apache.maven.plugins.shade.filter;
 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 java.io.File;
 23  
 import java.io.FileInputStream;
 24  
 import java.io.IOException;
 25  
 import java.io.InputStream;
 26  
 import java.util.HashSet;
 27  
 import java.util.Iterator;
 28  
 import java.util.Set;
 29  
 
 30  
 import org.apache.maven.artifact.Artifact;
 31  
 import org.apache.maven.plugin.logging.Log;
 32  
 import org.apache.maven.project.MavenProject;
 33  
 import org.codehaus.plexus.util.IOUtil;
 34  
 import org.vafer.jdependency.Clazz;
 35  
 import org.vafer.jdependency.Clazzpath;
 36  
 import org.vafer.jdependency.ClazzpathUnit;
 37  
 
 38  
 /**
 39  
  * A filter that prevents the inclusion of classes not required in the final jar.
 40  
  * 
 41  
  * @author Torsten Curdt
 42  
  */
 43  
 public class MinijarFilter
 44  
     implements Filter
 45  
 {
 46  
 
 47  
     private Log log;
 48  
 
 49  
     private Set removable;
 50  
 
 51  
     private int classes_kept;
 52  
 
 53  
     private int classes_removed;
 54  
 
 55  
     public MinijarFilter( MavenProject project, Log log )
 56  
         throws IOException
 57  0
     {
 58  
 
 59  0
         this.log = log;
 60  
 
 61  0
         Clazzpath cp = new Clazzpath();
 62  
 
 63  0
         ClazzpathUnit artifactUnit =
 64  
             cp.addClazzpathUnit( new FileInputStream( project.getArtifact().getFile() ), project.toString() );
 65  
 
 66  0
         for ( Iterator it = project.getArtifacts().iterator(); it.hasNext(); )
 67  
         {
 68  0
             Artifact dependency = (Artifact) it.next();
 69  
 
 70  0
             InputStream is = null;
 71  
             try
 72  
             {
 73  0
                 is = new FileInputStream( dependency.getFile() );
 74  0
                 cp.addClazzpathUnit( is, dependency.toString() );
 75  
             }
 76  
             finally
 77  
             {
 78  0
                 IOUtil.close( is );
 79  0
             }
 80  0
         }
 81  
 
 82  0
         removable = cp.getClazzes();
 83  0
         removePackages(artifactUnit);
 84  0
         removable.removeAll( artifactUnit.getClazzes() );
 85  0
         removable.removeAll( artifactUnit.getTransitiveDependencies() );
 86  0
     }
 87  
 
 88  
     private void removePackages(ClazzpathUnit artifactUnit)
 89  
     {
 90  0
         Set packageNames = new HashSet();
 91  0
         removePackages(artifactUnit.getClazzes(), packageNames);
 92  0
         removePackages(artifactUnit.getTransitiveDependencies(), packageNames);
 93  0
     }
 94  
 
 95  
     private void removePackages(Set clazzes, Set packageNames)
 96  
     {
 97  0
         Iterator it = clazzes.iterator();
 98  0
         while(it.hasNext())
 99  
         {
 100  0
             Clazz clazz = (Clazz) it.next();
 101  0
             String name = clazz.getName();
 102  0
             while(name.contains("."))
 103  
             {
 104  0
                 name = name.substring(0, name.lastIndexOf('.'));
 105  0
                 if(packageNames.add(name))
 106  
                 {
 107  0
                     removable.remove(new Clazz(name + ".package-info"));
 108  
                 }
 109  
             }
 110  0
         }
 111  0
     }
 112  
 
 113  
     public boolean canFilter( File jar )
 114  
     {
 115  0
         return true;
 116  
     }
 117  
 
 118  
     public boolean isFiltered( String classFile )
 119  
     {
 120  0
         String className = classFile.replace( '/', '.' ).replaceFirst( "\\.class$", "" );
 121  0
         Clazz clazz = new Clazz( className );
 122  
 
 123  0
         if ( removable.contains( clazz ) )
 124  
         {
 125  0
             log.debug( "Removing " + className );
 126  0
             classes_removed += 1;
 127  0
             return true;
 128  
         }
 129  
 
 130  0
         classes_kept += 1;
 131  0
         return false;
 132  
     }
 133  
 
 134  
     public void finished()
 135  
     {
 136  0
         int classes_total = classes_removed + classes_kept;
 137  0
         log.info( "Minimized " + classes_total + " -> " + classes_kept + " ("
 138  
             + (int) ( 100 * classes_kept / classes_total ) + "%)" );
 139  0
     }
 140  
 }