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.collections.typeof;
021    
022    import java.lang.reflect.ParameterizedType;
023    import java.lang.reflect.Type;
024    import java.lang.reflect.TypeVariable;
025    
026    import org.apache.isis.applib.annotation.TypeOf;
027    import org.apache.isis.core.metamodel.facetapi.FacetUtil;
028    import org.apache.isis.core.metamodel.facetapi.FeatureType;
029    import org.apache.isis.core.metamodel.facets.AnnotationBasedFacetFactoryAbstract;
030    import org.apache.isis.core.metamodel.facets.typeof.TypeOfFacetInferredFromArray;
031    import org.apache.isis.core.metamodel.facets.typeof.TypeOfFacetInferredFromGenerics;
032    import org.apache.isis.core.metamodel.specloader.collectiontyperegistry.CollectionTypeRegistry;
033    import org.apache.isis.core.metamodel.specloader.collectiontyperegistry.CollectionTypeRegistryAware;
034    
035    public class TypeOfAnnotationForCollectionsFacetFactory extends AnnotationBasedFacetFactoryAbstract implements
036        CollectionTypeRegistryAware {
037        private CollectionTypeRegistry collectionTypeRegistry;
038    
039        public TypeOfAnnotationForCollectionsFacetFactory() {
040            super(FeatureType.COLLECTIONS_ONLY);
041        }
042    
043        @Override
044        public void process(final ProcessMethodContext processMethodContext) {
045    
046            final TypeOf annotation = getAnnotation(processMethodContext.getMethod(), TypeOf.class);
047    
048            final Class<?> methodReturnType = processMethodContext.getMethod().getReturnType();
049            if (!collectionTypeRegistry.isCollectionType(methodReturnType)
050                && !collectionTypeRegistry.isArrayType(methodReturnType)) {
051                return;
052            }
053    
054            final Class<?> returnType = processMethodContext.getMethod().getReturnType();
055            if (returnType.isArray()) {
056                final Class<?> componentType = returnType.getComponentType();
057                FacetUtil.addFacet(new TypeOfFacetInferredFromArray(componentType, processMethodContext.getFacetHolder(),
058                    getSpecificationLookup()));
059                return;
060            }
061    
062            if (annotation != null) {
063                FacetUtil.addFacet(new TypeOfFacetAnnotationForCollection(annotation.value(), processMethodContext
064                    .getFacetHolder(), getSpecificationLookup()));
065                return;
066            }
067    
068            final Type type = processMethodContext.getMethod().getGenericReturnType();
069            if (!(type instanceof ParameterizedType)) {
070                return;
071            }
072    
073            final ParameterizedType parameterizedType = (ParameterizedType) type;
074            final Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
075            if (actualTypeArguments.length == 0) {
076                return;
077            }
078    
079            final Object actualTypeArgument = actualTypeArguments[0];
080            if (actualTypeArgument instanceof Class) {
081                final Class<?> actualType = (Class<?>) actualTypeArgument;
082                FacetUtil.addFacet(new TypeOfFacetInferredFromGenerics(actualType, processMethodContext.getFacetHolder(),
083                    getSpecificationLookup()));
084                return;
085            }
086    
087            if (actualTypeArgument instanceof TypeVariable) {
088    
089                // TODO: what to do here?
090                return;
091            }
092    
093        }
094    
095        @Override
096        public void setCollectionTypeRegistry(final CollectionTypeRegistry collectionTypeRegistry) {
097            this.collectionTypeRegistry = collectionTypeRegistry;
098        }
099    
100    }