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

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

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

Class.getDeclaredConstructors介绍

[英]Returns an array containing Constructor objects for all constructors declared in the class represented by this Class. If there are no constructors or if this Class represents an array class, a primitive type, or void then an empty array is returned.
[中]

代码示例

代码示例来源:origin: org.mockito/mockito-core

private Constructor<?> biggestConstructor(Class<?> clazz) {
    final List<? extends Constructor<?>> constructors = Arrays.asList(clazz.getDeclaredConstructors());
    Collections.sort(constructors, byParameterNumber);
    Constructor<?> constructor = constructors.get(0);
    checkParameterized(constructor, field);
    return constructor;
  }
}

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

/**
 * Runs {@link #testConstructor} on every constructor in class {@code c} that has at least {@code
 * minimalVisibility}.
 */
public void testConstructors(Class<?> c, Visibility minimalVisibility) {
 for (Constructor<?> constructor : c.getDeclaredConstructors()) {
  if (minimalVisibility.isVisible(constructor) && !isIgnored(constructor)) {
   testConstructor(constructor);
  }
 }
}

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

AnonymousClassInConstructor() {
  final int i = 1;
  final String s = "hello world";
  Class<?> anonymous =
    new Runnable() {
     @Override
     public void run() {
      System.out.println(s + i);
     }
    }.getClass();
  Constructor<?> constructor = anonymous.getDeclaredConstructors()[0];
  assertEquals(0, Invokable.from(constructor).getParameters().size());
 }
}

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

public void testPrivateClass() {
 NullPointerTester tester = new NullPointerTester();
 for (Constructor<?> constructor :
   PrivateClassWithPrivateConstructor.class.getDeclaredConstructors()) {
  tester.testConstructor(constructor);
 }
}

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

public void testAnonymousClassWithTwoParametersConstructor() {
 abstract class Base {
  @SuppressWarnings("unused") // called by reflection
  Base(String s, int i) {}
 }
 Class<?> anonymous = new Base("test", 0) {}.getClass();
 Constructor<?> constructor = anonymous.getDeclaredConstructors()[0];
 assertEquals(2, Invokable.from(constructor).getParameters().size());
}

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

public void testAnonymousClassDefaultConstructor() {
 final int i = 1;
 final String s = "hello world";
 Class<?> anonymous =
   new Runnable() {
    @Override
    public void run() {
     System.out.println(s + i);
    }
   }.getClass();
 Constructor<?> constructor = anonymous.getDeclaredConstructors()[0];
 assertEquals(0, Invokable.from(constructor).getParameters().size());
}

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

private static void doTestStaticAnonymousClassDefaultConstructor() {
 final int i = 1;
 final String s = "hello world";
 Class<?> anonymous =
   new Runnable() {
    @Override
    public void run() {
     System.out.println(s + i);
    }
   }.getClass();
 Constructor<?> constructor = anonymous.getDeclaredConstructors()[0];
 assertEquals(0, Invokable.from(constructor).getParameters().size());
}

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

public void testLocalClassDefaultConstructor() {
 final int i = 1;
 final String s = "hello world";
 class LocalWithDefaultConstructor implements Runnable {
  @Override
  public void run() {
   System.out.println(s + i);
  }
 }
 Constructor<?> constructor = LocalWithDefaultConstructor.class.getDeclaredConstructors()[0];
 assertEquals(0, Invokable.from(constructor).getParameters().size());
}

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

public void testNestedInnerClassDefaultConstructor() {
 Constructor<?> constructor =
   InnerWithDefaultConstructor.NestedInner.class.getDeclaredConstructors()[0];
 assertEquals(0, Invokable.from(constructor).getParameters().size());
}

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

public void testInnerClassDefaultConstructor() {
 Constructor<?> constructor = InnerWithDefaultConstructor.class.getDeclaredConstructors()[0];
 assertEquals(0, Invokable.from(constructor).getParameters().size());
}

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

public void testInnerClassWithAnnotatedConstructorParameter() {
 Constructor<?> constructor =
   InnerWithAnnotatedConstructorParameter.class.getDeclaredConstructors()[0];
 Invokable<?, ?> invokable = Invokable.from(constructor);
 assertEquals(1, invokable.getParameters().size());
 assertEquals(TypeToken.of(String.class), invokable.getParameters().get(0).getType());
}

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

public void testLocalClassWithAnnotatedConstructorParameter() throws Exception {
 class LocalWithAnnotatedConstructorParameter {
  @SuppressWarnings("unused") // called by reflection
  LocalWithAnnotatedConstructorParameter(@Nullable String s) {}
 }
 Constructor<?> constructor =
   LocalWithAnnotatedConstructorParameter.class.getDeclaredConstructors()[0];
 Invokable<?, ?> invokable = Invokable.from(constructor);
 assertEquals(1, invokable.getParameters().size());
 assertEquals(TypeToken.of(String.class), invokable.getParameters().get(0).getType());
}

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

public void testInnerClassWithOneParameterConstructor() {
 Constructor<?> constructor =
   InnerWithOneParameterConstructor.class.getDeclaredConstructors()[0];
 Invokable<?, ?> invokable = Invokable.from(constructor);
 assertEquals(1, invokable.getParameters().size());
 assertEquals(TypeToken.of(String.class), invokable.getParameters().get(0).getType());
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
public void testConstructor() {
  assertNotNull(new ObjectUtils());
  final Constructor<?>[] cons = ObjectUtils.class.getDeclaredConstructors();
  assertEquals(1, cons.length);
  assertTrue(Modifier.isPublic(cons[0].getModifiers()));
  assertTrue(Modifier.isPublic(ObjectUtils.class.getModifiers()));
  assertFalse(Modifier.isFinal(ObjectUtils.class.getModifiers()));
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
public void testConstructor() {
  assertNotNull(new CharSequenceUtils());
  final Constructor<?>[] cons = CharSequenceUtils.class.getDeclaredConstructors();
  assertEquals(1, cons.length);
  assertTrue(Modifier.isPublic(cons[0].getModifiers()));
  assertTrue(Modifier.isPublic(CharSequenceUtils.class.getModifiers()));
  assertFalse(Modifier.isFinal(CharSequenceUtils.class.getModifiers()));
}

代码示例来源:origin: org.apache.commons/commons-lang3

/**
 * Test that constructors are public, and work, etc.
 */
@Test
public void testConstructor() {
  assertNotNull(new LocaleUtils());
  final Constructor<?>[] cons = LocaleUtils.class.getDeclaredConstructors();
  assertEquals(1, cons.length);
  assertTrue(Modifier.isPublic(cons[0].getModifiers()));
  assertTrue(Modifier.isPublic(LocaleUtils.class.getModifiers()));
  assertFalse(Modifier.isFinal(LocaleUtils.class.getModifiers()));
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
public void testConstructor() {
  assertNotNull(new StringUtils());
  final Constructor<?>[] cons = StringUtils.class.getDeclaredConstructors();
  assertEquals(1, cons.length);
  assertTrue(Modifier.isPublic(cons[0].getModifiers()));
  assertTrue(Modifier.isPublic(StringUtils.class.getModifiers()));
  assertFalse(Modifier.isFinal(StringUtils.class.getModifiers()));
}

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

public void testLocalClassWithOneParameterConstructor() throws Exception {
 final int i = 1;
 final String s = "hello world";
 class LocalWithOneParameterConstructor {
  @SuppressWarnings("unused") // called by reflection
  public LocalWithOneParameterConstructor(String x) {
   System.out.println(s + i);
  }
 }
 Constructor<?> constructor =
   LocalWithOneParameterConstructor.class.getDeclaredConstructors()[0];
 Invokable<?, ?> invokable = Invokable.from(constructor);
 assertEquals(1, invokable.getParameters().size());
 assertEquals(TypeToken.of(String.class), invokable.getParameters().get(0).getType());
}

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

public void testInnerClassWithGenericConstructorParameter() {
 Constructor<?> constructor =
   InnerWithGenericConstructorParameter.class.getDeclaredConstructors()[0];
 Invokable<?, ?> invokable = Invokable.from(constructor);
 assertEquals(2, invokable.getParameters().size());
 assertEquals(new TypeToken<Iterable<String>>() {}, invokable.getParameters().get(0).getType());
 assertEquals(TypeToken.of(String.class), invokable.getParameters().get(1).getType());
}

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

public void testLocalClassWithGenericConstructorParameter() throws Exception {
 class LocalWithGenericConstructorParameter {
  @SuppressWarnings("unused") // called by reflection
  LocalWithGenericConstructorParameter(Iterable<String> it, String s) {}
 }
 Constructor<?> constructor =
   LocalWithGenericConstructorParameter.class.getDeclaredConstructors()[0];
 Invokable<?, ?> invokable = Invokable.from(constructor);
 assertEquals(2, invokable.getParameters().size());
 assertEquals(new TypeToken<Iterable<String>>() {}, invokable.getParameters().get(0).getType());
 assertEquals(TypeToken.of(String.class), invokable.getParameters().get(1).getType());
}

相关文章

Class类方法