Author: markh Date: Mon Mar 3 02:32:00 2008 New Revision: 633030 URL: http://svn.apache.org/viewvc?rev=633030&view=rev Log: Moved class utility methods to ClassUtils
Added: maven/sandbox/trunk/shared/maven-runtime/src/main/java/org/apache/maven/shared/runtime/ClassUtils.java (with props) Modified: maven/sandbox/trunk/shared/maven-runtime/src/main/java/org/apache/maven/shared/runtime/MavenRuntimeVisitorUtils.java Added: maven/sandbox/trunk/shared/maven-runtime/src/main/java/org/apache/maven/shared/runtime/ClassUtils.java URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/shared/maven-runtime/src/main/java/org/apache/maven/shared/runtime/ClassUtils.java?rev=633030&view=auto ============================================================================== --- maven/sandbox/trunk/shared/maven-runtime/src/main/java/org/apache/maven/shared/runtime/ClassUtils.java (added) +++ maven/sandbox/trunk/shared/maven-runtime/src/main/java/org/apache/maven/shared/runtime/ClassUtils.java Mon Mar 3 02:32:00 2008 @@ -0,0 +1,82 @@ +/* + * $HeadURL$ + * + * (c) 2008 IIZUKA Software Technologies Ltd. All rights reserved. + */ +package org.apache.maven.shared.runtime; + +import java.net.MalformedURLException; +import java.net.URL; + +import org.codehaus.plexus.util.StringUtils; + +/** + * Provides various utility methods for working with classes. + * + * @author Mark Hobson + * @version $Id$ + */ +final class ClassUtils +{ + // constructors ----------------------------------------------------------- + + private ClassUtils() + { + // private constructor for utility class + } + + // public methods --------------------------------------------------------- + + /** + * Gets a URL to the specified class's default package. For example, if the class <code>foo.Bar</code> is + * supplied, then a URL to the directory above <code>foo</code> is returned. If the class's default package + * resides at the root of a Jar, then a URL to the Jar file itself is returned. + * + * @param klass + * the class to obtain the base URL for + * @return a URL to the class's default package, or a URL to the owning Jar file if the default package resides at + * the root of a Jar + * @throws MalformedURLException + * if the base URL cannot be determined + */ + public static URL getBaseURL( Class klass ) + throws MalformedURLException + { + URL url = getURL( klass ); + + String className = klass.getName(); + + int n = StringUtils.countMatches( className, "." ); + String relativePath = StringUtils.repeat( "../", n ); + + URL baseURL = new URL( url, relativePath ); + + // unwrap Jar URL if at the root + if ( "jar".equals( baseURL.getProtocol() ) && baseURL.getPath().endsWith( "!/" ) ) + { + String basePath = baseURL.getPath(); + + basePath = basePath.substring( 0, basePath.length() - "!/".length() ); + + baseURL = new URL( basePath ); + } + + return baseURL; + } + + /** + * Gets a URL to the specified class. + * + * @param klass + * the class to obtain the URL for + * @return a URL to the class, or <code>null</code> if it cannot be found + */ + public static URL getURL( Class klass ) + { + ClassLoader classLoader = klass.getClassLoader(); + + String path = klass.getName().replace( '.', '/' ) + ".class"; + + return classLoader.getResource( path ); + } +} Propchange: maven/sandbox/trunk/shared/maven-runtime/src/main/java/org/apache/maven/shared/runtime/ClassUtils.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/sandbox/trunk/shared/maven-runtime/src/main/java/org/apache/maven/shared/runtime/ClassUtils.java ------------------------------------------------------------------------------ svn:keywords = "Author Date Id Revision" Modified: maven/sandbox/trunk/shared/maven-runtime/src/main/java/org/apache/maven/shared/runtime/MavenRuntimeVisitorUtils.java URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/shared/maven-runtime/src/main/java/org/apache/maven/shared/runtime/MavenRuntimeVisitorUtils.java?rev=633030&r1=633029&r2=633030&view=diff ============================================================================== --- maven/sandbox/trunk/shared/maven-runtime/src/main/java/org/apache/maven/shared/runtime/MavenRuntimeVisitorUtils.java (original) +++ maven/sandbox/trunk/shared/maven-runtime/src/main/java/org/apache/maven/shared/runtime/MavenRuntimeVisitorUtils.java Mon Mar 3 02:32:00 2008 @@ -28,7 +28,6 @@ import java.util.jar.JarInputStream; import org.codehaus.plexus.util.IOUtil; -import org.codehaus.plexus.util.StringUtils; /** * Provides various methods of applying Maven runtime visitors. @@ -102,7 +101,7 @@ { try { - accept( getBaseURL( klass ), visitor ); + accept( ClassUtils.getBaseURL( klass ), visitor ); } catch ( MalformedURLException exception ) { @@ -281,58 +280,5 @@ } return true; - } - - /** - * Gets a URL to the specified class's default package. For example, if the class <code>foo.Bar</code> is - * supplied, then a URL to the directory above <code>foo</code> is returned. If the class's default package - * resides at the root of a Jar, then a URL to the Jar file itself is returned. - * - * @param klass - * the class to obtain the base URL for - * @return a URL to the class's default package, or a URL to the owning Jar file if the default package resides at - * the root of a Jar - * @throws MalformedURLException - * if the base URL cannot be determined - */ - private static URL getBaseURL( Class klass ) - throws MalformedURLException - { - URL url = getURL( klass ); - - String className = klass.getName(); - - int n = StringUtils.countMatches( className, "." ); - String relativePath = StringUtils.repeat( "../", n ); - - URL baseURL = new URL( url, relativePath ); - - // unwrap Jar URL if at the root - if ( "jar".equals( baseURL.getProtocol() ) && baseURL.getPath().endsWith( "!/" ) ) - { - String basePath = baseURL.getPath(); - - basePath = basePath.substring( 0, basePath.length() - "!/".length() ); - - baseURL = new URL( basePath ); - } - - return baseURL; - } - - /** - * Gets a URL to the specified class. - * - * @param klass - * the class to obtain the URL for - * @return a URL to the class, or <code>null</code> if it cannot be found - */ - private static URL getURL( Class klass ) - { - ClassLoader classLoader = klass.getClassLoader(); - - String path = klass.getName().replace( '.', '/' ) + ".class"; - - return classLoader.getResource( path ); } }