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.channel.RoutineException;
import com.gh.bmd.jrt.util.ClassToken;

import java.lang.reflect.Constructor;
import java.util.Arrays;

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

import static com.gh.bmd.jrt.util.Reflection.NO_ARGS;
import static com.gh.bmd.jrt.util.Reflection.findConstructor;

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

* Created by davide-maestroni 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. * * @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 default constructor was found. */ @Nonnull public static InvocationFactory factoryOf( @Nonnull final Class> invocationClass) { return factoryOf(invocationClass, (Object[]) null); } /** * Builds and returns a new invocation factory creating instances of the specified class by * passing the specified arguments to the class constructor. *

* Note that inner and anonymous classes can be passed as well. Remember however that Java * creates synthetic constructors 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 have both the outer instance and all the * variables captured in the closure. * * @param invocationClass the invocation class. * @param args the invocation constructor arguments. * @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, @Nullable final Object... args) { return new DefaultInvocationFactory(invocationClass, args); } /** * Builds and returns a new invocation factory creating instances of the specified class token. * * @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 default constructor was found. */ @Nonnull public static InvocationFactory factoryOf( @Nonnull final ClassToken> invocationToken) { return factoryOf(invocationToken.getRawClass()); } /** * Builds and returns a new invocation factory creating instances of the specified class token * by passing the specified arguments to the class constructor. *

* Note that class tokens of inner and anonymous classes can be passed as well. Remember however * that Java creates synthetic constructors 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 have both the outer instance * and all the variables captured in the closure. * * @param invocationToken the invocation class token. * @param args the invocation constructor arguments. * @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, @Nullable final Object... args) { return factoryOf(invocationToken.getRawClass(), args); } /** * Builds and returns a new invocation factory creating instances of the specified object. * * @param invocation the invocation instance. * @param the input data type. * @param the output data type. * @return the invocation factory. * @throws java.lang.IllegalArgumentException if no default constructor was found. */ @Nonnull @SuppressWarnings("unchecked") public static InvocationFactory factoryOf( @Nonnull final Invocation invocation) { return factoryOf((Class>) invocation.getClass()); } /** * Builds and returns a new invocation factory creating instances of the specified object by * passing the specified arguments to the class constructor. *

* Note that inner and anonymous objects can be passed as well. Remember however that Java * creates synthetic constructors 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 have both the outer instance and all the * variables captured in the closure. * * @param invocation the invocation instance. * @param args the invocation constructor arguments. * @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 @SuppressWarnings("unchecked") public static InvocationFactory factoryOf( @Nonnull final Invocation invocation, @Nullable final Object... args) { return factoryOf((Class>) invocation.getClass(), args); } /** * Default implementation of an invocation factory. * * @param the input data type. * @param the output data type. */ public static class DefaultInvocationFactory implements InvocationFactory { private final Object[] mArgs; private final Constructor> mConstructor; /** * Constructor. * * @param invocationClass the invocation class. * @param args the invocation constructor arguments. * @throws java.lang.IllegalArgumentException if no constructor taking the specified objects * as parameters was found. */ @SuppressWarnings("ConstantConditions") private DefaultInvocationFactory( @Nonnull final Class> invocationClass, @Nullable final Object[] args) { final Object[] invocationArgs = (mArgs = (args != null) ? args.clone() : NO_ARGS); mConstructor = findConstructor(invocationClass, invocationArgs); } @Override public int hashCode() { // auto-generated code int result = Arrays.hashCode(mArgs); result = 31 * result + mConstructor.hashCode(); return result; } @Override public boolean equals(final Object o) { // auto-generated code if (this == o) { return true; } if (!(o instanceof DefaultInvocationFactory)) { return false; } final DefaultInvocationFactory that = (DefaultInvocationFactory) o; return Arrays.equals(mArgs, that.mArgs) && mConstructor.equals(that.mConstructor); } @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