Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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.injection;
 
 import java.util.List;
 
Injector scans fields of an object instance and checks if the specified IFieldValueFactory can provide a value for a field; if it can, the field is set to that value. Injector will ignore all non-null fields.

Author(s):
Igor Vaynberg (ivaynberg)
 
 public abstract class Injector
 {
 	private static final MetaDataKey<InjectorKEY = new MetaDataKey<Injector>()
 	{
 		private static final long serialVersionUID = 1L;
 	};
 
 	private final ClassMetaCache<Field[]> cache = new ClassMetaCache<Field[]>();

Binds current instance of the injector to the Application. After this method is called this instance of injector will be returned from subsequent calls to get() whenever the specified application object is active in the thread.

Parameters:
application
 
 	public void bind(final Application application)
 	{
 		application.setMetaData(this);
 	}

Returns:
Injector associated with the application instance
 
 	public static Injector get()
 	{
 		return Application.get().getMetaData();
 	}

Injects the specified object. This method is usually implemented by delegating to inject(java.lang.Object,org.apache.wicket.injection.IFieldValueFactory) with some IFieldValueFactory

 
 	public abstract void inject(Object object);

traverse fields in the class hierarchy of the object and set their value with a locator provided by the locator factory.

Parameters:
object
factory
 
 	protected void inject(final Object objectfinal IFieldValueFactory factory)
 	{
 		final Class<?> clazz = object.getClass();
 
 		Field[] fields = null;
 
 		// try cache
 		fields = .get(clazz);
 
 		if (fields == null)
 		{
 			// cache miss, discover fields
 			fields = findFields(clazzfactory);
 
 			// write to cache
 			.put(clazzfields);
 		}
 
 		for (final Field field : fields)
		{
			if (!field.isAccessible())
			{
				field.setAccessible(true);
			}
			try
			{
				if (field.get(object) == null)
				{
					Object value = factory.getFieldValue(fieldobject);
					if (value != null)
					{
						field.set(objectvalue);
					}
				}
			}
			{
				throw new RuntimeException("error while injecting object [" + object.toString() +
					"] of type [" + object.getClass().getName() + "]"e);
			}
			{
				throw new RuntimeException("error while injecting object [" + object.toString() +
					"] of type [" + object.getClass().getName() + "]"e);
			}
		}
	}

Returns an array of fields that can be injected using the given field value factory

Parameters:
clazz
factory
Returns:
an array of fields that can be injected using the given field value factory
	private Field[] findFields(Class<?> clazzfinal IFieldValueFactory factory)
	{
		List<Fieldmatched = new ArrayList<Field>();
		while (clazz != null)
		{
			Field[] fields = clazz.getDeclaredFields();
			for (final Field field : fields)
			{
				if (factory.supportsField(field))
				{
					matched.add(field);
				}
			}
			clazz = clazz.getSuperclass();
		}
		return matched.toArray(new Field[matched.size()]);
	}
New to GrepCode? Check out our FAQ X