com.siemensqms.qmsplus.sdk.util.ClassUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sdk Show documentation
Show all versions of sdk Show documentation
Developed By SiemensQMS,To Use For Developers
The newest version!
package com.siemensqms.qmsplus.sdk.util;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.type.filter.AssignableTypeFilter;
/**
* The class is a tool which includes functions that can be extracted for using invoked in sdk.
*
* @author Sherry.
*/
public class ClassUtils {
private static final Logger logger = LoggerFactory.getLogger(ClassUtils.class);
/**
* The function will convert message content to {@link List} whose generic paradigm is the
* type of implement class of {@code IConsumer}.
* @param content The content of message.
* @param implementClass The class who implements {@code IConsumer} interface.
* @return A {@code List} whose generic type is implementClass.
* @author Sherry.
*/
public static List convertJavaBeanContentAsList(String content, Class implementClass) {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
try {
/**
* Initializes a {@code List} object with implement class of IConsumer.
*/
TypeReference> typeRef = new TypeReference>() {
@Override
public Type getType() {
return new ParameterizedType() {
@Override
public Type[] getActualTypeArguments() {
return new Type[] {
implementClass
};
}
@Override
public Type getRawType() {
return List.class;
}
@Override
public Type getOwnerType() {
return null;
}
};
}
};
/**
* Convert message content to a {@code List} with implement class of IConsumer.
*/
return objectMapper.readValue(content, typeRef);
} catch (Exception e) {
/**
* If there is exception, it is caused by incorrect content conversion, then return null marks
* convert failed.
*/
return null;
}
}
/**
* The class is used to find all implement classes of given interface.
*
* @param c The interface needed to query it's implementation classes.
* @param basePackage Packages that need scanning which include implement classes.
* @return An array of classes which implement interface.
* @author Sherry.
*/
public static Class[] findAllImplementsClasses(Class c, String basePackage) {
/**
* Cancel component providers from the base pack scanning classpath, modify to specified path.
* Then add an type filter to the end of the inclusion list which matches given interface.
*/
ClassPathScanningCandidateComponentProvider scanner =
new ClassPathScanningCandidateComponentProvider(false);
scanner.addIncludeFilter(new AssignableTypeFilter(c));
Set components = scanner.findCandidateComponents(basePackage);
Class[] classes = new Class[components.size()];
int classPrimer = 0;
Iterator iterator = components.iterator();
while (iterator.hasNext()) {
BeanDefinition beanDefinition = iterator.next();
/**
* Use beanDefinition.getClass() is an {@code ScannedGenericBeanDefinition} object. So we
* can't directly use {@code BeanDefinition} to get the class which implements the interface.
*/
try {
classes[classPrimer++] = Class.forName(beanDefinition.getBeanClassName());
} catch (ClassNotFoundException e) {
logger.error("The class of implement {} is not found", e);
}
}
return classes;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy