001: /**
002: * Copyright (C) 2006 Google Inc.
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: */package com.google.inject;
016:
017: import com.google.inject.name.Named;
018: import com.google.inject.name.Names;
019: import java.lang.annotation.ElementType;
020: import java.lang.annotation.Retention;
021: import static java.lang.annotation.RetentionPolicy.RUNTIME;
022: import java.lang.annotation.Target;
023: import java.util.List;
024:
025: /**
026: * @author crazybob@google.com (Bob Lee)
027: */
028: public class ErrorHandlingTest {
029:
030: public static void main(String[] args) throws CreationException {
031: try {
032: Guice.createInjector(new MyModule());
033: } catch (CreationException e) {
034: e.printStackTrace();
035: System.err.println("--");
036: }
037:
038: Injector bad = Guice.createInjector(new AbstractModule() {
039: protected void configure() {
040: bind(String.class).toProvider(new Provider<String>() {
041: public String get() {
042: return null;
043: }
044: });
045: }
046: });
047: try {
048: bad.getInstance(String.class);
049: } catch (Exception e) {
050: e.printStackTrace();
051: System.err.println("--");
052: }
053: try {
054: bad.getInstance(NeedsString.class);
055: } catch (Exception e) {
056: e.printStackTrace();
057: System.err.println("--");
058: }
059: }
060:
061: static class NeedsString {
062: @Inject
063: String mofo;
064: }
065:
066: @Inject
067: @Named("missing")
068: static List<String> missing = null;
069:
070: static class Foo {
071: @Inject
072: public Foo(Runnable r) {
073: }
074:
075: @Inject
076: void setNames(List<String> names) {
077: }
078: }
079:
080: static class Bar {
081: // Invalid constructor.
082: Bar(String s) {
083: }
084:
085: @Inject
086: void setNumbers(@Named("numbers")
087: List<Integer> numbers) {
088: }
089:
090: @Inject
091: void bar(@Named("foo")
092: String s) {
093: }
094: }
095:
096: static class Tee {
097: @Inject
098: String s;
099:
100: @Inject
101: void tee(String s, int i) {
102: }
103:
104: @Inject
105: Invalid invalid;
106: }
107:
108: static class Invalid {
109: Invalid(String s) {
110: }
111: }
112:
113: @Singleton
114: @GoodScope
115: static class TooManyScopes {
116: }
117:
118: @Target(ElementType.TYPE)
119: @Retention(RUNTIME)
120: @ScopeAnnotation
121: @interface GoodScope {
122: }
123:
124: @interface BadScope {
125: }
126:
127: @ImplementedBy(String.class)
128: interface I {
129: }
130:
131: static class MyModule extends AbstractModule {
132: protected void configure() {
133: bind(Runnable.class);
134: bind(Foo.class);
135: bind(Bar.class);
136: bind(Tee.class);
137: bind(new TypeLiteral<List<String>>() {
138: });
139: bind(String.class).annotatedWith(Names.named("foo")).in(
140: Named.class);
141: bind(Key.get(Runnable.class)).to(Key.get(Runnable.class));
142: bind(TooManyScopes.class);
143: bindScope(BadScope.class, Scopes.SINGLETON);
144: bind(Object.class).toInstance(new Object() {
145: @Inject
146: void foo() {
147: throw new RuntimeException();
148: }
149: });
150: requestStaticInjection(ErrorHandlingTest.class);
151:
152: addError("I don't like %s", "you");
153:
154: Object o = "2";
155: try {
156: Integer i = (Integer) o;
157: } catch (Exception e) {
158: addError(e);
159: }
160:
161: bind(Module.class).toInstance(this );
162: bind(I.class);
163: }
164: }
165: }
|