forked from objectionary/eo
/
Moja.java
191 lines (179 loc) 路 6.11 KB
/
Moja.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2024 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eolang.maven;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.cactoos.iterable.Mapped;
import org.cactoos.list.ListOf;
import org.cactoos.set.SetOf;
/**
* Mutable mojo builder.
*
* @param <T> Type of mojo
* @since 0.1
*/
public final class Moja<T extends AbstractMojo> {
/**
* The type of mojo.
*/
private final Class<T> type;
/**
* All attributes.
*/
private final Map<String, Object> attrs;
/**
* Ctor.
*
* @param tpe The type
*/
public Moja(final Class<T> tpe) {
this.type = tpe;
this.attrs = new HashMap<>(0);
}
/**
* Add one more attribute and return self.
*
* @param attr The name
* @param value The value
* @return Itself
*/
public Moja<T> with(final String attr, final Object value) {
this.attrs.put(attr, value);
return this;
}
/**
* Copy attributes from the given Mojo.
*
* @param mojo Another mojo
* @return Itself
*/
@SuppressWarnings("PMD.AvoidAccessibilityAlteration")
public Moja<T> copy(final Object mojo) {
final Collection<String> mine = new ListOf<>(
new Mapped<>(
Field::getName,
Moja.fields(this.type)
)
);
for (final Field field : new SetOf<>(Moja.fields(mojo.getClass()))) {
if (!mine.contains(field.getName())) {
continue;
}
if (Modifier.isStatic(field.getModifiers())) {
continue;
}
try {
field.setAccessible(true);
this.with(field.getName(), field.get(mojo));
} catch (final IllegalAccessException ex) {
throw new IllegalStateException(ex);
}
}
return this;
}
/**
* Execute it.
*/
public void execute() {
try {
final AbstractMojo mojo = this.type.getConstructor().newInstance();
for (final Map.Entry<String, Object> ent : this.attrs.entrySet()) {
this.initField(this.type, mojo, ent);
}
mojo.execute();
} catch (final MojoExecutionException | MojoFailureException
| InstantiationException | IllegalAccessException
| NoSuchMethodException | InvocationTargetException ex) {
throw new IllegalStateException(ex);
}
}
@Override
public String toString() {
return String.format("Moja<%s>", this.type.getSimpleName());
}
/**
* List all fields of a class.
* @param cls The class
* @return List of fields
*/
private static Collection<Field> fields(final Class<?> cls) {
final Collection<Field> fields = new ArrayList<>(0);
Class<?> clazz = cls;
while (!clazz.equals(Object.class)) {
fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
clazz = clazz.getSuperclass();
}
return fields;
}
/**
* Init a field.
* @param clazz The mojo class
* @param mojo The mojo
* @param entry Field name and value
* @throws java.lang.IllegalAccessException If can't set field.
* @todo #1494:30min We have some doubts about using Logger.warn in initField method.
* Since it's important to notice the developer during of using the plugin that the property
* can't be set to the Mojo, we can't just remove Logger.warn. On the other hand, we will
* see warnings all the time during unit testing, which can be an insignificant problem.
*/
@SuppressWarnings("PMD.AvoidAccessibilityAlteration")
private void initField(
final Class<?> clazz,
final AbstractMojo mojo,
final Map.Entry<String, Object> entry
) throws IllegalAccessException {
final String name = entry.getKey();
final Optional<Field> declared = Arrays.stream(clazz.getDeclaredFields())
.filter(f -> f.getName().equals(name))
.findFirst();
if (declared.isPresent()) {
final Field field = declared.get();
field.setAccessible(true);
field.set(mojo, entry.getValue());
} else {
final Class<?> parent = clazz.getSuperclass();
if (parent == null) {
throw new IllegalStateException(
String.format(
"Can't find '%s' in '%s'",
name,
this.type.getCanonicalName()
)
);
} else {
this.initField(parent, mojo, entry);
}
}
}
}