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