Java Code Examples for com.google.inject.spi.Element#getSource()

The following examples show how to use com.google.inject.spi.Element#getSource() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BindingUtils.java    From dropwizard-guicey with MIT License 6 votes vote down vote up
/**
 * Resolve guice configuration element declaration module chain. The first list element is declaration module.
 * Later elements appear if module was installed by other module.
 *
 * @param element guice SPI element
 * @return modules chain from declaration point or single {@link #JIT_MODULE} if binding is a JIT binding
 */
public static List<String> getModules(final Element element) {
    final List<String> modules;
    if (element.getSource() instanceof ElementSource) {
        ElementSource source = (ElementSource) element.getSource();
        // if module was repackaged by elements api, we need an original source in order to build correct report
        if (source.getOriginalElementSource() != null) {
            source = source.getOriginalElementSource();
        }
        modules = source.getModuleClassNames();
    } else {
        // consider JIT binding
        modules = Collections.singletonList(JIT_MODULE);
    }
    return modules;
}
 
Example 2
Source File: GuiceModelUtils.java    From dropwizard-guicey with MIT License 5 votes vote down vote up
/**
 * NOTE: this will work only for elements, parsed with SPI api, and not for real bindings!
 *
 * @param element guice binding element
 * @return element declaration stacktrace element
 */
public static StackTraceElement getDeclarationSource(final Element element) {
    final Object source = element.getSource();
    StackTraceElement traceElement = null;
    if (source instanceof ElementSource) {
        final ElementSource src = (ElementSource) source;
        if (src.getDeclaringSource() instanceof StackTraceElement) {
            traceElement = (StackTraceElement) src.getDeclaringSource();
        } else if (src.getDeclaringSource() instanceof Method) {
            traceElement = (StackTraceElement) StackTraceElements.forMember((Method) src.getDeclaringSource());
        }
    }
    return traceElement;
}
 
Example 3
Source File: GuiceModelParser.java    From dropwizard-guicey with MIT License 5 votes vote down vote up
private static void fillSource(final BindingDeclaration dec, final Element element) {
    final StackTraceElement trace = GuiceModelUtils.getDeclarationSource(element);
    if (trace != null) {
        // using full stacktrace element to grant proper link highlight in idea
        dec.setSource(trace.toString());
        dec.setSourceLine(trace.getLineNumber());
    } else {
        final Object source = element.getSource();
        if (source instanceof Class) {
            dec.setSource(((Class) source).getName());
        } else {
            LOGGER.warn("Unknown element '{}' source: {}", dec, source);
        }
    }
}