@Test(expected = CreationException.class) public void tooManyConstructors() throws Exception { Guice.createInjector(new BaseModule() { @Override protected void configure() { bind(String.class).toInstance("foo"); bind(FooTooMany.class).toConstructor(getConstructor(FooTooMany.class)); } }); }
@SuppressWarnings("unchecked") protected <T> Constructor<T> getConstructor(Class<? extends T> cls) { Constructor<?>[] constructors = cls.getDeclaredConstructors(); if (constructors.length != 1) { final String msg = "a single constructor is required, class " + cls.getName() + " has " + constructors.length + " constructors"; throw new IllegalArgumentException(msg); } return (Constructor<T>) constructors[0]; }
BaseModule extends AbstractModule { @SuppressWarnings("unchecked") protected <T> Constructor<T> getConstructor(Class<? extends T> cls) { Constructor<?>[] constructors = cls.getDeclaredConstructors(); if (constructors.length != 1) { final String msg = "a single constructor is required, class " + cls.getName() + " has " + constructors.length + " constructors"; throw new IllegalArgumentException(msg); } return (Constructor<T>) constructors[0]; } }
BaseModule extends AbstractModule { @SuppressWarnings("unchecked") protected <T> Constructor<T> getConstructor(Class<? extends T> cls) { Constructor<?>[] constructors = cls.getDeclaredConstructors(); if (constructors.length != 1) { final String msg = "a single constructor is required, class " + cls.getName() + " has " + constructors.length + " constructors"; throw new IllegalArgumentException(msg); } return (Constructor<T>) constructors[0]; }  }
BaseModule extends AbstractModule { @SuppressWarnings("unchecked") protected <T> Constructor<T> getConstructor(Class<? extends T> cls) { Constructor<?>[] constructors = cls.getDeclaredConstructors(); if (constructors.length != 1) { final String msg = "a single constructor is required, class " + cls.getName() + " has " + constructors.length + " constructors"; throw new IllegalArgumentException(msg); } return (Constructor<T>) constructors[0]; }   }
BaseModule extends AbstractModule { @SuppressWarnings("unchecked") protected <T> Constructor<T> getConstructor(Class<? extends T> cls) { Constructor<?>[] constructors = cls.getDeclaredConstructors(); if (constructors.length != 1) { final String msg = "a single constructor is required, class " + cls.getName() + " has " + constructors.length + " constructors"; throw new IllegalArgumentException(msg); } return (Constructor<T>) constructors[0]; }    }