Coverage Summary for Class: Reflection (com.google.common.reflect)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| Reflection | 100% (1/1) | 60% (3/5) | 56.2% (9/16) |
1 /* 2 * Copyright (C) 2005 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15 package com.google.common.reflect; 16 17 import static com.google.common.base.Preconditions.checkArgument; 18 import static com.google.common.base.Preconditions.checkNotNull; 19 20 import com.google.common.annotations.Beta; 21 import java.lang.reflect.InvocationHandler; 22 import java.lang.reflect.Proxy; 23 24 /** 25 * Static utilities relating to Java reflection. 26 * 27 * @since 12.0 28 */ 29 @Beta 30 @ElementTypesAreNonnullByDefault 31 public final class Reflection { 32 33 /** 34 * Returns the package name of {@code clazz} according to the Java Language Specification (section 35 * 6.7). Unlike {@link Class#getPackage}, this method only parses the class name, without 36 * attempting to define the {@link Package} and hence load files. 37 */ 38 public static String getPackageName(Class<?> clazz) { 39 return getPackageName(clazz.getName()); 40 } 41 42 /** 43 * Returns the package name of {@code classFullName} according to the Java Language Specification 44 * (section 6.7). Unlike {@link Class#getPackage}, this method only parses the class name, without 45 * attempting to define the {@link Package} and hence load files. 46 */ 47 public static String getPackageName(String classFullName) { 48 int lastDot = classFullName.lastIndexOf('.'); 49 return (lastDot < 0) ? "" : classFullName.substring(0, lastDot); 50 } 51 52 /** 53 * Ensures that the given classes are initialized, as described in <a 54 * href="http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.4.2">JLS Section 55 * 12.4.2</a>. 56 * 57 * <p>WARNING: Normally it's a smell if a class needs to be explicitly initialized, because static 58 * state hurts system maintainability and testability. In cases when you have no choice while 59 * inter-operating with a legacy framework, this method helps to keep the code less ugly. 60 * 61 * @throws ExceptionInInitializerError if an exception is thrown during initialization of a class 62 */ 63 public static void initialize(Class<?>... classes) { 64 for (Class<?> clazz : classes) { 65 try { 66 Class.forName(clazz.getName(), true, clazz.getClassLoader()); 67 } catch (ClassNotFoundException e) { 68 throw new AssertionError(e); 69 } 70 } 71 } 72 73 /** 74 * Returns a proxy instance that implements {@code interfaceType} by dispatching method 75 * invocations to {@code handler}. The class loader of {@code interfaceType} will be used to 76 * define the proxy class. To implement multiple interfaces or specify a class loader, use {@link 77 * Proxy#newProxyInstance}. 78 * 79 * @throws IllegalArgumentException if {@code interfaceType} does not specify the type of a Java 80 * interface 81 */ 82 public static <T> T newProxy(Class<T> interfaceType, InvocationHandler handler) { 83 checkNotNull(handler); 84 checkArgument(interfaceType.isInterface(), "%s is not an interface", interfaceType); 85 Object object = 86 Proxy.newProxyInstance( 87 interfaceType.getClassLoader(), new Class<?>[] {interfaceType}, handler); 88 return interfaceType.cast(object); 89 } 90 91 private Reflection() {} 92 }