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/11/27 15:16:44 UTC

[juneau] branch master updated: JUNEAU-168 Serializer/Parser handling of proxy beans.

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 68fe931  JUNEAU-168 Serializer/Parser handling of proxy beans.
68fe931 is described below

commit 68fe9312cdfef077faf86ea5a3d0b14edec9861e
Author: JamesBognar <ja...@apache.org>
AuthorDate: Wed Nov 27 10:16:27 2019 -0500

    JUNEAU-168 Serializer/Parser handling of proxy beans.
---
 .../java/org/apache/juneau/BeanContextTest.java    | 53 ++++++++++++++++++++++
 .../src/main/java/org/apache/juneau/ClassMeta.java | 15 +++++-
 2 files changed, 67 insertions(+), 1 deletion(-)

diff --git a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/BeanContextTest.java b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/BeanContextTest.java
new file mode 100644
index 0000000..7202d7e
--- /dev/null
+++ b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/BeanContextTest.java
@@ -0,0 +1,53 @@
+// ***************************************************************************************************************************
+// * 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;
+
+import static org.junit.Assert.*;
+
+import org.junit.*;
+
+@SuppressWarnings({"rawtypes"})
+public class BeanContextTest {
+
+	BeanContext bc = BeanContext.DEFAULT;
+
+	public static interface A1 {
+		public int getF1();
+		public void setF1(int f1);
+	}
+
+	@Test
+	public void normalCachableBean() throws ExecutableException {
+		ClassMeta cm1 = bc.getClassMeta(A1.class), cm2 = bc.getClassMeta(A1.class);
+		assertTrue(cm1 == cm2);
+	}
+
+	interface A2 {
+		void foo(int x);
+	}
+
+	@Test
+	public void lambdaExpressionsNotCached() throws ExecutableException {
+		BeanContext bc = BeanContext.DEFAULT;
+		A2 fi = (x) -> System.out.println(x);
+		ClassMeta cm1 = bc.getClassMeta(fi.getClass()), cm2 = bc.getClassMeta(fi.getClass());
+		assertTrue(cm1 != cm2);
+	}
+
+	@Test
+	public void proxiesNotCached() throws ExecutableException {
+		A1 a1 = bc.createBeanSession().getBeanMeta(A1.class).newBean(null);
+		ClassMeta cm1 = bc.getClassMeta(a1.getClass()), cm2 = bc.getClassMeta(a1.getClass());
+		assertTrue(cm1 != cm2);
+	}
+}
\ No newline at end of file
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 385b703..fe532b0 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
@@ -149,7 +149,7 @@ public final class ClassMeta<T> implements Type {
 		wLock.lock();
 		try {
 			// We always immediately add this class meta to the bean context cache so that we can resolve recursive references.
-			if (beanContext != null && beanContext.cmCache != null)
+			if (beanContext != null && beanContext.cmCache != null && isCacheable(innerClass))
 				beanContext.cmCache.put(innerClass, this);
 
 			ClassMetaBuilder<T> builder = new ClassMetaBuilder(innerClass, beanContext, implClass, beanFilter, pojoSwaps, childPojoSwaps, example);
@@ -197,6 +197,19 @@ public final class ClassMeta<T> implements Type {
 	}
 
 	/**
+	 * Generated classes shouldn't be cacheable to prevent needlessly filling up the cache.
+	 */
+	private static boolean isCacheable(Class<?> c) {
+		String n = c.getName();
+		char x = n.charAt(n.length()-1);  // All generated classes appear to end with digits.
+		if (x >= '0' && x <= '9') {
+			if (n.indexOf("$$") != -1 || n.startsWith("sun") || n.startsWith("com.sun") || n.indexOf("$Proxy") != -1)
+				return false;
+		}
+		return true;
+	}
+
+	/**
 	 * Causes thread to wait until constructor has exited.
 	 */
 	final void waitForInit() {