1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.lang3; 18 19 import java.util.Objects; 20 21 /** 22 * Operations regarding the classpath. 23 * 24 * <p> 25 * The methods of this class do not allow {@code null} inputs. 26 * </p> 27 * 28 * @since 3.3 29 */ 30 //@Immutable 31 public class ClassPathUtils { 32 33 /** 34 * Converts a package name to a Java path ('/'). 35 * 36 * @param path the source path. 37 * @return a package name. 38 * @since 3.13.0 39 */ packageToPath(final String path)40 public static String packageToPath(final String path) { 41 return Objects.requireNonNull(path, "path").replace('.', '/'); 42 } 43 44 /** 45 * Converts a Java path ('/') to a package name. 46 * 47 * @param path the source path. 48 * @return a package name. 49 * @since 3.13.0 50 */ pathToPackage(final String path)51 public static String pathToPackage(final String path) { 52 return Objects.requireNonNull(path, "path").replace('/', '.'); 53 } 54 55 /** 56 * Returns the fully qualified name for the resource with name {@code resourceName} relative to the given context. 57 * 58 * <p> 59 * Note that this method does not check whether the resource actually exists. It only constructs the name. Null inputs are not allowed. 60 * </p> 61 * 62 * <pre> 63 * ClassPathUtils.toFullyQualifiedName(StringUtils.class, "StringUtils.properties") = "org.apache.commons.lang3.StringUtils.properties" 64 * </pre> 65 * 66 * @param context The context for constructing the name. 67 * @param resourceName the resource name to construct the fully qualified name for. 68 * @return the fully qualified name of the resource with name {@code resourceName}. 69 * @throws NullPointerException if either {@code context} or {@code resourceName} is null. 70 */ toFullyQualifiedName(final Class<?> context, final String resourceName)71 public static String toFullyQualifiedName(final Class<?> context, final String resourceName) { 72 Objects.requireNonNull(context, "context"); 73 Objects.requireNonNull(resourceName, "resourceName"); 74 return toFullyQualifiedName(context.getPackage(), resourceName); 75 } 76 77 /** 78 * Returns the fully qualified name for the resource with name {@code resourceName} relative to the given context. 79 * 80 * <p> 81 * Note that this method does not check whether the resource actually exists. It only constructs the name. Null inputs are not allowed. 82 * </p> 83 * 84 * <pre> 85 * ClassPathUtils.toFullyQualifiedName(StringUtils.class.getPackage(), "StringUtils.properties") = "org.apache.commons.lang3.StringUtils.properties" 86 * </pre> 87 * 88 * @param context The context for constructing the name. 89 * @param resourceName the resource name to construct the fully qualified name for. 90 * @return the fully qualified name of the resource with name {@code resourceName}. 91 * @throws NullPointerException if either {@code context} or {@code resourceName} is null. 92 */ toFullyQualifiedName(final Package context, final String resourceName)93 public static String toFullyQualifiedName(final Package context, final String resourceName) { 94 Objects.requireNonNull(context, "context"); 95 Objects.requireNonNull(resourceName, "resourceName"); 96 return context.getName() + "." + resourceName; 97 } 98 99 /** 100 * Returns the fully qualified path for the resource with name {@code resourceName} relative to the given context. 101 * 102 * <p> 103 * Note that this method does not check whether the resource actually exists. It only constructs the path. Null inputs are not allowed. 104 * </p> 105 * 106 * <pre> 107 * ClassPathUtils.toFullyQualifiedPath(StringUtils.class, "StringUtils.properties") = "org/apache/commons/lang3/StringUtils.properties" 108 * </pre> 109 * 110 * @param context The context for constructing the path. 111 * @param resourceName the resource name to construct the fully qualified path for. 112 * @return the fully qualified path of the resource with name {@code resourceName}. 113 * @throws NullPointerException if either {@code context} or {@code resourceName} is null. 114 */ toFullyQualifiedPath(final Class<?> context, final String resourceName)115 public static String toFullyQualifiedPath(final Class<?> context, final String resourceName) { 116 Objects.requireNonNull(context, "context"); 117 Objects.requireNonNull(resourceName, "resourceName"); 118 return toFullyQualifiedPath(context.getPackage(), resourceName); 119 } 120 121 /** 122 * Returns the fully qualified path for the resource with name {@code resourceName} relative to the given context. 123 * 124 * <p> 125 * Note that this method does not check whether the resource actually exists. It only constructs the path. Null inputs are not allowed. 126 * </p> 127 * 128 * <pre> 129 * ClassPathUtils.toFullyQualifiedPath(StringUtils.class.getPackage(), "StringUtils.properties") = "org/apache/commons/lang3/StringUtils.properties" 130 * </pre> 131 * 132 * @param context The context for constructing the path. 133 * @param resourceName the resource name to construct the fully qualified path for. 134 * @return the fully qualified path of the resource with name {@code resourceName}. 135 * @throws NullPointerException if either {@code context} or {@code resourceName} is null. 136 */ toFullyQualifiedPath(final Package context, final String resourceName)137 public static String toFullyQualifiedPath(final Package context, final String resourceName) { 138 Objects.requireNonNull(context, "context"); 139 Objects.requireNonNull(resourceName, "resourceName"); 140 return packageToPath(context.getName()) + "/" + resourceName; 141 } 142 143 /** 144 * {@link ClassPathUtils} instances should NOT be constructed in standard programming. Instead, the class should be used as 145 * {@code ClassPathUtils.toFullyQualifiedName(MyClass.class, "MyClass.properties");}. 146 * 147 * <p> 148 * This constructor is public to permit tools that require a JavaBean instance to operate. 149 * </p> 150 */ ClassPathUtils()151 public ClassPathUtils() { 152 } 153 154 } 155