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