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 2018/09/20 17:48:41 UTC

[juneau] branch master updated: Better error handling in ClassMeta.

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 78bb00b  Better error handling in ClassMeta.
78bb00b is described below

commit 78bb00b89313c77573246eac4059f3faf1c13689
Author: JamesBognar <ja...@apache.org>
AuthorDate: Thu Sep 20 13:47:59 2018 -0400

    Better error handling in ClassMeta.
---
 .../java/org/apache/juneau/PojoExamplesTest.java   |  9 ++++-----
 .../src/main/java/org/apache/juneau/ClassMeta.java | 23 +++++++++++++---------
 2 files changed, 18 insertions(+), 14 deletions(-)

diff --git a/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/PojoExamplesTest.java b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/PojoExamplesTest.java
index 7877e2e..d5a9d79 100644
--- a/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/PojoExamplesTest.java
+++ b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/PojoExamplesTest.java
@@ -13,7 +13,6 @@
 package org.apache.juneau;
 
 import static org.apache.juneau.testutils.TestUtils.*;
-import static org.junit.Assert.*;
 
 import org.apache.juneau.annotation.*;
 import org.junit.*;
@@ -205,22 +204,22 @@ public class PojoExamplesTest {
 		try {
 			bs.getClassMeta(F1.class);
 		} catch (Exception e) {
-			assertEquals("@Example used on invalid method 'org.apache.juneau.PojoExamplesTest$F1.example(String)'", e.getMessage());
+			assertContains(e.getMessage(), "@Example used on invalid method 'org.apache.juneau.PojoExamplesTest$F1.example(String)'");
 		}
 		try {
 			bs.getClassMeta(F2.class);
 		} catch (Exception e) {
-			assertEquals("@Example used on invalid method 'org.apache.juneau.PojoExamplesTest$F2.example()'", e.getMessage());
+			assertContains(e.getMessage(), "@Example used on invalid method 'org.apache.juneau.PojoExamplesTest$F2.example()'");
 		}
 		try {
 			bs.getClassMeta(F3.class);
 		} catch (Exception e) {
-			assertEquals("@Example used on invalid field 'public static java.lang.String org.apache.juneau.PojoExamplesTest$F3.F3'", e.getMessage());
+			assertContains(e.getMessage(), "@Example used on invalid field 'public static java.lang.String org.apache.juneau.PojoExamplesTest$F3.F3'");
 		}
 		try {
 			bs.getClassMeta(F4.class);
 		} catch (Exception e) {
-			assertEquals("@Example used on invalid field 'public org.apache.juneau.PojoExamplesTest$F4 org.apache.juneau.PojoExamplesTest$F4.f4'", e.getMessage());
+			assertContains(e.getMessage(), "@Example used on invalid field 'public org.apache.juneau.PojoExamplesTest$F4 org.apache.juneau.PojoExamplesTest$F4.f4'");
 		}
 	}
 
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 135813b..10e5d42 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
@@ -151,6 +151,8 @@ public final class ClassMeta<T> implements Type {
 	ClassMeta(Class<T> innerClass, BeanContext beanContext, Class<? extends T> implClass, BeanFilter beanFilter, PojoSwap<T,?>[] pojoSwaps, PojoSwap<?,?>[] childPojoSwaps, Object example) {
 		this.innerClass = innerClass;
 		this.beanContext = beanContext;
+		this.extMeta = new MetadataMap();
+		String notABeanReason = null;
 
 		wLock.lock();
 		try {
@@ -178,11 +180,10 @@ public final class ClassMeta<T> implements Type {
 			this.beanFilter = beanFilter;
 			this.pojoSwaps = builder.pojoSwaps.isEmpty() ? null : builder.pojoSwaps.toArray(new PojoSwap[builder.pojoSwaps.size()]);
 			this.builderSwap = builder.builderSwap;
-			this.extMeta = new MetadataMap();
 			this.keyType = builder.keyType;
 			this.valueType = builder.valueType;
 			this.elementType = builder.elementType;
-			this.notABeanReason = builder.notABeanReason;
+			notABeanReason = builder.notABeanReason;
 			this.beanMeta = builder.beanMeta;
 			this.initException = builder.initException;
 			this.typePropertyName = builder.typePropertyName;
@@ -202,7 +203,11 @@ public final class ClassMeta<T> implements Type {
 			this.readerTransform = builder.readerTransform;
 			this.inputStreamTransform = builder.inputStreamTransform;
 			this.stringTransform = builder.stringTransform;
+		} catch (ClassMetaRuntimeException e) {
+			notABeanReason = e.getMessage();
+			throw e;
 		} finally {
+			this.notABeanReason = notABeanReason;
 			wLock.unlock();
 		}
 	}
@@ -274,6 +279,7 @@ public final class ClassMeta<T> implements Type {
 	@SuppressWarnings("unchecked")
 	ClassMeta(ClassMeta<?>[] args) {
 		this.innerClass = (Class<T>) Object[].class;
+		this.extMeta = new MetadataMap();
 		this.args = args;
 		this.implClass = null;
 		this.childPojoSwaps = null;
@@ -308,7 +314,6 @@ public final class ClassMeta<T> implements Type {
 		this.pojoSwaps = null;
 		this.builderSwap = null;
 		this.beanFilter = null;
-		this.extMeta = new MetadataMap();
 		this.initException = null;
 		this.beanRegistry = null;
 		this.exampleMethod = null;
@@ -500,13 +505,13 @@ public final class ClassMeta<T> implements Type {
 			for (Field f : getAllFields(c, true)) {
 				if (f.isAnnotationPresent(ParentProperty.class)) {
 					if (isStatic(f))
-						throw new ClassMetaRuntimeException("@ParentProperty used on invalid field ''{0}''", f);
+						throw new ClassMetaRuntimeException("@ParentProperty used on invalid field ''{0}''.  Must be static.", f);
 					setAccessible(f, false);
 					parentPropertyMethod = new Setter.FieldSetter(f);
 				}
 				if (f.isAnnotationPresent(NameProperty.class)) {
 					if (isStatic(f))
-						throw new ClassMetaRuntimeException("@NameProperty used on invalid field ''{0}''", f);
+						throw new ClassMetaRuntimeException("@NameProperty used on invalid field ''{0}''.  Must be static.", f);
 					setAccessible(f, false);
 					namePropertyMethod = new Setter.FieldSetter(f);
 				}
@@ -515,7 +520,7 @@ public final class ClassMeta<T> implements Type {
 			for (Field f : c.getDeclaredFields()) {
 				if (f.isAnnotationPresent(Example.class)) {
 					if (! (isStatic(f) && isParentClass(innerClass, f.getType())))
-						throw new ClassMetaRuntimeException("@Example used on invalid field ''{0}''", f);
+						throw new ClassMetaRuntimeException("@Example used on invalid field ''{0}''.  Must be static and an instance of the type.", f);
 					setAccessible(f, false);
 					exampleField = f;
 				}
@@ -525,13 +530,13 @@ public final class ClassMeta<T> implements Type {
 			for (Method m : getAllMethods(c, true)) {
 				if (m.isAnnotationPresent(ParentProperty.class)) {
 					if (isStatic(m) || ! hasNumArgs(m, 1))
-						throw new ClassMetaRuntimeException("@ParentProperty used on invalid method ''{0}''", m);
+						throw new ClassMetaRuntimeException("@ParentProperty used on invalid method ''{0}''.  Must not be static and have one argument.", m);
 					setAccessible(m, false);
 					parentPropertyMethod = new Setter.MethodSetter(m);
 				}
 				if (m.isAnnotationPresent(NameProperty.class)) {
 					if (isStatic(m) || ! hasNumArgs(m, 1))
-						throw new ClassMetaRuntimeException("@NameProperty used on invalid method ''{0}''", m);
+						throw new ClassMetaRuntimeException("@NameProperty used on invalid method ''{0}''.  Must not be static and have one argument.", m);
 					setAccessible(m, false);
 					namePropertyMethod = new Setter.MethodSetter(m);
 				}
@@ -540,7 +545,7 @@ public final class ClassMeta<T> implements Type {
 			for (Method m : c.getDeclaredMethods()) {
 				if (m.isAnnotationPresent(Example.class)) {
 					if (! (isStatic(m) && hasFuzzyArgs(m, BeanSession.class) && isParentClass(innerClass, m.getReturnType())))
-						throw new ClassMetaRuntimeException("@Example used on invalid method ''{0}''", m);
+						throw new ClassMetaRuntimeException("@Example used on invalid method ''{0}''.  Must be static and return an instance of the declaring class.", m);
 					setAccessible(m, false);
 					exampleMethod = m;
 				}