Repository: incubator-juneau
Updated Branches:
  refs/heads/master 4fb5136f8 -> 7b4384b02


Simplify @Bean(subTypes) annotation. (2)

Project: http://git-wip-us.apache.org/repos/asf/incubator-juneau/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-juneau/commit/7b4384b0
Tree: http://git-wip-us.apache.org/repos/asf/incubator-juneau/tree/7b4384b0
Diff: http://git-wip-us.apache.org/repos/asf/incubator-juneau/diff/7b4384b0

Branch: refs/heads/master
Commit: 7b4384b027485facc949f9256a1c94ae5ee46a90
Parents: 4fb5136
Author: jamesbognar <[email protected]>
Authored: Tue Aug 30 16:11:57 2016 -0400
Committer: jamesbognar <[email protected]>
Committed: Tue Aug 30 16:15:00 2016 -0400

----------------------------------------------------------------------
 .../java/org/apache/juneau/ClassLexicon.java    | 128 +++++++++++++++++++
 .../java/org/apache/juneau/annotation/Bean.java |   2 +-
 .../juneau/transform/AnnotationBeanFilter.java  |   2 +-
 3 files changed, 130 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7b4384b0/juneau-core/src/main/java/org/apache/juneau/ClassLexicon.java
----------------------------------------------------------------------
diff --git a/juneau-core/src/main/java/org/apache/juneau/ClassLexicon.java 
b/juneau-core/src/main/java/org/apache/juneau/ClassLexicon.java
new file mode 100644
index 0000000..2dbf639
--- /dev/null
+++ b/juneau-core/src/main/java/org/apache/juneau/ClassLexicon.java
@@ -0,0 +1,128 @@
+/***************************************************************************************************************************
+ * 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 java.util.*;
+
+import org.apache.juneau.annotation.*;
+import org.apache.juneau.internal.*;
+
+/**
+ * A lookup table for resolving classes by name.
+ * <p>
+ * Class names are typically defined through the {@link Bean#name()} or {@link 
Pojo#name()} annotations.
+ *
+ * @author james.bognar
+ */
+public class ClassLexicon {
+
+       private final Map<String,Class<?>> map;
+       private final Map<Class<?>,String> reverseMap;
+
+       /**
+        * Constructor.
+        *
+        * @param classes
+        *      List of classes to add to this lexicon table.
+        *      Each class must be one of the following:
+        *      <ul>
+        *              <li>A bean class that specifies a value for {@link 
Bean#name() @Bean.name()}.
+        *              <li>A POJO class that specifies a value for {@link 
Pojo#name() @Pojo.name()}.
+        *              <li>A subclass of {@link ClassLexicon} whose contents 
will be added to this list.
+        *                      Note that this subclass must implement a no-arg 
constructor.
+        *      </ul>
+        */
+       public ClassLexicon(Class<?>...classes) {
+               this(null, classes);
+       }
+
+       /**
+        * Constructor with optional copy-from lexicon.
+        *
+        * @param copyFrom The lexicon to initialize the contents of this 
lexicon with.
+        * @param classes List of classes to add to this lexicon.
+        */
+       public ClassLexicon(ClassLexicon copyFrom, Class<?>...classes) {
+               Map<String,Class<?>> m = new HashMap<String,Class<?>>();
+               if (copyFrom != null)
+                       m.putAll(copyFrom.map);
+               for (Class<?> c : classes) {
+                       if (c != null) {
+                               if 
(ClassUtils.isParentClass(ClassLexicon.class, c)) {
+                                       try {
+                                               ClassLexicon l2 = 
(ClassLexicon)c.newInstance();
+                                               m.putAll(l2.map);
+                                       } catch (Exception e) {
+                                               throw new 
BeanRuntimeException(e);
+                                       }
+                               } else {
+                                       Pojo p = c.getAnnotation(Pojo.class);
+                                       Bean b = c.getAnnotation(Bean.class);
+                                       String name = null;
+                                       if (p != null && ! p.name().isEmpty()) {
+                                               name = p.name();
+                                       } else if (b != null && ! 
b.name().isEmpty()) {
+                                               name = b.name();
+                                       } else {
+                                               name = c.getName();
+                                       }
+                                       m.put(name, c);
+                               }
+                       }
+               }
+               this.map = Collections.unmodifiableMap(m);
+               Map<Class<?>,String> m2 = new HashMap<Class<?>,String>();
+               for (Map.Entry<String,Class<?>> e : map.entrySet())
+                       m2.put(e.getValue(), e.getKey());
+               this.reverseMap = Collections.unmodifiableMap(m2);
+       }
+
+
+       /**
+        * Alternate constructor for defining a lexicon through a user-defined 
name/class map.
+        *
+        * @param classMap
+        *      Contains the name/class pairs to add to this lookup table.
+        */
+       public ClassLexicon(Map<String,Class<?>> classMap) {
+               if (classMap == null)
+                       throw new BeanRuntimeException("Null passed to 
ClassLexicon(Map) constructor.");
+               Map<String,Class<?>> m = new HashMap<String,Class<?>>(classMap);
+               this.map = Collections.unmodifiableMap(m);
+               Map<Class<?>,String> m2 = new HashMap<Class<?>,String>();
+               for (Map.Entry<String,Class<?>> e : map.entrySet())
+                       m2.put(e.getValue(), e.getKey());
+               this.reverseMap = Collections.unmodifiableMap(m2);
+       }
+
+       /**
+        * Returns the class associated with the specified name.
+        *
+        * @param name The name associated with the class.
+        * @return The class associated with the specified name, or 
<jk>null</jk> if no association exists.
+        */
+       public Class<?> getClassForName(String name) {
+               return map.get(name);
+       }
+
+       /**
+        * Returns the name associated with the specified class.
+        *
+        * @param c The class associated with the name.
+        * @return The name associated with the specified class, or 
<jk>null</jk> if no association exists.
+        */
+       public String getNameForClass(Class<?> c) {
+               return reverseMap.get(c);
+       }
+}

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7b4384b0/juneau-core/src/main/java/org/apache/juneau/annotation/Bean.java
----------------------------------------------------------------------
diff --git a/juneau-core/src/main/java/org/apache/juneau/annotation/Bean.java 
b/juneau-core/src/main/java/org/apache/juneau/annotation/Bean.java
index 34b6414..2d4b7a9 100644
--- a/juneau-core/src/main/java/org/apache/juneau/annotation/Bean.java
+++ b/juneau-core/src/main/java/org/apache/juneau/annotation/Bean.java
@@ -243,7 +243,7 @@ public @interface Bean {
         * <p>
         *      This annotation is an alternative to using the {@link 
BeanFilter} class with an implemented {@link BeanFilter#getSubTypeProperty()} 
method.
         */
-       String subTypeProperty() default "";
+       String subTypeProperty() default "_class";
 
        /**
         * Used in conjunction with {@link #subTypeProperty()} to set up bean 
subtypes.

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/7b4384b0/juneau-core/src/main/java/org/apache/juneau/transform/AnnotationBeanFilter.java
----------------------------------------------------------------------
diff --git 
a/juneau-core/src/main/java/org/apache/juneau/transform/AnnotationBeanFilter.java
 
b/juneau-core/src/main/java/org/apache/juneau/transform/AnnotationBeanFilter.java
index 24c6d5a..edbd589 100644
--- 
a/juneau-core/src/main/java/org/apache/juneau/transform/AnnotationBeanFilter.java
+++ 
b/juneau-core/src/main/java/org/apache/juneau/transform/AnnotationBeanFilter.java
@@ -85,7 +85,7 @@ public final class AnnotationBeanFilter<T> extends 
BeanFilter<T> {
                                        stopClass = b.stopClass();
 
 
-                               if (! b.subTypeProperty().isEmpty()) {
+                               if (b.subTypes().length > 0) {
                                        subTypeProperty = b.subTypeProperty();
 
                                        for (Class<?> bst : b.subTypes()) {

Reply via email to