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.named.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.named.staticmethod.NamedFacetViaMethod; 038 039/** 040 * Sets up all the {@link Facet}s for an action in a single shot. 041 */ 042public class ActionParameterNamesMethodFacetFactory extends MethodPrefixBasedFacetFactoryAbstract { 043 044 private static final String[] PREFIXES = { MethodPrefixConstants.NAME_PREFIX }; 045 046 /** 047 * Note that the {@link Facet}s registered are the generic ones from 048 * noa-architecture (where they exist) 049 */ 050 public ActionParameterNamesMethodFacetFactory() { 051 super(FeatureType.ACTIONS_ONLY, OrphanValidation.VALIDATE, PREFIXES); 052 } 053 054 // /////////////////////////////////////////////////////// 055 // Actions 056 // /////////////////////////////////////////////////////// 057 058 @Override 059 public void process(final ProcessMethodContext processMethodContext) { 060 061 final FacetedMethod facetedMethod = processMethodContext.getFacetHolder(); 062 final List<FacetedMethodParameter> holderList = facetedMethod.getParameters(); 063 064 attachNamedFacetForParametersIfParameterNamesMethodIsFound(processMethodContext, holderList); 065 } 066 067 private static void attachNamedFacetForParametersIfParameterNamesMethodIsFound(final ProcessMethodContext processMethodContext, final List<FacetedMethodParameter> parameters) { 068 069 if (parameters.isEmpty()) { 070 return; 071 } 072 073 final Method actionMethod = processMethodContext.getMethod(); 074 final String capitalizedName = StringExtensions.asCapitalizedName(actionMethod.getName()); 075 076 final Class<?> cls = processMethodContext.getCls(); 077 final Method namesMethod = MethodFinderUtils.findMethod(cls, MethodScope.CLASS, MethodPrefixConstants.NAME_PREFIX + capitalizedName, String[].class, new Class[0]); 078 if (namesMethod == null) { 079 return; 080 } 081 try { 082 final String[] names = invokeNamesMethod(namesMethod, parameters.size()); 083 for (int i = 0; i < names.length; i++) { 084 // add facets directly to parameters, not to actions 085 FacetUtil.addFacet(new NamedFacetViaMethod(names[i], namesMethod, parameters.get(i))); 086 } 087 } finally { 088 processMethodContext.removeMethod(namesMethod); 089 } 090 } 091 092 private static String[] invokeNamesMethod(final Method namesMethod, final int numElementsRequired) { 093 String[] names = null; 094 try { 095 names = (String[]) MethodExtensions.invokeStatic(namesMethod, new Object[0]); 096 } catch (final ClassCastException ex) { 097 // ignore 098 } 099 if (names == null || names.length != numElementsRequired) { 100 throw new MetaModelException(namesMethod + " must return an String[] array of same size as number of parameters of action"); 101 } 102 return names; 103 } 104}