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.param.describedas.annotation;
021    
022    import java.lang.annotation.Annotation;
023    
024    import org.apache.isis.applib.annotation.DescribedAs;
025    import org.apache.isis.core.metamodel.facetapi.FacetHolder;
026    import org.apache.isis.core.metamodel.facetapi.FacetUtil;
027    import org.apache.isis.core.metamodel.facetapi.FeatureType;
028    import org.apache.isis.core.metamodel.facets.AnnotationBasedFacetFactoryAbstract;
029    import org.apache.isis.core.metamodel.facets.describedas.DescribedAsFacet;
030    import org.apache.isis.core.metamodel.spec.ObjectSpecification;
031    
032    public class DescribedAsAnnotationOnParameterFacetFactory extends AnnotationBasedFacetFactoryAbstract {
033    
034        public DescribedAsAnnotationOnParameterFacetFactory() {
035            super(FeatureType.PARAMETERS_ONLY);
036        }
037    
038        @Override
039        public void processParams(final ProcessParameterContext processParameterContext) {
040    
041            final int paramNum = processParameterContext.getParamNum();
042            final Class<?> parameterType = processParameterContext.getMethod().getParameterTypes()[paramNum];
043            final Annotation[] parameterAnnotations =
044                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 available
053            final DescribedAsFacet parameterTypeDescribedAsFacet = getDescribedAsFacet(parameterType);
054            if (parameterTypeDescribedAsFacet != null) {
055                FacetUtil.addFacet(new DescribedAsFacetForParameterDerivedFromType(parameterTypeDescribedAsFacet,
056                    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 = getSpecificationLookup().loadSpecification(type);
068            return paramTypeSpec.getFacet(DescribedAsFacet.class);
069        }
070    
071    }