private ImmutableList<EventualProvider<?>> introspectProviders() { ImmutableList.Builder<EventualProvider<?>> builder = ImmutableList.builder(); // FIXME handle method overriding? for (Class<?> t : type.getTypes().classes().rawTypes()) { if (t != Object.class) { for (Method m : t.getDeclaredMethods()) { if (m.isAnnotationPresent(Eventually.Provides.class)) { Errors methodErrors = errors.withSource(StackTraceElements.forMember(m)); Invokable<T, Object> invokable = type.method(m); if (eligibilityVerified(invokable, methodErrors)) { builder.add(providerFor(invokable, methodErrors)); } } } } } return builder.build(); }
/** * Returns a name for a Guice "source" object. This will typically be either a {@link * StackTraceElement} for when the binding is made to the instance, or a {@link Method} when a * provider method is used. */ @Override public String getSourceName(Object source) { if (source instanceof ElementSource) { source = ((ElementSource) source).getDeclaringSource(); } if (source instanceof Method) { source = StackTraceElements.forMember((Method) source); } if (source instanceof StackTraceElement) { return getFileString((StackTraceElement) source); } return stripPackages(source.toString()); }
/** * Returns the full call stack that ends just before the module {@link Module#configure(Binder) * configure(Binder)} method invocation. The return array is non-empty if stack trace collection * on. */ StackTraceElement[] getStackTrace() { int stackTraceSize = getStackTraceSize(); StackTraceElement[] callStack = new StackTraceElement[stackTraceSize]; int cursor = 0; ModuleSource current = this; while (current != null) { StackTraceElement[] chunk = StackTraceElements.convertToStackTraceElement(current.partialCallStack); int chunkSize = chunk.length; System.arraycopy(chunk, 0, callStack, cursor, chunkSize); current = current.parent; cursor = cursor + chunkSize; } return callStack; }
private static void formatInjectionPoint( Formatter formatter, Dependency<?> dependency, InjectionPoint injectionPoint, ElementSource elementSource) { Member member = injectionPoint.getMember(); Class<? extends Member> memberType = Classes.memberType(member); if (memberType == Field.class) { dependency = injectionPoint.getDependencies().get(0); formatter.format(" while locating %s%n", convert(dependency.getKey(), elementSource)); formatter.format(" for field at %s%n", StackTraceElements.forMember(member)); } else if (dependency != null) { formatter.format(" while locating %s%n", convert(dependency.getKey(), elementSource)); formatter.format(" for %s%n", formatParameter(dependency)); } else { formatSource(formatter, injectionPoint.getMember()); } }
public static void formatInjectionPoint(Formatter formatter, Dependency<?> dependency, InjectionPoint injectionPoint, ElementSource elementSource) { Member member = injectionPoint.getMember(); Class<? extends Member> memberType = Classes.memberType(member); if (memberType == Field.class) { dependency = injectionPoint.getDependencies().get(0); formatter.format(" while locating %s%n", convert(dependency.getKey(), elementSource)); formatter.format(" for field at %s%n", StackTraceElements.forMember(member)); } else if (dependency != null) { formatter.format(" while locating %s%n", convert(dependency.getKey(), elementSource)); formatter.format(" for parameter %s at %s%n", dependency.getParameterIndex(), StackTraceElements.forMember(member)); } else { formatSource(formatter, injectionPoint.getMember()); } }
public static void formatInjectionPoint(Formatter formatter, Dependency<?> dependency, InjectionPoint injectionPoint) { Member member = injectionPoint.getMember(); Class<? extends Member> memberType = Classes.memberType(member); if (memberType == Field.class) { dependency = injectionPoint.getDependencies().get(0); formatter.format(" while locating %s%n", convert(dependency.getKey())); formatter.format(" for field at %s%n", StackTraceElements.forMember(member)); } else if (dependency != null) { formatter.format(" while locating %s%n", convert(dependency.getKey())); formatter.format(" for parameter %s at %s%n", dependency.getParameterIndex(), StackTraceElements.forMember(member)); } else { formatSource(formatter, injectionPoint.getMember()); } }
Providers(@Nullable T providersInstance, Class<T> providerClass) { this.providersInstance = providersInstance; this.providersClass = providerClass; this.source = StackTraceElements.forType(providersClass); this.type = TypeToken.of(providersClass); this.errors = new Errors(source); this.scopeAnnotation = Annotations.findScopeAnnotation(errors, providersClass); this.providers = introspectProviders(); }
public void testGetSourceName_stackTraceElement() throws Exception { StackTraceElement element = (StackTraceElement) StackTraceElements.forMember(Obj.class.getField("field")); assertEquals( "Stack trace element should be identified by its file name and line number", "ShortNameFactoryTest.java:51", nameFactory.getSourceName(element)); }
/** Records the elements executed by {@code modules}. */ public static List<Element> getElements(Stage stage, Iterable<? extends Module> modules) { RecordingBinder binder = new RecordingBinder(stage); for (Module module : modules) { binder.install(module); } binder.scanForAnnotatedMethods(); for (RecordingBinder child : binder.privateBinders) { child.scanForAnnotatedMethods(); } // Free the memory consumed by the stack trace elements cache StackTraceElements.clearCache(); return Collections.unmodifiableList(binder.elements); }
/** * Returns a string describing where this dependency was bound. If the binding was just-in-time, * there is no valid binding source, so this describes the class in question. */ public String getBindingSource() { if (source instanceof Class) { return StackTraceElements.forType((Class) source).toString(); } else if (source instanceof Member) { return StackTraceElements.forMember((Member) source).toString(); } else { return source.toString(); } }
/** * Creates a new {@ElementSource} from the given parameters. * * @param originalElementSource The source of element that this element created from (if there is * any), otherwise {@code null}. * @param declaringSource the source (in)directly declared the element. * @param moduleSource the moduleSource when the element is bound * @param partialCallStack the partial call stack from the top module to where the element is * bound */ ElementSource( /* @Nullable */ ElementSource originalSource, Object declaringSource, ModuleSource moduleSource, StackTraceElement[] partialCallStack) { Preconditions.checkNotNull(declaringSource, "declaringSource cannot be null."); Preconditions.checkNotNull(moduleSource, "moduleSource cannot be null."); Preconditions.checkNotNull(partialCallStack, "partialCallStack cannot be null."); this.originalElementSource = originalSource; this.declaringSource = declaringSource; this.moduleSource = moduleSource; this.partialCallStack = StackTraceElements.convertToInMemoryStackTraceElement(partialCallStack); }
/** * Returns the sequence of method calls that ends at one of {@link com.google.inject.Binder} * {@code bindXXX()} methods and eventually defines the element. Note that {@link #getStackTrace} * lists {@link StackTraceElement StackTraceElements} 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. */ public StackTraceElement[] getStackTrace() { int modulesCallStackSize = moduleSource.getStackTraceSize(); int chunkSize = partialCallStack.length; int size = moduleSource.getStackTraceSize() + chunkSize; StackTraceElement[] callStack = new StackTraceElement[size]; System.arraycopy( StackTraceElements.convertToStackTraceElement(partialCallStack), 0, callStack, 0, chunkSize); System.arraycopy(moduleSource.getStackTrace(), 0, callStack, chunkSize, modulesCallStackSize); return callStack; }
@Override public String toString() { String annotationString = annotation.toString(); // Show @Provides w/o the com.google.inject prefix. if (annotation.annotationType() == Provides.class) { annotationString = "@Provides"; } else if (annotationString.endsWith("()")) { // Remove the common "()" suffix if there are no values. annotationString = annotationString.substring(0, annotationString.length() - 2); } return annotationString + " " + StackTraceElements.forMember(method); }
static void formatSource(Formatter formatter, Object source, ElementSource elementSource) { String modules = moduleSourceString(elementSource); if (source instanceof Dependency) { Dependency<?> dependency = (Dependency<?>) source; InjectionPoint injectionPoint = dependency.getInjectionPoint(); if (injectionPoint != null) { formatInjectionPoint(formatter, dependency, injectionPoint, elementSource); } else { formatSource(formatter, dependency.getKey(), elementSource); } } else if (source instanceof InjectionPoint) { formatInjectionPoint(formatter, null, (InjectionPoint) source, elementSource); } else if (source instanceof Class) { formatter.format(" at %s%s%n", StackTraceElements.forType((Class<?>) source), modules); } else if (source instanceof Member) { formatter.format(" at %s%s%n", StackTraceElements.forMember((Member) source), modules); } else if (source instanceof TypeLiteral) { formatter.format(" while locating %s%s%n", source, modules); } else if (source instanceof Key) { Key<?> key = (Key<?>) source; formatter.format(" while locating %s%n", convert(key, elementSource)); } else if (source instanceof Thread) { formatter.format(" in thread %s%n", source); } else { formatter.format(" at %s%s%n", source, modules); } }
static String formatParameter(Dependency<?> dependency) { int ordinal = dependency.getParameterIndex() + 1; return String.format( "the %s%s parameter of %s", ordinal, getOrdinalSuffix(ordinal), StackTraceElements.forMember(dependency.getInjectionPoint().getMember())); }
private String methodName(Class<? extends Annotation> annotation, String method, Object container) throws Exception { return "@" + annotation.getName() + " " + StackTraceElements.forMember(container.getClass().getDeclaredMethod(method)); }
/** * Returns a name for a Guice "source" object. This will typically be either * a {@link StackTraceElement} for when the binding is made to the instance, * or a {@link Method} when a provider method is used. */ public String getSourceName(Object source) { if (source instanceof ElementSource) { source = ((ElementSource) source).getDeclaringSource(); } if (source instanceof Method) { source = StackTraceElements.forMember((Method) source); } if (source instanceof StackTraceElement) { return getFileString((StackTraceElement) source); } return stripPackages(source.toString()); }
/** * Records the elements executed by {@code modules}. */ public static List<Element> getElements(Stage stage, Iterable<? extends Module> modules) { RecordingBinder binder = new RecordingBinder(stage); for (Module module : modules) { binder.install(module); } // Free the memory consumed by the stack trace elements cache StackTraceElements.clearCache(); binder.rehashKeys(); return Collections.unmodifiableList(binder.elements); }
/** * Returns a string describing where this dependency was bound. If the binding * was just-in-time, there is no valid binding source, so this describes the * class in question. */ public String getBindingSource() { if (source instanceof Class) { return StackTraceElements.forType((Class) source).toString(); } else if (source instanceof Member) { return StackTraceElements.forMember((Member) source).toString(); } else { return source.toString(); } }
/** * Creates a new {@link ModuleSource} Object. * @param parent the parent module {@link ModuleSource source} * @param module the corresponding module * @param partialCallStack the chunk of call stack that starts from the parent module {@link * Module#configure(Binder) configure(Binder)} call and ends just before the module {@link * Module#configure(Binder) configure(Binder)} method invocation */ private ModuleSource( /* @Nullable */ ModuleSource parent, Module module, StackTraceElement[] partialCallStack) { Preconditions.checkNotNull(module, "module cannot be null."); Preconditions.checkNotNull(partialCallStack, "partialCallStack cannot be null."); this.parent = parent; this.moduleClassName = module.getClass().getName(); this.partialCallStack = StackTraceElements.convertToInMemoryStackTraceElement(partialCallStack); }
/** * Creates a new {@ElementSource} from the given parameters. * @param originalElementSource The source of element that this element created from (if there is * any), otherwise {@code null}. * @param declaringSource the source (in)directly declared the element. * @param moduleSource the moduleSource when the element is bound * @param partialCallStack the partial call stack from the top module to where the element is * bound */ ElementSource(/* @Nullable */ ElementSource originalSource, Object declaringSource, ModuleSource moduleSource, StackTraceElement[] partialCallStack) { Preconditions.checkNotNull(declaringSource, "declaringSource cannot be null."); Preconditions.checkNotNull(moduleSource, "moduleSource cannot be null."); Preconditions.checkNotNull(partialCallStack, "partialCallStack cannot be null."); this.originalElementSource = originalSource; this.declaringSource = declaringSource; this.moduleSource = moduleSource; this.partialCallStack = StackTraceElements.convertToInMemoryStackTraceElement(partialCallStack); }
/** * Returns the sequence of method calls that ends at one of {@link Binder} {@code bindXXX()} * methods and eventually defines the element. Note that {@link #getStackTrace()} lists {@link * StackTraceElement StackTraceElements} 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. */ public StackTraceElement[] getStackTrace() { int modulesCallStackSize = moduleSource.getStackTraceSize(); int chunkSize = partialCallStack.length; int size = moduleSource.getStackTraceSize() + chunkSize; StackTraceElement[] callStack = new StackTraceElement[size]; System.arraycopy( StackTraceElements.convertToStackTraceElement(partialCallStack), 0, callStack, 0, chunkSize); System.arraycopy(moduleSource.getStackTrace(), 0, callStack, chunkSize, modulesCallStackSize); return callStack; }
public static void formatSource(Formatter formatter, Object source, ElementSource elementSource) { String modules = moduleSourceString(elementSource); if (source instanceof Dependency) { Dependency<?> dependency = (Dependency<?>) source; InjectionPoint injectionPoint = dependency.getInjectionPoint(); if (injectionPoint != null) { formatInjectionPoint(formatter, dependency, injectionPoint, elementSource); } else { formatSource(formatter, dependency.getKey(), elementSource); } } else if (source instanceof InjectionPoint) { formatInjectionPoint(formatter, null, (InjectionPoint) source, elementSource); } else if (source instanceof Class) { formatter.format(" at %s%s%n", StackTraceElements.forType((Class<?>) source), modules); } else if (source instanceof Member) { formatter.format(" at %s%s%n", StackTraceElements.forMember((Member) source), modules); } else if (source instanceof TypeLiteral) { formatter.format(" while locating %s%s%n", source, modules); } else if (source instanceof Key) { Key<?> key = (Key<?>) source; formatter.format(" while locating %s%n", convert(key, elementSource)); } else { formatter.format(" at %s%s%n", source, modules); } }
/** * Returns a name for a Guice "source" object. This will typically be either * a {@link StackTraceElement} for when the binding is made to the instance, * or a {@link Method} when a provider method is used. */ public String getSourceName(Object source) { if (source instanceof Method) { source = StackTraceElements.forMember((Method) source); } if (source instanceof StackTraceElement) { return getFileString((StackTraceElement) source); } return stripPackages(source.toString()); }
public static void formatSource(Formatter formatter, Object source) { if (source instanceof Dependency) { Dependency<?> dependency = (Dependency<?>) source; InjectionPoint injectionPoint = dependency.getInjectionPoint(); if (injectionPoint != null) { formatInjectionPoint(formatter, dependency, injectionPoint); } else { formatSource(formatter, dependency.getKey()); } } else if (source instanceof InjectionPoint) { formatInjectionPoint(formatter, null, (InjectionPoint) source); } else if (source instanceof Class) { formatter.format(" at %s%n", StackTraceElements.forType((Class<?>) source)); } else if (source instanceof Member) { formatter.format(" at %s%n", StackTraceElements.forMember((Member) source)); } else if (source instanceof TypeLiteral) { formatter.format(" while locating %s%n", source); } else if (source instanceof Key) { Key<?> key = (Key<?>) source; formatter.format(" while locating %s%n", convert(key)); } else { formatter.format(" at %s%n", source); } }
public StackTrace(StackTraceElement[] trace, Set<Class<?>> skip) { this.trace = StackTraceElements.convertToInMemoryStackTraceElement(trace); this.skip = skip; }
public StackTraceElement[] trace() { return StackTraceElements.convertToStackTraceElement(trace); }
@Override public String toString() { return "@CheckedProvides " + StackTraceElements.forMember(method); }
public void testGetSourceName_stackTraceElement() throws Exception { StackTraceElement element = (StackTraceElement) StackTraceElements.forMember(Obj.class.getField("field")); assertEquals("Stack trace element should be identified by its file name and line number", "ShortNameFactoryTest.java:52", nameFactory.getSourceName(element)); }
@Override public String toString() { return "@CheckedProvides " + StackTraceElements.forMember(method).toString(); }
@Override public String toString() { return "@Provides " + StackTraceElements.forMember(method).toString(); }
/** * Creates a new {@link ModuleSource} Object. * * @param parent the parent module {@link ModuleSource source} * @param module the corresponding module * @param partialCallStack the chunk of call stack that starts from the parent module {@link * Module#configure(Binder) configure(Binder)} call and ends just before the module {@link * Module#configure(Binder) configure(Binder)} method invocation */ private ModuleSource( /* @Nullable */ ModuleSource parent, Object module, StackTraceElement[] partialCallStack) { Preconditions.checkNotNull(module, "module cannot be null."); Preconditions.checkNotNull(partialCallStack, "partialCallStack cannot be null."); this.parent = parent; this.moduleClassName = module.getClass().getName(); this.partialCallStack = StackTraceElements.convertToInMemoryStackTraceElement(partialCallStack); }
/** * Returns the chunk of call stack that starts from the parent module {@link * Module#configure(Binder) configure(Binder)} call and ends just before the module {@link * Module#configure(Binder) configure(Binder)} method invocation. The return array is non-empty * only if stack trace collection is on. */ StackTraceElement[] getPartialCallStack() { return StackTraceElements.convertToStackTraceElement(partialCallStack); }