55package net .minecraftforge .gradleutils .shared ;
66
77import groovy .lang .GroovyObjectSupport ;
8- import org .codehaus .groovy .runtime .InvokerHelper ;
98import org .gradle .api .Action ;
109import org .gradle .api .Task ;
1110import org .gradle .api .Transformer ;
1211import org .gradle .api .file .Directory ;
1312import org .gradle .api .file .FileSystemLocation ;
13+ import org .gradle .api .logging .Logger ;
14+ import org .gradle .api .logging .Logging ;
1415import org .gradle .api .problems .Problem ;
1516import org .gradle .api .problems .ProblemGroup ;
1617import org .gradle .api .problems .ProblemId ;
2728import javax .inject .Inject ;
2829import java .io .File ;
2930import java .io .IOException ;
31+ import java .io .Serializable ;
3032import java .nio .file .Files ;
3133import java .util .Collection ;
32- import java .util .Objects ;
3334import java .util .function .Predicate ;
3435import java .util .stream .Collectors ;
3536import java .util .stream .Stream ;
3637
3738/// The enhanced problems contain several base helper members to help reduce duplicate code between Gradle plugins.
38- public abstract class EnhancedProblems extends GroovyObjectSupport implements Problems , Predicate <String > {
39+ public abstract class EnhancedProblems implements Serializable , Predicate <String > {
40+ private static final long serialVersionUID = 2037193772993696096L ;
41+
3942 /// The common message to send in [ProblemSpec#solution(String)] when reporting problems.
4043 protected static final String HELP_MESSAGE = "Consult the documentation or ask for help on the Forge Forums, GitHub, or Discord server." ;
4144
45+ /// The display name used in reported problems to describe the plugin using this.
4246 private final String displayName ;
47+ /// The problem group used when reporting problems using this.
4348 private final ProblemGroup problemGroup ;
4449
45- private final Problems delegate ;
46- private final Predicate <String > properties ;
50+ /// The problems instance provided by Gradle services.
51+ ///
52+ /// @return The problems instance
53+ /// @see <a href="https://docs.gradle.org/current/userguide/reporting_problems.html">Reporting Problems</a>
54+ /// @deprecated Does not handle if Problems API cannot be accessed. Use [#getDelegate()].
55+ @ Deprecated
56+ @ SuppressWarnings ("DeprecatedIsStillUsed" ) // Used by #getDelegate
57+ protected abstract @ Inject Problems getProblems ();
4758
48- @ Override
49- public ProblemReporter getReporter () {
50- return this .delegate .getReporter ();
59+ /// The provider factory provided by Gradle services.
60+ ///
61+ /// @return The provider factory
62+ /// @see <a href="https://docs.gradle.org/current/userguide/service_injection.html#providerfactory">ProviderFactory
63+ /// Service Injection</a>
64+ protected abstract @ Inject ProviderFactory getProviders ();
65+
66+ /// Gets the problems instance used by this enhanced problems.
67+ ///
68+ /// @return The delegate problems instance
69+ public final Problems getDelegate () {
70+ try {
71+ return this .getProblems ();
72+ } catch (Exception e ) {
73+ return EmptyReporter .AS_PROBLEMS ;
74+ }
5175 }
5276
53- /// Gets the problem group used by this problems instance. It is unique for the plugin.
77+ /// Gets the problem reporter used by the [delegate][#getDelegate()] problems instance.
78+ ///
79+ /// @return The problem reporter
80+ protected final ProblemReporter getReporter () {
81+ return this .getDelegate ().getReporter ();
82+ }
83+
84+ /// Gets the problem group used by this enhanced problems. It is unique for the plugin.
5485 ///
5586 /// @return The problem group
5687 public final ProblemGroup getProblemGroup () {
@@ -66,9 +97,13 @@ public final ProblemGroup getProblemGroup() {
6697 /// [Inject], and have no parameters, passing in static strings to this base constructor.
6798 protected EnhancedProblems (String name , String displayName ) {
6899 this .problemGroup = ProblemGroup .create (name , this .displayName = displayName );
100+ }
69101
70- this .delegate = this .unwrapProblems ();
71- this .properties = this .unwrapProperties ();
102+ /// Gets the logger to be used by this enhanced problems.
103+ ///
104+ /// @return The logger
105+ protected final Logger getLogger () {
106+ return Logging .getLogger (this .getClass ());
72107 }
73108
74109 /// Creates a problem ID to be used when reporting problems. The name must be unique so as to not potentially
@@ -88,8 +123,13 @@ protected final ProblemId id(String name, String displayName) {
88123 ///
89124 /// @param property The property to test
90125 /// @return If the property exists and is `true`
126+ @ Override
91127 public final boolean test (String property ) {
92- return this .properties .test (property );
128+ try {
129+ return isTrue (this .getProviders (), property );
130+ } catch (Exception e ) {
131+ return Boolean .getBoolean (property );
132+ }
93133 }
94134
95135
@@ -108,8 +148,8 @@ public final RuntimeException illegalPluginTarget(Exception e, Class<?> firstAll
108148 return this .getReporter ().throwing (e , id ("invalid-plugin-target" , "Invalid plugin target" ), spec -> spec
109149 .details (String .format (
110150 "Attempted to apply the %s plugin to an invalid target.\n " +
111- "This plugin can only be applied on the following types:\n " +
112- "%s" , this .displayName , Stream .concat (Stream .of (firstAllowedTarget ), Stream .of (allowedTargets )).map (Class ::getName ).collect (Collectors .joining (", " , "[" , "]" ))))
151+ "This plugin can only be applied on the following types:\n " +
152+ "%s" , this .displayName , Stream .concat (Stream .of (firstAllowedTarget ), Stream .of (allowedTargets )).map (Class ::getName ).collect (Collectors .joining (", " , "[" , "]" ))))
113153 .severity (Severity .ERROR )
114154 .stackLocation ()
115155 .solution ("Use a valid plugin target." )
@@ -125,7 +165,7 @@ public final RuntimeException illegalPluginTarget(Exception e, String allowedTar
125165 return this .getReporter ().throwing (e , id ("invalid-plugin-target" , "Invalid plugin target" ), spec -> spec
126166 .details (String .format (
127167 "Attempted to apply the %s plugin to an invalid target.\n " +
128- "This plugin can only be applied on %s" , this .displayName , allowedTargets ))
168+ "This plugin can only be applied on %s. " , this .displayName , allowedTargets ))
129169 .severity (Severity .ERROR )
130170 .stackLocation ()
131171 .solution ("Use a valid plugin target." )
@@ -158,8 +198,8 @@ public final void reportToolExecNotEnhanced(Task task) {
158198 this .getReporter ().report (id ("tool-exec-not-enhanced" , "ToolExec subclass doesn't implement EnhancedTask" ), spec -> spec
159199 .details (String .format (
160200 "Implementing subclass of ToolExecBase should also implement (a subclass of) EnhancedTask.\n " +
161- "Not doing so will result in global caches being ignored. Please check your implementations.\n " +
162- "Affected task: %s (%s)" , task , task .getClass ()))
201+ "Not doing so will result in global caches being ignored. Please check your implementations.\n " +
202+ "Affected task: %s (%s)" , task , task .getClass ()))
163203 .severity (Severity .WARNING )
164204 .stackLocation ()
165205 .solution ("Double check your task implementation." ));
@@ -172,7 +212,7 @@ public final void reportToolExecNotEnhanced(Class<?> task) {
172212 this .getReporter ().report (id ("enhanced-task-no-plugin" , "EnhancedTask doesn't implement #pluginType" ), spec -> spec
173213 .details (String .format (
174214 "Implementation of EnhancedTask must implement #pluginType\n " +
175- "Affected task type: %s" , task ))
215+ "Affected task type: %s" , task ))
176216 .severity (Severity .ERROR )
177217 .stackLocation ()
178218 .solution ("Double check your task implementation." ));
@@ -185,8 +225,8 @@ public final void reportToolExecEagerArgs(Task task) {
185225 this .getReporter ().report (id ("tool-exec-eager-args" , "ToolExecBase implementation adds arguments without using addArguments()" ), spec -> spec
186226 .details (String .format (
187227 "A ToolExecBase task is eagerly adding arguments using JavaExec#args without using ToolExecBase#addArguments.\n " +
188- "This may cause implementations or superclasses to have their arguments ignored or missing.\n " +
189- "Affected task: %s (%s)" , task , task .getClass ()))
228+ "This may cause implementations or superclasses to have their arguments ignored or missing.\n " +
229+ "Affected task: %s (%s)" , task , task .getClass ()))
190230 .severity (Severity .WARNING )
191231 .stackLocation ()
192232 .solution ("Use ToolExecBase#addArguments" ));
@@ -209,7 +249,7 @@ public final <T extends FileSystemLocation> Transformer<T, T> ensureFileLocation
209249 throw this .getReporter ().throwing (e , id ("cannot-ensure-directory" , "Failed to create directory" ), spec -> spec
210250 .details (String .format (
211251 "Failed to create a directory required for %s to function.\n " +
212- "Directory: %s" ,
252+ "Directory: %s" ,
213253 this .displayName , dir .getAbsolutePath ()))
214254 .severity (Severity .ERROR )
215255 .stackLocation ()
@@ -272,6 +312,8 @@ default RuntimeException toRTE(Throwable exception) {
272312 /* MINIMAL */
273313
274314 static abstract class Minimal extends EnhancedProblems implements HasPublicType {
315+ private static final long serialVersionUID = -6804792858587052477L ;
316+
275317 @ Inject
276318 public Minimal (String name , String displayName ) {
277319 super (name , displayName );
@@ -284,43 +326,6 @@ public TypeOf<?> getPublicType() {
284326 }
285327
286328
287- /* IMPL INSTANTIATION */
288-
289- /// The problems instance provided by Gradle services.
290- ///
291- /// @return The problems instance
292- /// @see <a href="https://docs.gradle.org/current/userguide/reporting_problems.html">Reporting Problems</a>
293- protected @ Inject Problems getProblems () {
294- throw new IllegalStateException ();
295- }
296-
297- /// The provider factory provided by Gradle services.
298- ///
299- /// @return The provider factory
300- /// @see <a href="https://docs.gradle.org/current/userguide/service_injection.html#providerfactory">ProviderFactory
301- /// Service Injection</a>
302- protected @ Inject ProviderFactory getProviders () {
303- throw new IllegalStateException ();
304- }
305-
306- private Problems unwrapProblems () {
307- try {
308- return this .getProblems ();
309- } catch (Exception e ) {
310- return EmptyReporter .AS_PROBLEMS ;
311- }
312- }
313-
314- private Predicate <String > unwrapProperties () {
315- try {
316- ProviderFactory providers = Objects .requireNonNull (this .getProviders ());
317- return property -> isTrue (providers , property );
318- } catch (Exception e ) {
319- return Boolean ::getBoolean ;
320- }
321- }
322-
323-
324329 /* IMPL UTILS */
325330
326331 private static @ Nullable Boolean getBoolean (Provider <? extends String > provider ) {
@@ -344,35 +349,4 @@ private static boolean isFalse(ProviderFactory providers, String property) {
344349 private static boolean isFalse (Provider <? extends String > provider ) {
345350 return Boolean .FALSE .equals (getBoolean (provider ));
346351 }
347-
348-
349- /* META CLASS */
350-
351- @ Override
352- public Object invokeMethod (String name , Object args ) {
353- try {
354- return super .invokeMethod (name , args );
355- } catch (Exception suppressed ) {
356- try {
357- return InvokerHelper .getMetaClass (this .delegate ).invokeMethod (this .delegate , name , args );
358- } catch (Exception e ) {
359- e .addSuppressed (suppressed );
360- throw e ;
361- }
362- }
363- }
364-
365- @ Override
366- public Object getProperty (String propertyName ) {
367- try {
368- return super .getProperty (propertyName );
369- } catch (Exception suppressed ) {
370- try {
371- return InvokerHelper .getProperty (this .delegate , propertyName );
372- } catch (Exception e ) {
373- e .addSuppressed (suppressed );
374- throw e ;
375- }
376- }
377- }
378352}
0 commit comments