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 ExecutableException Exception occurred on invoked constructor/method/field.
028    */
029   void set(Object object, Object value) throws ExecutableException;
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 ExecutableException {
044         try {
045            f.set(object, value);
046         } catch (IllegalArgumentException | IllegalAccessException e) {
047            throw new ExecutableException(e);
048         }
049      }
050   }
051
052   /**
053    * Method setter
054    */
055   static class MethodSetter implements Setter {
056
057      private final Method m;
058
059      public MethodSetter(Method m) {
060         this.m = m;
061      }
062
063      @Override /* Setter */
064      public void set(Object object, Object value) throws ExecutableException {
065         try {
066            m.invoke(object, value);
067         } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
068            throw new ExecutableException(e);
069         }
070      }
071   }
072}