Repository: incubator-juneau Updated Branches: refs/heads/master d87240712 -> e7ab5a47b
Eliminate Tranform class. Project: http://git-wip-us.apache.org/repos/asf/incubator-juneau/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-juneau/commit/e7ab5a47 Tree: http://git-wip-us.apache.org/repos/asf/incubator-juneau/tree/e7ab5a47 Diff: http://git-wip-us.apache.org/repos/asf/incubator-juneau/diff/e7ab5a47 Branch: refs/heads/master Commit: e7ab5a47b914a98221411f53654a43a5a310edbe Parents: d872407 Author: jamesbognar <[email protected]> Authored: Sun Aug 28 11:19:09 2016 -0400 Committer: jamesbognar <[email protected]> Committed: Sun Aug 28 11:19:09 2016 -0400 ---------------------------------------------------------------------- .../java/org/apache/juneau/BeanContext.java | 18 ++--- .../main/java/org/apache/juneau/ClassMeta.java | 49 ++++++------ .../org/apache/juneau/transform/BeanFilter.java | 24 +++--- .../org/apache/juneau/transform/PojoSwap.java | 7 +- .../org/apache/juneau/transform/Transform.java | 82 -------------------- .../java/org/apache/juneau/JacocoDummyTest.java | 2 - 6 files changed, 51 insertions(+), 131 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e7ab5a47/juneau-core/src/main/java/org/apache/juneau/BeanContext.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/main/java/org/apache/juneau/BeanContext.java b/juneau-core/src/main/java/org/apache/juneau/BeanContext.java index b21270e..2184d6c 100644 --- a/juneau-core/src/main/java/org/apache/juneau/BeanContext.java +++ b/juneau-core/src/main/java/org/apache/juneau/BeanContext.java @@ -37,7 +37,7 @@ import org.apache.juneau.transform.*; * This class servers multiple purposes: * <ul class='spaced-list'> * <li>Provides the ability to wrap beans inside {@link Map} interfaces. - * <li>Serves as a repository for metadata on POJOs, such as associated {@link Transform transforms}, {@link PropertyNamer property namers}, etc... + * <li>Serves as a repository for metadata on POJOs, such as associated {@link BeanFilter beanFilters}, {@link PropertyNamer property namers}, etc... * which are used to tailor how POJOs are serialized and parsed. * <li>Serves as a common utility class for all {@link Serializer Serializers} and {@link Parser Parsers} * for serializing and parsing Java beans. @@ -158,15 +158,15 @@ import org.apache.juneau.transform.*; * See {@link BeanConstructor @BeanConstructor} for more information. * * - * <h5 class='topic'>Transforms</h5> + * <h5 class='topic'>BeanFilters and PojoSwaps</h5> * <p> - * {@link Transform Transforms} are used to tailor how beans and non-beans are handled.<br> - * There are two subclasses of transforms: + * {@link BeanFilter BeanFilters} and {@link PojoSwap PojoSwaps} are used to tailor how beans and POJOs are handled.<br> * <ol class='spaced-list'> * <li>{@link BeanFilter} - Allows you to tailor handling of bean classes. * This class can be considered a programmatic equivalent to the {@link Bean} annotation when * annotating classes are not possible (e.g. you don't have access to the source). - * <li>{@link PojoSwap} - Allows you to convert objects to serializable forms. + * This includes specifying which properties are visible and the ability to programmatically override the execution of properties. + * <li>{@link PojoSwap} - Allows you to swap out non-serializable objects with serializable replacements. * </ol> * <p> * See {@link org.apache.juneau.transform} for more information. @@ -1412,7 +1412,7 @@ public class BeanContext extends Context { /** * Returns the {@link PojoSwap} associated with the specified class, or <jk>null</jk> if there is no - * pojo transform associated with the class. + * pojo swap associated with the class. * * @param <T> The class associated with the transform. * @param c The class associated with the transform. @@ -1422,7 +1422,7 @@ public class BeanContext extends Context { // Note: On first if (c != null) for (PojoSwap f : pojoSwaps) - if (isParentClass(f.forClass(), c)) + if (isParentClass(f.getNormalClass(), c)) return f; return null; } @@ -1435,7 +1435,7 @@ public class BeanContext extends Context { protected boolean hasChildPojoSwaps(Class<?> c) { if (c != null) for (PojoSwap f : pojoSwaps) - if (isParentClass(c, f.forClass())) + if (isParentClass(c, f.getNormalClass())) return true; return false; } @@ -1451,7 +1451,7 @@ public class BeanContext extends Context { protected <T> BeanFilter findBeanFilter(Class<T> c) { if (c != null) for (BeanFilter f : beanFilters) - if (isParentClass(f.forClass(), c)) + if (isParentClass(f.getBeanClass(), c)) return f; return null; } http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e7ab5a47/juneau-core/src/main/java/org/apache/juneau/ClassMeta.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/main/java/org/apache/juneau/ClassMeta.java b/juneau-core/src/main/java/org/apache/juneau/ClassMeta.java index b97ffad..3e57069 100644 --- a/juneau-core/src/main/java/org/apache/juneau/ClassMeta.java +++ b/juneau-core/src/main/java/org/apache/juneau/ClassMeta.java @@ -126,14 +126,9 @@ public final class ClassMeta<T> implements Type { ClassMeta init() { try { - Transform transform = findTransform(beanContext); - if (transform != null) { - if (transform.getType() == Transform.TransformType.BEAN) - beanFilter = (BeanFilter)transform; - else - pojoSwap = (PojoSwap)transform; - transformedClassMeta = (pojoSwap == null ? this : beanContext.getClassMeta(pojoSwap.getSwapClass())); - } + beanFilter = findBeanFilter(beanContext); + pojoSwap = findPojoSwap(beanContext); + transformedClassMeta = (pojoSwap == null ? this : beanContext.getClassMeta(pojoSwap.getSwapClass())); if (transformedClassMeta == null) transformedClassMeta = this; @@ -428,23 +423,12 @@ public final class ClassMeta<T> implements Type { return hasChildPojoSwaps; } - private Transform findTransform(BeanContext context) { + @SuppressWarnings("unchecked") + private BeanFilter<? extends T> findBeanFilter(BeanContext context) { try { - org.apache.juneau.annotation.Pojo b = innerClass.getAnnotation(org.apache.juneau.annotation.Pojo.class); - if (b != null) { - Class<?> c = b.swap(); - if (c != Null.class) { - if (ClassUtils.isParentClass(PojoSwap.class, c)) - return (Transform)c.newInstance(); - throw new RuntimeException("TODO - Surrogate classes not yet supported."); - } - } if (context == null) return null; - Transform f = context.findBeanFilter(innerClass); - if (f != null) - return f; - f = context.findPojoSwap(innerClass); + BeanFilter<? extends T> f = context.findBeanFilter(innerClass); if (f != null) return f; List<Bean> ba = ReflectionUtils.findAnnotations(Bean.class, innerClass); @@ -456,6 +440,27 @@ public final class ClassMeta<T> implements Type { } } + @SuppressWarnings("unchecked") + private PojoSwap<T,?> findPojoSwap(BeanContext context) { + try { + Pojo p = innerClass.getAnnotation(Pojo.class); + if (p != null) { + Class<?> c = p.swap(); + if (c != Null.class) { + if (ClassUtils.isParentClass(PojoSwap.class, c)) + return (PojoSwap<T,?>)c.newInstance(); + throw new RuntimeException("TODO - Surrogate classes not yet supported."); + } + } + if (context == null) + return null; + PojoSwap<T,?> f = context.findPojoSwap(innerClass); + return f; + } catch (Exception e) { + throw new RuntimeException(e); + } + } + /** * Locates the no-arg constructor for the specified class. * Constructor must match the visibility requirements specified by parameter 'v'. http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e7ab5a47/juneau-core/src/main/java/org/apache/juneau/transform/BeanFilter.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/main/java/org/apache/juneau/transform/BeanFilter.java b/juneau-core/src/main/java/org/apache/juneau/transform/BeanFilter.java index 0464fef..27dc211 100644 --- a/juneau-core/src/main/java/org/apache/juneau/transform/BeanFilter.java +++ b/juneau-core/src/main/java/org/apache/juneau/transform/BeanFilter.java @@ -28,9 +28,6 @@ import org.apache.juneau.internal.*; * This class can be considered a programmatic equivalent to using the {@link Bean @Bean} annotation on bean classes. * Thus, it can be used to perform the same function as the <code>@Bean</code> annotation when you don't have * the ability to annotate those classes (e.g. you don't have access to the source code). - * <p> - * Note that value returned by the {@link Transform#forClass()} method is automatically determined through reflection - * when the no-arg constructor is used. * * <p> * When defining bean filters, you can either call the setters in the contructor, or override getters. @@ -67,8 +64,9 @@ import org.apache.juneau.internal.*; * @author James Bognar ([email protected]) * @param <T> The class type that this filter applies to. */ -public abstract class BeanFilter<T> extends Transform { +public abstract class BeanFilter<T> { + private Class<T> beanClass; private String[] properties, excludeProperties; private LinkedHashMap<Class<?>, String> subTypes; private String subTypeAttr; @@ -82,7 +80,6 @@ public abstract class BeanFilter<T> extends Transform { @SuppressWarnings("unchecked") public BeanFilter() { super(); - this.type = TransformType.BEAN; Class<?> c = this.getClass().getSuperclass(); Type t = this.getClass().getGenericSuperclass(); @@ -98,7 +95,7 @@ public abstract class BeanFilter<T> extends Transform { if (pta.length > 0) { Type nType = pta[0]; if (nType instanceof Class) - this.forClass = (Class<T>)nType; + this.beanClass = (Class<T>)nType; else throw new RuntimeException("Unsupported parameter type: " + nType); @@ -125,11 +122,18 @@ public abstract class BeanFilter<T> extends Transform { * </dd> * </dl> * - * @param forClass The class that this bean filter applies to. + * @param beanClass The class that this bean filter applies to. + */ + public BeanFilter(Class<T> beanClass) { + this.beanClass = beanClass; + } + + /** + * Returns the bean class that this filter applies to. + * @return The bean class that this filter applies to. */ - public BeanFilter(Class<T> forClass) { - super(forClass); - this.type = TransformType.BEAN; + public Class<T> getBeanClass() { + return beanClass; } /** http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e7ab5a47/juneau-core/src/main/java/org/apache/juneau/transform/PojoSwap.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/main/java/org/apache/juneau/transform/PojoSwap.java b/juneau-core/src/main/java/org/apache/juneau/transform/PojoSwap.java index deb9092..518fd2a 100644 --- a/juneau-core/src/main/java/org/apache/juneau/transform/PojoSwap.java +++ b/juneau-core/src/main/java/org/apache/juneau/transform/PojoSwap.java @@ -89,7 +89,7 @@ import org.apache.juneau.serializer.*; * @param <T> The normal form of the class. * @param <S> The swapped form of the class. */ -public abstract class PojoSwap<T,S> extends Transform { +public abstract class PojoSwap<T,S> { Class<T> normalClass; Class<S> swapClass; @@ -294,11 +294,6 @@ public abstract class PojoSwap<T,S> extends Transform { // Overridden methods //-------------------------------------------------------------------------------- - @Override /* Transform */ - public Class<?> forClass() { - return normalClass; - } - @Override /* Object */ public String toString() { return getClass().getSimpleName() + '<' + getNormalClass().getSimpleName() + "," + getSwapClass().getSimpleName() + '>'; http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e7ab5a47/juneau-core/src/main/java/org/apache/juneau/transform/Transform.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/main/java/org/apache/juneau/transform/Transform.java b/juneau-core/src/main/java/org/apache/juneau/transform/Transform.java deleted file mode 100644 index 0fa213e..0000000 --- a/juneau-core/src/main/java/org/apache/juneau/transform/Transform.java +++ /dev/null @@ -1,82 +0,0 @@ -/*************************************************************************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - ***************************************************************************************************************************/ -package org.apache.juneau.transform; - -/** - * Parent class for all bean and POJO swaps. - * - * - * <h6 class='topic'>Description</h6> - * <p> - * Transforms are used to alter how POJOs are handled by bean contexts (and subsequently serializers and parsers). - * The are a very powerful feature of the Juneau framework that allows virtually any POJO to be serialized and parsed. - * For example, they can be used to... - * <ul class='spaced-list'> - * <li>Convert a non-serializable POJO into a serializable POJO during serialization (and optionally vis-versa during parsing). - * <li>Control various aspects of beans, such as what properties are visible, bean subclasses, etc... - * </ul> - * <p> - * There are 2 subclasses of transforms: - * <ul class='spaced-list'> - * <li>{@link PojoSwap} - Non-bean filters for converting POJOs into serializable equivalents. - * <li>{@link BeanFilter} - Bean filters for configuring how beans are handled. - * </ul> - * - * - * <h6 class='topic'>Additional information</h6> - * See {@link org.apache.juneau.transform} for more information. - * - * - * @author James Bognar ([email protected]) - */ -public class Transform { - - /** The transform subtype */ - public static enum TransformType { - /** PojoSwap */ - POJO, - /** BeanFilter */ - BEAN - } - - /** The class that this transform applies to. */ - protected Class<?> forClass; - - /** Whether this is a BeanFilter or PojoSwap. */ - protected TransformType type = TransformType.POJO; - - Transform() {} - - Transform(Class<?> forClass) { - this.forClass = forClass; - } - - - /** - * Returns the class that this transform applies to. - * - * @return The class that this transform applies to. - */ - public Class<?> forClass() { - return forClass; - } - - /** - * Returns whether this is an instance of {@link PojoSwap} or {@link BeanFilter}. - * - * @return The transform type. - */ - public TransformType getType() { - return type; - } -} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e7ab5a47/juneau-core/src/test/java/org/apache/juneau/JacocoDummyTest.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/test/java/org/apache/juneau/JacocoDummyTest.java b/juneau-core/src/test/java/org/apache/juneau/JacocoDummyTest.java index 3e6ca8d..85ede56 100755 --- a/juneau-core/src/test/java/org/apache/juneau/JacocoDummyTest.java +++ b/juneau-core/src/test/java/org/apache/juneau/JacocoDummyTest.java @@ -17,7 +17,6 @@ import java.lang.reflect.*; import org.apache.juneau.ini.*; import org.apache.juneau.internal.*; import org.apache.juneau.jena.*; -import org.apache.juneau.transform.*; import org.apache.juneau.xml.annotation.*; import org.junit.*; @@ -41,7 +40,6 @@ public class JacocoDummyTest { } ConfigFileFormat.valueOf(ConfigFileFormat.INI.toString()); - Transform.TransformType.valueOf(Transform.TransformType.POJO.toString()); RdfCollectionFormat.valueOf(RdfCollectionFormat.DEFAULT.toString()); XmlFormat.valueOf(XmlFormat.NORMAL.toString()); Visibility.valueOf(Visibility.DEFAULT.toString());
