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.modify; 021 022import java.lang.reflect.Method; 023 024import org.apache.isis.core.commons.lang.StringExtensions; 025import org.apache.isis.core.metamodel.facetapi.FacetHolder; 026import org.apache.isis.core.metamodel.facetapi.FacetUtil; 027import org.apache.isis.core.metamodel.facetapi.FeatureType; 028import org.apache.isis.core.metamodel.facets.object.notpersistable.NotPersistableFacet; 029import org.apache.isis.core.metamodel.methodutils.MethodScope; 030import org.apache.isis.core.progmodel.facets.MethodFinderUtils; 031import org.apache.isis.core.progmodel.facets.MethodPrefixBasedFacetFactoryAbstract; 032import org.apache.isis.core.progmodel.facets.MethodPrefixConstants; 033import org.apache.isis.core.progmodel.facets.members.disabled.DisabledFacet; 034import org.apache.isis.core.progmodel.facets.members.disabled.staticmethod.DisabledFacetAlwaysEverywhere; 035import org.apache.isis.core.progmodel.facets.properties.derived.inferred.NotPersistableFacetInferred; 036 037public class PropertySetAndClearFacetFactory extends MethodPrefixBasedFacetFactoryAbstract { 038 039 private static final String[] PREFIXES = { MethodPrefixConstants.SET_PREFIX, MethodPrefixConstants.CLEAR_PREFIX }; 040 041 public PropertySetAndClearFacetFactory() { 042 super(FeatureType.PROPERTIES_ONLY, OrphanValidation.VALIDATE, PREFIXES); 043 } 044 045 @Override 046 public void process(final ProcessMethodContext processMethodContext) { 047 048 final Method setMethod = attachPropertyModifyFacetIfSetterIsFound(processMethodContext); 049 final Method clearMethod = attachPropertyClearFacetIfClearMethodIsFound(processMethodContext); 050 051 attachPropertyClearFacetUsingSetterIfRequired(processMethodContext, setMethod, clearMethod); 052 } 053 054 /** 055 * Sets up the {@link PropertySetterFacetViaSetterMethod} to invoke the 056 * property's setter if available, but if none then marks the property as 057 * {@link NotPersistableFacet not-persistable} and {@link DisabledFacet 058 * disabled} otherwise. 059 */ 060 private static Method attachPropertyModifyFacetIfSetterIsFound(final ProcessMethodContext processMethodContext) { 061 062 final Method getMethod = processMethodContext.getMethod(); 063 final String capitalizedName = StringExtensions.asJavaBaseName(getMethod.getName()); 064 065 final Class<?> cls = processMethodContext.getCls(); 066 final Class<?> returnType = getMethod.getReturnType(); 067 final Class<?>[] paramTypes = new Class[] { returnType }; 068 final Method setMethod = MethodFinderUtils.findMethod(cls, MethodScope.OBJECT, MethodPrefixConstants.SET_PREFIX + capitalizedName, void.class, paramTypes); 069 processMethodContext.removeMethod(setMethod); 070 071 final FacetHolder property = processMethodContext.getFacetHolder(); 072 if (setMethod != null) { 073 FacetUtil.addFacet(new PropertySetterFacetViaSetterMethod(setMethod, property)); 074 FacetUtil.addFacet(new PropertyInitializationFacetViaSetterMethod(setMethod, property)); 075 } else { 076 FacetUtil.addFacet(new NotPersistableFacetInferred(property)); 077 FacetUtil.addFacet(new DisabledFacetAlwaysEverywhere(property)); 078 } 079 080 return setMethod; 081 } 082 083 private Method attachPropertyClearFacetIfClearMethodIsFound(final ProcessMethodContext processMethodContext) { 084 final Class<?> cls = processMethodContext.getCls(); 085 final Method getMethod = processMethodContext.getMethod(); 086 final FacetHolder property = processMethodContext.getFacetHolder(); 087 088 final String capitalizedName = StringExtensions.asJavaBaseName(getMethod.getName()); 089 final Method clearMethod = MethodFinderUtils.findMethod(cls, MethodScope.OBJECT, MethodPrefixConstants.CLEAR_PREFIX + capitalizedName, void.class, NO_PARAMETERS_TYPES); 090 091 if (clearMethod == null) { 092 return null; 093 } 094 processMethodContext.removeMethod(clearMethod); 095 096 FacetUtil.addFacet(new PropertyClearFacetViaClearMethod(clearMethod, property)); 097 098 return clearMethod; 099 } 100 101 private static void attachPropertyClearFacetUsingSetterIfRequired(final ProcessMethodContext processMethodContext, final Method setMethod, final Method clearMethod) { 102 103 if (clearMethod != null) { 104 return; 105 } 106 if (setMethod == null) { 107 return; 108 } 109 final FacetHolder property = processMethodContext.getFacetHolder(); 110 FacetUtil.addFacet(new PropertyClearFacetViaSetterMethod(setMethod, property)); 111 } 112 113}