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