| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ClassUtils |
|
| 1.3333333333333333;1,333 |
| 1 | package org.apache.maven.shared.runtime; | |
| 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.net.MalformedURLException; | |
| 23 | import java.net.URL; | |
| 24 | ||
| 25 | import org.codehaus.plexus.util.StringUtils; | |
| 26 | ||
| 27 | /** | |
| 28 | * Provides various utility methods for working with classes. | |
| 29 | * | |
| 30 | * @author <a href="mailto:markh@apache.org">Mark Hobson</a> | |
| 31 | * @version $Id: ClassUtils.java 831910 2009-11-02 15:05:33Z markh $ | |
| 32 | */ | |
| 33 | final class ClassUtils | |
| 34 | { | |
| 35 | // constructors ----------------------------------------------------------- | |
| 36 | ||
| 37 | /** | |
| 38 | * {@code ClassUtils} is not intended to be instantiated. | |
| 39 | */ | |
| 40 | private ClassUtils() | |
| 41 | 0 | { |
| 42 | 0 | throw new AssertionError(); |
| 43 | } | |
| 44 | ||
| 45 | // public methods --------------------------------------------------------- | |
| 46 | ||
| 47 | /** | |
| 48 | * Gets a URL to the specified class's default package. For example, if the class {@code foo.Bar} is supplied, then | |
| 49 | * a URL to the directory above {@code foo} is returned. | |
| 50 | * | |
| 51 | * @param klass | |
| 52 | * the class to obtain the base URL for | |
| 53 | * @return a URL to the class's default package | |
| 54 | * @throws MalformedURLException | |
| 55 | * if the base URL cannot be determined | |
| 56 | */ | |
| 57 | public static URL getBaseURL( Class<?> klass ) | |
| 58 | throws MalformedURLException | |
| 59 | { | |
| 60 | 56 | URL url = getURL( klass ); |
| 61 | ||
| 62 | 56 | String className = klass.getName(); |
| 63 | ||
| 64 | 56 | int n = StringUtils.countMatches( className, "." ); |
| 65 | 56 | String relativePath = StringUtils.repeat( "../", n ); |
| 66 | ||
| 67 | 56 | return new URL( url, relativePath ); |
| 68 | } | |
| 69 | ||
| 70 | /** | |
| 71 | * Gets a URL to the specified class. | |
| 72 | * | |
| 73 | * @param klass | |
| 74 | * the class to obtain the URL for | |
| 75 | * @return a URL to the class, or {@code null} if it cannot be found | |
| 76 | */ | |
| 77 | public static URL getURL( Class<?> klass ) | |
| 78 | { | |
| 79 | 56 | ClassLoader classLoader = klass.getClassLoader(); |
| 80 | ||
| 81 | 56 | String path = klass.getName().replace( '.', '/' ) + ".class"; |
| 82 | ||
| 83 | 56 | return classLoader.getResource( path ); |
| 84 | } | |
| 85 | } |