1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.rat;
20
21 import java.lang.annotation.ElementType;
22 import java.lang.annotation.Retention;
23 import java.lang.annotation.RetentionPolicy;
24 import java.lang.annotation.Target;
25 import java.util.function.Consumer;
26
27 import org.apache.commons.cli.Option;
28 import org.apache.rat.utils.DefaultLog;
29
30 import static java.lang.String.format;
31
32
33
34
35 public final class DeprecationReporter {
36
37
38
39
40 private DeprecationReporter() {
41
42 }
43
44
45
46
47 private static Consumer<Option> consumer = getDefault();
48
49
50
51
52
53 public static Consumer<Option> getDefault() {
54 return o -> {
55 StringBuilder buff = new StringBuilder();
56 if (o.getOpt() != null) {
57 buff.append("-").append(o.getOpt());
58 if (o.getLongOpt() != null) {
59 buff.append(", --").append(o.getLongOpt());
60 }
61 } else {
62 buff.append("--").append(o.getLongOpt());
63 }
64 DefaultLog.getInstance().warn(format("Option [%s] used. %s", buff, o.getDeprecated().toString()));
65 };
66 }
67
68
69
70
71
72 public static Consumer<Option> getLogReporter() {
73 return consumer;
74 }
75
76
77
78
79
80 public static void setLogReporter(final Consumer<Option> consumer) {
81 DeprecationReporter.consumer = consumer;
82 }
83
84
85
86
87 public static void resetLogReporter() {
88 DeprecationReporter.consumer = getDefault();
89 }
90
91
92
93
94
95 public static void logDeprecated(final Class<?> clazz) {
96 if (clazz.getAnnotation(Deprecated.class) != null) {
97 String name = format("Deprecated class used: %s ", clazz);
98 Info info = clazz.getAnnotation(Info.class);
99 if (info == null) {
100 DefaultLog.getInstance().warn(formatEntry(name, "", false, ""));
101 } else {
102 DefaultLog.getInstance().warn(formatEntry(format(name, clazz), info.since(), info.forRemoval(), info.use()));
103 }
104 }
105 }
106
107 private static String formatEntry(final String prefix, final String since, final boolean forRemoval, final String use) {
108 StringBuilder sb = new StringBuilder("Deprecated " + prefix);
109 if (forRemoval) {
110 sb.append(" Scheduled for removal");
111 if (!since.isEmpty()) {
112 sb.append(" since ").append(since);
113 }
114 sb.append(".");
115 } else if (!since.isEmpty()) {
116 sb.append(" Deprecated since ").append(since).append(".");
117 }
118 if (!use.isEmpty()) {
119 sb.append(" Use ").append(use).append(" instead.");
120 }
121 return sb.toString();
122 }
123
124
125
126
127
128
129
130
131 public static void logDeprecated(final String name, final String since, final boolean forRemoval, final String use) {
132 DefaultLog.getInstance().warn(formatEntry(name, since, forRemoval, use));
133 }
134
135
136
137
138
139 @Target({ElementType.TYPE})
140 @Retention(RetentionPolicy.RUNTIME)
141 public @interface Info {
142
143
144
145
146 String since() default "";
147
148
149
150
151
152 boolean forRemoval() default false;
153
154
155
156
157 String use() default "";
158 }
159 }