All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hyf.hotrefresh.core.exception.CompileException Maven / Gradle / Ivy

package com.hyf.hotrefresh.core.exception;

import javax.tools.Diagnostic;
import javax.tools.JavaFileObject;
import java.util.*;

/**
 * @author baB_hyf
 * @date 2021/12/12
 */
public class CompileException extends RefreshException {

    private List> diagnostics;

    public CompileException(String message) {
        super(message);
    }

    public CompileException(String message, Throwable cause) {
        super(message, cause);
    }

    public CompileException(String message, List> diagnostics) {
        super(message);
        this.diagnostics = diagnostics;
    }

    @Override
    public String getMessage() {
        return super.getMessage() + "\n" + getErrors();
    }

    private String getErrors() {
        StringBuilder errors = new StringBuilder();

        for (Map message : getErrorList()) {
            for (Map.Entry entry : message.entrySet()) {
                Object value = entry.getValue();
                if (value != null && !value.toString().isEmpty()) {
                    errors.append(entry.getKey());
                    errors.append(": ");
                    errors.append(value);
                }
                errors.append(" , ");
            }

            errors.append("\n");
        }

        return errors.toString();
    }

    private List> getErrorList() {
        List> messages = new ArrayList<>();
        if (diagnostics != null) {
            for (Diagnostic diagnostic : diagnostics) {
                Map message = new HashMap<>();
                message.put("line", diagnostic.getLineNumber());
                message.put("message", diagnostic.getMessage(Locale.US));
                messages.add(message);
            }
        }
        return messages;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy