类org.apache.commons.lang.reflect.ConstructorUtils源码实例Demo

下面列出了怎么用org.apache.commons.lang.reflect.ConstructorUtils的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: soundwave   文件: Ec2InstanceUpdateHandler.java
public Ec2InstanceUpdateHandler(CmdbInstanceStore cmdbStore, CloudInstanceStore cloudStore,
                                DailySnapshotStoreFactory dailySnapshotStoreFactory) {
  this.cmdbInstanceStore = cmdbStore;
  this.cloudInstanceStore = cloudStore;
  this.dailySnapshotStoreFactory = dailySnapshotStoreFactory;
  try {
    String className = Configuration.getProperties().getString("instance_factory",
        "com.pinterest.cmp.soundwave.pinterest.PinterestEsInstanceFactory");
    if (className.isEmpty()) {
      logger.error("instance_factory is not set");
    } else {
      Class factoryClass = Class.forName(className);
      this.esInstanceFactory = (AbstractEsInstanceFactory) ConstructorUtils.invokeConstructor(
          factoryClass, null);
      this.esInstanceFactory.setCloudInstanceStore(this.cloudInstanceStore);
    }
  } catch (Exception ex) {
    logger.error("Class {} cannot be loaded error {}",
        Configuration.getProperties().getString("instance_factory"), ex.getLocalizedMessage());
  }
}
 
@SuppressWarnings({"unchecked", "deprecation"})
public <T> T getService(Class<T> serviceClazz)
        throws NoSuchMethodException, IllegalAccessException,
        InvocationTargetException, InstantiationException {

    Class<?> paramTypes[] = new Class<?>[]{AWSCredentialsProvider.class, ClientConfiguration.class};

    ClientConfiguration newClientConfiguration = new ClientConfiguration(this.clientConfiguration);

    if (AmazonS3.class.isAssignableFrom(serviceClazz)) {
        newClientConfiguration = newClientConfiguration.withSignerOverride("AWSS3V4SignerType");
    } else {
        newClientConfiguration = newClientConfiguration.withSignerOverride(null);
    }

    Object params[] = new Object[]{provider, newClientConfiguration};

    T resultObj = (T) ConstructorUtils.invokeConstructor(serviceClazz, params, paramTypes);

    if (DEFAULT_REGION.equals(defaultString(region, DEFAULT_REGION))) {
        return resultObj;
    } else {
        for (ServiceEndpointFormatter formatter : ServiceEndpointFormatter.values()) {
            if (formatter.matches(resultObj)) {
                ((AmazonWebServiceClient) resultObj).setEndpoint(getEndpointFor(formatter));
                break;
            }
        }
    }

    return resultObj;
}
 
源代码3 项目: ts3j   文件: AbstractTeamspeakClientSocket.java
private PacketHandler createHandler(Class<? extends PacketHandler> clazz) throws ReflectiveOperationException {
    return (PacketHandler) ConstructorUtils.invokeConstructor(clazz, this);
}
 
源代码4 项目: soundwave   文件: ObjectAdapter.java
public static <T1, T2> T2 getObject(T1 object, Class<T2> adaptedClass) {

    try {
      Map<String, Field> objectFieldsMap = getAllFields(object.getClass());

      T2 adaptedObject = (T2) ConstructorUtils.invokeConstructor(adaptedClass, null);
      List<String> target = getFields(adaptedClass);
      for (String field : target) {

        // get The field of the adapted object
        Field targetField = adaptedClass.getDeclaredField(field);
        targetField.setAccessible(true);

        if (targetField.isAnnotationPresent(NestedField.class)) {

          NestedField annotation = targetField.getDeclaredAnnotation(NestedField.class);
          String[] hierarchy = StringUtils.split(annotation.src(), ".");
          Field nestedField = objectFieldsMap.get(hierarchy[0]);
          nestedField.setAccessible(true);
          Object fieldValue = nestedField.get(object);

          for (int i = 1; i < hierarchy.length; i++) {
            nestedField = nestedField.getType().getDeclaredField(hierarchy[i]);
            nestedField.setAccessible(true);
            fieldValue = nestedField.get(fieldValue);
          }

          // Set the last level value from hierarchy
          targetField.set(adaptedObject, fieldValue);

        } else {

          // Non nested field process as normal
          Field sourceField;
          if (targetField.isAnnotationPresent(StringDate.class)) {

            // Process date fields
            sourceField = objectFieldsMap.get(field);
            sourceField.setAccessible(true);

            if (sourceField.get(object) != null) {

              // Value is not null
              DateTime time = new DateTime(sourceField.get(object), DateTimeZone.UTC);
              targetField.set(adaptedObject, time.toString());
            } else {

              targetField.set(adaptedObject, "");
            }


          } else if (targetField.isAnnotationPresent(IgnoreAdaptation.class)) {
            // Leave field as it is. no processing.
          } else {

            sourceField = objectFieldsMap.get(field);
            sourceField.setAccessible(true);

            targetField.set(adaptedObject, sourceField.get(object));
          }
        }
      }

      return adaptedObject;

    } catch (Exception e) {
      logger.error(ExceptionUtils.getRootCauseMessage(e));
      return null;
    }


  }
 
 类所在包
 同包方法