001 /*
002 * Copyright 2008-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.codehaus.griffon.runtime.core.controller;
017
018 import griffon.core.artifact.GriffonController;
019 import griffon.core.controller.ActionMetadata;
020 import griffon.core.controller.ActionParameter;
021
022 import javax.annotation.Nonnull;
023 import javax.annotation.Nullable;
024 import java.lang.annotation.Annotation;
025 import java.lang.reflect.Method;
026 import java.util.Arrays;
027
028 import static griffon.util.GriffonNameUtils.requireNonBlank;
029 import static java.lang.System.arraycopy;
030 import static java.util.Objects.requireNonNull;
031
032 /**
033 * @author Andres Almiray
034 * @since 2.11.0
035 */
036 public class DefaultActionMetadata implements ActionMetadata {
037 private final String actionName;
038 private final String fullyQualifiedName;
039 private final Class<?> returnType;
040 private final Annotation[] annotations;
041 private final ActionParameter[] parameters;
042 private final boolean contextual;
043
044 public DefaultActionMetadata(@Nonnull GriffonController controller, @Nonnull String actionName, @Nonnull Method method) {
045 this.actionName = requireNonBlank(actionName, "Argument 'actionName' must not be blank");
046 requireNonNull(controller, "Argument 'controller' must not be null");
047 requireNonNull(method, "Argument 'method' must not be null");
048 this.fullyQualifiedName = controller.getTypeClass().getName() + "." + actionName;
049 this.returnType = method.getReturnType();
050
051 Annotation[] declaredAnnotations = method.getDeclaredAnnotations();
052 annotations = new Annotation[declaredAnnotations.length];
053 arraycopy(declaredAnnotations, 0, annotations, 0, annotations.length);
054
055 this.parameters = new ActionParameter[method.getParameterCount()];
056 Class<?>[] parameterTypes = method.getParameterTypes();
057 Annotation[][] parameterAnnotations = method.getParameterAnnotations();
058 if (this.parameters.length > 0) {
059 boolean c = false;
060 for (int index = 0; index < this.parameters.length; index++) {
061 this.parameters[index] = createActionParameter(index, parameterTypes[index], parameterAnnotations[index]);
062 c |= this.parameters[index].isContextual();
063 }
064 this.contextual = c;
065 } else {
066 this.contextual = false;
067 }
068 }
069
070 @Nonnull
071 protected ActionParameter createActionParameter(int index, @Nonnull Class<?> type, @Nullable Annotation[] annotations) {
072 return new DefaultActionParameter(this, index, type, annotations);
073 }
074
075 @Nonnull
076 @Override
077 public Annotation[] getAnnotations() {
078 return annotations;
079 }
080
081 @Nonnull
082 @Override
083 public Class<?> getReturnType() {
084 return returnType;
085 }
086
087 @Nonnull
088 @Override
089 public ActionParameter[] getParameters() {
090 return parameters;
091 }
092
093 @Nonnull
094 @Override
095 public String getActionName() {
096 return actionName;
097 }
098
099 @Nonnull
100 @Override
101 public String getFullyQualifiedName() {
102 return fullyQualifiedName;
103 }
104
105 @Override
106 public boolean hasContextualArgs() {
107 return contextual;
108 }
109
110 @Override
111 public String toString() {
112 final StringBuilder sb = new StringBuilder("DefaultActionMetadata{");
113 sb.append("actionName='").append(actionName).append('\'');
114 sb.append(", fullyQualifiedName='").append(fullyQualifiedName).append('\'');
115 sb.append(", returnType=").append(returnType);
116 sb.append(", annotations=").append(Arrays.toString(annotations));
117 sb.append(", parameters=").append(Arrays.toString(parameters));
118 sb.append('}');
119 return sb.toString();
120 }
121 }
|