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