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