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