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 static org.apache.juneau.internal.ClassUtils.*;
016
017import java.lang.reflect.*;
018import java.util.*;
019
020import org.apache.juneau.json.*;
021
022/**
023 * Provides an {@link InvocationHandler} for creating beans from bean interfaces.
024 *
025 * <p>
026 * If the {@code useInterfaceProxies} setting is enabled in {@link BeanContext}, this is the class that creates
027 * instances of beans from interfaces.
028 *
029 * @param <T> The interface class
030 */
031public class BeanProxyInvocationHandler<T> implements InvocationHandler {
032
033   private final BeanMeta<T> meta;                 // The BeanMeta for this instance
034   private Map<String, Object> beanProps;    // The map of property names to bean property values.
035
036   /**
037    * Constructs with the specified {@link BeanMeta}.
038    *
039    * @param meta The bean meta data.
040    */
041   public BeanProxyInvocationHandler(BeanMeta<T> meta) {
042      this.meta = meta;
043      this.beanProps = new HashMap<>();
044   }
045
046   /**
047    * Implemented to handle the method called.
048    */
049   @Override /* InvocationHandler */
050   public Object invoke(Object proxy, Method method, Object[] args) {
051      Class<?>[] paramTypes = method.getParameterTypes();
052      if (hasName(method, "equals") && hasArgs(method, java.lang.Object.class)) {
053         Object arg = args[0];
054         if (arg == null)
055            return false;
056         if (proxy == arg)
057            return true;
058         if (proxy.getClass() == arg.getClass()) {
059            InvocationHandler ih = Proxy.getInvocationHandler(arg);
060            if (ih instanceof BeanProxyInvocationHandler) {
061               return this.beanProps.equals(((BeanProxyInvocationHandler<?>)ih).beanProps);
062            }
063         }
064         BeanMap<Object> bean = this.meta.ctx.createSession().toBeanMap(arg);
065         return this.beanProps.equals(bean);
066      }
067
068      if (hasName(method, "hashCode") && (paramTypes.length == 0))
069         return Integer.valueOf(this.beanProps.hashCode());
070
071      if (hasName(method, "toString") && (paramTypes.length == 0))
072         return SimpleJsonSerializer.DEFAULT.toString(this.beanProps);
073
074      String prop = this.meta.getterProps.get(method);
075      if (prop != null)
076         return this.beanProps.get(prop);
077
078      prop = this.meta.setterProps.get(method);
079      if (prop != null) {
080         this.beanProps.put(prop, args[0]);
081         return null;
082      }
083
084      throw new UnsupportedOperationException("Unsupported bean method.  method=[ " + method + " ]");
085   }
086}