Java Code Examples for org.apache.commons.lang.reflect.ConstructorUtils#invokeConstructor()

The following examples show how to use org.apache.commons.lang.reflect.ConstructorUtils#invokeConstructor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Ec2InstanceUpdateHandler.java    From soundwave with Apache License 2.0 6 votes vote down vote up
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());
  }
}
 
Example 2
Source File: AWSClientFactory.java    From awseb-deployment-plugin with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 3
Source File: AbstractTeamspeakClientSocket.java    From ts3j with Apache License 2.0 4 votes vote down vote up
private PacketHandler createHandler(Class<? extends PacketHandler> clazz) throws ReflectiveOperationException {
    return (PacketHandler) ConstructorUtils.invokeConstructor(clazz, this);
}
 
Example 4
Source File: ObjectAdapter.java    From soundwave with Apache License 2.0 4 votes vote down vote up
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;
    }


  }