001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.rest.beans;
014
015import org.apache.juneau.*;
016import org.apache.juneau.annotation.*;
017
018/**
019 * Simple serializable bean description.
020 *
021 * <p>
022 * Given a particular class type, this serializes the class into the fully-qualified class name and the properties
023 * associated with the class.
024 *
025 * <p>
026 * Useful for rendering simple information about a bean during REST OPTIONS requests.
027 *
028 * <h5 class='section'>See Also:</h5><ul>
029 *    <li class='link'><a class="doclink" href="../../../../../index.html#jrs.UtilityBeans">Utility Beans</a>
030
031 * </ul>
032 */
033@Bean(properties="type,properties")
034public final class BeanDescription {
035
036   //-----------------------------------------------------------------------------------------------------------------
037   // Static
038   //-----------------------------------------------------------------------------------------------------------------
039
040   /**
041    * Static creator.
042    *
043    * @param c The bean being described.
044    * @return A new bean description.
045    */
046   public static BeanDescription of(Class<?> c) {
047      return new BeanDescription(c);
048   }
049
050   //-----------------------------------------------------------------------------------------------------------------
051   // Instance
052   //-----------------------------------------------------------------------------------------------------------------
053
054   /** The bean class type. */
055   public String type;
056
057   /** The bean properties. */
058   public BeanPropertyDescription[] properties;
059
060   /**
061    * Constructor
062    *
063    * @param c The bean class type.
064    */
065   public BeanDescription(Class<?> c) {
066      type = c.getName();
067      BeanMeta<?> bm = BeanContext.DEFAULT.getBeanMeta(c);
068      if (bm == null)
069         throw new BasicRuntimeException("Class ''{0}'' is not a valid bean.", c);
070      properties = new BeanPropertyDescription[bm.getPropertyMetas().size()];
071      int i = 0;
072      for (BeanPropertyMeta pm : bm.getPropertyMetas())
073         properties[i++] = new BeanPropertyDescription(pm.getName(), pm.getClassMeta());
074   }
075
076   /**
077    * Information about a bean property.
078    */
079   public static class BeanPropertyDescription {
080
081      /** The bean property name. */
082      public String name;
083
084      /** The bean property filtered class type. */
085      public String type;
086
087      /**
088       * Constructor.
089       *
090       * @param name The bean property name.
091       * @param type The bean property class type.
092       */
093      public BeanPropertyDescription(String name, ClassMeta<?> type) {
094         this.name = name;
095         this.type = type.getSerializedClassMeta(null).toString();
096      }
097   }
098}