Tabnine Logo
Class.getDeclaredConstructor
Code IndexAdd Tabnine to your IDE (free)

How to use
getDeclaredConstructor
method
in
java.lang.Class

Best Java code snippets using java.lang.Class.getDeclaredConstructor (Showing top 20 results out of 19,818)

origin: apache/incubator-dubbo

public static boolean checkZeroArgConstructor(Class clazz) {
  try {
    clazz.getDeclaredConstructor();
    return true;
  } catch (NoSuchMethodException e) {
    return false;
  }
}
origin: apache/incubator-dubbo

public static boolean checkZeroArgConstructor(Class clazz) {
  try {
    clazz.getDeclaredConstructor();
    return true;
  } catch (NoSuchMethodException e) {
    return false;
  }
}
origin: spring-projects/spring-framework

/**
 * Obtain an accessible constructor for the given class and parameters.
 * @param clazz the clazz to check
 * @param parameterTypes the parameter types of the desired constructor
 * @return the constructor reference
 * @throws NoSuchMethodException if no such constructor exists
 * @since 5.0
 */
public static <T> Constructor<T> accessibleConstructor(Class<T> clazz, Class<?>... parameterTypes)
    throws NoSuchMethodException {
  Constructor<T> ctor = clazz.getDeclaredConstructor(parameterTypes);
  makeAccessible(ctor);
  return ctor;
}
origin: spring-projects/spring-framework

public static Constructor getConstructor(Class type, Class[] parameterTypes) {
  try {
    Constructor constructor = type.getDeclaredConstructor(parameterTypes);
    constructor.setAccessible(true);
    return constructor;
  }
  catch (NoSuchMethodException e) {
    throw new CodeGenerationException(e);
  }
}
origin: spring-projects/spring-framework

protected Constructor<?> getUserDeclaredConstructor(Constructor<?> constructor) {
  Class<?> declaringClass = constructor.getDeclaringClass();
  Class<?> userClass = ClassUtils.getUserClass(declaringClass);
  if (userClass != declaringClass) {
    try {
      return userClass.getDeclaredConstructor(constructor.getParameterTypes());
    }
    catch (NoSuchMethodException ex) {
      // No equivalent constructor on user class (superclass)...
      // Let's proceed with the given constructor as we usually would.
    }
  }
  return constructor;
}
origin: org.mockito/mockito-core

private static Constructor<?> noArgConstructorOf(Class<?> type) {
  Constructor<?> constructor;
  try {
    constructor = type.getDeclaredConstructor();
  } catch (NoSuchMethodException e) {
    throw new MockitoException("Please ensure that the type '" + type.getSimpleName() + "' has a no-arg constructor.");
  }
  return constructor;
}
origin: alibaba/druid

public static XAConnection createXAConnection(Object factory, Connection physicalConn) throws SQLException {
  try {
    if (constructor == null) {
      constructor = JdbcXAConnection.class.getDeclaredConstructor(JdbcDataSourceFactory.class, int.class,
                                JdbcConnection.class);
      constructor.setAccessible(true);
    }
    int id = getNextId(XA_DATA_SOURCE);
    return constructor.newInstance(factory, id, physicalConn);
  } catch (Exception e) {
    throw new SQLException("createXAConnection error", e);
  }
}
origin: square/retrofit

@Override Object invokeDefaultMethod(Method method, Class<?> declaringClass, Object object,
  @Nullable Object... args) throws Throwable {
 // Because the service interface might not be public, we need to use a MethodHandle lookup
 // that ignores the visibility of the declaringClass.
 Constructor<Lookup> constructor = Lookup.class.getDeclaredConstructor(Class.class, int.class);
 constructor.setAccessible(true);
 return constructor.newInstance(declaringClass, -1 /* trusted */)
   .unreflectSpecial(method, declaringClass)
   .bindTo(object)
   .invokeWithArguments(args);
}
origin: spring-projects/spring-framework

private Member resolveMember() {
  ClassLoader loader = this.clazz.getClassLoader();
  Class<?>[] argTypes = new Class<?>[this.args.length];
  for (int i = 0; i < this.args.length; i++) {
    argTypes[i] = ClassUtils.resolveClassName(this.args[i].getClassName(), loader);
  }
  try {
    if (CONSTRUCTOR.equals(this.name)) {
      return this.clazz.getDeclaredConstructor(argTypes);
    }
    return this.clazz.getDeclaredMethod(this.name, argTypes);
  }
  catch (NoSuchMethodException ex) {
    throw new IllegalStateException("Method [" + this.name +
        "] was discovered in the .class file but cannot be resolved in the class object", ex);
  }
}
origin: spring-projects/spring-framework

  private void readObject(ObjectInputStream inputStream) throws IOException, ClassNotFoundException {
    inputStream.defaultReadObject();
    try {
      if (this.methodName != null) {
        this.methodParameter = new MethodParameter(
            this.declaringClass.getDeclaredMethod(this.methodName, this.parameterTypes), this.parameterIndex);
      }
      else {
        this.methodParameter = new MethodParameter(
            this.declaringClass.getDeclaredConstructor(this.parameterTypes), this.parameterIndex);
      }
    }
    catch (Throwable ex) {
      throw new IllegalStateException("Could not find original class structure", ex);
    }
  }
}
origin: libgdx/libgdx

/** Returns a {@link Constructor} that represents the constructor for the supplied class which takes the supplied parameter
 * types. */
static public Constructor getDeclaredConstructor (Class c, Class... parameterTypes) throws ReflectionException {
  try {
    return new Constructor(c.getDeclaredConstructor(parameterTypes));
  } catch (SecurityException e) {
    throw new ReflectionException("Security violation while getting constructor for class: " + c.getName(), e);
  } catch (NoSuchMethodException e) {
    throw new ReflectionException("Constructor not found for class: " + c.getName(), e);
  }
}
 
origin: libgdx/libgdx

/** Returns a {@link Constructor} that represents the constructor for the supplied class which takes the supplied parameter
 * types. */
static public Constructor getDeclaredConstructor (Class c, Class... parameterTypes) throws ReflectionException {
  try {
    return new Constructor(c.getDeclaredConstructor(parameterTypes));
  } catch (SecurityException e) {
    throw new ReflectionException("Security violation while getting constructor for class: " + c.getName(), e);
  } catch (NoSuchMethodException e) {
    throw new ReflectionException("Constructor not found for class: " + c.getName(), e);
  }
}
 
origin: google/guava

static Invokable<?, Prepender> constructor(Class<?>... parameterTypes) throws Exception {
 Constructor<Prepender> constructor = Prepender.class.getDeclaredConstructor(parameterTypes);
 return Invokable.from(constructor);
}
origin: google/guava

static Element constructor() throws Exception {
 Constructor<?> constructor = A.class.getDeclaredConstructor(Object.class);
 Element element = new Element(constructor);
 assertEquals(constructor.getName(), element.getName());
 assertEquals(A.class, element.getDeclaringClass());
 return element;
}
origin: google/guava

public void testConstructor_Ignored_ShouldPass() throws Exception {
 new NullPointerTester()
   .ignore(FailOnOneOfTwoConstructors.class.getDeclaredConstructor(String.class))
   .testAllPublicConstructors(FailOnOneOfTwoConstructors.class);
}
origin: spring-projects/spring-framework

@Test
public void forConstructor() throws Exception {
  Constructor<?> constructor = Constructors.class.getDeclaredConstructor(List.class);
  Type type = SerializableTypeWrapper.forMethodParameter(MethodParameter.forExecutable(constructor, 0));
  assertThat(type.toString(), equalTo("java.util.List<java.lang.String>"));
  assertSerializable(type);
}
origin: spring-projects/spring-framework

private void emitDefaultConstructor(ClassEmitter ce) {
  Constructor<Object> declaredConstructor;
  try {
    declaredConstructor = Object.class.getDeclaredConstructor();
  }
  catch (NoSuchMethodException e) {
    throw new IllegalStateException("Object should have default constructor ", e);
  }
  MethodInfo constructor = (MethodInfo) MethodInfoTransformer.getInstance().transform(declaredConstructor);
  CodeEmitter e = EmitUtils.begin_method(ce, constructor, Constants.ACC_PUBLIC);
  e.load_this();
  e.dup();
  Signature sig = constructor.getSignature();
  e.super_invoke_constructor(sig);
  e.return_value();
  e.end_method();
}
origin: spring-projects/spring-framework

@Test
public void annotatedConstructorParameterInStaticNestedClass() throws Exception {
  Constructor<?> constructor = NestedClass.class.getDeclaredConstructor(String.class);
  MethodParameter methodParameter = MethodParameter.forExecutable(constructor, 0);
  assertEquals(String.class, methodParameter.getParameterType());
  assertNotNull("Failed to find @Param annotation", methodParameter.getParameterAnnotation(Param.class));
}
origin: google/guava

public void testConstructor_returnType_hasTypeParameter() throws Exception {
 @SuppressWarnings("rawtypes") // Foo.class for Foo<T> is always raw type
 Class<WithConstructorAndTypeParameter> type = WithConstructorAndTypeParameter.class;
 @SuppressWarnings("rawtypes") // Foo.class
 Constructor<WithConstructorAndTypeParameter> constructor = type.getDeclaredConstructor();
 Invokable<?, ?> factory = Invokable.from(constructor);
 assertThat(factory.getTypeParameters()).hasLength(2);
 assertEquals(type.getTypeParameters()[0], factory.getTypeParameters()[0]);
 assertEquals(constructor.getTypeParameters()[0], factory.getTypeParameters()[1]);
 ParameterizedType returnType = (ParameterizedType) factory.getReturnType().getType();
 assertEquals(type, returnType.getRawType());
 assertEquals(
   ImmutableList.copyOf(type.getTypeParameters()),
   ImmutableList.copyOf(returnType.getActualTypeArguments()));
}
origin: google/guava

public void testConstructor_isOverridablel() throws Exception {
 Invokable<?, ?> delegate = Invokable.from(Foo.class.getDeclaredConstructor());
 assertFalse(delegate.isOverridable());
 assertFalse(delegate.isVarArgs());
}
java.langClassgetDeclaredConstructor

Javadoc

Returns a Constructor object which represents the constructor matching the given parameter types that is declared by the class represented by this Class. (Class[]) null is equivalent to the empty array.

Use #getConstructor if you want to search superclasses.

Popular methods of Class

  • getName
    Returns the name of the class represented by this Class. For a description of the format which is us
  • getSimpleName
  • getClassLoader
  • isAssignableFrom
    Determines if the class or interface represented by this Class object is either the same as, or is a
  • forName
    Returns the Class object associated with the class or interface with the given string name, using th
  • newInstance
    Returns a new instance of the class represented by this Class, created by invoking the default (that
  • getMethod
    Returns a Method object that reflects the specified public member method of the class or interface r
  • getResourceAsStream
    Finds a resource with a given name. The rules for searching resources associated with a given class
  • getSuperclass
    Returns the Class representing the superclass of the entity (class, interface, primitive type or voi
  • getConstructor
  • cast
    Casts an object to the class or interface represented by this Class object.
  • isInstance
  • cast,
  • isInstance,
  • getCanonicalName,
  • getDeclaredField,
  • isArray,
  • getAnnotation,
  • getDeclaredFields,
  • getResource,
  • getDeclaredMethod,
  • getMethods

Popular in Java

  • Making http post requests using okhttp
  • scheduleAtFixedRate (Timer)
  • notifyDataSetChanged (ArrayAdapter)
  • getSharedPreferences (Context)
  • BorderLayout (java.awt)
    A border layout lays out a container, arranging and resizing its components to fit in five regions:
  • Socket (java.net)
    Provides a client-side TCP socket.
  • SecureRandom (java.security)
    This class generates cryptographically secure pseudo-random numbers. It is best to invoke SecureRand
  • Locale (java.util)
    Locale represents a language/country/variant combination. Locales are used to alter the presentatio
  • JFileChooser (javax.swing)
  • Loader (org.hibernate.loader)
    Abstract superclass of object loading (and querying) strategies. This class implements useful common
  • Github Copilot alternatives
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now