001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau;
014
015import java.lang.reflect.*;
016
017/**
018 * Encapsulate a bean setter method that may be a method or field.
019 */
020public interface Setter {
021
022   /**
023    * Call the setter on the specified object.
024    *
025    * @param object The object to call the setter on
026    * @param value The value to set.
027    * @throws Exception
028    */
029   void set(Object object, Object value) throws Exception;
030
031   /**
032    * Field setter
033    */
034   static class FieldSetter implements Setter {
035
036      private final Field f;
037
038      public FieldSetter(Field f) {
039         this.f = f;
040      }
041
042      @Override /* Setter */
043      public void set(Object object, Object value) throws Exception {
044         f.set(object, value);
045      }
046   }
047
048   /**
049    * Method setter
050    */
051   static class MethodSetter implements Setter {
052
053      private final Method m;
054
055      public MethodSetter(Method m) {
056         this.m = m;
057      }
058
059      @Override /* Setter */
060      public void set(Object object, Object value) throws Exception {
061         m.invoke(object, value);
062      }
063   }
064}