Java Class.getDeclaredConstructor Examples

Java Class.getDeclaredConstructor - 30 examples found. These are the top rated real world Java examples of Class.getDeclaredConstructor extracted from open source projects. You can rate examples to help us improve the quality of examples.
 protected T createProviderInstanceFromType(
     final Class<? extends T> execClass, final String providerName)
     throws ProviderCreationException {
   boolean ctrfound = true;
   try {
     final Constructor<? extends T> method =
         execClass.getDeclaredConstructor(new Class[] {Framework.class});
     final T executor = method.newInstance(framework);
     return executor;
   } catch (NoSuchMethodException e) {
     ctrfound = false;
   } catch (Exception e) {
     throw new ProviderCreationException(
         "Unable to create provider instance: " + e.getMessage(), e, getName(), providerName);
   }
   try {
     final Constructor<? extends T> method = execClass.getDeclaredConstructor(new Class[0]);
     final T executor = method.newInstance();
     return executor;
   } catch (NoSuchMethodException e) {
     throw new ProviderCreationException(
         "No constructor found with signature (Framework) or (): " + e.getMessage(),
         e,
         getName(),
         providerName);
   } catch (Exception e) {
     throw new ProviderCreationException(
         "Unable to create provider instance: " + e.getMessage(), e, getName(), providerName);
   }
 }
Example #2
0
 /**
  * Get the dns resolver from class <code>resolverClassName</code> with optional <code>
  * hostRegionOverrides</code>.
  *
  * <p>It would try to load the class with the constructor with <code>hostRegionOverrides</code>.
  * If it fails, it would fall back to load the class with default empty constructor. The
  * interpretion of <code>hostRegionOverrides</code> is up to the implementation.
  *
  * @param resolverCls resolver class
  * @param hostRegionOverrides host region overrides
  * @return dns resolver
  */
 public static DNSToSwitchMapping getDNSResolver(
     Class<? extends DNSToSwitchMapping> resolverCls, String hostRegionOverrides) {
   // first try to construct the dns resolver with overrides
   Constructor<? extends DNSToSwitchMapping> constructor;
   Object[] parameters;
   try {
     constructor = resolverCls.getDeclaredConstructor(String.class);
     parameters = new Object[] {hostRegionOverrides};
   } catch (NoSuchMethodException nsme) {
     // no constructor with overrides
     try {
       constructor = resolverCls.getDeclaredConstructor();
       parameters = new Object[0];
     } catch (NoSuchMethodException nsme1) {
       throw new RuntimeException(
           "Unable to find constructor for dns resolver " + resolverCls, nsme1);
     }
   }
   constructor.setAccessible(true);
   try {
     return constructor.newInstance(parameters);
   } catch (InstantiationException ie) {
     throw new RuntimeException("Unable to instantiate dns resolver " + resolverCls, ie);
   } catch (IllegalAccessException iae) {
     throw new RuntimeException("Illegal access to dns resolver " + resolverCls, iae);
   } catch (InvocationTargetException ite) {
     throw new RuntimeException("Unable to construct dns resolver " + resolverCls, ite);
   }
 }
 private Constructor<?> getTypeExcludeFilterConstructor(Class<?> type)
     throws NoSuchMethodException {
   try {
     return type.getDeclaredConstructor(Class.class);
   } catch (Exception ex) {
     return type.getDeclaredConstructor();
   }
 }
Example #4
0
  private Task createTaskObject(
      Project project, Class<? extends Task> type, String name, boolean generateGetters) {
    if (!Task.class.isAssignableFrom(type)) {
      throw new GradleException(
          String.format(
              "Cannot create task of type '%s' as it does not implement the Task interface.",
              type.getSimpleName()));
    }

    Class<? extends Task> generatedType;
    if (generateGetters) {
      generatedType = generator.generate(type);
    } else {
      generatedType = type;
    }

    Constructor<? extends Task> constructor = null;
    Object[] params = null;
    try {
      constructor = generatedType.getDeclaredConstructor();
      params = new Object[0];
    } catch (NoSuchMethodException e) {
      // Ignore
    }
    try {
      constructor = generatedType.getDeclaredConstructor(Project.class, String.class);
      params = new Object[] {project, name};
    } catch (NoSuchMethodException e) {
      // Ignore
    }
    if (constructor == null) {
      throw new GradleException(
          String.format(
              "Cannot create task of type '%s' as it does not have an appropriate public constructor.",
              type.getSimpleName()));
    }

    AbstractTask.injectIntoNextInstance(project, name);
    try {
      return constructor.newInstance(params);
    } catch (InvocationTargetException e) {
      throw new GradleException(
          String.format("Could not create task of type '%s'.", type.getSimpleName()), e.getCause());
    } catch (Exception e) {
      throw new GradleException(
          String.format("Could not create task of type '%s'.", type.getSimpleName()), e);
    } finally {
      AbstractTask.injectIntoNextInstance(null, null);
    }
  }
  protected boolean hasValidProviderSignature(final Class clazz) {

    try {
      final Constructor method = clazz.getDeclaredConstructor(new Class[] {Framework.class});
      return null != method;
    } catch (NoSuchMethodException e) {
    }
    try {
      final Constructor method = clazz.getDeclaredConstructor(new Class[0]);
      return null != method;
    } catch (NoSuchMethodException e) {
    }
    return false;
  }
 /**
  * Finds the conversion method.
  *
  * @param <T> the type of the converter
  * @param cls the class to find a method for, not null
  * @return the method to call, null means use {@code toString}
  * @throws RuntimeException if invalid
  */
 private <T> Constructor<T> findFromStringConstructor(Class<T> cls) {
   Constructor<T> con;
   try {
     con = cls.getDeclaredConstructor(String.class);
   } catch (NoSuchMethodException ex) {
     try {
       con = cls.getDeclaredConstructor(CharSequence.class);
     } catch (NoSuchMethodException ex2) {
       return null;
     }
   }
   FromString fromString = con.getAnnotation(FromString.class);
   return fromString != null ? con : null;
 }