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 org.apache.juneau.*; 020import org.apache.juneau.annotation.*; 021 022/** 023 * Simple serializable bean description. 024 * 025 * <p> 026 * Given a particular class type, this serializes the class into the fully-qualified class name and the properties 027 * associated with the class. 028 * 029 * <p> 030 * Useful for rendering simple information about a bean during REST OPTIONS requests. 031 * 032 * <h5 class='section'>See Also:</h5><ul> 033 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/UtilityBeans">Utility Beans</a> 034 035 * </ul> 036 */ 037@Bean(properties="type,properties") 038public class BeanDescription { 039 040 //----------------------------------------------------------------------------------------------------------------- 041 // Static 042 //----------------------------------------------------------------------------------------------------------------- 043 044 /** 045 * Static creator. 046 * 047 * @param c The bean being described. 048 * @return A new bean description. 049 */ 050 public static BeanDescription of(Class<?> c) { 051 return new BeanDescription(c); 052 } 053 054 //----------------------------------------------------------------------------------------------------------------- 055 // Instance 056 //----------------------------------------------------------------------------------------------------------------- 057 058 /** The bean class type. */ 059 public String type; 060 061 /** The bean properties. */ 062 public BeanPropertyDescription[] properties; 063 064 /** 065 * Constructor 066 * 067 * @param c The bean class type. 068 */ 069 public BeanDescription(Class<?> c) { 070 type = c.getName(); 071 BeanMeta<?> bm = BeanContext.DEFAULT.getBeanMeta(c); 072 if (bm == null) 073 throw new BasicRuntimeException("Class ''{0}'' is not a valid bean.", c); 074 properties = new BeanPropertyDescription[bm.getPropertyMetas().size()]; 075 int i = 0; 076 for (BeanPropertyMeta pm : bm.getPropertyMetas()) 077 properties[i++] = new BeanPropertyDescription(pm.getName(), pm.getClassMeta()); 078 } 079 080 /** 081 * Information about a bean property. 082 */ 083 public static class BeanPropertyDescription { 084 085 /** The bean property name. */ 086 public String name; 087 088 /** The bean property filtered class type. */ 089 public String type; 090 091 /** 092 * Constructor. 093 * 094 * @param name The bean property name. 095 * @param type The bean property class type. 096 */ 097 public BeanPropertyDescription(String name, ClassMeta<?> type) { 098 this.name = name; 099 this.type = type.getSerializedClassMeta(null).toString(); 100 } 101 } 102}