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.staticmethod; 021 022import java.lang.reflect.Method; 023import java.util.List; 024 025import org.apache.isis.core.commons.lang.MethodExtensions; 026import org.apache.isis.core.commons.lang.StringExtensions; 027import org.apache.isis.core.metamodel.exceptions.MetaModelException; 028import org.apache.isis.core.metamodel.facetapi.Facet; 029import org.apache.isis.core.metamodel.facetapi.FacetUtil; 030import org.apache.isis.core.metamodel.facetapi.FeatureType; 031import org.apache.isis.core.metamodel.facets.FacetedMethod; 032import org.apache.isis.core.metamodel.facets.FacetedMethodParameter; 033import org.apache.isis.core.metamodel.methodutils.MethodScope; 034import org.apache.isis.core.progmodel.facets.MethodFinderUtils; 035import org.apache.isis.core.progmodel.facets.MethodPrefixBasedFacetFactoryAbstract; 036import org.apache.isis.core.progmodel.facets.MethodPrefixConstants; 037import org.apache.isis.core.progmodel.facets.members.describedas.staticmethod.DescribedAsFacetViaMethod; 038 039public class ActionParameterDescriptionsMethodFacetFactory extends MethodPrefixBasedFacetFactoryAbstract { 040 041 private static final String[] PREFIXES = { MethodPrefixConstants.DESCRIPTION_PREFIX }; 042 043 /** 044 * Note that the {@link Facet}s registered are the generic ones from 045 * noa-architecture (where they exist) 046 */ 047 public ActionParameterDescriptionsMethodFacetFactory() { 048 super(FeatureType.ACTIONS_ONLY, OrphanValidation.VALIDATE, PREFIXES); 049 } 050 051 // /////////////////////////////////////////////////////// 052 // Actions 053 // /////////////////////////////////////////////////////// 054 055 @Override 056 public void process(final ProcessMethodContext processMethodContext) { 057 058 final FacetedMethod facetedMethod = processMethodContext.getFacetHolder(); 059 final List<FacetedMethodParameter> holderList = facetedMethod.getParameters(); 060 061 attachDescribedAsFacetForParametersIfParameterDescriptionsMethodIsFound(processMethodContext, holderList); 062 } 063 064 private static void attachDescribedAsFacetForParametersIfParameterDescriptionsMethodIsFound(final ProcessMethodContext processMethodContext, final List<FacetedMethodParameter> parameters) { 065 066 if (parameters.isEmpty()) { 067 return; 068 } 069 070 final Method actionMethod = processMethodContext.getMethod(); 071 072 final String capitalizedName = StringExtensions.asCapitalizedName(actionMethod.getName()); 073 074 final Class<?> cls = processMethodContext.getCls(); 075 final Method descriptionMethod = MethodFinderUtils.findMethod(cls, MethodScope.CLASS, MethodPrefixConstants.DESCRIPTION_PREFIX + capitalizedName, String[].class, new Class[0]); 076 if (descriptionMethod == null) { 077 return; 078 } 079 processMethodContext.removeMethod(descriptionMethod); 080 081 final String[] descriptions = invokeDescriptionsMethod(descriptionMethod, parameters.size()); 082 for (int i = 0; i < descriptions.length; i++) { 083 // add facets directly to parameters, not to actions 084 FacetUtil.addFacet(new DescribedAsFacetViaMethod(descriptions[i], descriptionMethod, parameters.get(i))); 085 } 086 } 087 088 private static String[] invokeDescriptionsMethod(final Method descriptionMethod, final int numElementsRequired) { 089 String[] descriptions = null; 090 try { 091 descriptions = (String[]) MethodExtensions.invokeStatic(descriptionMethod, new Object[0]); 092 } catch (final ClassCastException ex) { 093 // ignore 094 } 095 if (descriptions == null || descriptions.length != numElementsRequired) { 096 throw new MetaModelException(descriptionMethod + " must return an String[] array of same size as number of parameters of action"); 097 } 098 return descriptions; 099 } 100 101}