Class ElementSource
- java.lang.Object
-
- com.google.inject.spi.ElementSource
-
public final class ElementSource extends java.lang.Object
Contains information about where and how anelement
was bound.The
declaring source
refers to a location in source code that defines the Guiceelement
. For example, if the element is created from a method annotated by @Provides, the declaring source of element would be the method itself.The
getStackTrace()
refers to the sequence of calls ends at one ofBinder
bindXXX()
methods and eventually defines the element. Note thatgetStackTrace()
listsStackTraceElements
in reverse chronological order. The first element (index zero) is the last method call and the last element is the first method invocation. By default, the stack trace is not collected. The default behavior can be changed by setting theguice_include_stack_traces
flag value. The value can be eitherOFF
,ONLY_FOR_DECLARING_SOURCE
orCOMPLETE
. Note that collecting stack traces for every binding can cause a performance hit when the injector is created.The sequence of class names of
modules
involved in the element creation can be retrieved bygetModuleClassNames()
. Similar togetStackTrace()
, the order is reverse chronological. The first module (index 0) is the module that installs theelement
. The last module is the root module.In order to support the cases where a Guice
element
is created from another Guiceelement
(original) (e.g., byElement.applyTo(com.google.inject.Binder)
), it also provides a reference to the original element source (getOriginalElementSource()
).- Since:
- 4.0
-
-
Field Summary
Fields Modifier and Type Field Description (package private) java.lang.Object
declaringSource
Refers to a single location in source code that causes the element creation.(package private) ModuleSource
moduleSource
Thesource
of module creates the element.(package private) ElementSource
originalElementSource
Thesource
of element that this element created from (if there is any), otherwisenull
.(package private) StackTraceElements.InMemoryStackTraceElement[]
partialCallStack
The partial call stack that starts at the last moduleconfigure(Binder)
call.
-
Constructor Summary
Constructors Constructor Description ElementSource(ElementSource originalSource, java.lang.Object declaringSource, ModuleSource moduleSource, java.lang.StackTraceElement[] partialCallStack)
Creates a new from the given parameters.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Object
getDeclaringSource()
Returns a single location in source code that defines the element.java.util.List<java.lang.String>
getModuleClassNames()
Returns the class names of modules involved in creating thisElement
.java.util.List<java.lang.Integer>
getModuleConfigurePositionsInStackTrace()
Returns the position ofconfigure(Binder)
method call in thestack trace
for modules that their classes returned bygetModuleClassNames()
.ElementSource
getOriginalElementSource()
Returns theElementSource
of the element this was created or copied from.java.lang.StackTraceElement[]
getStackTrace()
Returns the sequence of method calls that ends at one ofBinder
bindXXX()
methods and eventually defines the element.java.lang.String
toString()
ReturnsgetDeclaringSource().toString()
value.
-
-
-
Field Detail
-
originalElementSource
final ElementSource originalElementSource
Thesource
of element that this element created from (if there is any), otherwisenull
.
-
moduleSource
final ModuleSource moduleSource
Thesource
of module creates the element.
-
partialCallStack
final StackTraceElements.InMemoryStackTraceElement[] partialCallStack
The partial call stack that starts at the last moduleconfigure(Binder)
call. The value is empty if stack trace collection is off.
-
declaringSource
final java.lang.Object declaringSource
Refers to a single location in source code that causes the element creation. It can be any object such asConstructor
,Method
,Field
,StackTraceElement
, etc. For example, if the element is created from a method annotated by @Provides, the declaring source of element would be the method itself.
-
-
Constructor Detail
-
ElementSource
ElementSource(ElementSource originalSource, java.lang.Object declaringSource, ModuleSource moduleSource, java.lang.StackTraceElement[] partialCallStack)
Creates a new from the given parameters.- Parameters:
originalElementSource
- The source of element that this element created from (if there is any), otherwisenull
.declaringSource
- the source (in)directly declared the element.moduleSource
- the moduleSource when the element is boundpartialCallStack
- the partial call stack from the top module to where the element is bound
-
-
Method Detail
-
getOriginalElementSource
public ElementSource getOriginalElementSource()
Returns theElementSource
of the element this was created or copied from. If this was not created or copied from another element, returnsnull
.
-
getDeclaringSource
public java.lang.Object getDeclaringSource()
Returns a single location in source code that defines the element. It can be any object such asConstructor
,Method
,Field
,StackTraceElement
, etc. For example, if the element is created from a method annotated by @Provides, the declaring source of element would be the method itself.
-
getModuleClassNames
public java.util.List<java.lang.String> getModuleClassNames()
Returns the class names of modules involved in creating thisElement
. The first element (index 0) is the class name of module that defined the element, and the last element is the class name of root module.
-
getModuleConfigurePositionsInStackTrace
public java.util.List<java.lang.Integer> getModuleConfigurePositionsInStackTrace()
Returns the position ofconfigure(Binder)
method call in thestack trace
for modules that their classes returned bygetModuleClassNames()
. For example, if the stack trace looks like the following:Binder.bind()
ModuleTwo.configure()
Binder.install()
ModuleOne.configure()
- {@code theRest().
1 and 3 are returned.
In the cases where stack trace is not available (i.e., the stack trace was not collected), it returns -1 for all module positions.
-
getStackTrace
public java.lang.StackTraceElement[] getStackTrace()
Returns the sequence of method calls that ends at one ofBinder
bindXXX()
methods and eventually defines the element. Note thatgetStackTrace()
listsStackTraceElements
in reverse chronological order. The first element (index zero) is the last method call and the last element is the first method invocation. In the cases where stack trace is not available (i.e.,the stack trace was not collected), it returns an empty array.
-
toString
public java.lang.String toString()
ReturnsgetDeclaringSource().toString()
value.- Overrides:
toString
in classjava.lang.Object
-
-