Coverage Report - org.apache.maven.plugins.shade.relocation.SimpleRelocator
 
Classes in this File Line Coverage Branch Coverage Complexity
SimpleRelocator
100%
31/31
100%
22/22
2,8
 
 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.Iterator;
 23  
 import java.util.LinkedHashSet;
 24  
 import java.util.List;
 25  
 import java.util.Set;
 26  
 
 27  
 import org.codehaus.plexus.util.SelectorUtils;
 28  
 
 29  
 /**
 30  
  * @author Jason van Zyl
 31  
  * @author Mauro Talevi
 32  
  */
 33  
 public class SimpleRelocator
 34  
     implements Relocator
 35  
 {
 36  
     private String pattern;
 37  
 
 38  
     private String pathPattern;
 39  
 
 40  
     private String shadedPattern;
 41  
 
 42  
     private String shadedPathPattern;
 43  
 
 44  
     private Set excludes;
 45  
 
 46  
     public SimpleRelocator( String patt, String shadedPattern, List excludes )
 47  15
     {
 48  15
         this.pattern = patt.replace( '/', '.' );
 49  15
         this.pathPattern = patt.replace( '.', '/' );
 50  
 
 51  15
         if ( shadedPattern != null )
 52  
         {
 53  6
             this.shadedPattern = shadedPattern.replace( '/', '.' );
 54  6
             this.shadedPathPattern = shadedPattern.replace( '.', '/' );
 55  
         }
 56  
         else
 57  
         {
 58  9
             this.shadedPattern = "hidden." + this.pattern;
 59  9
             this.shadedPathPattern = "hidden/" + this.pathPattern;
 60  
         }
 61  
 
 62  15
         if ( excludes != null && !excludes.isEmpty() )
 63  
         {
 64  7
             this.excludes = new LinkedHashSet();
 65  
 
 66  7
             for ( Iterator i = excludes.iterator(); i.hasNext(); )
 67  
             {
 68  16
                 String e = (String) i.next();
 69  
 
 70  16
                 String classExclude = e.replace( '.', '/' );
 71  16
                 this.excludes.add( classExclude );
 72  
 
 73  16
                 if ( classExclude.endsWith( "/*" ) )
 74  
                 {
 75  7
                     String packageExclude = classExclude.substring( 0, classExclude.lastIndexOf( '/' ) );
 76  7
                     this.excludes.add( packageExclude );
 77  
                 }
 78  16
             }
 79  
         }
 80  15
     }
 81  
 
 82  
     public boolean canRelocatePath( String path )
 83  
     {
 84  110767
         if ( path.endsWith( ".class" ) )
 85  
         {
 86  432
             path = path.substring( 0, path.length() - 6 );
 87  
         }
 88  110767
         if ( excludes != null )
 89  
         {
 90  88576
             for ( Iterator i = excludes.iterator(); i.hasNext(); )
 91  
             {
 92  253819
                 String exclude = (String) i.next();
 93  
 
 94  253819
                 if ( SelectorUtils.matchPath( exclude, path, true ) )
 95  
                 {
 96  11016
                     return false;
 97  
                 }
 98  242803
             }
 99  
         }
 100  
 
 101  99751
         return path.startsWith( pathPattern );
 102  
     }
 103  
 
 104  
     public boolean canRelocateClass( String clazz )
 105  
     {
 106  4796
         return clazz.indexOf( '/' ) < 0 && canRelocatePath( clazz.replace( '.', '/' ) );
 107  
     }
 108  
 
 109  
     public String relocatePath( String path )
 110  
     {
 111  20937
         return path.replaceFirst( pathPattern, shadedPathPattern );
 112  
     }
 113  
 
 114  
     public String relocateClass( String clazz )
 115  
     {
 116  3
         return clazz.replaceFirst( pattern, shadedPattern );
 117  
     }
 118  
 }