All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opensearch.common.inject.assistedinject.FactoryProvider Maven / Gradle / Ivy

There is a newer version: 2.18.0
Show newest version
/*
 * SPDX-License-Identifier: Apache-2.0
 *
 * The OpenSearch Contributors require contributions made to
 * this file be licensed under the Apache-2.0 license or a
 * compatible open source license.
 */

/*
 * Copyright (C) 2007 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Modifications Copyright OpenSearch Contributors. See
 * GitHub history for details.
 */

package org.opensearch.common.inject.assistedinject;

import org.opensearch.common.inject.ConfigurationException;
import org.opensearch.common.inject.Inject;
import org.opensearch.common.inject.Injector;
import org.opensearch.common.inject.Provider;
import org.opensearch.common.inject.TypeLiteral;
import org.opensearch.common.inject.internal.Errors;
import org.opensearch.common.inject.spi.Dependency;
import org.opensearch.common.inject.spi.HasDependencies;
import org.opensearch.common.inject.spi.Message;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import static java.util.Collections.singleton;
import static java.util.Collections.unmodifiableSet;

/**
 * Provides a factory that combines the caller's arguments with injector-supplied values to
 * construct objects.
 * 

Defining a factory

* Create an interface whose methods return the constructed type, or any of its supertypes. The * method's parameters are the arguments required to build the constructed type. *
public interface PaymentFactory {
 *   Payment create(Date startDate, Money amount);
 * }
* You can name your factory methods whatever you like, such as create, createPayment * or newPayment. *

Creating a type that accepts factory parameters

* {@code constructedType} is a concrete class with an {@literal @}{@link Inject}-annotated * constructor. In addition to injector-supplied parameters, the constructor should have * parameters that match each of the factory method's parameters. Each factory-supplied parameter * requires an {@literal @}{@link Assisted} annotation. This serves to document that the parameter * is not bound by your application's modules. *
public class RealPayment implements Payment {
 *   {@literal @}Inject
 *   public RealPayment(
 *      CreditService creditService,
 *      AuthService authService,
 *      {@literal @}Assisted Date startDate,
 *      {@literal @}Assisted Money amount) {
 *     ...
 *   }
 * }
* Any parameter that permits a null value should also be annotated {@code @Nullable}. *

Configuring factories

* In your {@link org.opensearch.common.inject.Module module}, bind the factory interface to the returned * factory: *
bind(PaymentFactory.class).toProvider(
 *     FactoryProvider.newFactory(PaymentFactory.class, RealPayment.class));
* As a side-effect of this binding, Guice will inject the factory to initialize it for use. The * factory cannot be used until the injector has been initialized. *

Using the factory

* Inject your factory into your application classes. When you use the factory, your arguments * will be combined with values from the injector to construct an instance. *
public class PaymentAction {
 *   {@literal @}Inject private PaymentFactory paymentFactory;
 *   public void doPayment(Money amount) {
 *     Payment payment = paymentFactory.create(new Date(), amount);
 *     payment.apply();
 *   }
 * }
*

Making parameter types distinct

* The types of the factory method's parameters must be distinct. To use multiple parameters of * the same type, use a named {@literal @}{@link Assisted} annotation to disambiguate the * parameters. The names must be applied to the factory method's parameters: *
public interface PaymentFactory {
 *   Payment create(
 *       {@literal @}Assisted("startDate") Date startDate,
 *       {@literal @}Assisted("dueDate") Date dueDate,
 *       Money amount);
 * } 
* ...and to the concrete type's constructor parameters: *
public class RealPayment implements Payment {
 *   {@literal @}Inject
 *   public RealPayment(
 *      CreditService creditService,
 *      AuthService authService,
 *      {@literal @}Assisted("startDate") Date startDate,
 *      {@literal @}Assisted("dueDate") Date dueDate,
 *      {@literal @}Assisted Money amount) {
 *     ...
 *   }
 * }
*

Values are created by Guice

* Returned factories use child injectors to create values. The values are eligible for method * interception. In addition, {@literal @}{@literal Inject} members will be injected before they are * returned. *

Backwards compatibility using {@literal @}AssistedInject

* Instead of the {@literal @}Inject annotation, you may annotate the constructed classes with * {@literal @}{@link AssistedInject}. This triggers a limited backwards-compatibility mode. *

Instead of matching factory method arguments to constructor parameters using their names, the * parameters are matched by their order. The first factory method argument is * used for the first {@literal @}Assisted constructor parameter, etc.. Annotation names have no * effect. *

Returned values are not created by Guice. These types are not eligible for * method interception. They do receive post-construction member injection. * * @param The factory interface * @author [email protected] (Jerome Mourits) * @author [email protected] (Jesse Wilson) * @author [email protected] (Daniel Martin) * * @opensearch.internal */ public class FactoryProvider implements Provider, HasDependencies { /* * This class implements the old @AssistedInject implementation that manually matches constructors * to factory methods. */ private Injector injector; private final TypeLiteral factoryType; private final Map> factoryMethodToConstructor; private FactoryProvider(TypeLiteral factoryType, Map> factoryMethodToConstructor) { this.factoryType = factoryType; this.factoryMethodToConstructor = factoryMethodToConstructor; checkDeclaredExceptionsMatch(); } private void checkDeclaredExceptionsMatch() { for (Map.Entry> entry : factoryMethodToConstructor.entrySet()) { for (Class constructorException : entry.getValue().getDeclaredExceptions()) { if (!isConstructorExceptionCompatibleWithFactoryExeception(constructorException, entry.getKey().getExceptionTypes())) { throw newConfigurationException( "Constructor %s declares an exception, but no compatible " + "exception is thrown by the factory method %s", entry.getValue(), entry.getKey() ); } } } } private boolean isConstructorExceptionCompatibleWithFactoryExeception(Class constructorException, Class[] factoryExceptions) { for (Class factoryException : factoryExceptions) { if (factoryException.isAssignableFrom(constructorException)) { return true; } } return false; } @Override public Set> getDependencies() { Set> dependencies = new HashSet<>(); for (AssistedConstructor constructor : factoryMethodToConstructor.values()) { for (Parameter parameter : constructor.getAllParameters()) { if (!parameter.isProvidedByFactory()) { dependencies.add(Dependency.get(parameter.getPrimaryBindingKey())); } } } return unmodifiableSet(dependencies); } @Override public F get() { InvocationHandler invocationHandler = new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] creationArgs) throws Throwable { // pass methods from Object.class to the proxy if (method.getDeclaringClass().equals(Object.class)) { return method.invoke(this, creationArgs); } AssistedConstructor constructor = factoryMethodToConstructor.get(method); Object[] constructorArgs = gatherArgsForConstructor(constructor, creationArgs); Object objectToReturn = constructor.newInstance(constructorArgs); injector.injectMembers(objectToReturn); return objectToReturn; } public Object[] gatherArgsForConstructor(AssistedConstructor constructor, Object[] factoryArgs) { int numParams = constructor.getAllParameters().size(); int argPosition = 0; Object[] result = new Object[numParams]; for (int i = 0; i < numParams; i++) { Parameter parameter = constructor.getAllParameters().get(i); if (parameter.isProvidedByFactory()) { result[i] = factoryArgs[argPosition]; argPosition++; } else { result[i] = parameter.getValue(injector); } } return result; } }; @SuppressWarnings("unchecked") // we imprecisely treat the class literal of T as a Class Class factoryRawType = (Class) factoryType.getRawType(); return factoryRawType.cast( Proxy.newProxyInstance(factoryRawType.getClassLoader(), new Class[] { factoryRawType }, invocationHandler) ); } private static ConfigurationException newConfigurationException(String format, Object... args) { return new ConfigurationException(singleton(new Message(Errors.format(format, args)))); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy