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

com.gh.bmd.jrt.invocation.Invocations Maven / Gradle / Ivy

There is a newer version: 5.9.0
Show newest version
/**
 * 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.
 */
package com.gh.bmd.jrt.invocation;

import com.gh.bmd.jrt.common.ClassToken;
import com.gh.bmd.jrt.common.InvocationException;
import com.gh.bmd.jrt.common.Reflection;
import com.gh.bmd.jrt.common.RoutineException;

import java.lang.reflect.Constructor;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import static com.gh.bmd.jrt.common.Reflection.findConstructor;

/**
 * Utility class for creating invocation factory objects.
 * 

* Created by davide on 2/12/15. */ public class Invocations { /** * Avoid direct instantiation. */ protected Invocations() { } /** * Builds an returns a new invocation factory creating instances of the specified class token. *

* Note that class tokens of inner and anonymous class can be passed as well. Remember however * that Java creates synthetic constructor for such classes, so be sure to specify the correct * arguments to guarantee proper instantiation. In fact, inner classes always have the outer * instance as first constructor parameter, and anonymous classes has both the outer instance * and all the variables captured in the closure. * * @param invocationToken the invocation class token. * @param the input data type. * @param the output data type. * @return the invocation factory. * @throws java.lang.IllegalArgumentException if no constructor taking the specified objects as * parameters was found. */ @Nonnull public static InvocationFactory factoryOf( @Nonnull final ClassToken> invocationToken) { return factoryOf(invocationToken.getRawClass()); } /** * Builds an returns a new invocation factory always returning the specified invocation * instance.
* Note that, in order to avoid unexpected behaviors, the invocation instance must be stateless. * * @param invocation the invocation instance. * @param the input data type. * @param the output data type. * @return the invocation factory. */ @Nonnull @SuppressWarnings("unchecked") public static InvocationFactory factoryOf( @Nonnull final StatelessInvocation invocation) { return new StatelessInvocationFactory(invocation); } /** * Builds an returns a new invocation factory creating instances of the specified class. *

* Note that inner and anonymous classes can be passed as well. Remember however that Java * creates synthetic constructor for such classes, so be sure to specify the correct arguments * to guarantee proper instantiation. In fact, inner classes always have the outer instance as * first constructor parameter, and anonymous classes has both the outer instance and all the * variables captured in the closure. * * @param invocationClass the invocation class. * @param the input data type. * @param the output data type. * @return the invocation factory. * @throws java.lang.IllegalArgumentException if no constructor taking the specified objects as * parameters was found. */ @Nonnull public static InvocationFactory factoryOf( @Nonnull final Class> invocationClass) { return new DefaultInvocationFactory(findConstructor(invocationClass), Reflection.NO_ARGS); } /** * Creates and returns a factory builder passing the specified arguments to the invocation * constructor.
* Note that, in case no constructor taking the specified arguments as parameter is found, an * exception will be thrown. * * @param args the constructor arguments. * @return the factory builder. */ @Nonnull public static FactoryBuilder withArgs(@Nullable final Object... args) { return new FactoryBuilder(args); } /** * Implementation of a builder of invocation factories. */ public static class FactoryBuilder { private final Object[] mArgs; /** * Constructor. * * @param args the invocation constructor arguments. */ private FactoryBuilder(@Nullable final Object[] args) { mArgs = (args == null) ? Reflection.NO_ARGS : args; } /** * Builds an returns a new invocation factory creating instances of the specified class * token. *

* Note that class tokens of inner and anonymous class can be passed as well. Remember * however that Java creates synthetic constructor for such classes, so be sure to specify * the correct arguments to guarantee proper instantiation. In fact, inner classes always * have the outer instance as first constructor parameter, and anonymous classes has both * the outer instance and all the variables captured in the closure. * * @param invocationToken the invocation class token. * @param the input data type. * @param the output data type. * @return the invocation factory. * @throws java.lang.IllegalArgumentException if no constructor taking the specified objects * as parameters was found. */ @Nonnull public InvocationFactory factoryOf( @Nonnull final ClassToken> invocationToken) { return factoryOf(invocationToken.getRawClass()); } /** * Builds an returns a new invocation factory creating instances of the specified class. *

* Note that inner and anonymous classes can be passed as well. Remember however that Java * creates synthetic constructor for such classes, so be sure to specify the correct * arguments to guarantee proper instantiation. In fact, inner classes always have the outer * instance as first constructor parameter, and anonymous classes has both the outer * instance and all the variables captured in the closure. * * @param invocationClass the invocation class. * @param the input data type. * @param the output data type. * @return the invocation factory. * @throws java.lang.IllegalArgumentException if no constructor taking the specified objects * as parameters was found. */ @Nonnull public InvocationFactory factoryOf( @Nonnull final Class> invocationClass) { final Object[] args = mArgs; return new DefaultInvocationFactory( findConstructor(invocationClass, args), args); } } /** * Default implementation of an invocation factory. * * @param the input data type. * @param the output data type. */ private static class DefaultInvocationFactory implements InvocationFactory { private final Object[] mArgs; private Constructor> mConstructor; /** * Constructor. * * @param constructor the invocation constructor. * @param args the invocation constructor arguments. */ private DefaultInvocationFactory( @Nonnull final Constructor> constructor, @Nonnull final Object[] args) { mConstructor = constructor; mArgs = args; } @Nonnull @Override public Invocation newInvocation() { try { return mConstructor.newInstance(mArgs); } catch (final RoutineException e) { throw e; } catch (final Throwable t) { throw new InvocationException(t); } } } /** * Implementation of a factory of stateless invocations. * * @param the input data type. * @param the output data type. */ private static class StatelessInvocationFactory implements InvocationFactory { private final StatelessInvocation mInvocation; /** * Constructor. * * @param invocation the invocation instance. * @throws java.lang.NullPointerException if the instance is null. */ @SuppressWarnings("ConstantConditions") private StatelessInvocationFactory( @Nonnull final StatelessInvocation invocation) { if (invocation == null) { throw new NullPointerException("the invocation instance must not be null"); } mInvocation = invocation; } @Nonnull @Override public Invocation newInvocation() { return mInvocation; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy