001 /******************************************************************************* 002 * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 Sebastian 003 * Thomschke. 004 * 005 * All Rights Reserved. This program and the accompanying materials 006 * are made available under the terms of the Eclipse Public License v1.0 007 * which accompanies this distribution, and is available at 008 * http://www.eclipse.org/legal/epl-v10.html 009 * 010 * Contributors: 011 * Sebastian Thomschke - initial implementation. 012 *******************************************************************************/ 013 package net.sf.oval.context; 014 015 import java.lang.reflect.Field; 016 017 import net.sf.oval.internal.util.ReflectionUtils; 018 import net.sf.oval.internal.util.SerializableField; 019 020 /** 021 * @author Sebastian Thomschke 022 */ 023 public class FieldContext extends OValContext 024 { 025 private static final long serialVersionUID = 1L; 026 027 private final SerializableField field; 028 029 /** 030 * @param declaringClass 031 * @param fieldName 032 */ 033 public FieldContext(final Class< ? > declaringClass, final String fieldName) 034 { 035 final Field field = ReflectionUtils.getField(declaringClass, fieldName); 036 this.field = SerializableField.getInstance(field); 037 compileTimeType = field.getType(); 038 } 039 040 /** 041 * @param field 042 */ 043 public FieldContext(final Field field) 044 { 045 this.field = SerializableField.getInstance(field); 046 compileTimeType = field.getType(); 047 } 048 049 /** 050 * @return Returns the field. 051 */ 052 public Field getField() 053 { 054 return field.getField(); 055 } 056 057 /** 058 * {@inheritDoc} 059 */ 060 @Override 061 public String toString() 062 { 063 return field.getDeclaringClass().getName() + "." + field.getName(); 064 } 065 }