Coverage Report - org.apache.maven.plugins.shade.relocation.SimpleRelocator
 
Classes in this File Line Coverage Branch Coverage Complexity
SimpleRelocator
92 %
52/56
78 %
36/46
4,111
 
 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  17
         this( patt, shadedPattern, includes, excludes, false );
 55  17
     }
 56  
 
 57  
     public SimpleRelocator( String patt, String shadedPattern, List<String> includes, List<String> excludes,
 58  
                             boolean rawString )
 59  21
     {
 60  21
         this.rawString = rawString;
 61  
 
 62  21
         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  17
             if ( patt == null )
 73  
             {
 74  1
                 this.pattern = "";
 75  1
                 this.pathPattern = "";
 76  
             }
 77  
             else
 78  
             {
 79  16
                 this.pattern = patt.replace( '/', '.' );
 80  16
                 this.pathPattern = patt.replace( '.', '/' );
 81  
             }
 82  
 
 83  17
             if ( shadedPattern != null )
 84  
             {
 85  7
                 this.shadedPattern = shadedPattern.replace( '/', '.' );
 86  7
                 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  21
         this.includes = normalizePatterns( includes );
 96  21
         this.excludes = normalizePatterns( excludes );
 97  21
     }
 98  
 
 99  
     private static Set<String> normalizePatterns( Collection<String> patterns )
 100  
     {
 101  42
         Set<String> normalized = null;
 102  
 
 103  42
         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  42
         return normalized;
 123  
     }
 124  
 
 125  
     private boolean isIncluded( String path )
 126  
     {
 127  66519
         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  66519
         return true;
 139  
     }
 140  
 
 141  
     private boolean isExcluded( String path )
 142  
     {
 143  66519
         if ( excludes != null && !excludes.isEmpty() )
 144  
         {
 145  44328
             for ( String exclude : excludes )
 146  
             {
 147  127033
                 if ( SelectorUtils.matchPath( exclude, path, true ) )
 148  
                 {
 149  5516
                     return true;
 150  
                 }
 151  
             }
 152  
         }
 153  61003
         return false;
 154  
     }
 155  
 
 156  
     public boolean canRelocatePath( String path )
 157  
     {
 158  66521
         if ( rawString )
 159  
         {
 160  2
             return Pattern.compile( pathPattern ).matcher( path ).find();
 161  
         }
 162  
 
 163  66519
         if ( path.endsWith( ".class" ) )
 164  
         {
 165  264
             path = path.substring( 0, path.length() - 6 );
 166  
         }
 167  
 
 168  66519
         if ( !isIncluded( path ) || isExcluded( path ) )
 169  
         {
 170  5516
             return false;
 171  
         }
 172  
 
 173  61003
         return path.startsWith( pathPattern );
 174  
     }
 175  
 
 176  
     public boolean canRelocateClass( String clazz )
 177  
     {
 178  2886
         return !rawString && clazz.indexOf( '/' ) < 0 && canRelocatePath( clazz.replace( '.', '/' ) );
 179  
     }
 180  
 
 181  
     public String relocatePath( String path )
 182  
     {
 183  13669
         if ( rawString )
 184  
         {
 185  2
             return path.replaceAll( pathPattern, shadedPathPattern );
 186  
         }
 187  
         else
 188  
         {
 189  13667
             return path.replaceFirst( pathPattern, shadedPathPattern );
 190  
         }
 191  
     }
 192  
 
 193  
     public String relocateClass( String clazz )
 194  
     {
 195  3
         return clazz.replaceFirst( pattern, shadedPattern );
 196  
     }
 197  
 }