001/*
002 * Copyright (C) 2005 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.reflect;
016
017import static com.google.common.base.Preconditions.checkArgument;
018import static com.google.common.base.Preconditions.checkNotNull;
019
020import com.google.common.annotations.Beta;
021import java.lang.reflect.InvocationHandler;
022import java.lang.reflect.Proxy;
023
024/**
025 * Static utilities relating to Java reflection.
026 *
027 * @since 12.0
028 */
029@Beta
030@ElementTypesAreNonnullByDefault
031public final class Reflection {
032
033  /**
034   * Returns the package name of {@code clazz} according to the Java Language Specification (section
035   * 6.7). Unlike {@link Class#getPackage}, this method only parses the class name, without
036   * attempting to define the {@link Package} and hence load files.
037   */
038  public static String getPackageName(Class<?> clazz) {
039    return getPackageName(clazz.getName());
040  }
041
042  /**
043   * Returns the package name of {@code classFullName} according to the Java Language Specification
044   * (section 6.7). Unlike {@link Class#getPackage}, this method only parses the class name, without
045   * attempting to define the {@link Package} and hence load files.
046   */
047  public static String getPackageName(String classFullName) {
048    int lastDot = classFullName.lastIndexOf('.');
049    return (lastDot < 0) ? "" : classFullName.substring(0, lastDot);
050  }
051
052  /**
053   * Ensures that the given classes are initialized, as described in <a
054   * href="http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.4.2">JLS Section
055   * 12.4.2</a>.
056   *
057   * <p>WARNING: Normally it's a smell if a class needs to be explicitly initialized, because static
058   * state hurts system maintainability and testability. In cases when you have no choice while
059   * inter-operating with a legacy framework, this method helps to keep the code less ugly.
060   *
061   * @throws ExceptionInInitializerError if an exception is thrown during initialization of a class
062   */
063  public static void initialize(Class<?>... classes) {
064    for (Class<?> clazz : classes) {
065      try {
066        Class.forName(clazz.getName(), true, clazz.getClassLoader());
067      } catch (ClassNotFoundException e) {
068        throw new AssertionError(e);
069      }
070    }
071  }
072
073  /**
074   * Returns a proxy instance that implements {@code interfaceType} by dispatching method
075   * invocations to {@code handler}. The class loader of {@code interfaceType} will be used to
076   * define the proxy class. To implement multiple interfaces or specify a class loader, use {@link
077   * Proxy#newProxyInstance}.
078   *
079   * @throws IllegalArgumentException if {@code interfaceType} does not specify the type of a Java
080   *     interface
081   */
082  public static <T> T newProxy(Class<T> interfaceType, InvocationHandler handler) {
083    checkNotNull(handler);
084    checkArgument(interfaceType.isInterface(), "%s is not an interface", interfaceType);
085    Object object =
086        Proxy.newProxyInstance(
087            interfaceType.getClassLoader(), new Class<?>[] {interfaceType}, handler);
088    return interfaceType.cast(object);
089  }
090
091  private Reflection() {}
092}