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.internal;
014
015import java.lang.reflect.*;
016
017import org.apache.juneau.*;
018
019/**
020 * Encapsulate a bean setter method that may be a method or field.
021 *
022 * <h5 class='section'>See Also:</h5><ul>
023 * </ul>
024 */
025public interface Setter {
026
027   /**
028    * Call the setter on the specified object.
029    *
030    * @param object The object to call the setter on
031    * @param value The value to set.
032    * @throws ExecutableException Exception occurred on invoked constructor/method/field.
033    */
034   void set(Object object, Object value) throws ExecutableException;
035
036   /**
037    * Field setter
038    */
039   static class FieldSetter implements Setter {
040
041      private final Field f;
042
043      public FieldSetter(Field f) {
044         this.f = f;
045      }
046
047      @Override /* Setter */
048      public void set(Object object, Object value) throws ExecutableException {
049         try {
050            f.set(object, value);
051         } catch (IllegalArgumentException | IllegalAccessException e) {
052            throw new ExecutableException(e);
053         }
054      }
055   }
056
057   /**
058    * Method setter
059    */
060   static class MethodSetter implements Setter {
061
062      private final Method m;
063
064      public MethodSetter(Method m) {
065         this.m = m;
066      }
067
068      @Override /* Setter */
069      public void set(Object object, Object value) throws ExecutableException {
070         try {
071            m.invoke(object, value);
072         } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
073            throw new ExecutableException(e);
074         }
075      }
076   }
077}