DefaultActionParameter.java
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.controller.ActionMetadata;
019 import griffon.core.controller.ActionParameter;
020 import griffon.inject.Contextual;
021 
022 import javax.annotation.Nonnull;
023 import javax.annotation.Nullable;
024 import javax.inject.Named;
025 import java.lang.annotation.Annotation;
026 import java.util.Arrays;
027 
028 import static griffon.util.GriffonClassUtils.requireState;
029 import static griffon.util.GriffonNameUtils.isBlank;
030 import static java.lang.System.arraycopy;
031 
032 /**
033  @author Andres Almiray
034  @since 2.11.0
035  */
036 public class DefaultActionParameter implements ActionParameter {
037     private static final Annotation[] EMPTY_ARRAY = new Annotation[0];
038 
039     private final int index;
040     private final Class<?> type;
041     private final String name;
042     private final Annotation[] annotations;
043     private final boolean contextual;
044     private final boolean nullable;
045 
046     public DefaultActionParameter(@Nonnull ActionMetadata actionMetadata, int index, @Nonnull Class<?> type, @Nullable Annotation[] annotations) {
047         requireState(index >= 0"Parameter index must be equal or greater than 0 [" + index + "]");
048         this.index = index;
049         this.type = type;
050 
051         if (annotations != null) {
052             this.annotations = new Annotation[annotations.length];
053             arraycopy(annotations, 0this.annotations, 0, annotations.length);
054 
055             boolean c = false;
056             boolean u = false;
057             String n = null;
058             for (Annotation annotation : this.annotations) {
059                 if (Contextual.class.isAssignableFrom(annotation.annotationType())) {
060                     c = true;
061                 }
062                 if (Nonnull.class.isAssignableFrom(annotation.annotationType())) {
063                     u = true;
064                 }
065                 if (Named.class.isAssignableFrom(annotation.annotationType())) {
066                     n = ((Namedannotation).value();
067                 }
068             }
069             this.contextual = c;
070             this.nullable = !u;
071             this.name = !isBlank(n? n : resolveParameterName(actionMetadata, index);
072         else {
073             this.annotations = EMPTY_ARRAY;
074             this.contextual = false;
075             this.nullable = false;
076             this.name = resolveParameterName(actionMetadata, index);
077         }
078     }
079 
080     @Nonnull
081     protected String resolveParameterName(@Nonnull ActionMetadata actionMetadata, int index) {
082         return actionMetadata.getFullyQualifiedName() ".arg" + index;
083     }
084 
085     @Override
086     public int getIndex() {
087         return index;
088     }
089 
090     @Nonnull
091     @Override
092     public Annotation[] getAnnotations() {
093         return annotations;
094     }
095 
096     @Nonnull
097     @Override
098     public Class<?> getType() {
099         return type;
100     }
101 
102     @Nonnull
103     @Override
104     public String getName() {
105         return name;
106     }
107 
108     @Override
109     public boolean isContextual() {
110         return contextual;
111     }
112 
113     @Override
114     public boolean isNullable() {
115         return nullable;
116     }
117 
118     @Override
119     public String toString() {
120         final StringBuilder sb = new StringBuilder("DefaultActionParameter{");
121         sb.append("index=").append(index);
122         sb.append(", type=").append(type);
123         sb.append(", name=").append(name);
124         sb.append(", annotations=").append(Arrays.toString(annotations));
125         sb.append(", contextual=").append(contextual);
126         sb.append(", nullable=").append(nullable);
127         sb.append('}');
128         return sb.toString();
129     }
130 }