All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.infinispan.commons.configuration.attributes.CopyConstructorAttributeCopier Maven / Gradle / Ivy

There is a newer version: 15.1.0.Dev04
Show newest version
package org.infinispan.commons.configuration.attributes;

import java.lang.reflect.Constructor;

import org.infinispan.commons.CacheConfigurationException;

/**
 * CopyConstructorAttributeCopier. This {@link AttributeCopier} expects the attribute value class to have a copy constructor
 *
 * @author Tristan Tarrant
 * @since 9.2
 */
public class CopyConstructorAttributeCopier implements AttributeCopier {
   public static final AttributeCopier INSTANCE = new CopyConstructorAttributeCopier<>();

   private CopyConstructorAttributeCopier() {
      // Singleton constructor
   }

   @Override
   public T copyAttribute(T attribute) {
      try {
         Class klass = (Class) attribute.getClass();
         Constructor constructor = klass.getConstructor(klass);
         return constructor.newInstance(attribute);
      } catch (Exception e) {
         throw new CacheConfigurationException(e);
      }

   }

}