本文整理了Java中java.lang.Class.getMethod()
方法的一些代码示例,展示了Class.getMethod()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Class.getMethod()
方法的具体详情如下:
包路径:java.lang.Class
类名称:Class
方法名:getMethod
[英]Returns a Method object which represents the public method with the given name and parameter types. (Class[]) null is equivalent to the empty array.
This method first searches the class C represented by this Class, then the superclasses of C, and finally the interfaces implemented by C and its superclasses.
Use #getDeclaredMethod if you don't want to search superclasses.
[中]返回一个方法对象,该对象表示具有给定名称和参数类型的公共方法。(类[])null相当于空数组。
该方法首先搜索由该类表示的类C,然后搜索C的超类,最后搜索由C及其超类实现的接口。
如果不想搜索超类,请使用#getDeclaredMethod。
代码示例来源:origin: google/guava
private static boolean hasTest(Class<?> testClass, Iterable<String> testNames) {
for (String testName : testNames) {
try {
testClass.getMethod(testName);
return true;
} catch (NoSuchMethodException e) {
continue;
}
}
return false;
}
代码示例来源:origin: google/guava
private static Method getAddSuppressed() {
try {
return Throwable.class.getMethod("addSuppressed", Throwable.class);
} catch (Throwable e) {
return null;
}
}
代码示例来源:origin: google/guava
@Override
String typeName(Type type) {
try {
Method getTypeName = Type.class.getMethod("getTypeName");
return (String) getTypeName.invoke(type);
} catch (NoSuchMethodException e) {
throw new AssertionError("Type.getTypeName should be available in Java 8");
} catch (InvocationTargetException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
},
代码示例来源:origin: square/okhttp
private boolean api24IsCleartextTrafficPermitted(String hostname, Class<?> networkPolicyClass,
Object networkSecurityPolicy) throws InvocationTargetException, IllegalAccessException {
try {
Method isCleartextTrafficPermittedMethod = networkPolicyClass
.getMethod("isCleartextTrafficPermitted", String.class);
return (boolean) isCleartextTrafficPermittedMethod.invoke(networkSecurityPolicy, hostname);
} catch (NoSuchMethodException e) {
return api23IsCleartextTrafficPermitted(hostname, networkPolicyClass, networkSecurityPolicy);
}
}
代码示例来源:origin: square/okhttp
public CertificateChainCleaner buildCertificateChainCleaner(X509TrustManager trustManager) {
try {
Class<?> extensionsClass = Class.forName("android.net.http.X509TrustManagerExtensions");
Constructor<?> constructor = extensionsClass.getConstructor(X509TrustManager.class);
Object extensions = constructor.newInstance(trustManager);
Method checkServerTrusted = extensionsClass.getMethod(
"checkServerTrusted", X509Certificate[].class, String.class, String.class);
return new AndroidCertificateChainCleaner(extensions, checkServerTrusted);
} catch (Exception e) {
throw new AssertionError(e);
}
}
代码示例来源:origin: square/okhttp
private boolean api23IsCleartextTrafficPermitted(String hostname, Class<?> networkPolicyClass,
Object networkSecurityPolicy) throws InvocationTargetException, IllegalAccessException {
try {
Method isCleartextTrafficPermittedMethod = networkPolicyClass
.getMethod("isCleartextTrafficPermitted");
return (boolean) isCleartextTrafficPermittedMethod.invoke(networkSecurityPolicy);
} catch (NoSuchMethodException e) {
return super.isCleartextTrafficPermitted(hostname);
}
}
代码示例来源:origin: google/guava
@GwtIncompatible // reflection
public static Method getMethod(Class<?> clazz, String name) {
try {
return clazz.getMethod(name);
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
}
}
代码示例来源:origin: square/okhttp
@Override public boolean isCleartextTrafficPermitted(String hostname) {
try {
Class<?> networkPolicyClass = Class.forName("android.security.NetworkSecurityPolicy");
Method getInstanceMethod = networkPolicyClass.getMethod("getInstance");
Object networkSecurityPolicy = getInstanceMethod.invoke(null);
return api24IsCleartextTrafficPermitted(hostname, networkPolicyClass, networkSecurityPolicy);
} catch (ClassNotFoundException | NoSuchMethodException e) {
return super.isCleartextTrafficPermitted(hostname);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new AssertionError("unable to determine cleartext support", e);
}
}
代码示例来源:origin: ReactiveX/RxJava
ParamOverride(Class<?> clazz, int index, ParamMode mode, String name, Class<?>... arguments) {
this.clazz = clazz;
this.index = index;
this.mode = mode;
this.name = name;
this.arguments = arguments;
try {
clazz.getMethod(name, arguments);
} catch (Exception ex) {
throw new AssertionError(ex);
}
}
代码示例来源:origin: google/guava
private static Subscriber subscriber(
EventBus bus, Object target, String methodName, Class<?> eventType) {
try {
return Subscriber.create(bus, target, target.getClass().getMethod(methodName, eventType));
} catch (NoSuchMethodException e) {
throw new AssertionError(e);
}
}
代码示例来源:origin: google/guava
public void testMethod_notDeclaredByType() throws NoSuchMethodException {
Method sizeMethod = Map.class.getMethod("size");
try {
TypeToken.of(List.class).method(sizeMethod);
fail();
} catch (IllegalArgumentException expected) {
}
}
代码示例来源:origin: google/guava
public void testResolveType() throws Exception {
Method getFromList = List.class.getMethod("get", int.class);
TypeToken<?> returnType =
new TypeToken<List<String>>() {}.resolveType(getFromList.getGenericReturnType());
assertEquals(String.class, returnType.getType());
}
代码示例来源:origin: google/guava
public void testMethod_declaredBySuperclass() throws Exception {
Method toStringMethod = Object.class.getMethod("toString");
ImmutableList<String> list = ImmutableList.of("foo");
assertEquals(list.toString(), TypeToken.of(List.class).method(toStringMethod).invoke(list));
}
代码示例来源:origin: google/guava
public <T extends Number & List<String>> void testMethod_returnType_resolvedAgainstTypeBound()
throws NoSuchMethodException {
Method getMethod = List.class.getMethod("get", int.class);
Invokable<T, String> invokable =
new TypeToken<T>(getClass()) {}.method(getMethod).returning(String.class);
assertEquals(TypeToken.of(String.class), invokable.getReturnType());
}
代码示例来源:origin: google/guava
public <T extends List<String>> void testMethod_parameterTypes() throws NoSuchMethodException {
Method setMethod = List.class.getMethod("set", int.class, Object.class);
Invokable<T, ?> invokable = new TypeToken<T>(getClass()) {}.method(setMethod);
ImmutableList<Parameter> params = invokable.getParameters();
assertEquals(2, params.size());
assertEquals(TypeToken.of(int.class), params.get(0).getType());
assertEquals(TypeToken.of(String.class), params.get(1).getType());
}
代码示例来源:origin: google/guava
public void testStaticOneArgMethodsThatShouldFail() throws Exception {
for (String methodName : STATIC_ONE_ARG_METHODS_SHOULD_FAIL) {
Method method = OneArg.class.getMethod(methodName, String.class);
boolean foundProblem = false;
try {
new NullPointerTester().testMethodParameter(new OneArg(), method, 0);
} catch (AssertionFailedError expected) {
foundProblem = true;
}
assertTrue("Should report error in method " + methodName, foundProblem);
}
}
代码示例来源:origin: google/guava
public void testNonStaticOneArgMethodsThatShouldPass() throws Exception {
OneArg foo = new OneArg();
for (String methodName : NONSTATIC_ONE_ARG_METHODS_SHOULD_PASS) {
Method method = OneArg.class.getMethod(methodName, String.class);
try {
new NullPointerTester().testMethodParameter(foo, method, 0);
} catch (AssertionFailedError unexpected) {
fail("Should not have flagged method " + methodName);
}
}
}
代码示例来源:origin: google/guava
public void testTwoArgNullableNullable() throws Exception {
Method method = TwoArg.class.getMethod("nullableNullable", String.class, Integer.class);
for (TwoArg.Action first : TwoArg.Action.values()) {
for (TwoArg.Action second : TwoArg.Action.values()) {
TwoArg bar = new TwoArg(first, second);
verifyBarPass(method, bar); // All args nullable: anything goes!
}
}
}
代码示例来源:origin: google/guava
public void testMessageNoException() throws Exception {
Method method = OneArg.class.getMethod("staticOneArgShouldThrowNpeButDoesnt", String.class);
boolean foundProblem = false;
try {
new NullPointerTester().testMethodParameter(new OneArg(), method, 0);
} catch (AssertionFailedError expected) {
assertThat(expected.getMessage()).contains("index 0");
assertThat(expected.getMessage()).contains("[null]");
foundProblem = true;
}
assertTrue("Should report error when no exception is thrown", foundProblem);
}
代码示例来源:origin: google/guava
@AndroidIncompatible // Android runs ExampleDerivedInterfaceTester directly if it exists
public void testBuildTesterRequirements_methodClassConflict() throws Exception {
final Method method =
ExampleDerivedInterfaceTester.class.getMethod("testRequiringConflictingFeatures");
try {
FeatureUtil.buildTesterRequirements(method);
fail("Expected ConflictingRequirementsException");
} catch (ConflictingRequirementsException e) {
assertThat(e.getConflicts()).contains(ExampleBaseFeature.BASE_FEATURE_1);
assertEquals(method, e.getSource());
}
}
内容来源于网络,如有侵权,请联系作者删除!