Example usage for com.google.common.reflect Invokable invoke

List of usage examples for com.google.common.reflect Invokable invoke

Introduction

In this page you can find the example usage for com.google.common.reflect Invokable invoke.

Prototype


@SuppressWarnings("unchecked")
public final R invoke(@Nullable T receiver, Object... args)
        throws InvocationTargetException, IllegalAccessException 

Source Link

Document

Invokes with receiver as 'this' and args passed to the underlying method and returns the return value; or calls the underlying constructor with args and returns the constructed instance.

Usage

From source file:org.jclouds.json.internal.EnumTypeAdapterThatReturnsFromValue.java

@SuppressWarnings("cast")
public T deserialize(JsonElement json, Type classOfT, JsonDeserializationContext context)
        throws JsonParseException {
    try {/*from  w  w  w.j a  va 2  s.c  o m*/
        return (T) Enum.valueOf((Class<T>) classOfT, json.getAsString());
    } catch (IllegalArgumentException e) {
        try {
            Invokable<?, Object> converter = method((Class<?>) classOfT, "fromValue", String.class);
            return (T) converter.invoke(null, json.getAsString());
        } catch (Exception e1) {
            throw e;
        }
    }
}

From source file:org.opendaylight.ovsdb.lib.schema.DatabaseSchema.java

protected <E extends TableSchema<E>> E createTableSchema(Class<E> clazz, TableSchema<E> table) {
    Constructor<E> declaredConstructor = null;
    try {/*from  ww  w  . jav  a  2s  .co m*/
        declaredConstructor = clazz.getDeclaredConstructor(TableSchema.class);
    } catch (NoSuchMethodException e) {
        String message = String
                .format("Class %s does not have public constructor that accepts TableSchema object", clazz);
        throw new IllegalArgumentException(message, e);
    }
    Invokable<E, E> invokable = Invokable.from(declaredConstructor);
    try {
        return invokable.invoke(null, table);
    } catch (Exception e) {
        String message = String.format("Not able to create instance of class %s using public constructor "
                + "that accepts TableSchema object", clazz);
        throw new IllegalArgumentException(message, e);
    }
}

From source file:org.jclouds.rest.internal.InvokeAndCallGetOnFutures.java

@SuppressWarnings("unchecked")
@Override/*from w  ww. ja  v a2s  .c om*/
public Object apply(Invocation in) {
    @SuppressWarnings("rawtypes")
    Invokable target = sync2async.apply(in).getInvokable();
    Object returnVal;
    try {
        returnVal = target.invoke(receiver, in.getArgs().toArray());
    } catch (InvocationTargetException e) {
        throw propagate(e.getCause());
    } catch (IllegalAccessException e) {
        throw new Error("Method became inaccessible: " + toString(), e);
    }
    if (!isFuture(target))
        return returnVal;
    return getUnchecked(ListenableFuture.class.cast(returnVal));
}

From source file:net.navatwo.jfxproperties.LongPropertyInfo.java

private ObjLongConsumer<Object> setterFromMethod(Invokable<Object, Void> handle) {
    return (o, v) -> {
        try {/*from w w  w  . j ava  2  s  .c o m*/
            handle.invoke(o, v);
        } catch (InvocationTargetException | IllegalAccessException t) {
            throw new IllegalStateException(t);
        }
    };
}

From source file:net.navatwo.jfxproperties.IntegerPropertyInfo.java

private ObjIntConsumer<Object> setterFromMethod(Invokable<Object, Void> handle) {
    return (o, v) -> {
        try {/*  w  w  w. j  a  v a  2  s .co  m*/
            handle.invoke(o, v);
        } catch (InvocationTargetException | IllegalAccessException t) {
            throw new IllegalStateException(t);
        }
    };
}

From source file:net.navatwo.jfxproperties.DoublePropertyInfo.java

private ObjDoubleConsumer<Object> setterFromMethod(Invokable<Object, Void> handle) {
    return (o, v) -> {
        try {//from   ww w  .j  a v  a2  s. co  m
            handle.invoke(o, v);
        } catch (InvocationTargetException | IllegalAccessException t) {
            throw new IllegalStateException(t);
        }
    };
}

From source file:net.navatwo.jfxproperties.ObjectPropertyInfo.java

private BiConsumer<Object, V> setterFromMethod(Invokable<Object, Void> handle) {
    return (o, v) -> {
        try {//from   w w  w  .  j  a v  a 2 s.  c om
            handle.invoke(o, v);
        } catch (InvocationTargetException | IllegalAccessException t) {
            throw new IllegalStateException(t);
        }
    };
}

From source file:org.jclouds.util.TypeToken2.java

@SuppressWarnings("unchecked")
public <X, Y> TypeToken<T> where(TypeParameter2<X> typeParam1, TypeToken<X> typeArg1,
        TypeParameter2<Y> typeParam2, TypeToken<Y> typeArg2) {
    // resolving both parameters in one shot seems to work around 1635
    TypeResolver resolver = new TypeResolver();
    // where(Map) is package-private in TypeResolver
    Invokable<TypeResolver, TypeResolver> whereWithMap = Reflection2
            .<TypeResolver, TypeResolver>method(TypeResolver.class, "where", Map.class);
    try {//from w  ww  .  j  a  va2 s  . c o m
        resolver = whereWithMap.invoke(resolver, ImmutableMap.of(typeParam1.getTypeVariable(),
                typeArg1.getType(), typeParam2.getTypeVariable(), typeArg2.getType()));
    } catch (IllegalAccessException exception) {
        // should never happen
        throw new IllegalStateException(exception);
    } catch (InvocationTargetException exception) {
        // should never happen
        throw new IllegalStateException(exception);
    }
    return (TypeToken<T>) TypeToken.of(resolver.resolveType(getType()));
}