Skip to content

Commit 251f954

Browse files
committed
[MPMD-320] Correctly decode filename paths in classpath for toolchain
1 parent 38701da commit 251f954

File tree

2 files changed

+62
-5
lines changed

2 files changed

+62
-5
lines changed

src/main/java/org/apache/maven/plugins/pmd/exec/Executor.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@
2525
import java.io.IOException;
2626
import java.io.InputStream;
2727
import java.io.OutputStream;
28+
import java.io.UnsupportedEncodingException;
2829
import java.net.URL;
2930
import java.net.URLClassLoader;
31+
import java.net.URLDecoder;
32+
import java.nio.charset.StandardCharsets;
3033
import java.util.logging.Handler;
3134
import java.util.logging.Level;
3235
import java.util.logging.SimpleFormatter;
@@ -125,17 +128,23 @@ protected static String buildClasspath()
125128
return classpath.toString();
126129
}
127130

128-
private static void buildClasspath( StringBuilder classpath, ClassLoader cl )
131+
static void buildClasspath( StringBuilder classpath, ClassLoader cl )
129132
{
130133
if ( cl instanceof URLClassLoader )
131134
{
132135
for ( URL url : ( (URLClassLoader) cl ).getURLs() )
133136
{
134-
String urlString = url.toString();
135-
if ( urlString.startsWith( "file:" ) )
137+
if ( "file".equalsIgnoreCase( url.getProtocol() ) )
136138
{
137-
String f = urlString.substring( 5 ); // strip "file:"
138-
classpath.append( f ).append( File.pathSeparatorChar );
139+
try
140+
{
141+
String filename = URLDecoder.decode( url.getPath(), StandardCharsets.UTF_8.name() );
142+
classpath.append( new File( filename ).getPath() ).append( File.pathSeparatorChar );
143+
}
144+
catch ( UnsupportedEncodingException e )
145+
{
146+
LOG.warn( "Ignoring " + url + " in classpath due to UnsupportedEncodingException", e );
147+
}
139148
}
140149
}
141150
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.apache.maven.plugins.pmd.exec;
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.net.MalformedURLException;
24+
import java.net.URL;
25+
import java.net.URLClassLoader;
26+
27+
import org.apache.commons.lang3.SystemUtils;
28+
import org.junit.Assert;
29+
30+
import junit.framework.TestCase;
31+
32+
public class ExecutorTest extends TestCase
33+
{
34+
public void testBuildClasspath() throws MalformedURLException {
35+
String basename = "home/test/dir with space/mylib.jar";
36+
String pathname = new File("/", basename).getPath();
37+
if ( SystemUtils.IS_OS_WINDOWS )
38+
{
39+
pathname = new File( File.listRoots()[0], basename ).getPath();
40+
}
41+
URL[] urls = new URL[] { new File(pathname).toURI().toURL() };
42+
URLClassLoader mockedClassLoader = new URLClassLoader( urls );
43+
44+
StringBuilder classpath = new StringBuilder();
45+
Executor.buildClasspath(classpath, mockedClassLoader);
46+
Assert.assertEquals( pathname + File.pathSeparator, classpath.toString() );
47+
}
48+
}

0 commit comments

Comments
 (0)