新增JT1078 Template支持
This commit is contained in:
41
src/main/java/com/genersoft/iot/vmp/jt1078/util/Bin.java
Normal file
41
src/main/java/com/genersoft/iot/vmp/jt1078/util/Bin.java
Normal file
@@ -0,0 +1,41 @@
|
||||
package com.genersoft.iot.vmp.jt1078.util;
|
||||
|
||||
/**
|
||||
* 32位整型的二进制读写
|
||||
*/
|
||||
public class Bin {
|
||||
|
||||
private static final int[] bits = new int[32];
|
||||
|
||||
static {
|
||||
bits[0] = 1;
|
||||
for (int i = 1; i < bits.length; i++) {
|
||||
bits[i] = bits[i - 1] << 1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取n的第i位
|
||||
*
|
||||
* @param n int32
|
||||
* @param i 取值范围0-31
|
||||
*/
|
||||
public static boolean get(int n, int i) {
|
||||
return (n & bits[i]) == bits[i];
|
||||
}
|
||||
|
||||
/**
|
||||
* 不足位数从左边加0
|
||||
*/
|
||||
public static String strHexPaddingLeft(String data, int length) {
|
||||
int dataLength = data.length();
|
||||
if (dataLength < length) {
|
||||
StringBuilder dataBuilder = new StringBuilder(data);
|
||||
for (int i = dataLength; i < length; i++) {
|
||||
dataBuilder.insert(0, "0");
|
||||
}
|
||||
data = dataBuilder.toString();
|
||||
}
|
||||
return data;
|
||||
}
|
||||
}
|
||||
112
src/main/java/com/genersoft/iot/vmp/jt1078/util/ClassUtil.java
Normal file
112
src/main/java/com/genersoft/iot/vmp/jt1078/util/ClassUtil.java
Normal file
@@ -0,0 +1,112 @@
|
||||
package com.genersoft.iot.vmp.jt1078.util;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
||||
import org.springframework.core.io.support.ResourcePatternResolver;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class ClassUtil {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ClassUtil.class);
|
||||
|
||||
|
||||
public static Object getBean(Class<?> clazz) {
|
||||
if (clazz != null) {
|
||||
try {
|
||||
return clazz.getDeclaredConstructor().newInstance();
|
||||
} catch (Exception ex) {
|
||||
logger.error("ClassUtil:找不到指定的类", ex);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public static Object getBean(String className) {
|
||||
Class<?> clazz = null;
|
||||
try {
|
||||
clazz = Class.forName(className);
|
||||
} catch (Exception ex) {
|
||||
logger.error("ClassUtil:找不到指定的类");
|
||||
}
|
||||
if (clazz != null) {
|
||||
try {
|
||||
//获取声明的构造器--》创建实例
|
||||
return clazz.getDeclaredConstructor().newInstance();
|
||||
} catch (Exception ex) {
|
||||
logger.error("ClassUtil:找不到指定的类", ex);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取包下所有带注解的class
|
||||
*
|
||||
* @param packageName 包名
|
||||
* @param annotationClass 注解类型
|
||||
* @return list
|
||||
*/
|
||||
public static List<Class<?>> getClassList(String packageName, Class<? extends Annotation> annotationClass) {
|
||||
List<Class<?>> classList = getClassList(packageName);
|
||||
classList.removeIf(next -> !next.isAnnotationPresent(annotationClass));
|
||||
return classList;
|
||||
}
|
||||
|
||||
public static List<Class<?>> getClassList(String... packageName) {
|
||||
List<Class<?>> classList = new LinkedList<>();
|
||||
for (String s : packageName) {
|
||||
List<Class<?>> c = getClassList(s);
|
||||
classList.addAll(c);
|
||||
}
|
||||
return classList;
|
||||
}
|
||||
|
||||
public static List<Class<?>> getClassList(String packageName) {
|
||||
List<Class<?>> classList = new LinkedList<>();
|
||||
try {
|
||||
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
|
||||
Resource[] resources = resourcePatternResolver.getResources(packageName.replace(".", "/") + "/**/*.class");
|
||||
for (Resource resource : resources) {
|
||||
String url = resource.getURL().toString();
|
||||
|
||||
String[] split = url.split(packageName.replace(".", "/"));
|
||||
String s = split[split.length - 1];
|
||||
String className = s.replace("/", ".");
|
||||
className = className.substring(0, className.lastIndexOf("."));
|
||||
doAddClass(classList, packageName + className);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return classList;
|
||||
}
|
||||
|
||||
private static void doAddClass(List<Class<?>> classList, String className) {
|
||||
Class<?> cls = loadClass(className, false);
|
||||
classList.add(cls);
|
||||
}
|
||||
|
||||
public static Class<?> loadClass(String className, boolean isInitialized) {
|
||||
Class<?> cls;
|
||||
try {
|
||||
cls = Class.forName(className, isInitialized, getClassLoader());
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return cls;
|
||||
}
|
||||
|
||||
|
||||
public static ClassLoader getClassLoader() {
|
||||
return Thread.currentThread().getContextClassLoader();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user