package com.redhat.ceylon.common.tool;

import java.lang.reflect.Method;
import java.util.List;

/**
 * Helper methods for tools
 * @author tom
 */
public class Tools {

    private Tools() {}
    
    private static boolean isToolExecutionException(Throwable t) {
        for (Class<?> i : t.getClass().getInterfaces()) {
            if (ToolError.class.getName().equals(i.getName())) {
                return true;
                
            }
        }
        return false;
    }
    
    public static boolean isFatal(Throwable t) {
        if (isToolExecutionException(t)) {
            try {
                return (Boolean)t.getClass().getMethod("isFatal").invoke(t);
            } catch (ReflectiveOperationException e) {
                // fall through
            }
        }
        return true;
    }
    
    public static boolean writeErrorMessage(Throwable t, WordWrap w) {
        if (isToolExecutionException(t)) {
            for (Method m : t.getClass().getMethods()) {
                try {
                    if ("writeErrorMessage".equals(m.getName())) {
                        m.invoke(t, w);
                        return true;
                    }
                } catch (Exception e) {
                    Class<?> c1 = m.getParameterTypes()[0];
                    System.err.println(c1+ ", "+ c1.getClassLoader());
                    Class<?> c2 = w.getClass();
                    System.err.println(c2+ ", "+ c2.getClassLoader());
                    e.printStackTrace();
                    break;
                }
            }
        }
        return false;
    }
    
    public static void printToolSuggestions(ToolLoader toolLoader, WordWrap wrap, String toolName) {
        
        wrap.append(ToolMessages.msg("tool.unknown", Tools.progName(), toolName)).newline();
        final List<String> suggestions = toolLoader.typo(toolName);
        if (suggestions != null 
                && !suggestions.isEmpty()) {
            wrap.newline();
            wrap.append(ToolMessages.msg("tool.unknown.suggest"));
            wrap.setIndent(4);
            wrap.newline();
            for (String suggestion : suggestions) {
                wrap.append(suggestion).newline();
            }
            wrap.newline();
        }
    }

    public static String progName() {
        return System.getProperty("com.redhat.ceylon.common.tool.progname", "ceylon");
    }

}

@@ -37,64 +37,8 @@
 l;%0A%0A
-import java.lang.reflect.Method;%0Aimport java.util.List;%0A
 %0A/**
@@ -139,2088 +139,8 @@
    %0A
-    private static boolean isToolExecutionException(Throwable t) %7B%0A        for (Class%3C?%3E i : t.getClass().getInterfaces()) %7B%0A            if (ToolError.class.getName().equals(i.getName())) %7B%0A                return true;%0A                %0A            %7D%0A        %7D%0A        return false;%0A    %7D%0A    %0A    public static boolean isFatal(Throwable t) %7B%0A        if (isToolExecutionException(t)) %7B%0A            try %7B%0A                return (Boolean)t.getClass().getMethod(%22isFatal%22).invoke(t);%0A            %7D catch (ReflectiveOperationException e) %7B%0A                // fall through%0A            %7D%0A        %7D%0A        return true;%0A    %7D%0A    %0A    public static boolean writeErrorMessage(Throwable t, WordWrap w) %7B%0A        if (isToolExecutionException(t)) %7B%0A            for (Method m : t.getClass().getMethods()) %7B%0A                try %7B%0A                    if (%22writeErrorMessage%22.equals(m.getName())) %7B%0A                        m.invoke(t, w);%0A                        return true;%0A                    %7D%0A                %7D catch (Exception e) %7B%0A                    Class%3C?%3E c1 = m.getParameterTypes()%5B0%5D;%0A                    System.err.println(c1+ %22, %22+ c1.getClassLoader());%0A                    Class%3C?%3E c2 = w.getClass();%0A                    System.err.println(c2+ %22, %22+ c2.getClassLoader());%0A                    e.printStackTrace();%0A                    break;%0A                %7D%0A            %7D%0A        %7D%0A        return false;%0A    %7D%0A    %0A    public static void printToolSuggestions(ToolLoader toolLoader, WordWrap wrap, String toolName) %7B%0A        %0A        wrap.append(ToolMessages.msg(%22tool.unknown%22, Tools.progName(), toolName)).newline();%0A        final List%3CString%3E suggestions = toolLoader.typo(toolName);%0A        if (suggestions != null %0A                && !suggestions.isEmpty()) %7B%0A            wrap.newline();%0A            wrap.append(ToolMessages.msg(%22tool.unknown.suggest%22));%0A            wrap.setIndent(4);%0A            wrap.newline();%0A            for (String suggestion : suggestions) %7B%0A                wrap.append(suggestion).newline();%0A            %7D%0A            wrap.newline();%0A        %7D%0A    %7D%0A%0A
     

Remove some unused methods, since the hack used for ceylon/ceylon-runtime#20 is no longer needed.