com.google.gwt.inject.rebind.reflect
Class ReflectUtil

java.lang.Object
  extended by com.google.gwt.inject.rebind.reflect.ReflectUtil

public class ReflectUtil
extends java.lang.Object

Utility providing helper methods around reflection.


Nested Class Summary
static class ReflectUtil.SignatureBuilder
          Builder that produces the signature of a method.
 
Constructor Summary
ReflectUtil()
           
 
Method Summary
static java.lang.String formatParameterName(int position)
          Returns a string representing a parameter name for a method signature.
static Key<?> getProvidedKey(Key<?> key)
          Given a parameterized type (such as a Provider<Foo>) return the parameter (Foo).
static java.lang.String getSourceName(java.lang.reflect.Type type)
          Returns a string representation of the passed type's name while ensuring that all type names (base and parameters) are converted to source type names.
static java.lang.String getSourceName(TypeLiteral<?> typeLiteral)
          Alternate toString method for TypeLiterals that fixes a JDK bug that was replicated in Guice.
static java.lang.String getUserPackageName(Key<?> key)
          Return the name of the package from which the given key can be used.
static java.lang.String getUserPackageName(TypeLiteral<?> typeLiteral)
          Return the name of the package from which the given type can be used.
static boolean hasAccessibleDefaultConstructor(java.lang.Class<?> clazz)
          Returns true if the given class has a non-private default constructor, or has no constructor at all.
static boolean isClassOrInterface(java.lang.reflect.Type type)
          Returns true if the passed type is either a class or an interface (but not a primitive, enum or similar).
static boolean isPrivate(java.lang.Class<?> type)
          Returns true if the passed type's visibility is private.
static boolean isPrivate(java.lang.reflect.Member member)
          Returns true if the passed member's visibility is private.
static boolean isPrivate(TypeLiteral<?> type)
          Returns true if the passed type's visibility is private.
static boolean isPublic(java.lang.Class<?> type)
          Returns true if the passed type's visibility is public.
static int nonAbstractModifiers(MethodLiteral<?,java.lang.reflect.Method> method)
          If present, strips the "abstract" modifier from the passed method's modifiers.
static ReflectUtil.SignatureBuilder signatureBuilder(MethodLiteral<?,?> method)
          Builds the signature of a method with all types in source form.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReflectUtil

public ReflectUtil()
Method Detail

getSourceName

public static java.lang.String getSourceName(TypeLiteral<?> typeLiteral)
                                      throws NoSourceNameException
Alternate toString method for TypeLiterals that fixes a JDK bug that was replicated in Guice. See the related Guice bug for details. Also replaces all binary with source names in the types involved (base type and type parameters).

Parameters:
typeLiteral - type for which string will be returned
Returns:
String representation of type
Throws:
NoSourceNameException - if source name is not available for type

getSourceName

public static java.lang.String getSourceName(java.lang.reflect.Type type)
                                      throws NoSourceNameException
Returns a string representation of the passed type's name while ensuring that all type names (base and parameters) are converted to source type names.

Parameters:
type - type for which string will be returned
Returns:
String representation of type
Throws:
NoSourceNameException - if source name is not available for type

getUserPackageName

public static java.lang.String getUserPackageName(TypeLiteral<?> typeLiteral)
Return the name of the package from which the given type can be used.

Returns a package from which all the type names contained in the given type literal are visible. Throws IllegalArgumentException if there is no such package. If there are multiple such packages, then the type name can be used from any package; the package containing the outermost class is used arbitrarily.

This method is intentionally not overloaded on Class, because it's normally an error to use a raw Class token to determine the package in which to manipulate a type.


getUserPackageName

public static java.lang.String getUserPackageName(Key<?> key)
Return the name of the package from which the given key can be used.

Returns a package from which all the type names contained in the given key are visible. Throws IllegalArgumentException if there is no such package.


isPublic

public static boolean isPublic(java.lang.Class<?> type)
Returns true if the passed type's visibility is public.


isPrivate

public static boolean isPrivate(java.lang.Class<?> type)
Returns true if the passed type's visibility is private.


isPrivate

public static boolean isPrivate(TypeLiteral<?> type)
Returns true if the passed type's visibility is private.


isPrivate

public static boolean isPrivate(java.lang.reflect.Member member)
Returns true if the passed member's visibility is private.


signatureBuilder

public static ReflectUtil.SignatureBuilder signatureBuilder(MethodLiteral<?,?> method)
Builds the signature of a method with all types in source form.


formatParameterName

public static java.lang.String formatParameterName(int position)
Returns a string representing a parameter name for a method signature.

Use this method to keep parameter names the same throughout Gin code. Creating synthetic parameter names is necessary since java reflection does not expose source parameter names.

Parameters:
position - position of the parameter in the signature
Returns:
parameter name

nonAbstractModifiers

public static int nonAbstractModifiers(MethodLiteral<?,java.lang.reflect.Method> method)
If present, strips the "abstract" modifier from the passed method's modifiers.

Useful since interface methods are abstract but we're often writing an implementation for them.


isClassOrInterface

public static boolean isClassOrInterface(java.lang.reflect.Type type)
Returns true if the passed type is either a class or an interface (but not a primitive, enum or similar).

Parameters:
type - class to be checked
Returns:
true if the passed type is a class or interface

getProvidedKey

public static Key<?> getProvidedKey(Key<?> key)
Given a parameterized type (such as a Provider<Foo>) return the parameter (Foo).


hasAccessibleDefaultConstructor

public static boolean hasAccessibleDefaultConstructor(java.lang.Class<?> clazz)
Returns true if the given class has a non-private default constructor, or has no constructor at all.