package org.jenkinsci.deprecatedusage;

import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

public class Reports {
    private final DeprecatedApi deprecatedApi;
    private final Map<String, DeprecatedUsage> deprecatedUsageByPlugin;

    public Reports(DeprecatedApi deprecatedApi, Map<String, DeprecatedUsage> deprecatedUsageByPlugin) {
        super();
        this.deprecatedApi = deprecatedApi;
        this.deprecatedUsageByPlugin = deprecatedUsageByPlugin;
    }

    public void report() {
        log("deprecated classes in jenkins.war : " + format(deprecatedApi.getClasses()));
        log("");
        log("deprecated methods in jenkins.war : " + format(deprecatedApi.getMethods()));
        log("");
        log("deprecated fields in jenkins.war : " + format(deprecatedApi.getFields()));
        log("");

        final Set<String> deprecatedClassesUsed = new TreeSet<String>();
        final Set<String> deprecatedMethodsUsed = new TreeSet<String>();
        final Set<String> deprecatedFieldsUsed = new TreeSet<String>();
        for (final Map.Entry<String, DeprecatedUsage> entry : deprecatedUsageByPlugin.entrySet()) {
            final String plugin = entry.getKey();
            final DeprecatedUsage deprecatedUsage = entry.getValue();
            deprecatedClassesUsed.addAll(deprecatedUsage.getClasses());
            deprecatedMethodsUsed.addAll(deprecatedUsage.getMethods());
            deprecatedFieldsUsed.addAll(deprecatedUsage.getFields());

            log("deprecated api used in plugin " + plugin + " :");
            if (!deprecatedUsage.getClasses().isEmpty()) {
                log("   classes : " + format(deprecatedUsage.getClasses()));
            }
            if (!deprecatedUsage.getMethods().isEmpty()) {
                log("   methods : " + format(deprecatedUsage.getMethods()));
            }
            if (!deprecatedUsage.getFields().isEmpty()) {
                log("   fields : " + format(deprecatedUsage.getFields()));
            }
        }
        log("");

        log("deprecated classes used in plugins : " + format(deprecatedClassesUsed));
        log("");
        log("deprecated methods used in plugins : " + format(deprecatedMethodsUsed));
        log("");
        log("deprecated fields used in plugins : " + format(deprecatedFieldsUsed));
        log("");
        log("plugins using a deprecated api : "
                + new TreeSet<String>(deprecatedUsageByPlugin.keySet()));
        log("");

        log(deprecatedApi.getClasses().size() + " deprecated and public classes in jenkins.war");
        log(deprecatedApi.getMethods().size() + " deprecated and public methods in jenkins.war");
        log(deprecatedApi.getFields().size() + " deprecated and public fields in jenkins.war");
        log(deprecatedUsageByPlugin.size() + " plugins using a deprecated api");
        log(deprecatedClassesUsed.size() + " deprecated classes used in plugins");
        log(deprecatedMethodsUsed.size() + " deprecated methods used in plugins");
        log(deprecatedFieldsUsed.size() + " deprecated fields used in plugins");
        // TODO print deprecated api not used in plugins
    }

    private static String format(Set<String> classesOrFieldsOrMethods) {
        // replace "org/mypackage/Myclass" by "org.mypackage.Myclass"
        return classesOrFieldsOrMethods.toString().replace('/', '.');
    }

    private static void log(String message) {
        Log.log(message);
    }
}

@@ -33,16 +33,48 @@
 usage;%0A%0A
+import java.util.LinkedHashSet;%0A
 import j
@@ -2496,32 +2496,1147 @@
       log(%22%22);%0A%0A
+        final Set%3CString%3E deprecatedClassesNotUsed = filterOnJenkins(deprecatedApi.getClasses());%0A        final Set%3CString%3E deprecatedMethodsNotUsed = filterOnJenkins(deprecatedApi.getMethods());%0A        final Set%3CString%3E deprecatedFieldsNotUsed = filterOnJenkins(deprecatedApi.getFields());%0A        deprecatedClassesNotUsed.removeAll(deprecatedClassesUsed);%0A        deprecatedMethodsNotUsed.removeAll(deprecatedMethodsUsed);%0A        deprecatedFieldsNotUsed.removeAll(deprecatedFieldsUsed);%0A        log(%22deprecated and public Jenkins classes not used in latest published plugins : %22%0A                + format(deprecatedClassesNotUsed));%0A        log(%22%22);%0A        log(%22deprecated and public Jenkins methods not used in latest published plugins : %22%0A                + format(deprecatedMethodsNotUsed));%0A        log(%22%22);%0A        log(%22deprecated and public Jenkins fields not used in latest published plugins : %22%0A                + format(deprecatedFieldsNotUsed));%0A        log(%22%22);%0A        log(%22plugins using a deprecated api : %22%0A                + new TreeSet%3CString%3E(deprecatedUsageByPlugin.keySet()));%0A        log(%22%22);%0A%0A
         log(depr
@@ -4251,56 +4251,420 @@
     
-// TODO print deprecated api not used in
+log(deprecatedClassesNotUsed.size()%0A                + %22 deprecated and public Jenkins classes not used in latest published plugins%22);%0A        log(deprecatedMethodsNotUsed.size()%0A                + %22 deprecated and public Jenkins methods not used in latest published plugins%22);%0A        log(deprecatedFieldsNotUsed.size()%0A                + %22 deprecated and public Jenkins fields not used in latest published
  plugins
 %0A   
@@ -4659,16 +4659,19 @@
  plugins
+%22);
 %0A    %7D%0A%0A
@@ -4882,32 +4882,564 @@
 ', '.');%0A    %7D%0A%0A
+    private static Set%3CString%3E filterOnJenkins(Set%3CString%3E classesOrFieldsOrMethods) %7B%0A        final Set%3CString%3E filtered = new LinkedHashSet%3C%3E();%0A        for (final String classOrFieldOrMethod : classesOrFieldsOrMethods) %7B%0A            if (classOrFieldOrMethod.startsWith(%22jenkins/%22)%0A                    %7C%7C classOrFieldOrMethod.startsWith(%22hudson/%22)%0A                    %7C%7C classOrFieldOrMethod.startsWith(%22org/kohsuke/%22)) %7B%0A                filtered.add(classOrFieldOrMethod);%0A            %7D%0A        %7D%0A        return filtered;%0A    %7D%0A%0A
     private stat

print deprecated api not used in plugins