Java Class.getConstructor Examples

Java Class.getConstructor - 30 examples found. These are the top rated real world Java examples of Class.getConstructor extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
 public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
     throws BeansException {
   if (annotationPackage == null || annotationPackage.length() == 0) {
     return;
   }
   if (beanFactory instanceof BeanDefinitionRegistry) {
     try {
       // init scanner
       Class<?> scannerClass =
           ClassUtils.loadClass(
               "org.springframework.context.annotation.ClassPathBeanDefinitionScanner");
       Object scanner =
           scannerClass
               .getConstructor(new Class<?>[] {BeanDefinitionRegistry.class, boolean.class})
               .newInstance(new Object[] {(BeanDefinitionRegistry) beanFactory, true});
       // add filter
       Class<?> filterClass =
           ClassUtils.loadClass("org.springframework.core.type.filter.AnnotationTypeFilter");
       Object filter = filterClass.getConstructor(Class.class).newInstance(Service.class);
       Method addIncludeFilter =
           scannerClass.getMethod(
               "addIncludeFilter",
               ClassUtils.loadClass("org.springframework.core.type.filter.TypeFilter"));
       addIncludeFilter.invoke(scanner, filter);
       // scan packages
       String[] packages = Constants.COMMA_SPLIT_PATTERN.split(annotationPackage);
       Method scan = scannerClass.getMethod("scan", new Class<?>[] {String[].class});
       scan.invoke(scanner, new Object[] {packages});
     } catch (Throwable e) {
       // spring 2.0
     }
   }
 }
  static {
    Class c;
    Constructor constr;
    int constrParamCnt;
    try {
      c = ClassUtil.forName("java.util.concurrent.ConcurrentHashMap");
      try {
        constr = c.getConstructor(new Class[] {Integer.TYPE, Float.TYPE, Integer.TYPE});
        constrParamCnt = 3;
      } catch (Exception e) {
        throw new RuntimeException("Failed to get ConcurrentHashMap constructor", e);
      }
    } catch (ClassNotFoundException e) {
      c = HashMap.class;
      try {
        constr = c.getConstructor(new Class[] {Integer.TYPE, Float.TYPE});
        constrParamCnt = 2;
      } catch (Exception e2) {
        throw new RuntimeException("Failed to get HashMap constructor", e2);
      }
    }

    bestHashMapClass = c;
    bestHashMapClassConstructor = constr;
    bestHashMapClassConstructorParamCnt = constrParamCnt;
  }
  public CircuitElementView instantiateElement(
      UUID elementUUID,
      float positionX,
      float positionY,
      CircuitElementManager elementManager,
      WireManager wireManager)
      throws InvocationTargetException, IllegalAccessException, InstantiationException,
          NoSuchMethodException {
    Class<? extends CircuitElementView> viewType = ElementTypeUUID.VIEW_MAP.get(elementUUID);
    if (viewType == null) throw new IllegalArgumentException("Missing element view UUID");

    Class<? extends CircuitElement> elementType = ElementTypeUUID.ELEMENT_MAP.get(elementUUID);
    if (elementType == null) throw new IllegalArgumentException("Missing element type UUID");

    Constructor<? extends CircuitElementView> viewConstructor;
    viewConstructor =
        viewType.getConstructor(
            Context.class, CircuitElement.class, float.class, float.class, WireManager.class);

    Constructor<? extends CircuitElement> elementConstructor;
    elementConstructor = elementType.getConstructor(CircuitElementManager.class);

    CircuitElement element = elementConstructor.newInstance(elementManager);

    return viewConstructor.newInstance(context, element, 0, 0, wireManager);
  }
  @SuppressWarnings({"fallthrough"})
  private static Permission getPermission(String type, String name, String actions)
      throws ClassNotFoundException, InstantiationException, IllegalAccessException,
          NoSuchMethodException, InvocationTargetException {
    Class<?> clazz = Class.forName(type);
    int nArgs = actions != null ? 2 : name != null ? 1 : 0;

    switch (nArgs) {
      case 0:
        try {
          return (Permission) clazz.getConstructor().newInstance();
        } catch (NoSuchMethodException e) {
        }

      case 1:
        try {
          return (Permission) clazz.getConstructor(ARGS_NAME).newInstance(name);
        } catch (NoSuchMethodException e) {
        }

      case 2:
        return (Permission) clazz.getConstructor(ARGS_NAME_ACTIONS).newInstance(name, actions);
    }

    assert false;
    return null;
  }
Example #5
0
 /**
  * Factory method, equivalent to a "fromXML" for step creation. Looks for a class with the same
  * name as the XML tag, with the first letter capitalized. For example, &lt;call /&gt; is
  * abbot.script.Call.
  */
 public static Step createStep(Resolver resolver, Element el) throws InvalidScriptException {
   String tag = el.getName();
   Map attributes = createAttributeMap(el);
   String name = tag.substring(0, 1).toUpperCase() + tag.substring(1);
   if (tag.equals(TAG_WAIT)) {
     attributes.put(TAG_WAIT, "true");
     name = "Assert";
   }
   try {
     name = "abbot.script." + name;
     Log.debug("Instantiating " + name);
     Class cls = Class.forName(name);
     try {
       // Steps with contents require access to the XML element
       Class[] argTypes = new Class[] {Resolver.class, Element.class, Map.class};
       Constructor ctor = cls.getConstructor(argTypes);
       return (Step) ctor.newInstance(new Object[] {resolver, el, attributes});
     } catch (NoSuchMethodException nsm) {
       // All steps must support this ctor
       Class[] argTypes = new Class[] {Resolver.class, Map.class};
       Constructor ctor = cls.getConstructor(argTypes);
       return (Step) ctor.newInstance(new Object[] {resolver, attributes});
     }
   } catch (ClassNotFoundException cnf) {
     String msg = Strings.get("step.unknown_tag", new Object[] {tag});
     throw new InvalidScriptException(msg);
   } catch (InvocationTargetException ite) {
     Log.warn(ite);
     throw new InvalidScriptException(ite.getTargetException().getMessage());
   } catch (Exception exc) {
     Log.warn(exc);
     throw new InvalidScriptException(exc.getMessage());
   }
 }
Example #6
0
 public <T> T createProxy(
     ModelElementState state,
     StructSchema<T> viewSchema,
     @Nullable StructSchema<? extends T> delegateSchema,
     TypeConverter typeConverter) {
   try {
     Class<? extends T> generatedClass = getGeneratedImplementation(viewSchema, delegateSchema);
     if (generatedClass == null) {
       throw new IllegalStateException(
           "No managed implementation class available for: " + viewSchema.getType());
     }
     if (delegateSchema == null) {
       Constructor<? extends T> constructor =
           generatedClass.getConstructor(ModelElementState.class, TypeConverter.class);
       return constructor.newInstance(state, typeConverter);
     } else {
       ModelType<? extends T> delegateType = delegateSchema.getType();
       Object delegate = state.getBackingNode().getPrivateData(delegateType);
       Constructor<? extends T> constructor =
           generatedClass.getConstructor(
               ModelElementState.class, TypeConverter.class, delegateType.getConcreteClass());
       return constructor.newInstance(state, typeConverter, delegate);
     }
   } catch (InvocationTargetException e) {
     throw UncheckedException.throwAsUncheckedException(e.getTargetException());
   } catch (Exception e) {
     throw UncheckedException.throwAsUncheckedException(e);
   }
 }