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