Coverage Report - org.apache.maven.plugins.shade.relocation.SimpleRelocator
 
Classes in this File Line Coverage Branch Coverage Complexity
SimpleRelocator
88%
52/59
75%
39/52
4.1
 
 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 org.codehaus.plexus.util.SelectorUtils;
 23  
 
 24  
 import java.util.Collection;
 25  
 import java.util.LinkedHashSet;
 26  
 import java.util.List;
 27  
 import java.util.Set;
 28  
 import java.util.regex.Pattern;
 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<String> includes;
 47  
 
 48  
     private final Set<String> excludes;
 49  
 
 50  
     private final boolean rawString;
 51  
 
 52  
     public SimpleRelocator( String patt, String shadedPattern, List<String> includes, List<String> excludes )
 53  
     {
 54  18
         this( patt, shadedPattern, includes, excludes, false );
 55  18
     }
 56  
 
 57  
     public SimpleRelocator( String patt, String shadedPattern, List<String> includes, List<String> excludes,
 58  
                             boolean rawString )
 59  22
     {
 60  22
         this.rawString = rawString;
 61  
 
 62  22
         if ( rawString )
 63  
         {
 64  4
             this.pathPattern = patt;
 65  4
             this.shadedPathPattern = shadedPattern;
 66  
 
 67  4
             this.pattern = null; // not used for raw string relocator
 68  4
             this.shadedPattern = null; // not used for raw string relocator
 69  
         }
 70  
         else
 71  
         {
 72  18
             if ( patt == null )
 73  
             {
 74  1
                 this.pattern = "";
 75  1
                 this.pathPattern = "";
 76  
             }
 77  
             else
 78  
             {
 79  17
                 this.pattern = patt.replace( '/', '.' );
 80  17
                 this.pathPattern = patt.replace( '.', '/' );
 81  
             }
 82  
 
 83  18
             if ( shadedPattern != null )
 84  
             {
 85  8
                 this.shadedPattern = shadedPattern.replace( '/', '.' );
 86  8
                 this.shadedPathPattern = shadedPattern.replace( '.', '/' );
 87  
             }
 88  
             else
 89  
             {
 90  10
                 this.shadedPattern = "hidden." + this.pattern;
 91  10
                 this.shadedPathPattern = "hidden/" + this.pathPattern;
 92  
             }
 93  
         }
 94  
 
 95  22
         this.includes = normalizePatterns( includes );
 96  22
         this.excludes = normalizePatterns( excludes );
 97  22
     }
 98  
 
 99  
     private static Set<String> normalizePatterns( Collection<String> patterns )
 100  
     {
 101  44
         Set<String> normalized = null;
 102  
 
 103  44
         if ( patterns != null && !patterns.isEmpty() )
 104  
         {
 105  7
             normalized = new LinkedHashSet<String>();
 106  
 
 107  7
             for ( String pattern : patterns )
 108  
             {
 109  
 
 110  16
                 String classPattern = pattern.replace( '.', '/' );
 111  
 
 112  16
                 normalized.add( classPattern );
 113  
 
 114  16
                 if ( classPattern.endsWith( "/*" ) )
 115  
                 {
 116  7
                     String packagePattern = classPattern.substring( 0, classPattern.lastIndexOf( '/' ) );
 117  7
                     normalized.add( packagePattern );
 118  
                 }
 119  16
             }
 120  
         }
 121  
 
 122  44
         return normalized;
 123  
     }
 124  
 
 125  
     private boolean isIncluded( String path )
 126  
     {
 127  110767
         if ( includes != null && !includes.isEmpty() )
 128  
         {
 129  0
             for ( String include : includes )
 130  
             {
 131  0
                 if ( SelectorUtils.matchPath( include, path, true ) )
 132  
                 {
 133  0
                     return true;
 134  
                 }
 135  
             }
 136  0
             return false;
 137  
         }
 138  110767
         return true;
 139  
     }
 140  
 
 141  
     private boolean isExcluded( String path )
 142  
     {
 143  110767
         if ( excludes != null && !excludes.isEmpty() )
 144  
         {
 145  88576
             for ( String exclude : excludes )
 146  
             {
 147  253819
                 if ( SelectorUtils.matchPath( exclude, path, true ) )
 148  
                 {
 149  11016
                     return true;
 150  
                 }
 151  
             }
 152  
         }
 153  99751
         return false;
 154  
     }
 155  
 
 156  
     public boolean canRelocatePath( String path )
 157  
     {
 158  110769
         if ( rawString )
 159  
         {
 160  2
             return Pattern.compile( pathPattern ).matcher( path ).find();
 161  
         }
 162  
 
 163  110767
         if ( path.endsWith( ".class" ) )
 164  
         {
 165  432
             path = path.substring( 0, path.length() - 6 );
 166  
         }
 167  
 
 168  110767
         if ( !isIncluded( path ) || isExcluded( path ) )
 169  
         {
 170  11016
             return false;
 171  
         }
 172  
 
 173  
         // Allow for annoying option of an extra / on the front of a path. See MSHADE-119; comes from getClass().getResource("/a/b/c.properties").
 174  99751
         return path.startsWith( pathPattern ) || path.startsWith ( "/" + pathPattern );
 175  
     }
 176  
 
 177  
     public boolean canRelocateClass( String clazz )
 178  
     {
 179  4796
         return !rawString && clazz.indexOf( '/' ) < 0 && canRelocatePath( clazz.replace( '.', '/' ) );
 180  
     }
 181  
 
 182  
     public String relocatePath( String path )
 183  
     {
 184  20940
         if ( rawString )
 185  
         {
 186  2
             return path.replaceAll( pathPattern, shadedPathPattern );
 187  
         }
 188  
         else
 189  
         {
 190  20938
             return path.replaceFirst( pathPattern, shadedPathPattern );
 191  
         }
 192  
     }
 193  
 
 194  
     public String relocateClass( String clazz )
 195  
     {
 196  3
         return clazz.replaceFirst( pattern, shadedPattern );
 197  
     }
 198  
 
 199  
         public String applyToSourceContent(String sourceContent)
 200  
         {
 201  0
         if ( rawString )
 202  
         {
 203  0
                 return sourceContent;
 204  
         }
 205  
         else
 206  
         {
 207  0
             return sourceContent.replaceAll( "\\b" + pattern, shadedPattern );
 208  
         }
 209  
         }
 210  
 }