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 java.lang.reflect.InvocationHandler;
021import java.lang.reflect.Proxy;
022
023/**
024 * Static utilities relating to Java reflection.
025 *
026 * @since 12.0
027 */
028@ElementTypesAreNonnullByDefault
029public final class Reflection {
030
031  /**
032   * Returns the package name of {@code clazz} according to the Java Language Specification (section
033   * 6.7). Unlike {@link Class#getPackage}, this method only parses the class name, without
034   * attempting to define the {@link Package} and hence load files.
035   */
036  public static String getPackageName(Class<?> clazz) {
037    return getPackageName(clazz.getName());
038  }
039
040  /**
041   * Returns the package name of {@code classFullName} according to the Java Language Specification
042   * (section 6.7). Unlike {@link Class#getPackage}, this method only parses the class name, without
043   * attempting to define the {@link Package} and hence load files.
044   */
045  public static String getPackageName(String classFullName) {
046    int lastDot = classFullName.lastIndexOf('.');
047    return (lastDot < 0) ? "" : classFullName.substring(0, lastDot);
048  }
049
050  /**
051   * Ensures that the given classes are initialized, as described in <a
052   * href="http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.4.2">JLS Section
053   * 12.4.2</a>.
054   *
055   * <p>WARNING: Normally it's a smell if a class needs to be explicitly initialized, because static
056   * state hurts system maintainability and testability. In cases when you have no choice while
057   * interoperating with a legacy framework, this method helps to keep the code less ugly.
058   *
059   * @throws ExceptionInInitializerError if an exception is thrown during initialization of a class
060   */
061  public static void initialize(Class<?>... classes) {
062    for (Class<?> clazz : classes) {
063      try {
064        Class.forName(clazz.getName(), true, clazz.getClassLoader());
065      } catch (ClassNotFoundException e) {
066        throw new AssertionError(e);
067      }
068    }
069  }
070
071  /**
072   * Returns a proxy instance that implements {@code interfaceType} by dispatching method
073   * invocations to {@code handler}. The class loader of {@code interfaceType} will be used to
074   * define the proxy class. To implement multiple interfaces or specify a class loader, use {@link
075   * Proxy#newProxyInstance}.
076   *
077   * @throws IllegalArgumentException if {@code interfaceType} does not specify the type of a Java
078   *     interface
079   */
080  public static <T> T newProxy(Class<T> interfaceType, InvocationHandler handler) {
081    checkNotNull(handler);
082    checkArgument(interfaceType.isInterface(), "%s is not an interface", interfaceType);
083    Object object =
084        Proxy.newProxyInstance(
085            interfaceType.getClassLoader(), new Class<?>[] {interfaceType}, handler);
086    return interfaceType.cast(object);
087  }
088
089  private Reflection() {}
090}