♻️ Refactoring code.use "@UtilityClass" annotation

This commit is contained in:
lishangbu 2019-03-21 01:33:28 +08:00
parent d528abd77e
commit c8ff9b708c
6 changed files with 42 additions and 32 deletions

View File

@ -16,6 +16,7 @@
package com.pig4cloud.pig.common.core.util;
import lombok.experimental.UtilityClass;
import org.springframework.core.BridgeMethodResolver;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.core.MethodParameter;
@ -33,8 +34,9 @@ import java.lang.reflect.Method;
*
* @author L.cm
*/
@UtilityClass
public class ClassUtils extends org.springframework.util.ClassUtils {
private static final ParameterNameDiscoverer PARAMETERNAMEDISCOVERER = new DefaultParameterNameDiscoverer();
private final ParameterNameDiscoverer PARAMETERNAMEDISCOVERER = new DefaultParameterNameDiscoverer();
/**
* 获取方法参数信息
@ -43,7 +45,7 @@ public class ClassUtils extends org.springframework.util.ClassUtils {
* @param parameterIndex 参数序号
* @return {MethodParameter}
*/
public static MethodParameter getMethodParameter(Constructor<?> constructor, int parameterIndex) {
public MethodParameter getMethodParameter(Constructor<?> constructor, int parameterIndex) {
MethodParameter methodParameter = new SynthesizingMethodParameter(constructor, parameterIndex);
methodParameter.initParameterNameDiscovery(PARAMETERNAMEDISCOVERER);
return methodParameter;
@ -56,7 +58,7 @@ public class ClassUtils extends org.springframework.util.ClassUtils {
* @param parameterIndex 参数序号
* @return {MethodParameter}
*/
public static MethodParameter getMethodParameter(Method method, int parameterIndex) {
public MethodParameter getMethodParameter(Method method, int parameterIndex) {
MethodParameter methodParameter = new SynthesizingMethodParameter(method, parameterIndex);
methodParameter.initParameterNameDiscovery(PARAMETERNAMEDISCOVERER);
return methodParameter;
@ -70,7 +72,7 @@ public class ClassUtils extends org.springframework.util.ClassUtils {
* @param <A> 泛型标记
* @return {Annotation}
*/
public static <A extends Annotation> A getAnnotation(Method method, Class<A> annotationType) {
public <A extends Annotation> A getAnnotation(Method method, Class<A> annotationType) {
Class<?> targetClass = method.getDeclaringClass();
// The method may be on an interface, but we need attributes from the target class.
// If the target class is null, the method will be unchanged.
@ -95,7 +97,7 @@ public class ClassUtils extends org.springframework.util.ClassUtils {
* @param <A> 泛型标记
* @return {Annotation}
*/
public static <A extends Annotation> A getAnnotation(HandlerMethod handlerMethod, Class<A> annotationType) {
public <A extends Annotation> A getAnnotation(HandlerMethod handlerMethod, Class<A> annotationType) {
// 先找方法再找方法上的类
A annotation = handlerMethod.getMethodAnnotation(annotationType);
if (null != annotation) {

View File

@ -20,6 +20,7 @@ import cn.hutool.core.codec.Base64;
import cn.hutool.json.JSONUtil;
import com.pig4cloud.pig.common.core.exception.CheckedException;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.HttpHeaders;
@ -45,9 +46,10 @@ import java.nio.charset.StandardCharsets;
* @author L.cm
*/
@Slf4j
@UtilityClass
public class WebUtils extends org.springframework.web.util.WebUtils {
private static final String BASIC_ = "Basic ";
private static final String UNKNOWN = "unknown";
private final String BASIC_ = "Basic ";
private final String UNKNOWN = "unknown";
/**
* 判断是否ajax请求
@ -56,7 +58,7 @@ public class WebUtils extends org.springframework.web.util.WebUtils {
* @param handlerMethod HandlerMethod
* @return 是否ajax请求
*/
public static boolean isBody(HandlerMethod handlerMethod) {
public boolean isBody(HandlerMethod handlerMethod) {
ResponseBody responseBody = ClassUtils.getAnnotation(handlerMethod, ResponseBody.class);
return responseBody != null;
}
@ -67,7 +69,7 @@ public class WebUtils extends org.springframework.web.util.WebUtils {
* @param name cookie name
* @return cookie value
*/
public static String getCookieVal(String name) {
public String getCookieVal(String name) {
HttpServletRequest request = WebUtils.getRequest();
Assert.notNull(request, "request from RequestContextHolder is null");
return getCookieVal(request, name);
@ -80,7 +82,7 @@ public class WebUtils extends org.springframework.web.util.WebUtils {
* @param name cookie name
* @return cookie value
*/
public static String getCookieVal(HttpServletRequest request, String name) {
public String getCookieVal(HttpServletRequest request, String name) {
Cookie cookie = getCookie(request, name);
return cookie != null ? cookie.getValue() : null;
}
@ -91,7 +93,7 @@ public class WebUtils extends org.springframework.web.util.WebUtils {
* @param response HttpServletResponse
* @param key cookie key
*/
public static void removeCookie(HttpServletResponse response, String key) {
public void removeCookie(HttpServletResponse response, String key) {
setCookie(response, key, null, 0);
}
@ -103,7 +105,7 @@ public class WebUtils extends org.springframework.web.util.WebUtils {
* @param value cookie value
* @param maxAgeInSeconds maxage
*/
public static void setCookie(HttpServletResponse response, String name, String value, int maxAgeInSeconds) {
public void setCookie(HttpServletResponse response, String name, String value, int maxAgeInSeconds) {
Cookie cookie = new Cookie(name, value);
cookie.setPath("/");
cookie.setMaxAge(maxAgeInSeconds);
@ -116,7 +118,7 @@ public class WebUtils extends org.springframework.web.util.WebUtils {
*
* @return {HttpServletRequest}
*/
public static HttpServletRequest getRequest() {
public HttpServletRequest getRequest() {
return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
}
@ -125,7 +127,7 @@ public class WebUtils extends org.springframework.web.util.WebUtils {
*
* @return {HttpServletResponse}
*/
public static HttpServletResponse getResponse() {
public HttpServletResponse getResponse() {
return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
}
@ -135,7 +137,7 @@ public class WebUtils extends org.springframework.web.util.WebUtils {
* @param response HttpServletResponse
* @param result 结果对象
*/
public static void renderJson(HttpServletResponse response, Object result) {
public void renderJson(HttpServletResponse response, Object result) {
renderJson(response, result, MediaType.APPLICATION_JSON_UTF8_VALUE);
}
@ -146,7 +148,7 @@ public class WebUtils extends org.springframework.web.util.WebUtils {
* @param result 结果对象
* @param contentType contentType
*/
public static void renderJson(HttpServletResponse response, Object result, String contentType) {
public void renderJson(HttpServletResponse response, Object result, String contentType) {
response.setCharacterEncoding("UTF-8");
response.setContentType(contentType);
try (PrintWriter out = response.getWriter()) {
@ -161,7 +163,7 @@ public class WebUtils extends org.springframework.web.util.WebUtils {
*
* @return {String}
*/
public static String getIP() {
public String getIP() {
return getIP(WebUtils.getRequest());
}
@ -171,7 +173,7 @@ public class WebUtils extends org.springframework.web.util.WebUtils {
* @param request HttpServletRequest
* @return {String}
*/
public static String getIP(HttpServletRequest request) {
public String getIP(HttpServletRequest request) {
Assert.notNull(request, "HttpServletRequest is null");
String ip = request.getHeader("X-Requested-For");
if (StringUtils.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
@ -201,7 +203,7 @@ public class WebUtils extends org.springframework.web.util.WebUtils {
* @return
*/
@SneakyThrows
public static String[] getClientId(ServerHttpRequest request) {
public String[] getClientId(ServerHttpRequest request) {
String header = request.getHeaders().getFirst(HttpHeaders.AUTHORIZATION);
if (header == null || !header.startsWith(BASIC_)) {

View File

@ -21,6 +21,7 @@ import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.http.HttpUtil;
import com.pig4cloud.pig.admin.api.entity.SysLog;
import com.pig4cloud.pig.common.core.constant.CommonConstants;
import lombok.experimental.UtilityClass;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
@ -35,8 +36,9 @@ import java.util.Objects;
*
* @author L.cm
*/
@UtilityClass
public class SysLogUtils {
public static SysLog getSysLog() {
public SysLog getSysLog() {
HttpServletRequest request = ((ServletRequestAttributes) Objects
.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
SysLog sysLog = new SysLog();
@ -56,7 +58,7 @@ public class SysLogUtils {
*
* @return clientId
*/
private static String getClientId() {
private String getClientId() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication instanceof OAuth2Authentication) {
OAuth2Authentication auth2Authentication = (OAuth2Authentication) authentication;
@ -70,7 +72,7 @@ public class SysLogUtils {
*
* @return username
*/
private static String getUsername() {
private String getUsername() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null) {
return null;

View File

@ -18,6 +18,7 @@ package com.pig4cloud.pig.common.security.util;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.CharsetUtil;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
@ -30,8 +31,9 @@ import java.io.IOException;
* 认证授权相关工具类
*/
@Slf4j
@UtilityClass
public class AuthUtils {
private static final String BASIC_ = "Basic ";
private final String BASIC_ = "Basic ";
/**
* 从header 请求中的clientId/clientsecect
@ -40,7 +42,7 @@ public class AuthUtils {
* @throws RuntimeException if the Basic header is not present or is not valid
* Base64
*/
public static String[] extractAndDecodeHeader(String header)
public String[] extractAndDecodeHeader(String header)
throws IOException {
byte[] base64Token = header.substring(6).getBytes("UTF-8");
@ -69,7 +71,7 @@ public class AuthUtils {
* @return
* @throws IOException
*/
public static String[] extractAndDecodeHeader(HttpServletRequest request)
public String[] extractAndDecodeHeader(HttpServletRequest request)
throws IOException {
String header = request.getHeader(HttpHeaders.AUTHORIZATION);

View File

@ -40,7 +40,7 @@ public class SecurityUtils {
/**
* 获取Authentication
*/
public static Authentication getAuthentication() {
public Authentication getAuthentication() {
return SecurityContextHolder.getContext().getAuthentication();
}

View File

@ -25,6 +25,7 @@ import com.pig4cloud.pig.codegen.entity.GenConfig;
import com.pig4cloud.pig.codegen.entity.TableEntity;
import com.pig4cloud.pig.common.core.constant.CommonConstants;
import com.pig4cloud.pig.common.core.exception.CheckedException;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
@ -49,6 +50,7 @@ import java.util.zip.ZipOutputStream;
* @date 2019/2/1
*/
@Slf4j
@UtilityClass
public class GenUtils {
private static final String ENTITY_JAVA_VM = "Entity.java.vm";
@ -62,7 +64,7 @@ public class GenUtils {
private static final String API_JS_VM = "api.js.vm";
private static final String CRUD_JS_VM = "crud.js.vm";
private static List<String> getTemplates() {
private List<String> getTemplates() {
List<String> templates = new ArrayList<>();
templates.add("template/Entity.java.vm");
templates.add("template/Mapper.java.vm");
@ -81,7 +83,7 @@ public class GenUtils {
/**
* 生成代码
*/
public static void generatorCode(GenConfig genConfig, Map<String, String> table,
public void generatorCode(GenConfig genConfig, Map<String, String> table,
List<Map<String, String>> columns, ZipOutputStream zip) {
//配置信息
Configuration config = getConfig();
@ -210,14 +212,14 @@ public class GenUtils {
/**
* 列名转换成Java属性名
*/
private static String columnToJava(String columnName) {
private String columnToJava(String columnName) {
return WordUtils.capitalizeFully(columnName, new char[]{'_'}).replace("_", "");
}
/**
* 表名转换成Java类名
*/
private static String tableToJava(String tableName, String tablePrefix) {
private String tableToJava(String tableName, String tablePrefix) {
if (StringUtils.isNotBlank(tablePrefix)) {
tableName = tableName.replace(tablePrefix, "");
}
@ -227,7 +229,7 @@ public class GenUtils {
/**
* 获取配置信息
*/
private static Configuration getConfig() {
private Configuration getConfig() {
try {
return new PropertiesConfiguration("generator.properties");
} catch (ConfigurationException e) {
@ -238,7 +240,7 @@ public class GenUtils {
/**
* 获取文件名
*/
private static String getFileName(String template, String className, String packageName, String moduleName) {
private String getFileName(String template, String className, String packageName, String moduleName) {
String packagePath = CommonConstants.BACK_END_PROJECT + File.separator + "src" + File.separator + "main" + File.separator + "java" + File.separator;
if (StringUtils.isNotBlank(packageName)) {
packagePath += packageName.replace(".", File.separator) + File.separator + moduleName + File.separator;