You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@juneau.apache.org by ja...@apache.org on 2019/08/09 16:55:53 UTC

[juneau] branch master updated: Clean up unused AutoStringSwap class.

This is an automated email from the ASF dual-hosted git repository.

jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git


The following commit(s) were added to refs/heads/master by this push:
     new 9be96f5  Clean up unused AutoStringSwap class.
9be96f5 is described below

commit 9be96f505669fc139c99096d5eb92ec81e3b40f9
Author: JamesBognar <ja...@apache.org>
AuthorDate: Fri Aug 9 12:55:03 2019 -0400

    Clean up unused AutoStringSwap class.
---
 .../juneau/transform/AutoStringSwapTest.java       | 271 ---------------------
 .../src/main/java/org/apache/juneau/ClassMeta.java |   3 -
 .../apache/juneau/transform/AutoStringSwap.java    | 147 -----------
 .../org/apache/juneau/transform/DefaultSwaps.java  |   1 +
 .../org/apache/juneau/transforms/ClassSwap.java    |  32 +++
 .../11.Transforms/02.DefaultPojoSwaps.html         |   3 +-
 .../{07.SwapMethods.html => 03.AutoPojoSwaps.html} |   2 +-
 7 files changed, 36 insertions(+), 423 deletions(-)

diff --git a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/transform/AutoStringSwapTest.java b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/transform/AutoStringSwapTest.java
deleted file mode 100644
index 87e2e73..0000000
--- a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/transform/AutoStringSwapTest.java
+++ /dev/null
@@ -1,271 +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;
-
-import static org.junit.Assert.*;
-
-import java.util.*;
-
-import org.apache.juneau.annotation.*;
-import org.apache.juneau.parser.*;
-import org.apache.juneau.reflect.*;
-import org.junit.*;
-import org.junit.runners.*;
-
-@FixMethodOrder(MethodSorters.NAME_ASCENDING)
-@SuppressWarnings({"unchecked","rawtypes"})
-public class AutoStringSwapTest {
-
-	private static PojoSwap find(Class<?> c) {
-		return AutoStringSwap.find(ClassInfo.of(c));
-	}
-
-	//------------------------------------------------------------------------------------------------------------------
-	// Unswap methods
-	//------------------------------------------------------------------------------------------------------------------
-
-	public static class B01 {
-		public static B01 fromString(String o) {
-			assertEquals("foo", o);
-			return new B01();
-		}
-	}
-	public static class B02 {
-		public static B02 fromValue(String o) {
-			assertEquals("foo", o);
-			return new B02();
-		}
-	}
-	public static class B03 {
-		public static B03 valueOf(String o) {
-			assertEquals("foo", o);
-			return new B03();
-		}
-	}
-	public static class B04 {
-		public static B04 parse(String o) {
-			assertEquals("foo", o);
-			return new B04();
-		}
-	}
-	public static class B05 {
-		public static B05 parseString(String o) {
-			assertEquals("foo", o);
-			return new B05();
-		}
-	}
-	public static class B06 {
-		public static B06 forName(String o) {
-			assertEquals("foo", o);
-			return new B06();
-		}
-	}
-	public static class B07 {
-		public static B07 forString(String o) {
-			assertEquals("foo", o);
-			return new B07();
-		}
-	}
-
-	@Test
-	public void b01_unswap_fromString() throws Exception {
-		assertNotNull(find(B01.class).unswap(null, "foo", null));
-	}
-
-	@Test
-	public void b02_unswap_fromValue() throws Exception {
-		assertNotNull(find(B02.class).unswap(null, "foo", null));
-	}
-
-	@Test
-	public void b03_unswap_valueOf() throws Exception {
-		assertNotNull(find(B03.class).unswap(null, "foo", null));
-	}
-
-	@Test
-	public void b04_unswap_parse() throws Exception {
-		assertNotNull(find(B04.class).unswap(null, "foo", null));
-	}
-
-	@Test
-	public void b05_unswap_parseString() throws Exception {
-		assertNotNull(find(B05.class).unswap(null, "foo", null));
-	}
-
-	@Test
-	public void b06_unswap_forName() throws Exception {
-		assertNotNull(find(B06.class).unswap(null, "foo", null));
-	}
-
-	@Test
-	public void b07_unswap_forString() throws Exception {
-		assertNotNull(find(B07.class).unswap(null, "foo", null));
-	}
-
-	//------------------------------------------------------------------------------------------------------------------
-	// Unswap constructor
-	//------------------------------------------------------------------------------------------------------------------
-
-	public static class C01 {
-		public C01() {}
-		public C01(String o) {
-			assertEquals("foo", o);
-		}
-	}
-	public static class C02 {
-		public C02() {}
-		public C02(String o) {
-			throw new RuntimeException("foo");
-		}
-	}
-	public static class C03 {
-		public C03() {}
-		public C03(String o) throws ParseException {
-			throw new ParseException("foo");
-		}
-	}
-
-	@Test
-	public void c01_unswapConstructor() throws Exception {
-		assertNotNull(find(C01.class).unswap(null, "foo", null));
-	}
-
-	@Test(expected = ParseException.class)
-	public void c02_unswapConstructor_runtimeException() throws Exception {
-		assertNotNull(find(C02.class).unswap(null, "foo", null));
-	}
-
-	@Test(expected = ParseException.class)
-	public void c03_unswapConstructor_parseException() throws Exception {
-		assertNotNull(find(C03.class).unswap(null, "foo", null));
-	}
-
-	//------------------------------------------------------------------------------------------------------------------
-	// Ignore class
-	//------------------------------------------------------------------------------------------------------------------
-
-	@BeanIgnore
-	public static class D01 {
-		public static D01 fromString(String s) {
-			return new D01();
-		}
-	}
-	public static class D02 {
-		public class D02A {
-			public D02A fromString(String s) {
-				return new D02A();
-			}
-		}
-	}
-
-	@Test
-	public void d01_ignoreClass_beanIgnore() throws Exception {
-		assertNull(find(D01.class));
-	}
-
-	@Test
-	public void d02_ignoreClass_memberClass() throws Exception {
-		assertNull(find(D02.D02A.class));
-	}
-
-	//------------------------------------------------------------------------------------------------------------------
-	// Ignore unswap method
-	//------------------------------------------------------------------------------------------------------------------
-
-	public static class F01 {
-		@BeanIgnore
-		public static F01 fromString(String o) {
-			return null;
-		}
-	}
-	public static class F02 {
-		@Deprecated
-		public static F02 fromString(String o) {
-			return null;
-		}
-	}
-	public static class F03 {
-		public static Object fromString(String o) {
-			return null;
-		}
-	}
-	public static class F04 {
-		public static F04 fromString(Map<String,String> o) {
-			return null;
-		}
-	}
-	public static class F05 {
-		public F05 fromString(String o) {
-			return null;
-		}
-	}
-	public static class F06 {
-		public static F06 createx(String o) {
-			return null;
-		}
-	}
-
-	@Test
-	public void f01_ignoreUnswapMethod_beanIgnore() throws Exception {
-		assertNull(find(F01.class));
-	}
-
-	@Test
-	public void f02_ignoreUnswapMethod_deprecated() throws Exception {
-		assertNull(find(F02.class));
-	}
-
-	@Test
-	public void f03_ignoreUnswapMethod_wrongReturnType() throws Exception {
-		assertNull(find(F03.class));
-	}
-
-	@Test
-	public void f04_ignoreUnswapMethod_wrongParameters() throws Exception {
-		assertNull(find(F04.class));
-	}
-
-	@Test
-	public void f05_ignoreUnswapMethod_notStatic() throws Exception {
-		assertNull(find(F05.class));
-	}
-
-	@Test
-	public void f06_ignoreUnswapMethod_wrongName() throws Exception {
-		assertNull(find(F06.class));
-	}
-
-	//------------------------------------------------------------------------------------------------------------------
-	// Ignore constructor
-	//------------------------------------------------------------------------------------------------------------------
-
-	public static class G01 {
-		@BeanIgnore
-		public G01(String o) {}
-	}
-
-	public static class G02 {
-		@Deprecated
-		public G02(String o) {}
-	}
-
-	@Test
-	public void g01_ignoreUnswapConstructor_beanIgnore() throws Exception {
-		assertNull(find(G01.class));
-	}
-
-	@Test
-	public void g02_ignoreUnswapConstructor_deprecated() throws Exception {
-		assertNull(find(G02.class));
-	}
-}
diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ClassMeta.java b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ClassMeta.java
index bd59a8e..2a0dc9e 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ClassMeta.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ClassMeta.java
@@ -693,9 +693,6 @@ public final class ClassMeta<T> implements Type {
 				defaultSwap = AutoMapSwap.find(ci);
 			if (defaultSwap == null)
 				defaultSwap = AutoListSwap.find(ci);
-			// TODO
-			//if (defaultSwap == null)
-			//	defaultSwap = AutoStringSwap.find(ci);
 			if (defaultSwap != null)
 				l.add(defaultSwap);
 		}
diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/transform/AutoStringSwap.java b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/transform/AutoStringSwap.java
deleted file mode 100644
index dc8b6c2..0000000
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/transform/AutoStringSwap.java
+++ /dev/null
@@ -1,147 +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;
-
-import static org.apache.juneau.internal.ClassUtils.*;
-import static org.apache.juneau.internal.CollectionUtils.*;
-
-import java.lang.reflect.*;
-import java.util.*;
-
-import org.apache.juneau.*;
-import org.apache.juneau.annotation.*;
-import org.apache.juneau.parser.*;
-import org.apache.juneau.reflect.*;
-import org.apache.juneau.serializer.*;
-
-/**
- * A dynamic POJO swap based on reflection of a Java class that converts POJOs to serializable String objects.
- *
- * <p>
- * Looks for methods on the class that can be called to swap-in surrogate String objects before serialization and swap-out
- * surrogate String objects after parsing.
- *
- * <h5 class='figure'>Valid unswap methods (N = Normal type)</h5>
- * <ul>
- * 	<li class='jm'><c><jk>public static</jk> N fromString(String)</c>
- * 	<li class='jm'><c><jk>public static</jk> N fromString(BeanSession, String)</c>
- * 	<li class='jm'><c><jk>public static</jk> N fromValue(String)</c>
- * 	<li class='jm'><c><jk>public static</jk> N fromValue(BeanSession, String)</c>
- * 	<li class='jm'><c><jk>public static</jk> N valueOf(String)</c>
- * 	<li class='jm'><c><jk>public static</jk> N valueOf(BeanSession, String)</c>
- * 	<li class='jm'><c><jk>public static</jk> N parse(String)</c>
- * 	<li class='jm'><c><jk>public static</jk> N parse(BeanSession, String)</c>
- * 	<li class='jm'><c><jk>public static</jk> N parseString(String)</c>
- * 	<li class='jm'><c><jk>public static</jk> N parseString(BeanSession, String)</c>
- * 	<li class='jm'><c><jk>public static</jk> N forName(String)</c>
- * 	<li class='jm'><c><jk>public static</jk> N forName(BeanSession, String)</c>
- * 	<li class='jm'><c><jk>public static</jk> N forString(String)</c>
- * 	<li class='jm'><c><jk>public static</jk> N forString(BeanSession, String)</c>
- * 	<li class='jm'><c><jk>public</jk> N(String)</c>
- * </ul>
- *
- * <p>
- * Classes are ignored if any of the following are true:
- * <ul>
- * 	<li>Classes annotated with {@link BeanIgnore @BeanIgnore}.
- * 	<li>Non-static member classes.
- * </ul>
- *
- * <p>
- * Members/constructors are ignored if any of the following are true:
- * <ul>
- * 	<li>Members/constructors annotated with {@link BeanIgnore @BeanIgnore}.
- * 	<li>Deprecated members/constructors.
- * </ul>
- *
- * @param <T> The normal class type.
- */
-public class AutoStringSwap<T> extends org.apache.juneau.transform.StringSwap<T> {
-
-	private static final Set<String>
-		UNSWAP_METHOD_NAMES = newUnmodifiableHashSet("fromString", "fromValue", "valueOf", "parse", "parseString", "forName", "forString");
-
-	/**
-	 * Look for constructors and methods on this class and construct a dynamic swap if it's possible to do so.
-	 *
-	 * @param ci The class to try to constructor a dynamic swap on.
-	 * @return A POJO swap instance, or <jk>null</jk> if one could not be created.
-	 */
-	@SuppressWarnings({ "rawtypes" })
-	public static PojoSwap<?,?> find(ClassInfo ci) {
-
-		if (shouldIgnore(ci))
-			return null;
-
-		for (MethodInfo m : ci.getPublicMethods())
-			if (isUnswapMethod(m, ci))
-				return new AutoStringSwap(ci, m, null);
-
-		for (ConstructorInfo cs : ci.getPublicConstructors())
-			if (isUnswapConstructor(cs))
-				return new AutoStringSwap(ci, null, cs);
-
-		return null;
-	}
-
-	private static boolean shouldIgnore(ClassInfo ci) {
-		return
-			ci.hasAnnotation(BeanIgnore.class)
-			|| ci.isNonStaticMemberClass();
-	}
-
-	private static boolean isUnswapMethod(MethodInfo mi, ClassInfo ci) {
-		return
-			mi.isNotDeprecated()
-			&& mi.isStatic()
-			&& mi.hasName(UNSWAP_METHOD_NAMES)
-			&& mi.hasFuzzyParamTypes(BeanSession.class, String.class)
-			&& mi.hasReturnTypeParent(ci)
-			&& ! mi.hasAnnotation(BeanIgnore.class);
-	}
-
-	private static boolean isUnswapConstructor(ConstructorInfo cs) {
-		return
-			cs.isNotDeprecated()
-			&& cs.hasParamTypes(String.class)
-			&& ! cs.hasAnnotation(BeanIgnore.class);
-	}
-
-	//------------------------------------------------------------------------------------------------------------------
-
-	private final Method unswapMethod;
-	private final Constructor<?> unswapConstructor;
-
-	private AutoStringSwap(ClassInfo ci, MethodInfo unswapMethod, ConstructorInfo unswapConstructor) {
-		super(ci.inner());
-		this.unswapMethod = unswapMethod == null ? null : unswapMethod.inner();
-		this.unswapConstructor = unswapConstructor == null ? null : unswapConstructor.inner();
-	}
-
-	@Override /* PojoSwap */
-	public String swap(BeanSession session, T o) throws SerializeException {
-		return o.toString();
-	}
-
-	@SuppressWarnings("unchecked")
-	@Override /* PojoSwap */
-	public T unswap(BeanSession session, String f, ClassMeta<?> hint) throws ParseException {
-		try {
-			if (unswapMethod != null)
-				return (T)unswapMethod.invoke(null, getMatchingArgs(unswapMethod.getParameterTypes(), session, f));
-			return (T)unswapConstructor.newInstance(f);
-		} catch (Exception e) {
-			throw ParseException.create(e);
-		}
-	}
-}
diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/transform/DefaultSwaps.java b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/transform/DefaultSwaps.java
index 69f63a0..0667bfe 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/transform/DefaultSwaps.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/transform/DefaultSwaps.java
@@ -32,6 +32,7 @@ public class DefaultSwaps {
 		POJO_SWAPS.put(Enumeration.class, new EnumerationSwap());
 		POJO_SWAPS.put(Iterator.class, new IteratorSwap());
 		POJO_SWAPS.put(Locale.class, new LocaleSwap());
+		POJO_SWAPS.put(Class.class, new ClassSwap());
 		POJO_SWAPS.put(Calendar.class, new TemporalCalendarSwap.IsoOffsetDateTime());
 		POJO_SWAPS.put(Date.class, new TemporalDateSwap.IsoLocalDateTime());
 		POJO_SWAPS.put(Instant.class, new TemporalSwap.IsoInstant());
diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/transforms/ClassSwap.java b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/transforms/ClassSwap.java
new file mode 100644
index 0000000..04b7554
--- /dev/null
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/transforms/ClassSwap.java
@@ -0,0 +1,32 @@
+// ***************************************************************************************************************************
+// * 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.transforms;
+
+import org.apache.juneau.*;
+import org.apache.juneau.transform.*;
+
+/**
+ * Transforms {@link Class} objects to and from {@link String Strings}.
+ */
+public class ClassSwap extends StringSwap<Class<?>> {
+
+	@Override /* PojoSwap */
+	public String swap(BeanSession session, Class<?> o) throws Exception {
+		return o == null ? null : o.getName();
+	}
+
+	@Override /* PojoSwap */
+	public Class<?> unswap(BeanSession session, String o, ClassMeta<?> hint) throws Exception {
+		return o == null ? null : Class.forName(o);
+	}
+}
diff --git a/juneau-doc/docs/Topics/02.juneau-marshall/11.Transforms/02.DefaultPojoSwaps.html b/juneau-doc/docs/Topics/02.juneau-marshall/11.Transforms/02.DefaultPojoSwaps.html
index d76af81..2556448 100644
--- a/juneau-doc/docs/Topics/02.juneau-marshall/11.Transforms/02.DefaultPojoSwaps.html
+++ b/juneau-doc/docs/Topics/02.juneau-marshall/11.Transforms/02.DefaultPojoSwaps.html
@@ -13,7 +13,7 @@
  ***************************************************************************************************************************/
  -->
 
-{todo} Default PojoSwaps
+Default PojoSwaps
 
 <p>
 	By default, all serializers and parsers have built in <c>PojoSwaps</c> defined for the following common data types:
@@ -22,6 +22,7 @@
 	<li class='jc'>{@link java.util.Enumeration}
 	<li class='jc'>{@link java.util.Iterator}
 	<li class='jc'>{@link java.util.Locale}
+	<li class='jc'>{@link java.util.Class}
 	<li class='jc'>{@link java.util.Calendar} - ISO offset date-time.
 	<li class='jc'>{@link java.util.Date} - Local date-time
 	<li class='jc'>{@link java.time.Instant} - ISO instant.
diff --git a/juneau-doc/docs/Topics/02.juneau-marshall/11.Transforms/07.SwapMethods.html b/juneau-doc/docs/Topics/02.juneau-marshall/11.Transforms/03.AutoPojoSwaps.html
similarity index 99%
rename from juneau-doc/docs/Topics/02.juneau-marshall/11.Transforms/07.SwapMethods.html
rename to juneau-doc/docs/Topics/02.juneau-marshall/11.Transforms/03.AutoPojoSwaps.html
index 91ff45b..f222843 100644
--- a/juneau-doc/docs/Topics/02.juneau-marshall/11.Transforms/07.SwapMethods.html
+++ b/juneau-doc/docs/Topics/02.juneau-marshall/11.Transforms/03.AutoPojoSwaps.html
@@ -13,7 +13,7 @@
  ***************************************************************************************************************************/
  -->
 
-{updated} Swap Methods
+{new} Auto-detected POJO swaps
 
 <p>
 	Various methods can be defined on a class directly to affect how it gets serialized.