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.swap;
018
019import static org.apache.juneau.commons.utils.CollectionUtils.*;
020import static org.apache.juneau.commons.utils.Utils.*;
021import static org.apache.juneau.internal.ClassUtils2.*;
022
023import java.lang.reflect.*;
024import java.util.*;
025
026import org.apache.juneau.*;
027import org.apache.juneau.annotation.*;
028import org.apache.juneau.commons.reflect.*;
029import org.apache.juneau.parser.*;
030import org.apache.juneau.serializer.*;
031
032/**
033 * A dynamic object swap based on reflection of a Java class that converts Objects to serializable Maps.
034 *
035 * <p>
036 * Looks for methods on the class that can be called to swap-in surrogate Map objects before serialization and swap-out
037 * surrogate Map objects after parsing.
038 *
039 * <h5 class='figure'>Valid surrogate objects</h5>
040 * <ul>
041 *    <li class='jc'>Any subclass of {@link Map}
042 * </ul>
043 *
044 * <h5 class='figure'>Valid swap methods (S = Swapped type)</h5>
045 * <ul>
046 *    <li class='jm'><c><jk>public</jk> S toMap()</c>
047 *    <li class='jm'><c><jk>public</jk> S toMap(BeanSession)</c>
048 *    <li class='jm'><c><jk>public</jk> S toJsonMap()</c>
049 *    <li class='jm'><c><jk>public</jk> S toJsonMap(BeanSession)</c>
050 * </ul>
051 *
052 * <h5 class='figure'>Valid unswap methods (N = Normal type, S = Swapped type)</h5>
053 * <ul>
054 *    <li class='jm'><c><jk>public static</jk> N fromMap(S)</c>
055 *    <li class='jm'><c><jk>public static</jk> N fromMap(BeanSession, S)</c>
056 *    <li class='jm'><c><jk>public static</jk> N fromJsonMap(S)</c>
057 *    <li class='jm'><c><jk>public static</jk> N fromJsonMap(BeanSession, S)</c>
058 *    <li class='jm'><c><jk>public static</jk> N create(S)</c>
059 *    <li class='jm'><c><jk>public static</jk> N create(BeanSession, S)</c>
060 *    <li class='jm'><c><jk>public static</jk> N valueOf(S)</c>
061 *    <li class='jm'><c><jk>public static</jk> N valueOf(BeanSession, S)</c>
062 *    <li class='jm'><c><jk>public</jk> N(S)</c>
063 * </ul>
064 *
065 * <p>
066 * Classes are ignored if any of the following are true:
067 * <ul>
068 *    <li>Classes annotated with {@link BeanIgnore @BeanIgnore}.
069 *    <li>Non-static member classes.
070 * </ul>
071 *
072 * <p>
073 * Members/constructors are ignored if any of the following are true:
074 * <ul>
075 *    <li>Members/constructors annotated with {@link BeanIgnore @BeanIgnore}.
076 *    <li>Deprecated members/constructors.
077 * </ul>
078 *
079 * <h5 class='section'>See Also:</h5><ul>
080 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/SwapBasics">Swap Basics</a>
081 * </ul>
082 *
083 * @param <T> The normal class type.
084 */
085public class AutoMapSwap<T> extends ObjectSwap<T,Map<?,?>> {
086
087   // @formatter:off
088   private static final Set<String>
089      SWAP_METHOD_NAMES = u(set("toMap", "toJsonMap")),
090      UNSWAP_METHOD_NAMES = u(set("fromMap", "fromJsonMap", "create", "valueOf"));
091   // @formatter:on
092
093   /**
094    * Look for constructors and methods on this class and construct a dynamic swap if it's possible to do so.
095    *
096    * @param bc The bean context to use for looking up annotations.
097    * @param ci The class to try to constructor a dynamic swap on.
098    * @return An object swap instance, or <jk>null</jk> if one could not be created.
099    */
100   @SuppressWarnings({ "rawtypes" })
101   public static ObjectSwap<?,?> find(BeanContext bc, ClassInfo ci) {
102
103      if (shouldIgnore(bc, ci))
104         return null;
105
106      // Find swap() method if present.
107      for (var m : ci.getAllMethods()) {
108         if (isSwapMethod(bc, m)) {
109
110            var rt = m.getReturnType();
111
112            var mi = ci.getMethod(x -> isUnswapMethod(bc, x, ci, rt)).orElse(null);
113            if (nn(mi))
114               return new AutoMapSwap(bc, ci, m, mi, null);
115
116            var cs = ci.getDeclaredConstructor(x -> isUnswapConstructor(bc, x, rt)).orElse(null);
117            if (nn(cs))
118               return new AutoMapSwap(bc, ci, m, null, cs);
119
120            return new AutoMapSwap(bc, ci, m, null, null);
121         }
122      }
123
124      return null;
125   }
126
127   private static boolean isSwapMethod(BeanContext bc, MethodInfo mi) {
128      // @formatter:off
129      return
130         mi.isNotDeprecated()
131         && mi.isNotStatic()
132         && mi.isVisible(bc.getBeanMethodVisibility())
133         && mi.hasAnyName(SWAP_METHOD_NAMES)
134         && mi.hasReturnTypeParent(Map.class)
135         && mi.hasParameterTypesLenient(BeanSession.class)
136         && ! mi.getMatchingMethods().stream().anyMatch(m2 -> bc.getAnnotationProvider().has(BeanIgnore.class, m2));
137      // @formatter:on
138   }
139
140   private static boolean isUnswapConstructor(BeanContext bc, ConstructorInfo cs, ClassInfo rt) {
141      // @formatter:off
142      return
143         cs.isNotDeprecated()
144            && cs.isVisible(bc.getBeanConstructorVisibility())
145            && cs.hasParameterTypeParents(rt)
146            && ! bc.getAnnotationProvider().has(BeanIgnore.class, cs);
147      // @formatter:on
148   }
149
150   private static boolean isUnswapMethod(BeanContext bc, MethodInfo mi, ClassInfo ci, ClassInfo rt) {
151      // @formatter:off
152      return
153         mi.isNotDeprecated()
154         && mi.isStatic()
155         && mi.isVisible(bc.getBeanMethodVisibility())
156         && mi.hasAnyName(UNSWAP_METHOD_NAMES)
157         && mi.hasParameterTypesLenient(BeanSession.class, rt.inner())
158         && mi.hasReturnTypeParent(ci)
159         && ! mi.getMatchingMethods().stream().anyMatch(m2 -> bc.getAnnotationProvider().has(BeanIgnore.class, m2));
160      // @formatter:on
161   }
162
163   private static boolean shouldIgnore(BeanContext bc, ClassInfo ci) {
164      return ci.isNonStaticMemberClass() || bc.getAnnotationProvider().has(BeanIgnore.class, ci);
165   }
166
167   //------------------------------------------------------------------------------------------------------------------
168
169   private final Method swapMethod, unswapMethod;
170   private final Constructor<?> unswapConstructor;
171
172   private AutoMapSwap(BeanContext bc, ClassInfo ci, MethodInfo swapMethod, MethodInfo unswapMethod, ConstructorInfo unswapConstructor) {
173      super(ci.inner(), swapMethod.inner().getReturnType());
174      this.swapMethod = bc.getBeanMethodVisibility().transform(swapMethod.inner());
175      this.unswapMethod = unswapMethod == null ? null : bc.getBeanMethodVisibility().transform(unswapMethod.inner());
176      this.unswapConstructor = unswapConstructor == null ? null : bc.getBeanConstructorVisibility().transform(unswapConstructor.inner());
177   }
178
179   @Override /* Overridden from ObjectSwap */
180   public Map<?,?> swap(BeanSession session, Object o) throws SerializeException {
181      try {
182         return (Map<?,?>)swapMethod.invoke(o, getMatchingArgs(swapMethod.getParameterTypes(), session));
183      } catch (Exception e) {
184         throw SerializeException.create(e);
185      }
186   }
187
188   @SuppressWarnings("unchecked")
189   @Override /* Overridden from ObjectSwap */
190   public T unswap(BeanSession session, Map<?,?> o, ClassMeta<?> hint) throws ParseException {
191      try {
192         if (nn(unswapMethod))
193            return (T)unswapMethod.invoke(null, getMatchingArgs(unswapMethod.getParameterTypes(), session, o));
194         if (nn(unswapConstructor))
195            return (T)unswapConstructor.newInstance(o);
196         return super.unswap(session, o, hint);
197      } catch (Exception e) {
198         throw ParseException.create(e);
199      }
200   }
201}