/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.wicket.guice;

import java.lang.reflect.Field;

import org.apache.wicket.Application;
import org.apache.wicket.Component;
import org.apache.wicket.WicketRuntimeException;
import org.apache.wicket.application.IComponentInstantiationListener;
import org.apache.wicket.proxy.LazyInitProxyFactory;

import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Module;

/**
 * Injects fields/members of components using Guice.
 * 
 * @author Alastair Maw
 */
public class GuiceComponentInjector implements IComponentInstantiationListener
{	
	public GuiceComponentInjector(Application app, Module ... modules)
	{
		this(app, Guice.createInjector(modules));
	}

	public GuiceComponentInjector(Application app, Injector injector)
	{
		app.setMetaData(GuiceInjectorHolder.INJECTOR_KEY, new GuiceInjectorHolder(injector));
	}
	
	public void onInstantiation(Component component)
	{
		Class<?> current = component.getClass();
		do
		{
			Field[] currentFields = current.getDeclaredFields();
			for (final Field field : currentFields)
			{
				if (field.getAnnotation(Inject.class) != null)
				{
					Object proxy = LazyInitProxyFactory.createProxy(field.getType(), new GuiceProxyTargetLocator(field.getType()));
					try {
						if (!field.isAccessible())
						{
							field.setAccessible(true);
						}
						field.set(component, proxy);
					}
					catch (IllegalAccessException e) {
						throw new WicketRuntimeException("Error Guice-injecting field " + field.getName() + " in " + component, e);
					}
				}
			}
			current = current.getSuperclass();
		}
		// use null check just in case Component is in a different classloader.
		while (current != null && current != Component.class);
	}
}

@@ -1142,16 +1142,56 @@
 .Guice;%0A
+import com.google.inject.ImplementedBy;%0A
 import c
@@ -1348,237 +1348,1438 @@
 %0A * 
-%0A * @author Alastair Maw%0A */%0Apublic class GuiceComponentInjector implements IComponentInstantiationListener%0A%7B%09%0A%09public GuiceComponentInjector(Application app, Module ... modules)%0A%09%7B%0A%09%09this(app, Guice.createInjector(modules));%0A%09%7D%0A
+%3Cp%3E%0A * Add this to your application in its %7B@link Application#init()%7D method like%0A * so:%0A * %3Cpre%3E%0A * addComponentInstantiationListener(new GuiceComponentInjector(this));%0A * %3C/pre%3E%0A * %3Cp%3E%0A * There are different constructors for this object depending on how you want to%0A * wire things. See the javadoc for the constructors for more information.%0A * %0A * @author Alastair Maw%0A */%0Apublic class GuiceComponentInjector implements IComponentInstantiationListener%0A%7B%0A%09/**%0A%09 * Creates a new Wicket GuiceComponentInjector instance.%0A%09 * %3Cp%3E%0A%09 * Internally this will create a new Guice %7B@link Injector%7D instance, with%0A%09 * no %7B@link Module%7D instances. This is only useful if your beans have%0A%09 * appropriate %7B@link ImplementedBy%7D annotations on them so that they can be%0A%09 * automatically picked up with no extra configuration code.%0A%09 * %0A%09 * @param app%0A%09 */%0A%09public GuiceComponentInjector(Application app)%0A%09%7B%0A%09%09this(app, new Module%5B0%5D);%0A%09%7D%0A%09%0A%09/**%0A%09 * Creates a new Wicket GuiceComponentInjector instance, using the supplied%0A%09 * Guice %7B@link Module%7D instances to create a new Guice %7B@link Injector%7D%0A%09 * instance internally.%0A%09 * %0A%09 * @param app%0A%09 * @param modules%0A%09 */%0A%09public GuiceComponentInjector(Application app, Module ... modules)%0A%09%7B%0A%09%09this(app, Guice.createInjector(modules));%0A%09%7D%0A%0A%09/**%0A%09 * Creates a new Wicket GuiceComponentInjector instance, using the provided%0A%09 * Guice %7B@link Injector%7D instance.%0A%09 * %0A%09 * @param app%0A%09 * @param modules%0A%09 */
 %0A%09pu

Improve javadoc.