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.actions.typeof.annotation;
021
022import java.lang.reflect.GenericDeclaration;
023import java.lang.reflect.Method;
024import java.lang.reflect.ParameterizedType;
025import java.lang.reflect.Type;
026import java.lang.reflect.TypeVariable;
027
028import org.apache.isis.applib.annotation.TypeOf;
029import org.apache.isis.core.metamodel.facetapi.FacetUtil;
030import org.apache.isis.core.metamodel.facetapi.FeatureType;
031import org.apache.isis.core.metamodel.facets.Annotations;
032import org.apache.isis.core.metamodel.facets.FacetFactoryAbstract;
033import org.apache.isis.core.metamodel.facets.typeof.TypeOfFacetInferredFromArray;
034import org.apache.isis.core.metamodel.facets.typeof.TypeOfFacetInferredFromGenerics;
035import org.apache.isis.core.metamodel.specloader.collectiontyperegistry.CollectionTypeRegistry;
036import org.apache.isis.core.metamodel.specloader.collectiontyperegistry.CollectionTypeRegistryAware;
037
038public class TypeOfAnnotationForActionsFacetFactory extends FacetFactoryAbstract implements CollectionTypeRegistryAware {
039    private CollectionTypeRegistry collectionTypeRegistry;
040
041    public TypeOfAnnotationForActionsFacetFactory() {
042        super(FeatureType.ACTIONS_ONLY);
043    }
044
045    @Override
046    public void process(final ProcessMethodContext processMethodContext) {
047
048        final Method method = processMethodContext.getMethod();
049        final Class<?> methodReturnType = method.getReturnType();
050        if (!collectionTypeRegistry.isCollectionType(methodReturnType) && !collectionTypeRegistry.isArrayType(methodReturnType)) {
051            return;
052        }
053
054        final Class<?> returnType = method.getReturnType();
055        if (returnType.isArray()) {
056            final Class<?> componentType = returnType.getComponentType();
057            FacetUtil.addFacet(new TypeOfFacetInferredFromArray(componentType, processMethodContext.getFacetHolder(), getSpecificationLoader()));
058            return;
059        }
060
061        final TypeOf annotation = Annotations.getAnnotation(method, TypeOf.class);
062        if (annotation != null) {
063            FacetUtil.addFacet(new TypeOfFacetAnnotationForAction(annotation.value(), processMethodContext.getFacetHolder(), getSpecificationLoader()));
064            return;
065        }
066
067        final Type type = method.getGenericReturnType();
068        if (!(type instanceof ParameterizedType)) {
069            return;
070        }
071
072        final ParameterizedType methodParameterizedType = (ParameterizedType) type;
073        final Type[] methodActualTypeArguments = methodParameterizedType.getActualTypeArguments();
074        if (methodActualTypeArguments.length == 0) {
075            return;
076        }
077
078        final Object methodActualTypeArgument = methodActualTypeArguments[0];
079        if (methodActualTypeArgument instanceof Class) {
080            final Class<?> actualType = (Class<?>) methodActualTypeArgument;
081            FacetUtil.addFacet(new TypeOfFacetInferredFromGenerics(actualType, processMethodContext.getFacetHolder(), getSpecificationLoader()));
082            return;
083        }
084
085        if (methodActualTypeArgument instanceof TypeVariable) {
086
087            TypeVariable<?> methodTypeVariable = (TypeVariable<?>) methodActualTypeArgument;
088            final GenericDeclaration methodGenericClassDeclaration = methodTypeVariable.getGenericDeclaration();
089            
090            // try to match up with the actual type argument of the generic superclass.
091            final Type genericSuperclass = processMethodContext.getCls().getGenericSuperclass();
092            if(genericSuperclass instanceof ParameterizedType) {
093                final ParameterizedType parameterizedTypeOfSuperclass = (ParameterizedType)genericSuperclass;
094                if(parameterizedTypeOfSuperclass.getRawType() == methodGenericClassDeclaration) {
095                    final Type[] genericSuperClassActualTypeArguments = parameterizedTypeOfSuperclass.getActualTypeArguments();
096                    // simplification: if there's just one, then use it.
097                    if(methodActualTypeArguments.length == 1) {
098                        final Type actualType = genericSuperClassActualTypeArguments[0];
099                        if(actualType instanceof Class) {
100                            // just being safe
101                            Class<?> actualCls = (Class<?>) actualType;
102                            FacetUtil.addFacet(new TypeOfFacetInferredFromGenerics(actualCls, processMethodContext.getFacetHolder(), getSpecificationLoader()));
103                            return;
104                        }
105                    }
106                }
107            }
108            
109            // TODO: otherwise, what to do?
110            return;
111        }
112
113    }
114
115    @Override
116    public void setCollectionTypeRegistry(final CollectionTypeRegistry collectionTypeRegistry) {
117        this.collectionTypeRegistry = collectionTypeRegistry;
118    }
119
120}