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.marshall.swaps;
018
019import org.apache.juneau.marshall.*;
020import org.apache.juneau.marshall.swap.spi.*;
021
022/**
023 * Thin delegating swap for {@link Class} values driven by the context-configured {@link ClassFormat}.
024 *
025 * <p>
026 * Replaces the legacy {@code ClassSwap} (deleted in 9.6) — provides format-aware {@link Class}
027 * serialization for root-level / map-key / map-value / collection-element positions where no
028 * per-property {@link MarshalledProp#classFormat() @MarshalledProp(classFormat=…)} or
029 * {@link Marshalled#classFormat() @Marshalled(classFormat=…)} swap is installed.
030 *
031 * <p>
032 * Reads the context's {@link MarshallingContext#getClassFormat()} at swap/unswap time so changes to the
033 * setting propagate without rebuilding {@link DefaultSwaps}. When the context is at
034 * {@link ClassFormat#NOT_SET} the swap falls through to {@link ClassFormat#FQCN}.
035 *
036 * <h5 class='section'>See Also:</h5><ul>
037 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/Swaps">Swap</a>
038 * </ul>
039 */
040public class ClassFormatSwap extends StringSwap<Class<?>> {
041
042   @Override /* Overridden from ObjectSwap */
043   public String swap(MarshallingSession session, Class<?> o) {
044      if (o == null)
045         return null;
046      var fmt = resolveFormat(session);
047      return ClassFormat.format(o, fmt);
048   }
049
050   @Override /* Overridden from ObjectSwap */
051   public Class<?> unswap(MarshallingSession session, String o, ClassMeta<?> hint) {
052      if (o == null)
053         return null;
054      var fmt = resolveFormat(session);
055      ClassLoader cl = session != null ? session.getClassLoader() : null;
056      if (cl == null)
057         cl = Thread.currentThread().getContextClassLoader();
058      return ClassFormat.parse(o, fmt, cl);
059   }
060
061   private static ClassFormat resolveFormat(MarshallingSession session) {
062      if (session == null)
063         return ClassFormat.FQCN;
064      var fmt = session.getMarshallingContext().getClassFormat();
065      return (fmt == null || fmt == ClassFormat.NOT_SET) ? ClassFormat.FQCN : fmt;
066   }
067}