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 java.util.List;

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 and 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 (see {@link #withArgs(Object...)}). 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); } /** * Builds and 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 (see {@link #withArgs(Object...)}). 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()); } /** * 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 parameters 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); } /** * Interface defining a function taking no parameters. * * @param the result data type. */ public interface Function0 { /** * Calls this function. * * @return the result. */ OUTPUT call(); } /** * Interface defining a function taking a single parameter. * * @param the first parameter type. * @param the result data type. */ public interface Function1 { /** * Calls this function. * * @param param1 the first parameter. * @return the result. */ OUTPUT call(INPUT1 param1); } /** * Interface defining a function taking two parameters. * * @param the first parameter type. * @param the second parameter type. * @param the result data type. */ public interface Function2 { /** * Calls this function. * * @param param1 the first parameter. * @param param2 the second parameter. * @return the result. */ OUTPUT call(INPUT1 param1, INPUT2 param2); } /** * Interface defining a function taking three parameters. * * @param the first parameter type. * @param the second parameter type. * @param the third parameter type. * @param the result data type. */ public interface Function3 { /** * Calls this function. * * @param param1 the first parameter. * @param param2 the second parameter. * @param param3 the third parameter. * @return the result. */ OUTPUT call(INPUT1 param1, INPUT2 param2, INPUT3 param3); } /** * Interface defining a function taking four parameters. * * @param the first parameter type. * @param the second parameter type. * @param the third parameter type. * @param the fourth parameter type. * @param the result data type. */ public interface Function4 { /** * Calls this function. * * @param param1 the first parameter. * @param param2 the second parameter. * @param param3 the third parameter. * @param param4 the fourth parameter. * @return the result. */ OUTPUT call(INPUT1 param1, INPUT2 param2, INPUT3 param3, INPUT4 param4); } /** * Interface defining a function taking an undefined number of parameters. * * @param the parameters type. * @param the result data type. */ public interface FunctionN { /** * Calls this function. * * @param params the list of parameters. * @return the result. */ OUTPUT call(@Nonnull final List params); } /** * 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.clone(); } /** * Builds and 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 and 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 public Invocation newInvocation() { try { return mConstructor.newInstance(mArgs); } catch (final RoutineException e) { throw e; } catch (final Throwable t) { throw new InvocationException(t); } } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy