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.transform; 014 015import java.lang.reflect.*; 016import java.util.*; 017 018import org.apache.juneau.*; 019import org.apache.juneau.annotation.*; 020import org.apache.juneau.parser.*; 021import org.apache.juneau.reflect.*; 022import org.apache.juneau.serializer.*; 023 024/** 025 * Specialized {@link PojoSwap} for {@link Surrogate} classes. 026 * 027 * @param <T> The class type that this transform applies to. 028 * @param <F> The transformed class type. 029 */ 030public class SurrogateSwap<T,F> extends PojoSwap<T,F> { 031 032 private Constructor<F> constructor; // public F(T t); 033 private Method unswapMethod; // public T build(); 034 035 /** 036 * Constructor. 037 * 038 * @param forClass The normal class. 039 * @param constructor The constructor on the surrogate class that takes the normal class as a parameter. 040 * @param unswapMethod The static method that converts surrogate objects into normal objects. 041 */ 042 protected SurrogateSwap(Class<T> forClass, Constructor<F> constructor, Method unswapMethod) { 043 super(forClass, constructor.getDeclaringClass()); 044 this.constructor = constructor; 045 this.unswapMethod = unswapMethod; 046 } 047 048 /** 049 * Given the specified surrogate class, return the list of POJO swaps. 050 * 051 * <p> 052 * A transform is returned for each public 1-arg constructor found. 053 * Returns an empty list if no public 1-arg constructors are found. 054 * 055 * @param c The surrogate class. 056 * @param bc The bean context to use for looking up annotations. 057 * @return The list of POJO swaps that apply to this class. 058 */ 059 @SuppressWarnings({"unchecked", "rawtypes"}) 060 public static List<SurrogateSwap<?,?>> findPojoSwaps(Class<?> c, BeanContext bc) { 061 List<SurrogateSwap<?,?>> l = new LinkedList<>(); 062 ClassInfo ci = ClassInfo.of(c); 063 for (ConstructorInfo cc : ci.getPublicConstructors()) { 064 if (! bc.hasAnnotation(BeanIgnore.class, cc) && cc.hasNumParams(1) && cc.isPublic()) { 065 Class<?> pt = cc.getRawParamType(0); 066 if (! pt.equals(c.getDeclaringClass())) { 067 // Find the unswap method if there is one. 068 Method unswapMethod = null; 069 for (MethodInfo m : ci.getPublicMethods()) { 070 if (m.getReturnType().is(pt) && m.isPublic()) 071 unswapMethod = m.inner(); 072 } 073 074 l.add(new SurrogateSwap(pt, cc.inner(), unswapMethod)); 075 } 076 } 077 } 078 return l; 079 } 080 081 @Override /* PojoSwap */ 082 public F swap(BeanSession session, T o) throws SerializeException { 083 try { 084 return constructor.newInstance(o); 085 } catch (Exception e) { 086 throw new SerializeException(e); 087 } 088 } 089 090 @Override /* PojoSwap */ 091 @SuppressWarnings("unchecked") 092 public T unswap(BeanSession session, F f, ClassMeta<?> hint) throws ParseException { 093 if (unswapMethod == null) 094 throw new ParseException("unswap() method not implement on surrogate class ''{1}''", 095 f.getClass().getName(), getNormalClass().getFullName()); 096 try { 097 return (T)unswapMethod.invoke(f); 098 } catch (Exception e) { 099 throw new ParseException(e); 100 } 101 } 102}