Coverage Report - org.apache.maven.plugins.shade.relocation.SimpleRelocator
 
Classes in this File Line Coverage Branch Coverage Complexity
SimpleRelocator
87%
40/46
75%
27/36
3
 
 1  
 package org.apache.maven.plugins.shade.relocation;
 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.util.Collection;
 23  
 import java.util.Iterator;
 24  
 import java.util.LinkedHashSet;
 25  
 import java.util.List;
 26  
 import java.util.Set;
 27  
 
 28  
 import org.codehaus.plexus.util.SelectorUtils;
 29  
 
 30  
 /**
 31  
  * @author Jason van Zyl
 32  
  * @author Mauro Talevi
 33  
  */
 34  
 public class SimpleRelocator
 35  
     implements Relocator
 36  
 {
 37  
 
 38  
     private final String pattern;
 39  
 
 40  
     private final String pathPattern;
 41  
 
 42  
     private final String shadedPattern;
 43  
 
 44  
     private final String shadedPathPattern;
 45  
 
 46  
     private final Set includes;
 47  
 
 48  
     private final Set excludes;
 49  
 
 50  
     public SimpleRelocator( String patt, String shadedPattern, List includes, List excludes )
 51  15
     {
 52  15
         this.pattern = patt.replace( '/', '.' );
 53  15
         this.pathPattern = patt.replace( '.', '/' );
 54  
 
 55  15
         if ( shadedPattern != null )
 56  
         {
 57  6
             this.shadedPattern = shadedPattern.replace( '/', '.' );
 58  6
             this.shadedPathPattern = shadedPattern.replace( '.', '/' );
 59  
         }
 60  
         else
 61  
         {
 62  9
             this.shadedPattern = "hidden." + this.pattern;
 63  9
             this.shadedPathPattern = "hidden/" + this.pathPattern;
 64  
         }
 65  
 
 66  15
         this.includes = normalizePatterns( includes );
 67  15
         this.excludes = normalizePatterns( excludes );
 68  15
     }
 69  
 
 70  
     private static Set normalizePatterns( Collection patterns )
 71  
     {
 72  30
         Set normalized = null;
 73  
 
 74  30
         if ( patterns != null && !patterns.isEmpty() )
 75  
         {
 76  7
             normalized = new LinkedHashSet();
 77  
 
 78  7
             for ( Iterator i = patterns.iterator(); i.hasNext(); )
 79  
             {
 80  16
                 String pattern = (String) i.next();
 81  
 
 82  16
                 String classPattern = pattern.replace( '.', '/' );
 83  
 
 84  16
                 normalized.add( classPattern );
 85  
 
 86  16
                 if ( classPattern.endsWith( "/*" ) )
 87  
                 {
 88  7
                     String packagePattern = classPattern.substring( 0, classPattern.lastIndexOf( '/' ) );
 89  7
                     normalized.add( packagePattern );
 90  
                 }
 91  16
             }
 92  
         }
 93  
 
 94  30
         return normalized;
 95  
     }
 96  
 
 97  
     private boolean isIncluded( String path )
 98  
     {
 99  110767
         if ( includes != null && !includes.isEmpty() )
 100  
         {
 101  0
             for ( Iterator i = includes.iterator(); i.hasNext(); )
 102  
             {
 103  0
                 String include = (String) i.next();
 104  
 
 105  0
                 if ( SelectorUtils.matchPath( include, path, true ) )
 106  
                 {
 107  0
                     return true;
 108  
                 }
 109  0
             }
 110  0
             return false;
 111  
         }
 112  110767
         return true;
 113  
     }
 114  
 
 115  
     private boolean isExcluded( String path )
 116  
     {
 117  110767
         if ( excludes != null && !excludes.isEmpty() )
 118  
         {
 119  88576
             for ( Iterator i = excludes.iterator(); i.hasNext(); )
 120  
             {
 121  253819
                 String exclude = (String) i.next();
 122  
 
 123  253819
                 if ( SelectorUtils.matchPath( exclude, path, true ) )
 124  
                 {
 125  11016
                     return true;
 126  
                 }
 127  242803
             }
 128  
         }
 129  99751
         return false;
 130  
     }
 131  
 
 132  
     public boolean canRelocatePath( String path )
 133  
     {
 134  110767
         if ( path.endsWith( ".class" ) )
 135  
         {
 136  432
             path = path.substring( 0, path.length() - 6 );
 137  
         }
 138  
 
 139  110767
         if ( !isIncluded( path ) || isExcluded( path ) )
 140  
         {
 141  11016
             return false;
 142  
         }
 143  
 
 144  99751
         return path.startsWith( pathPattern );
 145  
     }
 146  
 
 147  
     public boolean canRelocateClass( String clazz )
 148  
     {
 149  4796
         return clazz.indexOf( '/' ) < 0 && canRelocatePath( clazz.replace( '.', '/' ) );
 150  
     }
 151  
 
 152  
     public String relocatePath( String path )
 153  
     {
 154  20937
         return path.replaceFirst( pathPattern, shadedPathPattern );
 155  
     }
 156  
 
 157  
     public String relocateClass( String clazz )
 158  
     {
 159  3
         return clazz.replaceFirst( pattern, shadedPattern );
 160  
     }
 161  
 }