本文整理汇总了Java中com.google.common.reflect.Invokable.from方法的典型用法代码示例。如果您正苦于以下问题:Java Invokable.from方法的具体用法?Java Invokable.from怎么用?Java Invokable.from使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.common.reflect.Invokable
的用法示例。
在下文中一共展示了Invokable.from方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testAllAuroraSchedulerManagerIfaceMethodsHaveAuthorizingParam
import com.google.common.reflect.Invokable; //导入方法依赖的package包/类
@Test
public void testAllAuroraSchedulerManagerIfaceMethodsHaveAuthorizingParam() throws Exception {
for (Method declaredMethod : AuroraSchedulerManager.Iface.class.getDeclaredMethods()) {
Invokable<?, ?> invokable = Invokable.from(declaredMethod);
Collection<Parameter> parameters = invokable.getParameters();
Invokable<?, ?> annotatedInvokable = Invokable.from(
AnnotatedAuroraAdmin.class.getDeclaredMethod(
invokable.getName(),
FluentIterable.from(parameters)
.transform(input -> input.getType().getRawType())
.toList()
.toArray(new Class<?>[0])));
Collection<Parameter> annotatedParameters = Collections2.filter(
annotatedInvokable.getParameters(),
input -> input.getAnnotation(AuthorizingParam.class) != null);
assertFalse(
"Method " + invokable + " should have at least 1 " + AuthorizingParam.class.getName()
+ " annotation but none were found.",
annotatedParameters.isEmpty());
}
}
示例2: getBackingInvokable
import com.google.common.reflect.Invokable; //导入方法依赖的package包/类
public Invokable<?, ?> getBackingInvokable() {
//We don't call this very often (if at all), so look it up every time
//rather than burn a field on all Methods.
Class<?> klass = getParent().getBackingClass();
if (klass == null) return null;
MethodType type = getType();
if (hasReceiver())
type = type.dropFirstArgument();
List<Class<?>> paramTypes = new ArrayList<>();
for (RegularType t : type.getParameterTypes()) {
Class<?> backingParamClass = t.getKlass().getBackingClass();
//Live Methods can only have live Classes as parameter types.
if (backingParamClass == null) return null;
paramTypes.add(backingParamClass);
}
try {
Class<?>[] array = paramTypes.toArray(new Class<?>[paramTypes.size()]);
if (getName().equals("<init>"))
return Invokable.from(klass.getDeclaredConstructor(array));
else
return Invokable.from(klass.getDeclaredMethod(getName(), array));
} catch (NoSuchMethodException ex) {
throw new AssertionError(String.format("Can't happen! Class %s doesn't have a %s(%s) method?", klass, getName(), paramTypes), ex);
}
}
示例3: invokable
import com.google.common.reflect.Invokable; //导入方法依赖的package包/类
private static Invokable<?, ?> invokable(@Nullable Object instance, Method method) {
if (instance == null) {
return Invokable.from(method);
} else {
return TypeToken.of(instance.getClass()).method(method);
}
}
示例4: forAllPublicStaticMethods
import com.google.common.reflect.Invokable; //导入方法依赖的package包/类
/**
* Returns an object responsible for performing sanity tests against the return values
* of all public static methods declared by {@code cls}, excluding superclasses.
*/
public FactoryMethodReturnValueTester forAllPublicStaticMethods(Class<?> cls) {
ImmutableList.Builder<Invokable<?, ?>> builder = ImmutableList.builder();
for (Method method : cls.getDeclaredMethods()) {
Invokable<?, ?> invokable = Invokable.from(method);
invokable.setAccessible(true);
if (invokable.isPublic() && invokable.isStatic() && !invokable.isSynthetic()) {
builder.add(invokable);
}
}
return new FactoryMethodReturnValueTester(cls, builder.build(), "public static methods");
}
示例5: have_updateState_method_visible_to_package_only
import com.google.common.reflect.Invokable; //导入方法依赖的package包/类
@Test
public void have_updateState_method_visible_to_package_only() throws NoSuchMethodException {
boolean methodFound = false;
final Method[] methods = AbstractVersionableEntity.class.getDeclaredMethods();
for (Method method : methods) {
if ("updateState".equals(method.getName())) {
final Invokable<?, Object> updateState = Invokable.from(method);
assertTrue(updateState.isPackagePrivate());
methodFound = true;
}
}
assertTrue("Cannot check 'updateState(...)' in " + AbstractVersionableEntity.class,
methodFound);
}
示例6: invokableFor
import com.google.common.reflect.Invokable; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
@Override
protected Invokable<R, R> invokableFor(Class<A> argumentClass) {
for (Method method : instanceClass.getDeclaredMethods()) {
if (Modifier.isPublic(method.getModifiers()) && Modifier.isStatic(method.getModifiers())
&& method.getReturnType().isAssignableFrom(instanceClass) && method.getParameterTypes().length == 1
&& method.getParameterTypes()[0].isAssignableFrom(argumentClass)) {
return (Invokable<R, R>) Invokable.from(method);
}
}
throw new IllegalArgumentException("No static factory method found in class '" + instanceClass
+ "' for argument type '" + argumentClass + "'");
}
示例7: invokableFor
import com.google.common.reflect.Invokable; //导入方法依赖的package包/类
@Override
protected Invokable<R, R> invokableFor(Class<A> constructorArgumentClass) {
try {
return Invokable.from(instanceClass.getConstructor(constructorArgumentClass));
} catch (NoSuchMethodException | SecurityException e) {
throw new IllegalArgumentException("The class '" + instanceClass
+ "' does not have a public constructor requiring exactly one argument of type '"
+ constructorArgumentClass + "'.", e);
}
}
示例8: invokable
import com.google.common.reflect.Invokable; //导入方法依赖的package包/类
private static Invokable<?, ?> invokable(@NullableDecl Object instance, Method method) {
if (instance == null) {
return Invokable.from(method);
} else {
return TypeToken.of(instance.getClass()).method(method);
}
}
示例9: forAllPublicStaticMethods
import com.google.common.reflect.Invokable; //导入方法依赖的package包/类
/**
* Returns an object responsible for performing sanity tests against the return values of all
* public static methods declared by {@code cls}, excluding superclasses.
*/
public FactoryMethodReturnValueTester forAllPublicStaticMethods(Class<?> cls) {
ImmutableList.Builder<Invokable<?, ?>> builder = ImmutableList.builder();
for (Method method : cls.getDeclaredMethods()) {
Invokable<?, ?> invokable = Invokable.from(method);
invokable.setAccessible(true);
if (invokable.isPublic() && invokable.isStatic() && !invokable.isSynthetic()) {
builder.add(invokable);
}
}
return new FactoryMethodReturnValueTester(cls, builder.build(), "public static methods");
}
示例10: testSynchronousParser
import com.google.common.reflect.Invokable; //导入方法依赖的package包/类
@Test
public void testSynchronousParser() throws NoSuchMethodException {
Invokable<?, Object> testSyncronousMethod = Invokable.from(this.getClass().getDeclaredMethod("testSyncronousMethod"));
Invokable<?, Object> testAsyncronousMethod = Invokable.from(this.getClass().getDeclaredMethod("testAsyncronousMethod"));
Invokable<?, Object> testVoidMethod = Invokable.from(this.getClass().getDeclaredMethod("testVoidMethod"));
System.out.println(isAsynchronous(testSyncronousMethod));
SmartAssert.assertSoft(isAsynchronous(testSyncronousMethod), is(false), "Incorrect synchronous method detection");
SmartAssert.assertSoft(isAsynchronous(testAsyncronousMethod), is(true), "Incorrect asynchronous method detection");
SmartAssert.assertSoft(isAsynchronous(testVoidMethod), is(false), "Incorrect void method detection");
}
示例11: lambda
import com.google.common.reflect.Invokable; //导入方法依赖的package包/类
/**
* Implement the given functional interface by delegating to the given
* method and target object. The signature of the method is verified to
* be compatible with the interface, using the same rules as lexical
* method references.
*/
public static <T, U> T lambda(TypeToken<T> samType, Method implMethod, @Nullable U target) {
final boolean instance = !Members.isStatic(implMethod);
if(instance) {
Preconditions.checkNotNull(target);
} else {
checkArgument(target == null);
}
final MethodHandles.Lookup lookup;
final MethodType callSiteType;
final Invokable<U, Object> implInvokable;
if(instance) {
lookup = MethodHandleUtils.privateLookup(target.getClass());
callSiteType = MethodType.methodType(samType.getRawType(), target.getClass());
implInvokable = (Invokable<U, Object>) TypeToken.of(target.getClass()).method(implMethod);
} else {
lookup = MethodHandleUtils.privateLookup(implMethod.getDeclaringClass());
callSiteType = MethodType.methodType(samType.getRawType());
implInvokable = (Invokable<U, Object>) Invokable.from(implMethod);
}
final Method samMethod = samMethod(samType.getRawType());
final MethodType samMethodType = methodType(samMethod);
final Invokable<T, Object> samInvokable = samType.method(samMethod);
final MethodType samInvokableType = methodType(samInvokable);
final String error = invocationFailureReason(samInvokable, implInvokable);
if(error != null) {
throw new MethodFormException(implMethod, "could not be adapted to functional interface " + samType + ": " + error);
}
try {
final MethodHandle factory = LambdaMetafactory.metafactory(
lookup,
samMethod.getName(),
callSiteType,
samMethodType,
lookup.unreflect(implMethod),
samInvokableType
).getTarget();
return (T) (instance ? factory.invoke(target) : factory.invoke());
} catch(Throwable e) {
throw new MethodFormException(implMethod, "could not be adapted to functional interface " + samType, e);
}
}
示例12: allNonDeprecatedMethodsTakeOptions
import com.google.common.reflect.Invokable; //导入方法依赖的package包/类
@Test
public void allNonDeprecatedMethodsTakeOptions() throws IOException, NoSuchMethodException {
for (Class aClass : getAllModels()) {
HashSet<Class<?>> interfaces = new HashSet<Class<?>>(Arrays.<Class<?>>asList(aClass.getInterfaces()));
for (Method method : aClass.getMethods()) {
// Skip methods not declared on the base class.
if (method.getDeclaringClass() != aClass) {
continue;
}
// Skip equals
if (method.getName().equals("equals")) {
continue;
}
// Skip setters
if (method.getName().startsWith("set")) {
continue;
}
// Skip getters
if (method.getName().startsWith("get")) {
continue;
}
// If more than one method with the same parameter types is declared in a class, and one of these
// methods has a return type that is more specific than any of the others, that method is returned;
// otherwise one of the methods is chosen arbitrarily.
Method mostSpecificMethod = aClass.getDeclaredMethod(method.getName(), method.getParameterTypes());
if (!method.equals(mostSpecificMethod)) {
continue;
}
Invokable<?, Object> invokable = Invokable.from(method);
// Skip private methods.
if (invokable.isPrivate()) {
continue;
}
// Skip deprecated methods - we need to keep them around, but aren't asserting their type.
if (invokable.isAnnotationPresent(Deprecated.class)) {
continue;
}
ImmutableList<Parameter> parameters = invokable.getParameters();
// Skip empty parameter lists - assume the author is using default values for the RequestOptions
if (parameters.isEmpty()) {
continue;
}
Parameter lastParam = parameters.get(parameters.size() - 1);
Class<?> finalParamType = lastParam.getType().getRawType();
// Skip methods that have exactly one param which is a map.
if (Map.class.equals(finalParamType) && parameters.size() == 1) {
continue;
}
// Skip `public static Foo retrieve(String id) {...` helper methods
if (String.class.equals(finalParamType) && parameters.size() == 1 && "retrieve".equals(method.getName())) {
continue;
}
// Skip the `public static Card createCard(String id) {...` helper method on Customer.
if (String.class.equals(finalParamType) && parameters.size() == 1 && "createCard".equals(method.getName())) {
continue;
}
if (RequestOptions.class.isAssignableFrom(finalParamType)) {
continue;
}
Assert.assertTrue(
String.format("Methods on %ss like %s.%s should take a final parameter as a %s parameter.%n", APIResource.class.getSimpleName(), aClass.getSimpleName(), method.getName(), RequestOptions.class.getSimpleName()),
RequestOptions.class.isAssignableFrom(finalParamType));
}
}
}
示例13: getDataFromCsvFile
import com.google.common.reflect.Invokable; //导入方法依赖的package包/类
/**
* Expects a csv file with headers matching those of the input attributes and output attributes.
* For example, given an input object with x attribute and output object with y attribute the csv file would be:
*
* input.x,output.y
* 1,2
* 3,4
*
*/
@DataProvider(name = "csv")
public static Iterator<Object[]> getDataFromCsvFile(Method testMethod) throws IOException {
Invokable<?, Object> invokable = Invokable.from(testMethod);
final Class<?> inputClass = invokable.getParameters().get(0).getType().getRawType();
final Class<?> outputClass = invokable.getParameters().get(1).getType().getRawType();
return csvToContracts(inputClass, outputClass, getCsvIterator(invokable));
}