Created
July 7, 2014 18:03
-
-
Save gissuebot/fbe9a230a96cb8fecee9 to your computer and use it in GitHub Desktop.
Migrated attachment for Guice issue 213, comment 0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright (C) 2008 Google Inc. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package publicobject; | |
import com.google.inject.AbstractModule; | |
import com.google.inject.Guice; | |
import com.google.inject.Inject; | |
import com.google.inject.Injector; | |
import com.google.inject.InjectorGrapher; | |
import java.io.IOException; | |
import java.lang.reflect.Type; | |
import java.util.Date; | |
public class Graphed { | |
static class DeLorean { | |
@Inject TimeCircuits timeCircuits; | |
@Inject FluxCapacitor fluxCapacitor; | |
@Inject EnergySource energySource; | |
} | |
static class FluxCapacitor { | |
@Inject TimeCircuits timeCircuits; | |
} | |
static class TimeCircuits { | |
Date whereYouveBeen; | |
Date whereYouAre; | |
Date whereYourGoing; | |
} | |
interface EnergySource { | |
String generateOnePointTwentyOneGigawatts(); | |
} | |
static class Plutonium implements EnergySource { | |
public String generateOnePointTwentyOneGigawatts() { | |
return "newk-you-ler"; | |
} | |
} | |
static class LightningBolt implements EnergySource { | |
public String generateOnePointTwentyOneGigawatts() { | |
return "thunderstruck!"; | |
} | |
} | |
public static void main(String[] args) throws IOException { | |
Injector injector = Guice.createInjector(new AbstractModule() { | |
protected void configure() { | |
bind(EnergySource.class).to(Plutonium.class); | |
bind(FluxCapacitor.class); | |
bind(DeLorean.class); | |
} | |
}); | |
InjectorGrapher grapher = new InjectorGrapher(injector, System.out) { | |
public String renderType(Type type) { | |
if (type instanceof Class) { | |
Class<?> clas = (Class<?>) type; | |
return clas.getSimpleName(); | |
} else { | |
return super.renderType(type); | |
} | |
} | |
}; | |
grapher.graph(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright (C) 2008 Google Inc. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package com.google.inject; | |
import com.google.inject.internal.MoreTypes; | |
import com.google.inject.spi.BindingVisitor; | |
import com.google.inject.spi.ClassBinding; | |
import com.google.inject.spi.ConstantBinding; | |
import com.google.inject.spi.ConvertedConstantBinding; | |
import com.google.inject.spi.HasInjections; | |
import com.google.inject.spi.InjectionPoint; | |
import com.google.inject.spi.InstanceBinding; | |
import com.google.inject.spi.LinkedBinding; | |
import com.google.inject.spi.LinkedProviderBinding; | |
import com.google.inject.spi.ProviderBinding; | |
import com.google.inject.spi.ProviderInstanceBinding; | |
import java.io.IOException; | |
import java.io.PrintStream; | |
import java.lang.reflect.Constructor; | |
import java.lang.reflect.Field; | |
import java.lang.reflect.Member; | |
import java.lang.reflect.Method; | |
import java.lang.reflect.Type; | |
import java.util.logging.Logger; | |
public class InjectorGrapher implements BindingVisitor<Object> { | |
private final Injector injector; | |
private final PrintStream out; | |
public InjectorGrapher(Injector injector, PrintStream out) { | |
this.injector = injector; | |
this.out = out; | |
} | |
public void graph() throws IOException { | |
out.println("digraph injector {"); | |
for (Binding<?> binding : injector.getBindings().values()) { | |
if (skipBinding(binding)) { | |
continue; | |
} | |
out.print(" \""); | |
out.print(renderKey(binding.getKey())); | |
out.print("\" -> \""); | |
binding.accept(this); | |
out.print("\""); | |
out.print(" [arrowhead=onormal]"); | |
out.print(";"); | |
out.println(); | |
if (binding instanceof HasInjections) { | |
HasInjections hasInjections = (HasInjections) binding; | |
for (InjectionPoint injectionPoint : hasInjections.getInjectionPoints()) { | |
out.print(" \""); | |
out.print(renderKey(binding.getKey())); | |
out.print("\" -> \""); | |
out.print(renderKey(injectionPoint.getKey())); | |
out.print("\""); | |
out.print(" [label="); | |
out.print(renderMember(injectionPoint)); | |
out.print("]"); | |
out.println(); | |
} | |
} | |
} | |
out.println("}"); | |
} | |
private static final Key<Logger> loggerKey = Key.get(Logger.class); | |
public boolean skipBinding(Binding binding) { | |
return binding.getKey().getRawType().getPackage() == Guice.class.getPackage() | |
|| loggerKey.equals(binding.getKey()); | |
} | |
public String renderKey(Key key) { | |
StringBuilder sb = new StringBuilder(); | |
if (key.getAnnotationType() != null) { | |
sb.append("@").append(renderType(key.getAnnotationType())).append(" "); | |
} | |
sb.append(renderType(key.getTypeLiteral().getType())); | |
return sb.toString(); | |
} | |
public String renderType(Type type) { | |
return MoreTypes.toString(type); | |
} | |
public String renderMember(InjectionPoint injectionPoint) { | |
Member member = injectionPoint.getMember(); | |
Class<? extends Member> memberType = MoreTypes.memberType(member); | |
if (memberType == Method.class) { | |
return member.getName() + "#" + injectionPoint.getParameterIndex(); | |
} else if (memberType == Constructor.class) { | |
return "<init>"; | |
} else if (memberType == Field.class) { | |
return member.getName(); | |
} else { | |
throw new AssertionError(); | |
} | |
} | |
public void visit(LinkedBinding<?> linkedBinding) { | |
out.print(renderKey(linkedBinding.getTarget().getKey())); | |
} | |
public void visit(InstanceBinding<?> instanceBinding) { | |
out.print(instanceBinding.toString()); | |
} | |
public void visit(ProviderInstanceBinding<?> providerInstanceBinding) { | |
out.print(providerInstanceBinding.toString()); | |
} | |
public void visit(LinkedProviderBinding<?> linkedProviderBinding) { | |
out.print(linkedProviderBinding.getKey()); | |
} | |
public void visit(ProviderBinding<?> binding) { | |
throw new UnsupportedOperationException(); | |
} | |
public void visit(ClassBinding<?> classBinding) { | |
out.print(renderType(classBinding.getBoundClass())); | |
out.print(".<init>()"); | |
} | |
public void visit(ConstantBinding<?> constantBinding) { | |
out.print(constantBinding.getValue()); | |
} | |
public void visit(ConvertedConstantBinding<?> convertedConstantBinding) { | |
out.print(convertedConstantBinding.getValue()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment