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
020 package org.apache.isis.core.progmodel.facets.actions.defaults.method;
021
022 import java.lang.reflect.Method;
023 import java.util.Collections;
024 import java.util.List;
025
026 import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
027 import org.apache.isis.core.metamodel.adapter.util.AdapterInvokeUtils;
028 import org.apache.isis.core.metamodel.facetapi.Facet;
029 import org.apache.isis.core.metamodel.facetapi.FacetHolder;
030 import org.apache.isis.core.metamodel.facets.ImperativeFacet;
031 import org.apache.isis.core.metamodel.facets.actions.defaults.ActionDefaultsFacetAbstract;
032 import org.apache.isis.core.metamodel.facets.actions.invoke.ActionInvocationFacet;
033 import org.apache.isis.core.progmodel.facets.actions.invoke.ActionInvocationFacetViaMethod;
034
035 public class ActionDefaultsFacetViaMethod extends ActionDefaultsFacetAbstract implements ImperativeFacet {
036
037 private final Method defaultMethod;
038
039 @SuppressWarnings("unused")
040 private final Method actionMethod;
041
042 public ActionDefaultsFacetViaMethod(final Method defaultMethod, final FacetHolder holder) {
043 super(holder, false);
044 this.defaultMethod = defaultMethod;
045 this.actionMethod = determineActionMethod(holder);
046 }
047
048 private static Method determineActionMethod(final FacetHolder holder) {
049 Method method2;
050 final Facet actionInvocationFacet = holder.getFacet(ActionInvocationFacet.class);
051 if (actionInvocationFacet instanceof ActionInvocationFacetViaMethod) {
052 final ActionInvocationFacetViaMethod facetViaMethod =
053 (ActionInvocationFacetViaMethod) actionInvocationFacet;
054 method2 = facetViaMethod.getMethods().get(0);
055 } else {
056 method2 = null;
057 }
058 return method2;
059 }
060
061 /**
062 * Returns a singleton list of the {@link Method} provided in the constructor.
063 */
064 @Override
065 public List<Method> getMethods() {
066 return Collections.singletonList(defaultMethod);
067 }
068
069 @Override
070 public boolean impliesResolve() {
071 return true;
072 }
073
074 @Override
075 public boolean impliesObjectChanged() {
076 return false;
077 }
078
079 @Override
080 public Object[] getDefaults(final ObjectAdapter owningAdapter) {
081 return (Object[]) AdapterInvokeUtils.invoke(defaultMethod, owningAdapter);
082 }
083
084 @Override
085 protected String toStringValues() {
086 return "method=" + defaultMethod;
087 }
088
089 }