13693261870
2025-07-02 6708810c4de34dfb9513061432d656f91d56ee3a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.ruoyi.buss.common.liquor;
 
import javax.tools.Diagnostic;
import javax.tools.JavaFileObject;
import java.util.*;
 
/**
 * This code mainly from: Arthas project
 *
 * @author noear
 * @since 1.3.12
 * */
public class DynamicCompilerException extends RuntimeException {
    private static final long serialVersionUID = 1L;
    private List<Diagnostic<? extends JavaFileObject>> diagnostics;
 
    public DynamicCompilerException(String message, List<Diagnostic<? extends JavaFileObject>> diagnostics) {
        super(message);
        this.diagnostics = diagnostics;
    }
 
    public DynamicCompilerException(Throwable cause, List<Diagnostic<? extends JavaFileObject>> diagnostics) {
        super(cause);
        this.diagnostics = diagnostics;
    }
 
    private String getErrors() {
        StringBuilder buf = new StringBuilder();
 
        if (diagnostics != null) {
            for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics) {
                String packagePath = getPackagePath(diagnostic);
                String diagnosticString = diagnostic.toString();
 
                if (packagePath == null) {
                    buf.append(diagnosticString).append("\n");
                } else {
                    if (diagnosticString.startsWith(packagePath)) {
                        buf.append(diagnosticString).append("\n");
                    } else {
                        buf.append(packagePath).append(diagnosticString).append("\n");
                    }
                }
            }
        }
 
        if (buf.length() > 0) {
            buf.setLength(buf.length() - 1);
        }
 
        return buf.toString();
    }
 
    private String getPackagePath(Diagnostic<? extends JavaFileObject> diagnostic) {
        try {
            String source = diagnostic.getSource().getCharContent(true).toString();
            if (source.startsWith("package ")) {
                int end = source.indexOf(";");
                if (end > 0) {
                    return "/" + source.substring("package ".length(), end).replace(".", "/");
                }
            }
        } catch (Exception skip) {
 
        }
 
        return null;
    }
 
    @Override
    public String getMessage() {
        return super.getMessage() + ":\n\n" + getErrors();
    }
}