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.properties.mandatory.staticmethod; 021 022import java.lang.reflect.Method; 023 024import org.apache.isis.core.commons.lang.MethodExtensions; 025import org.apache.isis.core.commons.lang.StringExtensions; 026import org.apache.isis.core.metamodel.exceptions.MetaModelException; 027import org.apache.isis.core.metamodel.facetapi.FacetHolder; 028import org.apache.isis.core.metamodel.facetapi.FacetUtil; 029import org.apache.isis.core.metamodel.facetapi.FeatureType; 030import org.apache.isis.core.metamodel.methodutils.MethodScope; 031import org.apache.isis.core.progmodel.facets.MethodFinderUtils; 032import org.apache.isis.core.progmodel.facets.MethodPrefixBasedFacetFactoryAbstract; 033import org.apache.isis.core.progmodel.facets.MethodPrefixConstants; 034 035public class PropertyOptionalFacetFactory extends MethodPrefixBasedFacetFactoryAbstract { 036 037 private static final String[] PREFIXES = { MethodPrefixConstants.OPTIONAL_PREFIX }; 038 039 public PropertyOptionalFacetFactory() { 040 super(FeatureType.PROPERTIES_ONLY, OrphanValidation.VALIDATE, PREFIXES); 041 } 042 043 @Override 044 public void process(final ProcessMethodContext processMethodContext) { 045 046 attachMandatoryFacetIfOptionalMethodIsFound(processMethodContext); 047 } 048 049 private static void attachMandatoryFacetIfOptionalMethodIsFound(final ProcessMethodContext processMethodContext) { 050 final Method method = processMethodContext.getMethod(); 051 052 final String capitalizedName = StringExtensions.asJavaBaseName(method.getName()); 053 final Class<?> returnType = method.getReturnType(); 054 055 final Class<?> cls = processMethodContext.getCls(); 056 final Method optionalMethod = MethodFinderUtils.findMethod(cls, MethodScope.CLASS, MethodPrefixConstants.OPTIONAL_PREFIX + capitalizedName, boolean.class, NO_PARAMETERS_TYPES); 057 processMethodContext.removeMethod(optionalMethod); 058 059 if (!indicatesOptional(optionalMethod)) { 060 return; 061 } 062 if (returnType.isPrimitive()) { 063 throw new MetaModelException(cls.getName() + "#" + capitalizedName + " cannot be an optional property as it is of a primitive type"); 064 } 065 final FacetHolder property = processMethodContext.getFacetHolder(); 066 FacetUtil.addFacet(new MandatoryFacetOptionalViaMethodForProperty(property)); 067 } 068 069 private static boolean indicatesOptional(final Method method) { 070 if (method != null) { 071 Boolean optionalMethodReturnValue = null; 072 try { 073 optionalMethodReturnValue = (Boolean) MethodExtensions.invoke(method, new Object[0]); 074 } catch (final ClassCastException ex) { 075 // ignore 076 } 077 if (optionalMethodReturnValue == null) { 078 throw new MetaModelException("method " + method + " should return a boolean"); 079 } 080 return optionalMethodReturnValue.booleanValue(); 081 } 082 return false; 083 } 084 085}