001/*
002 * Copyright 2013 Atteo.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.atteo.moonshine.services.internal;
017
018import java.util.ArrayList;
019import java.util.List;
020
021import org.atteo.moonshine.services.ServiceInfo;
022
023import com.google.inject.Binding;
024import com.google.inject.Module;
025import com.google.inject.spi.DefaultElementVisitor;
026import com.google.inject.spi.Element;
027import com.google.inject.spi.Elements;
028import com.google.inject.spi.PrivateElements;
029
030public class GuiceBindingsHelper {
031
032    public static void printServiceElements(List<? extends ServiceInfo> infos) {
033        for (ServiceInfo info : infos) {
034
035            System.out.println("Service: " + info.getName() + " {");
036            printElements(info.getElements(), 1);
037            System.out.println("}");
038        }
039    }
040
041    public static void printBindings(Iterable<Module> modules) {
042        for (Module module : modules) {
043            System.out.println("-");
044            printBindings(module);
045        }
046    }
047
048    public static void printBindings(Module module) {
049        printElements(Elements.getElements(module), 1);
050    }
051
052    public static void printElements(List<Element> elements, final int indentation) {
053        for (Element element : elements) {
054            printElement(element, indentation);
055        }
056    }
057
058    public static void printPrivateElements(PrivateElements elements, final int indentation) {
059        indent(indentation);
060        System.out.println("privateElements {");
061        List<Element> exposedElements = new ArrayList<>();
062        List<Element> privateElements = new ArrayList<>();
063        for (Element element : elements.getElements()) {
064            if (element instanceof Binding<?> && elements.getExposedKeys().contains(((Binding)element).getKey())) {
065                exposedElements.add(element);
066            } else {
067                privateElements.add(element);
068            }
069        }
070        if (!exposedElements.isEmpty()) {
071            indent(indentation + 1);
072            System.out.println("// exposed:");
073        }
074        for (Element element : exposedElements) {
075            printElement(element, indentation + 1);
076        }
077        if (!privateElements.isEmpty()) {
078            indent(indentation + 1);
079            System.out.println("// private:");
080        }
081        for (Element element : privateElements) {
082            printElement(element, indentation + 1);
083        }
084        indent(indentation);
085        System.out.println("}");
086    }
087
088    public static void printElement(Element element, final int indentation) {
089        element.acceptVisitor(new DefaultElementVisitor<Void>() {
090            @Override
091            public Void visit(PrivateElements privateElements) {
092                printPrivateElements(privateElements, indentation);
093                return null;
094            }
095
096            @Override
097            protected Void visitOther(Element element) {
098                indent(indentation);
099                System.out.println(element);
100                return null;
101            }
102        });
103    }
104
105    public static void indent(int n) {
106        for (int i = 0; i < n; i++) {
107            System.out.print("    ");
108        }
109    }
110}