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