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
020package org.apache.isis.core.progmodel.facets.param.describedas.annotation;
021
022import java.lang.annotation.Annotation;
023
024import org.apache.isis.applib.annotation.DescribedAs;
025import org.apache.isis.core.metamodel.facetapi.FacetHolder;
026import org.apache.isis.core.metamodel.facetapi.FacetUtil;
027import org.apache.isis.core.metamodel.facetapi.FeatureType;
028import org.apache.isis.core.metamodel.facets.Annotations;
029import org.apache.isis.core.metamodel.facets.FacetFactoryAbstract;
030import org.apache.isis.core.metamodel.facets.describedas.DescribedAsFacet;
031import org.apache.isis.core.metamodel.spec.ObjectSpecification;
032
033public class DescribedAsAnnotationOnParameterFacetFactory extends FacetFactoryAbstract {
034
035    public DescribedAsAnnotationOnParameterFacetFactory() {
036        super(FeatureType.PARAMETERS_ONLY);
037    }
038
039    @Override
040    public void processParams(final ProcessParameterContext processParameterContext) {
041
042        final int paramNum = processParameterContext.getParamNum();
043        final Class<?> parameterType = processParameterContext.getMethod().getParameterTypes()[paramNum];
044        final Annotation[] parameterAnnotations = Annotations.getParameterAnnotations(processParameterContext.getMethod())[paramNum];
045        for (final Annotation parameterAnnotation : parameterAnnotations) {
046            if (parameterAnnotation instanceof DescribedAs) {
047                FacetUtil.addFacet(create((DescribedAs) parameterAnnotation, processParameterContext.getFacetHolder()));
048                return;
049            }
050        }
051
052        // otherwise, fall back to a description on the parameter's type, if
053        // available
054        final DescribedAsFacet parameterTypeDescribedAsFacet = getDescribedAsFacet(parameterType);
055        if (parameterTypeDescribedAsFacet != null) {
056            FacetUtil.addFacet(new DescribedAsFacetForParameterDerivedFromType(parameterTypeDescribedAsFacet, processParameterContext.getFacetHolder()));
057            return;
058        }
059
060    }
061
062    private DescribedAsFacet create(final DescribedAs annotation, final FacetHolder holder) {
063        return annotation == null ? null : new DescribedAsFacetAnnotationOnParameter(annotation.value(), holder);
064    }
065
066    private DescribedAsFacet getDescribedAsFacet(final Class<?> type) {
067        final ObjectSpecification paramTypeSpec = getSpecificationLoader().loadSpecification(type);
068        return paramTypeSpec.getFacet(DescribedAsFacet.class);
069    }
070
071}