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.helper;
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>
029 * <ul>
030 *    <li class='link'>{@doc juneau-rest-server.RestMethod.PredefinedHelperBeans}
031 * </ul>
032 */
033@Bean(properties="type,properties")
034public final class BeanDescription {
035
036   /** The bean class type. */
037   public String type;
038
039   /** The bean properties. */
040   public BeanPropertyDescription[] properties;
041
042   /**
043    * Constructor
044    *
045    * @param c The bean class type.
046    */
047   public BeanDescription(Class<?> c) {
048      type = c.getName();
049      BeanMeta<?> bm = BeanContext.DEFAULT.getBeanMeta(c);
050      if (bm == null)
051         throw new FormattedRuntimeException("Class ''{0}'' is not a valid bean.", c);
052      properties = new BeanPropertyDescription[bm.getPropertyMetas().size()];
053      int i = 0;
054      for (BeanPropertyMeta pm : bm.getPropertyMetas())
055         properties[i++] = new BeanPropertyDescription(pm.getName(), pm.getClassMeta());
056   }
057
058   /**
059    * Information about a bean property.
060    */
061   public static class BeanPropertyDescription {
062
063      /** The bean property name. */
064      public String name;
065
066      /** The bean property filtered class type. */
067      public String type;
068
069      /**
070       * Constructor.
071       *
072       * @param name The bean property name.
073       * @param type The bean property class type.
074       */
075      public BeanPropertyDescription(String name, ClassMeta<?> type) {
076         this.name = name;
077         this.type = type.getSerializedClassMeta(null).toString();
078      }
079   }
080}