@Override public String getSqlSelect() { throw ExceptionUtils.mpe("can not use this method for \"%s\"", "getSqlSelect"); }
/** * 获取 columnName */ protected String columnToString(R column) { if (column instanceof String) { return (String) column; } throw ExceptionUtils.mpe("not support this column !"); }
/** * 断言这个 boolean 为 true * <p>为 false 则抛出异常</p> * * @param expression boolean 值 * @param message 消息 */ public static void isTrue(boolean expression, String message, Object... params) { if (!expression) { throw ExceptionUtils.mpe(message, params); } }
@Override public String getSqlSelect() { throw ExceptionUtils.mpe("can not use this method for \"%s\"", "getSqlSelect"); }
@Override public String getSqlSet() { throw ExceptionUtils.mpe("can not use this method for \"%s\"", "getSqlSet"); }
@Override public String getSqlSet() { throw ExceptionUtils.mpe("can not use this method for \"%s\"", "getSqlSet"); }
@Override public T getEntity() { throw ExceptionUtils.mpe("can not use this method for \"%s\"", "getEntity"); }
@Override public MergeSegments getExpression() { throw ExceptionUtils.mpe("can not use this method for \"%s\"", "getExpression"); }
@Override public String getCustomSqlSegment() { throw ExceptionUtils.mpe("can not use this method for \"%s\"", "getCustomSqlSegment"); }
@Override public String getSqlSegment() { throw ExceptionUtils.mpe("can not use this method for \"%s\"", "getSqlSegment"); }
/** * 请仅在确定类存在的情况下调用该方法 * * @param name 类名称 * @return 返回转换后的 Class */ public static Class<?> toClassConfident(String name) { try { return Class.forName(name); } catch (ClassNotFoundException e) { throw ExceptionUtils.mpe("找不到指定的class!请仅在明确确定会有 class 的时候,调用该方法", e); } }
/** * 发现设置多个主键注解抛出异常 */ private static void throwExceptionId(Class<?> clazz) { throw ExceptionUtils.mpe("There must be only one, Discover multiple @TableId annotation in %s", clazz.getName()); }
/** * Blob 转为 String 格式 * * @param blob Blob 对象 * @return 转换后的 */ public static String blob2String(Blob blob) { if (null != blob) { try { byte[] returnValue = blob.getBytes(1, (int) blob.length()); return new String(returnValue, StandardCharsets.UTF_8); } catch (Exception e) { throw ExceptionUtils.mpe("Blob Convert To String Error!"); } } return null; }
/** * MD5 Base64 加密 * * @param str 待加密的字符串 * @return 加密后的字符串 */ public static String md5Base64(String str) { //确定计算方法 try { MessageDigest md5 = MessageDigest.getInstance(Constants.MD5); //加密后的字符串 byte[] src = md5.digest(str.getBytes(StandardCharsets.UTF_8)); return Base64.getEncoder().encodeToString(src); } catch (Exception e) { throw ExceptionUtils.mpe(e); } } }
/** * 根据指定的 class , 实例化一个对象,根据构造参数来实例化 * <p> * 在 java9 及其之后的版本 Class.newInstance() 方法已被废弃 * * @param clazz 需要实例化的对象 * @param <T> 类型,由输入类型决定 * @return 返回新的实例 */ public static <T> T newInstance(Class<T> clazz) { try { Constructor<T> constructor = clazz.getDeclaredConstructor(); constructor.setAccessible(true); return constructor.newInstance(); } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { throw ExceptionUtils.mpe("实例化对象时出现错误,请尝试给 %s 添加无参的构造方法", e, clazz.getName()); } }
/** * 判断数据库类型 * * @return 类型枚举值 */ public DbType getDbType() { if (null == dbType) { if (driverName.contains("mysql")) { dbType = DbType.MYSQL; } else if (driverName.contains("oracle")) { dbType = DbType.ORACLE; } else if (driverName.contains("postgresql")) { dbType = DbType.POSTGRE_SQL; } else if (driverName.contains("sqlserver")) { dbType = DbType.SQL_SERVER; } else if (driverName.contains("db2")) { dbType = DbType.DB2; } else if (driverName.contains("mariadb")) { dbType = DbType.MARIADB; } else if(driverName.contains("h2")){ dbType = DbType.H2; }else { throw ExceptionUtils.mpe("Unknown type of database!"); } } return dbType; }
/** * 获取 public get方法的值 * * @param cls ignore * @param entity 实体 * @param str 属性字符串内容 * @return Object */ public static Object getMethodValue(Class<?> cls, Object entity, String str) { Map<String, Field> fieldMaps = getFieldMap(cls); try { Assert.notEmpty(fieldMaps, "Error: NoSuchField in %s for %s. Cause:", cls.getSimpleName(), str); Method method = cls.getMethod(getMethodCapitalize(fieldMaps.get(str), str)); return method.invoke(entity); } catch (NoSuchMethodException e) { throw ExceptionUtils.mpe("Error: NoSuchMethod in %s. Cause:", e, cls.getSimpleName()); } catch (IllegalAccessException e) { throw ExceptionUtils.mpe("Error: Cannot execute a private method. in %s. Cause:", e, cls.getSimpleName()); } catch (InvocationTargetException e) { throw ExceptionUtils.mpe("Error: InvocationTargetException on getMethodValue. Cause:" + e); } }
/** * 通过反序列化转换 lambda 表达式,该方法只能序列化 lambda 表达式,不能序列化接口实现或者正常非 lambda 写法的对象 * * @param lambda lambda对象 * @return 返回解析后的 SerializedLambda */ public static SerializedLambda resolve(SFunction lambda) { if (!lambda.getClass().isSynthetic()) { throw ExceptionUtils.mpe("该方法仅能传入 lambda 表达式产生的合成类"); } try (ObjectInputStream objIn = new ObjectInputStream(new ByteArrayInputStream(SerializationUtils.serialize(lambda))) { @Override protected Class<?> resolveClass(ObjectStreamClass objectStreamClass) throws IOException, ClassNotFoundException { Class<?> clazz = super.resolveClass(objectStreamClass); return clazz == java.lang.invoke.SerializedLambda.class ? SerializedLambda.class : clazz; } }) { return (SerializedLambda) objIn.readObject(); } catch (ClassNotFoundException | IOException e) { throw ExceptionUtils.mpe("This is impossible to happen", e); } }
private String getColumn(SerializedLambda lambda, boolean onlyColumn) { String fieldName = StringUtils.resolveFieldName(lambda.getImplMethodName()); if (!initColumnMap || !columnMap.containsKey(fieldName.toUpperCase(Locale.ENGLISH))) { String entityClassName = lambda.getImplClassName(); columnMap = LambdaUtils.getColumnMap(entityClassName); Assert.notEmpty(columnMap, "cannot find column's cache for \"%s\", so you cannot used \"%s\"!", entityClassName, typedThis.getClass()); initColumnMap = true; } return Optional.ofNullable(columnMap.get(fieldName.toUpperCase(Locale.ENGLISH))) .map(onlyColumn ? ColumnCache::getColumn : ColumnCache::getColumnSelect) .orElseThrow(() -> ExceptionUtils.mpe("your property named \"%s\" cannot find the corresponding database column name!", fieldName)); } }
/** * 获取逻辑删除字段的 sql 脚本 * * @param startWithAnd 是否以 and 开头 * @param deleteValue 是否需要的是逻辑删除值 * @return sql 脚本 */ public String getLogicDeleteSql(boolean startWithAnd, boolean deleteValue) { if (logicDelete) { TableFieldInfo field = fieldList.stream().filter(TableFieldInfo::isLogicDelete).findFirst() .orElseThrow(() -> ExceptionUtils.mpe("can't find the logicFiled from table {%s}", tableName)); String formatStr = field.isCharSequence() ? "'%s'" : "%s"; String logicDeleteSql = field.getColumn() + EQUALS + String.format(formatStr, deleteValue ? field.getLogicDeleteValue() : field.getLogicNotDeleteValue()); if (startWithAnd) { logicDeleteSql = " AND " + logicDeleteSql; } return logicDeleteSql; } return EMPTY; } }