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