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.jsonschema;
014
015import java.lang.reflect.*;
016
017import org.apache.juneau.*;
018import org.apache.juneau.jsonschema.annotation.*;
019import org.apache.juneau.parser.*;
020
021/**
022 * Metadata on bean properties specific to the JSON-Schema pulled from the {@link Schema @Schema} annotation
023 * on the bean property.
024 */
025public class JsonSchemaBeanPropertyMeta extends BeanPropertyMetaExtended {
026
027   /**
028    * Default instance.
029    */
030   public static final JsonSchemaBeanPropertyMeta DEFAULT = new JsonSchemaBeanPropertyMeta();
031
032   private final ObjectMap schema;
033
034   /**
035    * Constructor.
036    *
037    * @param bpm The metadata of the bean property of this additional metadata.
038    */
039   public JsonSchemaBeanPropertyMeta(BeanPropertyMeta bpm) {
040      super(bpm);
041
042      this.schema = new ObjectMap();
043
044      Field field = bpm.getInnerField();
045      Method getter = bpm.getGetter(), setter = bpm.getSetter();
046
047      try {
048         if (field != null)
049            schema.appendAll(SchemaUtils.asMap(field.getAnnotation(Schema.class)));
050         if (getter != null)
051            schema.appendAll(SchemaUtils.asMap(getter.getAnnotation(Schema.class)));
052         if (setter != null)
053            schema.appendAll(SchemaUtils.asMap(setter.getAnnotation(Schema.class)));
054      } catch (ParseException e) {
055         throw new RuntimeException(e);
056      }
057   }
058
059   private JsonSchemaBeanPropertyMeta() {
060      super(null);
061      this.schema = ObjectMap.EMPTY_MAP;
062   }
063
064   /**
065    * Returns the schema information gathered from all the {@link Schema @Schema} annotations on the bean property.
066    *
067    * @return The schema information as a generic map.  Never <jk>null</jk>.
068    */
069   protected ObjectMap getSchema() {
070      return schema;
071   }
072}