001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.core.util;
018:
019: import java.lang.reflect.Field;
020:
021: /**
022: *
023: * @author kimchy
024: */
025: public class FieldInvoker {
026:
027: private Class targetClass;
028:
029: private String targetField;
030:
031: private Field fieldObject;
032:
033: private Object targetObject;
034:
035: private Object targetValue;
036:
037: public FieldInvoker(Class targetClass, String tagetField) {
038: setTargetClass(targetClass);
039: setTargetField(tagetField);
040: }
041:
042: public FieldInvoker prepare() throws NoSuchFieldException {
043: Assert.notNull(this .targetClass, "targetClass is required");
044: Assert.notNull(this .targetField, "targetMethod is required");
045:
046: this .fieldObject = targetClass
047: .getDeclaredField(this .targetField);
048: this .fieldObject.setAccessible(true);
049:
050: return this ;
051: }
052:
053: /**
054: * Set the target class on which to call the target method.
055: * Only necessary when the target method is static; else,
056: * a target object needs to be specified anyway.
057: * @see #setTargetObject
058: */
059: public void setTargetClass(Class targetClass) {
060: this .targetClass = targetClass;
061: }
062:
063: /**
064: * Return the target class on which to call the target method.
065: */
066: public Class getTargetClass() {
067: return targetClass;
068: }
069:
070: /**
071: * Set the target object on which to call the target method.
072: * Only necessary when the target method is not static;
073: * else, a target class is sufficient.
074: * @see #setTargetClass
075: */
076: public void setTargetObject(Object targetObject) {
077: this .targetObject = targetObject;
078: if (targetObject != null) {
079: this .targetClass = targetObject.getClass();
080: }
081: }
082:
083: /**
084: * Return the target object on which to call the target method.
085: */
086: public Object getTargetObject() {
087: return targetObject;
088: }
089:
090: /**
091: * Set the name of the field to be invoked.
092: * @see #setTargetClass
093: * @see #setTargetObject
094: */
095: public void setTargetField(String targetField) {
096: this .targetField = targetField;
097: }
098:
099: /**
100: * Return the name of the field to be invoked.
101: */
102: public String getTargetField() {
103: return targetField;
104: }
105:
106: public Object getTargetValue() {
107: return targetValue;
108: }
109:
110: /**
111: * Sets the value that will be set to the field
112: */
113: public void setTargetValue(Object targetValue) {
114: this .targetValue = targetValue;
115: }
116:
117: public Object get() throws IllegalAccessException {
118: return get(this .targetObject);
119: }
120:
121: public Object get(Object targetObject)
122: throws IllegalAccessException {
123: Assert.notNull(targetObject, "targetObject is required");
124: return this .fieldObject.get(targetObject);
125: }
126:
127: public void set() throws IllegalAccessException {
128: set(this .targetObject, this .targetValue);
129: }
130:
131: public void set(Object targetObject, Object targetValue)
132: throws IllegalAccessException {
133: Assert.notNull(targetObject, "targetObject is required");
134: Assert.notNull(targetValue, "targetVaue is required");
135: this.fieldObject.set(targetObject, targetValue);
136: }
137: }
|