// Copyright (c) 2003-present, Jodd Team (http://jodd.org)
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

package jodd.core;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

/**
 * Methods that requires different implementations on various Java Platforms.
 */
public class JavaBridge {

	private static final String RESOURCE = JavaBridge.class.getName().replace('.', '/') + ".class";

	/**
	 * Returns urls for the classloader
	 *
	 * @param classLoader classloader in which to find urls
	 * @return list of urls or {@code null} if not found
	 */
	public static URL[] getURLs(final ClassLoader classLoader) {
		if (classLoader instanceof URLClassLoader) {
			URLClassLoader urlClassLoader = (URLClassLoader) classLoader;
			return urlClassLoader.getURLs();
		}

		URL url = currentModuleURL();

		if (url == null) {
			return new URL[0];
		}

		return new URL[] {url};
	}

	private static URL currentModuleURL() {
		URL url = JavaBridge.class.getClassLoader().getResource(RESOURCE);

		if (url == null) {
			return null;
		}

		// use root
		String urlString = url.toString();
		int ndx = urlString.indexOf(RESOURCE);
		urlString = urlString.substring(0, ndx) + urlString.substring(ndx + RESOURCE.length());

		try {
			return new URL(urlString);
		} catch (MalformedURLException ignore) {
			return null;
		}
	}

}
@@ -1487,16 +1487,70 @@
 sLoader;
+%0Aimport java.util.LinkedHashSet;%0Aimport java.util.Set;
 %0A%0A/**%0A *
@@ -1658,30 +1658,29 @@
 ge %7B%0A%0A%09p
-rivate
+ublic
  static 
 final St
@@ -1675,88 +1675,88 @@
 tic 
-final String RESOURCE = JavaBridge.class
+URL%5B%5D getURLs(Class clazz) %7B%0A%09%09return getURLs(clazz
 .get
-Name().replace('.', '/') + %22.class%22;
+ClassLoader(), clazz);%0A%09%7D
 %0A%0A%09/
@@ -1977,16 +1977,226 @@
 ader) %7B%0A
+%09%09return getURLs(classLoader, JavaBridge.class);%0A%09%7D%0A%0A%09private static URL%5B%5D getURLs(ClassLoader classLoader, final Class clazz) %7B%0A%09%09final Set%3CURL%3E urls = new LinkedHashSet%3C%3E();%0A%0A%09%09while (classLoader != null) %7B%0A%09
 %09%09if (cl
@@ -2234,16 +2234,17 @@
 ader) %7B%0A
+%09
 %09%09%09URLCl
@@ -2300,16 +2300,17 @@
 Loader;%0A
+%09
 %09%09%09retur
@@ -2339,19 +2339,21 @@
 Ls();%0A%09%09
+%09
 %7D%0A%0A
+%09
 %09%09URL ur
@@ -2361,38 +2361,55 @@
  = c
-urrent
+lass
 ModuleU
-RL(
+rl(classLoader, clazz
 );%0A%0A%09%09
+%09
 if (url 
 == n
@@ -2396,33 +2396,33 @@
 z);%0A%0A%09%09%09if (url 
-=
+!
 = null) %7B%0A%09%09%09ret
@@ -2422,56 +2422,119 @@
 %0A%09%09%09
-return new URL%5B0%5D;%0A%09%09%7D%0A%0A%09%09return new URL%5B%5D %7Burl%7D
+%09urls.add(url);%0A%09%09%09%7D%0A%09%09%09classLoader = classLoader.getParent();%0A%09%09%7D%0A%0A%09%09return urls.toArray(new URL%5Burls.size()%5D)
 ;%0A%09%7D
@@ -2560,61 +2560,187 @@
 RL c
-urrent
+lass
 ModuleU
-RL() %7B%0A%09%09URL url = JavaBridge.class.getC
+rl(ClassLoader classLoader, Class clazz) %7B%0A%09%09if (clazz == null) %7B%0A%09%09%09return null;%0A%09%09%7D%0A%09%09final String name = clazz.getName().replace('.', '/') + %22.class%22;%0A%0A%09%09URL url = c
 lass
@@ -2745,18 +2745,16 @@
 ssLoader
-()
 .getReso
@@ -2758,24 +2758,20 @@
 esource(
-RESOURCE
+name
 );%0A%0A%09%09if
@@ -2889,24 +2889,20 @@
 indexOf(
-RESOURCE
+name
 );%0A%09%09url
@@ -2970,16 +2970,12 @@
 x + 
-RESOURCE
+name
 .len

Update JavaBridge API for Java8.