View Javadoc
1   /*******************************************************************************
2    * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 Sebastian
3    * Thomschke.
4    * 
5    * All Rights Reserved. This program and the accompanying materials
6    * are made available under the terms of the Eclipse Public License v1.0
7    * which accompanies this distribution, and is available at
8    * http://www.eclipse.org/legal/epl-v10.html
9    * 
10   * Contributors:
11   *     Sebastian Thomschke - initial implementation.
12   *******************************************************************************/
13  package net.sf.oval.context;
14  
15  import java.lang.reflect.Field;
16  
17  import net.sf.oval.internal.util.ReflectionUtils;
18  import net.sf.oval.internal.util.SerializableField;
19  
20  /**
21   * @author Sebastian Thomschke
22   */
23  public class FieldContext extends OValContext
24  {
25  	private static final long serialVersionUID = 1L;
26  
27  	private final SerializableField field;
28  
29  	/**
30  	 * @param declaringClass
31  	 * @param fieldName
32  	 */
33  	public FieldContext(final Class< ? > declaringClass, final String fieldName)
34  	{
35  		final Field field = ReflectionUtils.getField(declaringClass, fieldName);
36  		this.field = SerializableField.getInstance(field);
37  		compileTimeType = field.getType();
38  	}
39  
40  	/**
41  	 * @param field
42  	 */
43  	public FieldContext(final Field field)
44  	{
45  		this.field = SerializableField.getInstance(field);
46  		compileTimeType = field.getType();
47  	}
48  
49  	/**
50  	 * @return Returns the field.
51  	 */
52  	public Field getField()
53  	{
54  		return field.getField();
55  	}
56  
57  	/**
58  	 * {@inheritDoc}
59  	 */
60  	@Override
61  	public String toString()
62  	{
63  		return field.getDeclaringClass().getName() + "." + field.getName();
64  	}
65  }