Harrison
3 years ago
19 changed files with 605 additions and 160 deletions
@ -0,0 +1,11 @@
|
||||
package com.fr.env.detect.base; |
||||
|
||||
/** |
||||
* created by Harrison on 2022/05/25 |
||||
**/ |
||||
public class DetectorConstants { |
||||
|
||||
public static final String JAR_HELP_LINK = ""; |
||||
|
||||
public static final String FINE_DB_HELP_LINK = ""; |
||||
} |
@ -0,0 +1,16 @@
|
||||
package com.fr.env.detect.base; |
||||
|
||||
import com.fr.general.build.BuildInfo; |
||||
import com.fr.stable.StringUtils; |
||||
|
||||
/** |
||||
* created by Harrison on 2022/05/25 |
||||
**/ |
||||
public class DetectorUtil { |
||||
|
||||
public static boolean isDesignerJar(BuildInfo info) { |
||||
|
||||
return StringUtils.contains(info.getJar(), "fine-report-designer"); |
||||
} |
||||
|
||||
} |
@ -1,22 +0,0 @@
|
||||
package com.fr.env.detect.bean; |
||||
|
||||
/** |
||||
* created by Harrison on 2022/05/25 |
||||
**/ |
||||
public enum DetectorStatus { |
||||
|
||||
/** |
||||
* 成功 |
||||
*/ |
||||
SUCCESS, |
||||
|
||||
/** |
||||
* 失败 |
||||
*/ |
||||
FAILED, |
||||
|
||||
/** |
||||
* 异常 |
||||
*/ |
||||
UNKNOWN |
||||
} |
@ -0,0 +1,4 @@
|
||||
/** |
||||
* 国际化相关的异常 |
||||
*/ |
||||
package com.fr.env.detect.exception.intelli; |
@ -0,0 +1,165 @@
|
||||
package com.fr.env.detect.impl.converter; |
||||
|
||||
import com.fr.env.detect.bean.DetectorResult; |
||||
import com.fr.env.detect.thowable.ThrowableConverter; |
||||
import com.fr.stable.resource.ResourceLoader; |
||||
|
||||
import javax.el.MethodNotFoundException; |
||||
import java.io.IOException; |
||||
import java.net.URL; |
||||
import java.util.ArrayList; |
||||
import java.util.Collections; |
||||
import java.util.Enumeration; |
||||
import java.util.HashMap; |
||||
import java.util.HashSet; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import java.util.Set; |
||||
import java.util.regex.Matcher; |
||||
import java.util.regex.Pattern; |
||||
|
||||
/** |
||||
* 类抛出异常的转换 |
||||
* 原则:可以找不到,但不要误报 |
||||
* |
||||
* created by Harrison on 2022/05/24 |
||||
**/ |
||||
public class ClassConfictConvertor implements ThrowableConverter { |
||||
|
||||
private Map<Class<?>, ClassNameConverter> throwableMap = new HashMap<>(); |
||||
|
||||
private static final Pattern JAR_PATTERN = Pattern.compile("([\\w+-\\.]*\\.jar)"); |
||||
|
||||
public ClassConfictConvertor() { |
||||
|
||||
// 类异常
|
||||
this.throwableMap.put(ClassNotFoundException.class, Converter.CLASS); |
||||
this.throwableMap.put(NoClassDefFoundError.class, Converter.CLASS); |
||||
this.throwableMap.put(ClassCastException.class, Converter.CLASS); |
||||
this.throwableMap.put(IncompatibleClassChangeError.class, Converter.CLASS); |
||||
|
||||
// 方法异常
|
||||
this.throwableMap.put(MethodNotFoundException.class, Converter.METHOD); |
||||
this.throwableMap.put(NoSuchMethodException.class, Converter.METHOD); |
||||
this.throwableMap.put(NoSuchMethodError.class, Converter.METHOD); |
||||
} |
||||
|
||||
@Override |
||||
public boolean accept(Throwable throwable) { |
||||
|
||||
Throwable sign = throwable; |
||||
while (sign != null) { |
||||
if (throwableMap.containsKey(sign.getClass())) { |
||||
return true; |
||||
} |
||||
sign = sign.getCause(); |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
@Override |
||||
public DetectorResult convert(Throwable throwable) { |
||||
|
||||
Iterable<String> classNames = Collections.emptyList(); |
||||
Throwable sign = throwable; |
||||
while (sign != null) { |
||||
if (throwableMap.containsKey(sign.getClass())) { |
||||
classNames = throwableMap.get(sign.getClass()) |
||||
.converter(throwable); |
||||
} |
||||
sign = sign.getCause(); |
||||
} |
||||
|
||||
for (String className : classNames) { |
||||
String classFile = "/" + className.replaceAll("\\.", "/") + ".class"; |
||||
try { |
||||
Enumeration<URL> urls = ResourceLoader.getResources(classFile, this.getClass()); |
||||
List<URL> urlList = new ArrayList<>(); |
||||
while (urls.hasMoreElements()) { |
||||
URL url = urls.nextElement(); |
||||
urlList.add(url); |
||||
} |
||||
Set<String> allPath = new HashSet<>(); |
||||
for (URL url : urlList) { |
||||
String file = url.getFile(); |
||||
Matcher matcher = JAR_PATTERN.matcher(url.getFile()); |
||||
if (matcher.find()) { |
||||
String jar = matcher.group(); |
||||
allPath.add(jar); |
||||
} else { |
||||
boolean containsClasses = file.contains("classes"); |
||||
if (containsClasses) { |
||||
allPath.add("classes"); |
||||
} |
||||
} |
||||
} |
||||
} catch (IOException ignore) { |
||||
} |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
private interface ClassNameConverter { |
||||
|
||||
/** |
||||
* 将异常解析为类名,可能有多个 |
||||
* |
||||
* @param throwable 异常 |
||||
* @return 类名 |
||||
*/ |
||||
Iterable<String> converter(Throwable throwable); |
||||
} |
||||
|
||||
public enum Converter implements ClassNameConverter { |
||||
|
||||
/** |
||||
* 类匹配 |
||||
*/ |
||||
CLASS { |
||||
|
||||
/** |
||||
* 匹配 111.222.333 |
||||
* 至少有一个 111. |
||||
* 至少有一个 333 |
||||
*/ |
||||
private final Pattern CLASS_PATTERN = Pattern.compile("((\\w+\\.)+(\\w+))"); |
||||
|
||||
@Override |
||||
public Iterable<String> converter(Throwable throwable) { |
||||
|
||||
String message = throwable.getMessage(); |
||||
Matcher matcher = CLASS_PATTERN.matcher(message); |
||||
List<String> names = new ArrayList<>(); |
||||
while (matcher.find()) { |
||||
String className = matcher.group(); |
||||
names.add(className); |
||||
} |
||||
return names; |
||||
} |
||||
}, |
||||
|
||||
/** |
||||
* 方法匹配 |
||||
*/ |
||||
METHOD { |
||||
|
||||
/** |
||||
* 后面有 .method()xxxx |
||||
*/ |
||||
private final Pattern METHOD_PATTERN = Pattern.compile("((\\w+\\.)+(\\w+))(\\.\\w+\\(\\))"); |
||||
|
||||
@Override |
||||
public Iterable<String> converter(Throwable throwable) { |
||||
|
||||
String message = throwable.getMessage(); |
||||
Matcher matcher = METHOD_PATTERN.matcher(message); |
||||
List<String> names = new ArrayList<>(); |
||||
while (matcher.find()) { |
||||
String className = matcher.group(); |
||||
names.add(className); |
||||
} |
||||
return names; |
||||
} |
||||
} |
||||
} |
||||
} |
@ -1,53 +0,0 @@
|
||||
package com.fr.env.detect.impl.converter; |
||||
|
||||
import com.fr.env.detect.bean.DetectorResult; |
||||
import com.fr.env.detect.thowable.ThrowableConverter; |
||||
|
||||
import javax.el.MethodNotFoundException; |
||||
import java.util.HashSet; |
||||
import java.util.Set; |
||||
|
||||
/** |
||||
* 类抛出异常的转换 |
||||
* created by Harrison on 2022/05/24 |
||||
**/ |
||||
public class ClassThrowableConvertor implements ThrowableConverter { |
||||
|
||||
private Set<Class<?>> throwableClassSet = new HashSet<>(); |
||||
|
||||
private Set<Class<?>> throwableMethodSet = new HashSet<>(); |
||||
|
||||
public ClassThrowableConvertor() { |
||||
|
||||
// 类异常
|
||||
this.throwableClassSet.add(ClassNotFoundException.class); |
||||
this.throwableClassSet.add(NoClassDefFoundError.class); |
||||
this.throwableClassSet.add(ClassCastException.class); |
||||
this.throwableClassSet.add(IncompatibleClassChangeError.class); |
||||
|
||||
// 方法异常
|
||||
this.throwableMethodSet.add(MethodNotFoundException.class); |
||||
this.throwableMethodSet.add(NoSuchMethodException.class); |
||||
this.throwableMethodSet.add(NoSuchMethodError.class); |
||||
} |
||||
|
||||
@Override |
||||
public boolean accept(Throwable throwable) { |
||||
|
||||
Throwable sign = throwable; |
||||
while (sign != null) { |
||||
if (throwableClassSet.contains(sign.getClass()) || throwableMethodSet.contains(sign.getClass())) { |
||||
return true; |
||||
} |
||||
sign = sign.getCause(); |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
@Override |
||||
public DetectorResult convert(Throwable throwable) { |
||||
|
||||
// todo
|
||||
return null; |
||||
} |
||||
} |
@ -0,0 +1,23 @@
|
||||
package com.fr.env.detect.impl.converter; |
||||
|
||||
import org.junit.Test; |
||||
|
||||
public class ClassConfictConvertorTest { |
||||
|
||||
@Test |
||||
public void testInnerFinder() { |
||||
|
||||
ClassNotFoundException ex1 = new ClassNotFoundException("Class 111.222.333 not found"); |
||||
Iterable<String> names = ClassConfictConvertor.Converter.CLASS.converter(ex1); |
||||
|
||||
System.out.println(); |
||||
} |
||||
|
||||
@Test |
||||
public void testConverter() { |
||||
|
||||
ClassNotFoundException ex1 = new ClassNotFoundException("com.zaxxer.hikari.HikariConfig"); |
||||
ClassConfictConvertor convertor = new ClassConfictConvertor(); |
||||
convertor.convert(ex1); |
||||
} |
||||
} |
Loading…
Reference in new issue