001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020 package org.apache.isis.core.progmodel.facets.properties.defaults.fromtype;
021
022 import org.apache.isis.core.metamodel.adapter.map.AdapterMap;
023 import org.apache.isis.core.metamodel.adapter.map.AdapterMapAware;
024 import org.apache.isis.core.metamodel.facetapi.FacetUtil;
025 import org.apache.isis.core.metamodel.facetapi.FeatureType;
026 import org.apache.isis.core.metamodel.facets.AnnotationBasedFacetFactoryAbstract;
027 import org.apache.isis.core.metamodel.facets.properties.defaults.PropertyDefaultFacet;
028 import org.apache.isis.core.metamodel.spec.ObjectSpecification;
029 import org.apache.isis.core.progmodel.facets.object.defaults.DefaultedFacet;
030
031 public class PropertyDefaultDerivedFromTypeFacetFactory extends AnnotationBasedFacetFactoryAbstract implements
032 AdapterMapAware {
033
034 private AdapterMap adapterMap;
035
036 public PropertyDefaultDerivedFromTypeFacetFactory() {
037 super(FeatureType.PROPERTIES_ONLY);
038 }
039
040 /**
041 * If there is a {@link DefaultedFacet} on the properties return type, then installs a {@link PropertyDefaultFacet}
042 * for the property with the same default.
043 */
044 @Override
045 public void process(final ProcessMethodContext processMethodContext) {
046 // don't overwrite any defaults that might already picked up
047 final PropertyDefaultFacet existingDefaultFacet =
048 processMethodContext.getFacetHolder().getFacet(PropertyDefaultFacet.class);
049 if (existingDefaultFacet != null && !existingDefaultFacet.isNoop()) {
050 return;
051 }
052
053 // try to infer defaults from the underlying return type
054 final Class<?> returnType = processMethodContext.getMethod().getReturnType();
055 final DefaultedFacet returnTypeDefaultedFacet = getDefaultedFacet(returnType);
056 if (returnTypeDefaultedFacet != null) {
057 final PropertyDefaultFacetDerivedFromDefaultedFacet propertyFacet =
058 new PropertyDefaultFacetDerivedFromDefaultedFacet(returnTypeDefaultedFacet,
059 processMethodContext.getFacetHolder(), getAdapterMap());
060 FacetUtil.addFacet(propertyFacet);
061 }
062 }
063
064 private DefaultedFacet getDefaultedFacet(final Class<?> paramType) {
065 final ObjectSpecification paramTypeSpec = getSpecificationLookup().loadSpecification(paramType);
066 return paramTypeSpec.getFacet(DefaultedFacet.class);
067 }
068
069 // ////////////////////////////////////////////////////////////////////
070 // Injected
071 // ////////////////////////////////////////////////////////////////////
072
073 public AdapterMap getAdapterMap() {
074 return adapterMap;
075 }
076
077 @Override
078 public void setAdapterMap(final AdapterMap adapterMap) {
079 this.adapterMap = adapterMap;
080 }
081
082 }