1.根据方法名,获取类的对应的方法

        Method changeSessionIdMethod = ReflectionUtils.findMethod(HttpServletRequest.class, "changeSessionId");
        if(changeSessionIdMethod == null) {
            throw new IllegalStateException("HttpServletRequest.changeSessionId is undefined. Are you using a Servlet 3.1+ environment?");
        }
        System.out.println(changeSessionIdMethod);

2.休眠当前线程

TimeUnit.MILLISECONDS.sleep(1000);

 3. 替换配置文件中特殊的变量将变量中含有 @{test.test} 将变量 test.test 替换为系统中的其他值。

private static final PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("@{", "}", ":", false);
    
    private static final PlaceholderResolver placeholderResolver=new PlaceholderResolver(){
        @Override
        public String resolvePlaceholder(String key) {
            
            return System.getProperty(key);
        }
        
    };

 4.获取对象的方法名称。dubbo 获取dubbo接口里的方法

String[] methods = Wrapper.getWrapper(interfaceClass).getMethodNames();

 5. {0} 字符串格式化。

        String template="test1={0},test2={1},test3={2}";
        Object[] objects=new Object[]{0,1,"test2"};
        String emailContent = MessageFormat.format(template, objects);
        System.out.println(emailContent);

 6.spring HiddenHttpMethodFilter 将http里post方法转化为参数里的方法

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {

        HttpServletRequest requestToUse = request;

        if ("POST".equals(request.getMethod()) && request.getAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE) == null) {
            String paramValue = request.getParameter(this.methodParam);
            if (StringUtils.hasLength(paramValue)) {
                requestToUse = new HttpMethodRequestWrapper(request, paramValue);
            }
        }

        filterChain.doFilter(requestToUse, response);
    }

 7.判断classpath 中是否存在某个类   原理:先判断是否是基础类型, 再 根据  ClassLoader clToUse = classLoader;          (clToUse != null ? clToUse.loadClass(name) : Class.forName(name))

org.springframework.util.ClassUtils  

        if (ClassUtils.isPresent(REACTIVE_WEB_ENVIRONMENT_CLASS, null)
                && !ClassUtils.isPresent(MVC_WEB_ENVIRONMENT_CLASS, null)) {
            return WebApplicationType.REACTIVE;
        }
        for (String className : WEB_ENVIRONMENT_CLASSES) {
            if (!ClassUtils.isPresent(className, null)) {
                return WebApplicationType.NONE;
            }
        }

 9.获取classpath下的文件,和 jar包里的文件。

    public static void main(String[] args) throws Exception {
        Enumeration<URL> urls = ClassLoader.getSystemResources("META-INF/services/ch.qos.logback.classic.spi.Configurator");
        while (urls.hasMoreElements()) {
            URL url = urls.nextElement();
            System.out.println(url.getPath());
        }
    }
// 从url 读取文件内容
BufferedReader reader = new BufferedReader(new InputStreamReader(resourceURL.openStream(), "utf-8"));  

 10.向目录里写文件,目录和文件不能直接用字符串链接,如目录为:.,文件名为:test.log,直接链接为.log会出错。

public class FileUtil {

    public static File getFile(String path,String fileName) throws IOException{
        File directory=new File(path);
        String filePath=directory.getCanonicalPath();
        return new File(filePath+"/"+fileName);
    }
}

 11.字符串按字节长度截取

    public static byte[] subBytes(byte[] src, int begin, int count) {
        byte[] bs = new byte[count];
        System.arraycopy(src, begin, bs, 0, count);
        return bs;
    }
if (!StringUtil.isNullOrEmpty(po.getContent())){
            byte[] contentB= new byte[0];
            try {
                contentB = po.getContent().getBytes("utf-8");
                if (contentB.length>1024){
                    po.setContent(new String(StringUtil.subBytes(contentB,0,1024),"utf-8"));
                }
            } catch (UnsupportedEncodingException e) {
                String result="content bytes length > 1024,convert error";
                po.setContent(result);
                log.error(result,e);
            }
        }

 12.获取类的调用链路

    private Class<?> deduceMainApplicationClass() {
        try {
            StackTraceElement[] stackTrace = new RuntimeException().getStackTrace();
            for (StackTraceElement stackTraceElement : stackTrace) {
                if ("main".equals(stackTraceElement.getMethodName())) {
                    return Class.forName(stackTraceElement.getClassName());
                }
            }
        }
        catch (ClassNotFoundException ex) {
            // Swallow and continue
        }
        return null;
    }