java.lang.Class.getConstructor()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(8.9k)|赞(0)|评价(0)|浏览(357)

本文整理了Java中java.lang.Class.getConstructor()方法的一些代码示例,展示了Class.getConstructor()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Class.getConstructor()方法的具体详情如下:
包路径:java.lang.Class
类名称:Class
方法名:getConstructor

Class.getConstructor介绍

[英]Returns a Constructor object which represents the public constructor matching the given parameter types. (Class[]) null is equivalent to the empty array.

See #getMethod for details of the search order. Use #getDeclaredConstructor if you don't want to search superclasses.
[中]返回一个构造函数对象,该对象表示与给定参数类型匹配的公共构造函数。(类[])null相当于空数组。
有关搜索顺序的详细信息,请参见#getMethod。如果不想搜索超类,请使用#getDeclaredConstructor。

代码示例

代码示例来源:origin: apache/incubator-dubbo

public SqlDateDeserializer(Class cl)
    throws NoSuchMethodException {
  _cl = cl;
  _constructor = cl.getConstructor(new Class[]{long.class});
}

代码示例来源:origin: apache/incubator-dubbo

private boolean isWrapperClass(Class<?> clazz) {
  try {
    clazz.getConstructor(type);
    return true;
  } catch (NoSuchMethodException e) {
    return false;
  }
}

代码示例来源:origin: apache/incubator-dubbo

public StringValueDeserializer(Class cl) {
  try {
    _cl = cl;
    _constructor = cl.getConstructor(new Class[]{String.class});
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}

代码示例来源: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: google/guava

private static @Nullable Constructor<Thread> getBigThreadConstructor() {
  try {
   return Thread.class.getConstructor(
     ThreadGroup.class, Runnable.class, String.class, long.class, boolean.class);
  } catch (Throwable t) {
   // Probably pre Java 9. We'll fall back to Thread.inheritableThreadLocals.
   return null;
  }
 }
}

代码示例来源:origin: spring-projects/spring-framework

public static Constructor findConstructor(String desc, ClassLoader loader) {
  try {
    int lparen = desc.indexOf('(');
    String className = desc.substring(0, lparen).trim();
    return getClass(className, loader).getConstructor(parseTypes(desc, loader));
  }
  catch (ClassNotFoundException | NoSuchMethodException ex) {
    throw new CodeGenerationException(ex);
  }
}

代码示例来源:origin: google/guava

public void testConstructor_notDeclaredByType() throws NoSuchMethodException {
 Constructor<String> constructor = String.class.getConstructor();
 try {
  TypeToken.of(Object.class).constructor(constructor);
  fail();
 } catch (IllegalArgumentException expected) {
 }
}

代码示例来源:origin: google/guava

public void testConstructor_declaredBySuperclass() throws NoSuchMethodException {
 Constructor<Object> constructor = Object.class.getConstructor();
 try {
  TypeToken.of(String.class).constructor(constructor);
  fail();
 } catch (IllegalArgumentException expected) {
 }
}

代码示例来源:origin: google/guava

public <T extends CannotConstruct<AssertionError>> void testConstructor_exceptionTypes()
  throws NoSuchMethodException {
 @SuppressWarnings("rawtypes") // Reflection API skew
 Constructor<CannotConstruct> constructor = CannotConstruct.class.getConstructor();
 Invokable<T, ?> invokable = new TypeToken<T>(getClass()) {}.constructor(constructor);
 assertThat(invokable.getExceptionTypes()).contains(TypeToken.of(AssertionError.class));
}

代码示例来源:origin: google/guava

public <T extends Container<String>> void testConstructor_parameterTypes()
  throws NoSuchMethodException {
 @SuppressWarnings("rawtypes") // Reflection API skew
 Constructor<Container> constructor = Container.class.getConstructor(Object.class);
 Invokable<T, ?> invokable = new TypeToken<T>(getClass()) {}.constructor(constructor);
 ImmutableList<Parameter> params = invokable.getParameters();
 assertEquals(1, params.size());
 assertEquals(TypeToken.of(String.class), params.get(0).getType());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void forConstructorParameter() throws Exception {
  Constructor<Constructors> constructor = Constructors.class.getConstructor(List.class);
  ResolvableType type = ResolvableType.forConstructorParameter(constructor, 0);
  assertThat(type.getType(), equalTo(constructor.getGenericParameterTypes()[0]));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void javaLangAnnotationTypeViaFindMergedAnnotation() throws Exception {
  Constructor<?> deprecatedCtor = Date.class.getConstructor(String.class);
  assertEquals(deprecatedCtor.getAnnotation(Deprecated.class), findMergedAnnotation(deprecatedCtor, Deprecated.class));
  assertEquals(Date.class.getAnnotation(Deprecated.class), findMergedAnnotation(Date.class, Deprecated.class));
}

代码示例来源:origin: google/guava

public void testConstructor_getOwnerType() throws NoSuchMethodException {
 @SuppressWarnings("rawtypes") // raw class ArrayList.class
 Constructor<ArrayList> constructor = ArrayList.class.getConstructor();
 assertEquals(
   TypeToken.of(ArrayList.class),
   TypeToken.of(ArrayList.class).constructor(constructor).getOwnerType());
 assertEquals(
   new TypeToken<ArrayList<String>>() {},
   new TypeToken<ArrayList<String>>() {}.constructor(constructor).getOwnerType());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void resolveTypeVariableFromConstructorParameter() throws Exception {
  Constructor<?> constructor = Constructors.class.getConstructor(List.class);
  ResolvableType type = ResolvableType.forConstructorParameter(constructor, 0);
  assertThat(type.resolve(), equalTo((Class) List.class));
  assertThat(type.resolveGeneric(0), equalTo((Class) CharSequence.class));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void resolveTypeVariableFromConstructorParameterWithImplementsClass() throws Exception {
  Constructor<?> constructor = Constructors.class.getConstructor(Map.class);
  ResolvableType type = ResolvableType.forConstructorParameter(
      constructor, 0, TypedConstructors.class);
  assertThat(type.resolve(), equalTo((Class) Map.class));
  assertThat(type.resolveGeneric(0), equalTo((Class) String.class));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void resolveUnknownTypeVariableFromConstructorParameter() throws Exception {
  Constructor<?> constructor = Constructors.class.getConstructor(Map.class);
  ResolvableType type = ResolvableType.forConstructorParameter(constructor, 0);
  assertThat(type.resolve(), equalTo((Class) Map.class));
  assertThat(type.resolveGeneric(0), nullValue());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void consParameterNameDiscoveryNoArgs() throws NoSuchMethodException {
  Constructor<TestObject> noArgsCons = TestObject.class.getConstructor();
  String[] names = discoverer.getParameterNames(noArgsCons);
  assertNotNull("should find cons info", names);
  assertEquals("no argument names", 0, names.length);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void consParameterNameDiscoveryArgs() throws NoSuchMethodException {
  Constructor<TestObject> twoArgCons = TestObject.class.getConstructor(String.class, int.class);
  String[] names = discoverer.getParameterNames(twoArgCons);
  assertNotNull("should find cons info", names);
  assertEquals("one argument", 2, names.length);
  assertEquals("name", names[0]);
  assertEquals("age", names[1]);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void serialize() throws Exception {
  testSerialization(ResolvableType.forClass(List.class));
  testSerialization(ResolvableType.forField(Fields.class.getField("charSequenceList")));
  testSerialization(ResolvableType.forMethodParameter(Methods.class.getMethod("charSequenceParameter", List.class), 0));
  testSerialization(ResolvableType.forMethodReturnType(Methods.class.getMethod("charSequenceReturn")));
  testSerialization(ResolvableType.forConstructorParameter(Constructors.class.getConstructor(List.class), 0));
  testSerialization(ResolvableType.forField(Fields.class.getField("charSequenceList")).getGeneric());
  ResolvableType deserializedNone = testSerialization(ResolvableType.NONE);
  assertThat(deserializedNone, sameInstance(ResolvableType.NONE));
}

代码示例来源:origin: google/guava

public void testConstructor_equals() throws NoSuchMethodException {
 Constructor<?> defaultConstructor = ArrayList.class.getConstructor();
 Constructor<?> oneArgConstructor = ArrayList.class.getConstructor(int.class);
 new EqualsTester()
   .addEqualityGroup(Invokable.from(defaultConstructor), Invokable.from(defaultConstructor))
   .addEqualityGroup(Invokable.from(oneArgConstructor))
   .addEqualityGroup(new TypeToken<ArrayList<Integer>>() {}.constructor(defaultConstructor))
   .addEqualityGroup(new TypeToken<ArrayList<String>>() {}.constructor(defaultConstructor))
   .addEqualityGroup(new TypeToken<ArrayList<Integer>>() {}.constructor(oneArgConstructor))
   .addEqualityGroup(new TypeToken<ArrayList<String>>() {}.constructor(oneArgConstructor))
   .testEquals();
}

相关文章

Class类方法