View Javadoc
1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  
15  package org.basepom.mojo.propertyhelper.fields;
16  
17  import static com.google.common.base.Preconditions.checkState;
18  import static java.lang.String.format;
19  
20  import org.basepom.mojo.propertyhelper.Field;
21  import org.basepom.mojo.propertyhelper.FieldContext;
22  import org.basepom.mojo.propertyhelper.ValueProvider;
23  import org.basepom.mojo.propertyhelper.definitions.MacroDefinition;
24  import org.basepom.mojo.propertyhelper.macros.MacroType;
25  
26  import java.util.Optional;
27  import java.util.StringJoiner;
28  
29  import com.google.common.annotations.VisibleForTesting;
30  import org.apache.maven.plugin.MojoExecutionException;
31  
32  public final class MacroField extends Field<String, MacroDefinition> {
33  
34      private final ValueProvider valueProvider;
35      private final FieldContext context;
36  
37      @VisibleForTesting
38      public static MacroField forTesting(MacroDefinition dateDefinition, ValueProvider valueProvider, FieldContext fieldContext) {
39          return new MacroField(dateDefinition, valueProvider, fieldContext);
40      }
41  
42      public MacroField(final MacroDefinition macroDefinition, final ValueProvider valueProvider, final FieldContext context) {
43          super(macroDefinition, context);
44  
45          this.valueProvider = valueProvider;
46          this.context = context;
47      }
48  
49      @Override
50      public String getFieldName() {
51          return fieldDefinition.getId();
52      }
53  
54      @Override
55      public String getValue() throws MojoExecutionException {
56          final Optional<String> type = fieldDefinition.getMacroType();
57          final MacroType macroType;
58  
59          try {
60              if (type.isPresent()) {
61                  macroType = context.getMacros().get(type.get());
62                  checkState(macroType != null, "Could not locate macro '%s'", type.get());
63              } else {
64                  final Optional<String> macroClassName = fieldDefinition.getMacroClass();
65                  checkState(macroClassName.isPresent(), "No definition for macro '%s' found!", fieldDefinition.getId());
66                  final Class<? extends MacroType> macroClass = (Class<? extends MacroType>) Class.forName(macroClassName.get());
67                  macroType = macroClass.getDeclaredConstructor().newInstance();
68              }
69  
70              return formatResult(macroType.getValue(fieldDefinition, valueProvider, context).orElse(null));
71  
72          } catch (ReflectiveOperationException e) {
73              throw new MojoExecutionException(format("Could not instantiate '%s'", fieldDefinition), e);
74          }
75      }
76  
77      @Override
78      public String toString() {
79          return new StringJoiner(", ", MacroField.class.getSimpleName() + "[", "]")
80              .add("valueProvider=" + valueProvider)
81              .add("context=" + context)
82              .toString();
83      }
84  }