@SuppressWarnings({"unchecked"}) private Constructor<T> getConstructor() { return (Constructor<T>) constructorInjectionPoint.getMember(); }
@Override public Logger get(InternalContext context, Dependency<?> dependency, boolean linked) { InjectionPoint injectionPoint = dependency.getInjectionPoint(); return injectionPoint == null ? Logger.getAnonymousLogger() : Logger.getLogger(injectionPoint.getMember().getDeclaringClass().getName()); }
private void injectValues(Object target, boolean sharedFields, Specification specInstance) throws IllegalAccessException { for (InjectionPoint point : injectionPoints) { if (!(point.getMember() instanceof Field)) throw new GuiceExtensionException("Method injection is not supported; use field injection instead"); Field field = (Field)point.getMember(); if (field.isAnnotationPresent(Shared.class) != sharedFields) continue; Object value = injector.getInstance(point.getDependencies().get(0).getKey()); if (MOCK_UTIL.isMock(value)) { MOCK_UTIL.attachMock(value, specInstance); } field.setAccessible(true); field.set(target, value); } } }
public SingleFieldInjector(InjectorImpl injector, InjectionPoint injectionPoint, Errors errors) throws ErrorsException { this.injectionPoint = injectionPoint; this.field = (Field) injectionPoint.getMember(); this.dependency = injectionPoint.getDependencies().get(0); // Ewwwww... field.setAccessible(true); binding = injector.getBindingOrThrow(dependency.getKey(), errors, JitLimitation.NO_JIT); }
@SuppressWarnings("unchecked") // the constructor promises to construct 'T's ProxyConstructor( Enhancer enhancer, InjectionPoint injectionPoint, Callback[] callbacks, ImmutableMap<Method, List<MethodInterceptor>> methodInterceptors) { this.enhanced = enhancer.createClass(); // this returns a cached class if possible this.injectionPoint = injectionPoint; this.constructor = (Constructor<T>) injectionPoint.getMember(); this.callbacks = callbacks; this.methodInterceptors = methodInterceptors; this.fastClass = newFastClassForMember(enhanced, constructor); this.constructorIndex = fastClass.getIndex(constructor.getParameterTypes()); }
SingleMethodInjector(InjectorImpl injector, InjectionPoint injectionPoint, Errors errors) throws ErrorsException { this.injectionPoint = injectionPoint; final Method method = (Method) injectionPoint.getMember(); methodInvoker = createMethodInvoker(method); parameterInjectors = injector.getParametersInjectors(injectionPoint.getDependencies(), errors); }
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 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()); } }
@Override public ConstructionProxy<T> create() { @SuppressWarnings("unchecked") // the injection point is for a constructor of T final Constructor<T> constructor = (Constructor<T>) injectionPoint.getMember(); /*if[AOP]*/ try { net.sf.cglib.reflect.FastClass fc = BytecodeGen.newFastClassForMember(constructor); if (fc != null) { int index = fc.getIndex(constructor.getParameterTypes()); // We could just fall back to reflection in this case but I believe this should actually // be impossible. Preconditions.checkArgument( index >= 0, "Could not find constructor %s in fast class", constructor); return new FastClassProxy<T>(injectionPoint, constructor, fc, index); } } catch (net.sf.cglib.core.CodeGenerationException e) { /* fall-through */ } /*end[AOP]*/ return new ReflectiveProxy<T>(injectionPoint, constructor); }
/** Returns the injectors for the specified injection points. */ ImmutableList<SingleMemberInjector> getInjectors( Set<InjectionPoint> injectionPoints, Errors errors) { List<SingleMemberInjector> injectors = Lists.newArrayList(); for (InjectionPoint injectionPoint : injectionPoints) { try { Errors errorsForMember = injectionPoint.isOptional() ? new Errors(injectionPoint) : errors.withSource(injectionPoint); SingleMemberInjector injector = injectionPoint.getMember() instanceof Field ? new SingleFieldInjector(this.injector, injectionPoint, errorsForMember) : new SingleMethodInjector(this.injector, injectionPoint, errorsForMember); injectors.add(injector); } catch (ErrorsException ignoredForNow) { // ignored for now } } return ImmutableList.copyOf(injectors); } }
Constructor<T> constructor = (Constructor<T>) injectionPoint.getMember(); declaringClass = constructor.getDeclaringClass();
throws InternalProvisionException { if (dependency.getInjectionPoint().getMember() instanceof Method) { Method annotated = (Method) dependency.getInjectionPoint().getMember(); if (annotated.isAnnotationPresent(Provides.class)) { switch (InternalFlags.getNullableProvidesOption()) { (dependency.getParameterIndex() != -1) ? Messages.formatParameter(dependency) : StackTraceElements.forMember(dependency.getInjectionPoint().getMember());
@Override @SuppressWarnings("unchecked") // the raw constructor member and declaring type always agree public void applyTo(Binder binder) { InjectionPoint constructor = getConstructor(); getScoping() .applyTo( binder .withSource(getSource()) .bind(getKey()) .toConstructor( (Constructor) getConstructor().getMember(), (TypeLiteral) constructor.getDeclaringType())); }
try { constructorInjector = InjectionPoint.forConstructorOf(key.getTypeLiteral()); if (failIfNotExplicit && !hasAtInject((Constructor) constructorInjector.getMember())) { errors.atInjectRequired(rawType); Class<?> annotatedType = constructorInjector.getMember().getDeclaringClass(); Class<? extends Annotation> scopeAnnotation = findScopeAnnotation(errors, annotatedType); if (scopeAnnotation != null) {
public <T> Key<T> prepareMethod( Binder binder, Annotation annotation, Key<T> key, InjectionPoint injectionPoint) { Method method = (Method) injectionPoint.getMember(); AnnotationOrError mapKey = findMapKeyAnnotation(binder, method); if (annotation instanceof Provides) {
if (dependency.getInjectionPoint().getMember() instanceof Constructor) { if (dependency.getParameterIndex() == 0 && dependency.getKey().equals(keyC1)) { foundC1 = true; fail("Did not expect constructor dependency with key " + dependency.getKey() + " at parameter index " + dependency.getParameterIndex()); } else if (dependency.getInjectionPoint().getMember() instanceof Method) { if (dependency.getKey().equals(keyV1)) { foundV1 = true; fail("Did not expect method dependency with key " + dependency.getKey()); } else if (dependency.getInjectionPoint().getMember() instanceof Field) { if (dependency.getKey().equals(keyF1)) { foundF1 = true;
public SingleFieldInjector(InjectorImpl injector, InjectionPoint injectionPoint, Errors errors) throws ErrorsException { this.injectionPoint = injectionPoint; this.field = (Field) injectionPoint.getMember(); this.dependency = injectionPoint.getDependencies().get(0); // Ewwwww... field.setAccessible(true); binding = injector.getBindingOrThrow(dependency.getKey(), errors, JitLimitation.NO_JIT); }
public SingleFieldInjector(InjectorImpl injector, InjectionPoint injectionPoint, Errors errors) throws ErrorsException { this.injectionPoint = injectionPoint; this.field = (Field) injectionPoint.getMember(); this.dependency = injectionPoint.getDependencies().get(0); // Ewwwww... field.setAccessible(true); binding = injector.getBindingOrThrow(dependency.getKey(), errors, JitLimitation.NO_JIT); }
SingleMethodInjector(InjectorImpl injector, InjectionPoint injectionPoint, Errors errors) throws ErrorsException { this.injectionPoint = injectionPoint; final Method method = (Method) injectionPoint.getMember(); methodInvoker = createMethodInvoker(method); parameterInjectors = injector.getParametersInjectors(injectionPoint.getDependencies(), errors); }
@SuppressWarnings("unchecked") private <T> ScopedBindingBuilder bindNewSpyProvider(Key<T> key) { TypeLiteral<T> type = key.getTypeLiteral(); InjectionPoint constructorInjectionPoint = InjectionPoint.forConstructorOf(type); Key<T> relayingKey = Key.get(type, JukitoInternal.class); bind(relayingKey).toConstructor((Constructor<T>) constructorInjectionPoint.getMember()); return bind(key).toProvider(new SpyProvider<T>(getProvider(relayingKey), relayingKey)); }