Mongoosej.Blog.Software.Programming.Java.Aspect.经典代码


List中remove()方法的陷阱,被坑惨了!
原来 Lamda 表达式是这样写的

获取调用栈

  1. // springboot利用栈跟踪获取启动类
  2. private Class<?> deduceMainApplicationClass() {
  3. try {
  4. StackTraceElement[] stackTrace = new RuntimeException().getStackTrace();
  5. for (StackTraceElement stackTraceElement : stackTrace) {
  6. if ("main".equals(stackTraceElement.getMethodName())) {
  7. return Class.forName(stackTraceElement.getClassName());
  8. }
  9. }
  10. }
  11. catch (ClassNotFoundException ex) {
  12. // Swallow and continue
  13. }
  14. return null;
  15. }

枚举valueOf方法捕获异常

  1. try {
  2. // valueOf是枚举类的默认方法,该方法如果根据key找不到没觉会抛出IllegalArgumentException
  3. DB100.valueOf(key);
  4. } catch(IllegalArgumentException e) {
  5. continue;
  6. }