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