ClassPathUtils.java

  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. import java.util.Objects;

  19. /**
  20.  * Operations regarding the classpath.
  21.  *
  22.  * <p>
  23.  * The methods of this class do not allow {@code null} inputs.
  24.  * </p>
  25.  *
  26.  * @since 3.3
  27.  */
  28. //@Immutable
  29. public class ClassPathUtils {

  30.     /**
  31.      * Converts a package name to a Java path ('/').
  32.      *
  33.      * @param path the source path.
  34.      * @return a package name.
  35.      * @throws NullPointerException if {@code path} is null.
  36.      * @since 3.13.0
  37.      */
  38.     public static String packageToPath(final String path) {
  39.         return Objects.requireNonNull(path, "path").replace('.', '/');
  40.     }

  41.     /**
  42.      * Converts a Java path ('/') to a package name.
  43.      *
  44.      * @param path the source path.
  45.      * @return a package name.
  46.      * @throws NullPointerException if {@code path} is null.
  47.      * @since 3.13.0
  48.      */
  49.     public static String pathToPackage(final String path) {
  50.         return Objects.requireNonNull(path, "path").replace('/', '.');
  51.     }

  52.     /**
  53.      * Returns the fully qualified name for the resource with name {@code resourceName} relative to the given context.
  54.      *
  55.      * <p>
  56.      * Note that this method does not check whether the resource actually exists. It only constructs the name. Null inputs are not allowed.
  57.      * </p>
  58.      *
  59.      * <pre>
  60.      * ClassPathUtils.toFullyQualifiedName(StringUtils.class, "StringUtils.properties") = "org.apache.commons.lang3.StringUtils.properties"
  61.      * </pre>
  62.      *
  63.      * @param context      The context for constructing the name.
  64.      * @param resourceName the resource name to construct the fully qualified name for.
  65.      * @return the fully qualified name of the resource with name {@code resourceName}.
  66.      * @throws NullPointerException if either {@code context} or {@code resourceName} is null.
  67.      */
  68.     public static String toFullyQualifiedName(final Class<?> context, final String resourceName) {
  69.         Objects.requireNonNull(context, "context");
  70.         Objects.requireNonNull(resourceName, "resourceName");
  71.         return toFullyQualifiedName(context.getPackage(), resourceName);
  72.     }

  73.     /**
  74.      * Returns the fully qualified name for the resource with name {@code resourceName} relative to the given context.
  75.      *
  76.      * <p>
  77.      * Note that this method does not check whether the resource actually exists. It only constructs the name. Null inputs are not allowed.
  78.      * </p>
  79.      *
  80.      * <pre>
  81.      * ClassPathUtils.toFullyQualifiedName(StringUtils.class.getPackage(), "StringUtils.properties") = "org.apache.commons.lang3.StringUtils.properties"
  82.      * </pre>
  83.      *
  84.      * @param context      The context for constructing the name.
  85.      * @param resourceName the resource name to construct the fully qualified name for.
  86.      * @return the fully qualified name of the resource with name {@code resourceName}.
  87.      * @throws NullPointerException if either {@code context} or {@code resourceName} is null.
  88.      */
  89.     public static String toFullyQualifiedName(final Package context, final String resourceName) {
  90.         Objects.requireNonNull(context, "context");
  91.         Objects.requireNonNull(resourceName, "resourceName");
  92.         return context.getName() + "." + resourceName;
  93.     }

  94.     /**
  95.      * Returns the fully qualified path for the resource with name {@code resourceName} relative to the given context.
  96.      *
  97.      * <p>
  98.      * Note that this method does not check whether the resource actually exists. It only constructs the path. Null inputs are not allowed.
  99.      * </p>
  100.      *
  101.      * <pre>
  102.      * ClassPathUtils.toFullyQualifiedPath(StringUtils.class, "StringUtils.properties") = "org/apache/commons/lang3/StringUtils.properties"
  103.      * </pre>
  104.      *
  105.      * @param context      The context for constructing the path.
  106.      * @param resourceName the resource name to construct the fully qualified path for.
  107.      * @return the fully qualified path of the resource with name {@code resourceName}.
  108.      * @throws NullPointerException if either {@code context} or {@code resourceName} is null.
  109.      */
  110.     public static String toFullyQualifiedPath(final Class<?> context, final String resourceName) {
  111.         Objects.requireNonNull(context, "context");
  112.         Objects.requireNonNull(resourceName, "resourceName");
  113.         return toFullyQualifiedPath(context.getPackage(), resourceName);
  114.     }

  115.     /**
  116.      * Returns the fully qualified path for the resource with name {@code resourceName} relative to the given context.
  117.      *
  118.      * <p>
  119.      * Note that this method does not check whether the resource actually exists. It only constructs the path. Null inputs are not allowed.
  120.      * </p>
  121.      *
  122.      * <pre>
  123.      * ClassPathUtils.toFullyQualifiedPath(StringUtils.class.getPackage(), "StringUtils.properties") = "org/apache/commons/lang3/StringUtils.properties"
  124.      * </pre>
  125.      *
  126.      * @param context      The context for constructing the path.
  127.      * @param resourceName the resource name to construct the fully qualified path for.
  128.      * @return the fully qualified path of the resource with name {@code resourceName}.
  129.      * @throws NullPointerException if either {@code context} or {@code resourceName} is null.
  130.      */
  131.     public static String toFullyQualifiedPath(final Package context, final String resourceName) {
  132.         Objects.requireNonNull(context, "context");
  133.         Objects.requireNonNull(resourceName, "resourceName");
  134.         return packageToPath(context.getName()) + "/" + resourceName;
  135.     }

  136.     /**
  137.      * {@link ClassPathUtils} instances should NOT be constructed in standard programming. Instead, the class should be used as
  138.      * {@code ClassPathUtils.toFullyQualifiedName(MyClass.class, "MyClass.properties");}.
  139.      *
  140.      * <p>
  141.      * This constructor is public to permit tools that require a JavaBean instance to operate.
  142.      * </p>
  143.      *
  144.      * @deprecated TODO Make private in 4.0.
  145.      */
  146.     @Deprecated
  147.     public ClassPathUtils() {
  148.         // empty
  149.     }

  150. }