Skip to content

Commit

Permalink
Added DeprecationReporter and logged deprecated uses
Browse files Browse the repository at this point in the history
  • Loading branch information
Claudenw committed May 12, 2024
1 parent c341ecb commit 008577f
Show file tree
Hide file tree
Showing 29 changed files with 334 additions and 114 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package org.apache.rat;

import org.apache.commons.cli.Option;
import org.apache.rat.utils.DefaultLog;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.function.Consumer;

import static java.lang.String.format;

/**
* Reporting methods for deprecated objects.
*/
public class DeprecationReporter {
/**
* Deprecated Command line option consumer.
*/
public static Consumer<Option> LOG_DEPRECATED = o -> {
StringBuilder buff = new StringBuilder();
if (o.getOpt() != null) {
buff.append("-").append(o.getOpt());
if (o.getLongOpt() != null) {
buff.append(", --").append(o.getLongOpt());
}
} else {
buff.append("--").append(o.getLongOpt());
}
DefaultLog.getInstance().warn(format("Option [%s] used. %s", buff, o.getDeprecated().toString()));
};

/**
* Log Deprecated class use.
* @param clazz the Deprecated class to log
*/
public static void logDeprecated(Class<?> clazz) {
if (clazz.getAnnotation(Deprecated.class) != null) {
StringBuilder sb = new StringBuilder(format("Deprecated class used: %s ", clazz));
Info info = clazz.getAnnotation(Info.class);
if (info != null) {
if (info.forRemoval()) {
sb.append(" Scheduled for removal");
if (!info.since().isEmpty()) {
sb.append( " since ").append(info.since());
}
sb.append(".");
} else if (!info.since().isEmpty()) {
sb.append(" Deprecated since ").append(info.since()).append(".");
}
if (!info.use().isEmpty()) {
sb.append(" Use ").append(info.use()).append(" instead.");
}
}
DefaultLog.getInstance().warn(sb.toString());
}
}

/**
* Annotation to provide deprecation information for Java8.
* TODO remove this when Java 8 no longer supported.
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Info {
/**
* The common name for the component. If not specified the name of the field or class is used.
* @return the component name.
*/
String since() default "";

/**
* The description of the component.
* @return the component description.
*/
boolean forRemoval() default false;
/**
* The component type
* @return the component type.
*/
String use() default "";
}
}
72 changes: 52 additions & 20 deletions apache-rat-core/src/main/java/org/apache/rat/Report.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public String desc() {
/**
* Adds license headers to files missing headers.
*/
private static final DeprecatedAttributes addAttributes = DeprecatedAttributes.builder().setForRemoval(true).setSince("1.17.0")
private static final DeprecatedAttributes addAttributes = DeprecatedAttributes.builder().setForRemoval(true).setSince("0.17.0")
.setDescription("Use '-A' or --addLicense instead.").get();
static final OptionGroup ADD = new OptionGroup()
.addOption(Option.builder("a").hasArg(false)
Expand All @@ -148,7 +148,7 @@ public String desc() {
.desc("Define the output file where to write a report to (default is System.out).")
.converter(Converter.FILE).build();

static final DeprecatedAttributes dirAttributes = DeprecatedAttributes.builder().setForRemoval(true).setSince("1.17.0")
static final DeprecatedAttributes dirAttributes = DeprecatedAttributes.builder().setForRemoval(true).setSince("0.17.0")
.setDescription("Use '--'").get();
static final Option DIR = Option.builder().option("d").longOpt("dir").hasArg()
.desc(format( "[%s] %s", dirAttributes, "Used to indicate end of list when using --exclude.")).argName("DirOrArchive")
Expand Down Expand Up @@ -262,6 +262,13 @@ public String desc() {
.converter(s -> ReportConfiguration.Processing.valueOf(s.toUpperCase()))
.build();


private static void logDeprecated(Option option) {
if (option.isDeprecated()) {
DeprecationReporter.LOG_DEPRECATED.accept(option);
}
}

private static String asString(Object[] args) {
return Arrays.stream(args).map(Object::toString).collect(Collectors.joining(", "));
}
Expand All @@ -276,13 +283,13 @@ private static String asString(Object[] args) {
public static void main(String[] args) throws Exception {
ReportConfiguration configuration = parseCommands(args, Report::printUsage);
if (configuration != null) {
configuration.validate(DefaultLog.INSTANCE::error);
configuration.validate(DefaultLog.getInstance()::error);
new Reporter(configuration).output();
}
}

/**
* Parses the standard options to create a ReportConfiguraton.
* Parses the standard options to create a ReportConfiguration.
*
* @param args the arguments to parse
* @param helpCmd the help command to run when necessary.
Expand All @@ -294,7 +301,7 @@ public static ReportConfiguration parseCommands(String[] args, Consumer<Options>
}

/**
* Parses the standard options to create a ReportConfiguraton.
* Parses the standard options to create a ReportConfiguration.
*
* @param args the arguments to parse
* @param helpCmd the help command to run when necessary.
Expand All @@ -305,23 +312,23 @@ public static ReportConfiguration parseCommands(String[] args, Consumer<Options>
public static ReportConfiguration parseCommands(String[] args, Consumer<Options> helpCmd, boolean noArgs) throws IOException {
Options opts = buildOptions();
CommandLine cl;
Consumer<Option> logDeprecated = o -> DefaultLog.INSTANCE.warn( format("Deprecated option %s used. %s", o.getOpt(), o.getDeprecated().toString()));
try {
//DefaultParser.builder().setDeprecatedHandler()
cl = DefaultParser.builder().setDeprecatedHandler(logDeprecated).build().parse(opts, args);
cl = DefaultParser.builder().setDeprecatedHandler(DeprecationReporter.LOG_DEPRECATED).build().parse(opts, args);
} catch (ParseException e) {
DefaultLog.INSTANCE.error(e.getMessage());
DefaultLog.INSTANCE.error("Please use the \"--help\" option to see a list of valid commands and options");
DefaultLog.getInstance().error(e.getMessage());
DefaultLog.getInstance().error("Please use the \"--help\" option to see a list of valid commands and options");
System.exit(1);
return null; // dummy return (won't be reached) to avoid Eclipse complaint about possible NPE
// for "cl"
}

if (cl.hasOption(LOG_LEVEL)) {
logDeprecated(LOG_LEVEL);
DefaultLog log = (DefaultLog) DefaultLog.getInstance();
try {
DefaultLog.INSTANCE.setLevel(cl.getParsedOptionValue(LOG_LEVEL));
log.setLevel(cl.getParsedOptionValue(LOG_LEVEL));
} catch (ParseException e) {
logParseException(e, LOG_LEVEL, cl, DefaultLog.INSTANCE.getLevel());
logParseException(e, LOG_LEVEL, cl, log.getLevel());
}
}
if (cl.hasOption(HELP)) {
Expand All @@ -342,15 +349,25 @@ public static ReportConfiguration parseCommands(String[] args, Consumer<Options>
}

private static void logParseException(ParseException e, Option opt, CommandLine cl, Object dflt) {
DefaultLog.INSTANCE.warn(format("Invalid %s specified: %s ", opt.getOpt(), cl.getOptionValue(opt)));
DefaultLog.INSTANCE.warn(format("%s set to: %s", opt.getOpt(), dflt));
DefaultLog.getInstance().warn(format("Invalid %s specified: %s ", opt.getOpt(), cl.getOptionValue(opt)));
DefaultLog.getInstance().warn(format("%s set to: %s", opt.getOpt(), dflt));
DefaultLog.getInstance().debug(e);
}

static ReportConfiguration createConfiguration(String baseDirectory, CommandLine cl) throws IOException {
final ReportConfiguration configuration = new ReportConfiguration(DefaultLog.INSTANCE);
final ReportConfiguration configuration = new ReportConfiguration(DefaultLog.getInstance());

if (cl.hasOption(DIR)) {
logDeprecated(DIR);
}

if (cl.hasOption(DRY_RUN)) {
logDeprecated(DRY_RUN);
configuration.setDryRun(cl.hasOption(DRY_RUN));
}

configuration.setDryRun(cl.hasOption(DRY_RUN));
if (cl.hasOption(LIST_FAMILIES)) {
logDeprecated(LIST_FAMILIES);
try {
configuration.listFamilies(cl.getParsedOptionValue(LIST_FAMILIES));
} catch (ParseException e) {
Expand All @@ -359,6 +376,7 @@ static ReportConfiguration createConfiguration(String baseDirectory, CommandLine
}

if (cl.hasOption(LIST_LICENSES)) {
logDeprecated(LIST_LICENSES);
try {
configuration.listLicenses(cl.getParsedOptionValue(LIST_LICENSES));
} catch (ParseException e) {
Expand All @@ -367,6 +385,7 @@ static ReportConfiguration createConfiguration(String baseDirectory, CommandLine
}

if (cl.hasOption(ARCHIVE)) {
logDeprecated(ARCHIVE);
try {
configuration.setArchiveProcessing(cl.getParsedOptionValue(ARCHIVE));
} catch (ParseException e) {
Expand All @@ -375,6 +394,7 @@ static ReportConfiguration createConfiguration(String baseDirectory, CommandLine
}

if (cl.hasOption(OUT)) {
logDeprecated(OUT);
try {
configuration.setOut((File) cl.getParsedOptionValue(OUT));
} catch (ParseException e) {
Expand All @@ -383,21 +403,29 @@ static ReportConfiguration createConfiguration(String baseDirectory, CommandLine
}

if (cl.hasOption(SCAN_HIDDEN_DIRECTORIES)) {
logDeprecated(SCAN_HIDDEN_DIRECTORIES);
configuration.setDirectoriesToIgnore(FalseFileFilter.FALSE);
}

if (ADD.getSelected() != null) {
// @TODO remove this block when Commons-cli version 1.7.1 or higher is used
Arrays.stream(cl.getOptions()).filter( o -> o.getOpt().equals("a")).forEach( o -> cl.hasOption(o.getOpt()));
if (cl.hasOption(FORCE)) logDeprecated(FORCE);
if (cl.hasOption(COPYRIGHT)) logDeprecated(COPYRIGHT);
// remove that block ---^
configuration.setAddLicenseHeaders(cl.hasOption(FORCE) ? AddLicenseHeaders.FORCED : AddLicenseHeaders.TRUE);
configuration.setCopyrightMessage(cl.getOptionValue(COPYRIGHT));
}

if (cl.hasOption(EXCLUDE_CLI)) {
logDeprecated(EXCLUDE_CLI);
String[] excludes = cl.getOptionValues(EXCLUDE_CLI);
if (excludes != null) {
final FilenameFilter filter = parseExclusions(Arrays.asList(excludes));
configuration.setFilesToIgnore(filter);
}
} else if (cl.hasOption(EXCLUDE_FILE_CLI)) {
logDeprecated(EXCLUDE_FILE_CLI);
String excludeFileName = cl.getOptionValue(EXCLUDE_FILE_CLI);
if (excludeFileName != null) {
final FilenameFilter filter = parseExclusions(
Expand All @@ -407,13 +435,15 @@ static ReportConfiguration createConfiguration(String baseDirectory, CommandLine
}

if (cl.hasOption(XML)) {
logDeprecated(XML);
configuration.setStyleReport(false);
} else {
configuration.setStyleReport(true);
if (cl.hasOption(STYLESHEET_CLI)) {
logDeprecated(STYLESHEET_CLI);
String[] style = cl.getOptionValues(STYLESHEET_CLI);
if (style.length != 1) {
DefaultLog.INSTANCE.error("Please specify a single stylesheet");
DefaultLog.getInstance().error("Please specify a single stylesheet");
System.exit(1);
}

Expand All @@ -428,14 +458,16 @@ static ReportConfiguration createConfiguration(String baseDirectory, CommandLine

Defaults.Builder defaultBuilder = Defaults.builder();
if (cl.hasOption(NO_DEFAULTS)) {
logDeprecated(NO_DEFAULTS);
defaultBuilder.noDefault();
}
if (cl.hasOption(LICENSES)) {
logDeprecated(LICENSES);
for (String fn : cl.getOptionValues(LICENSES)) {
defaultBuilder.add(fn);
}
}
Defaults defaults = defaultBuilder.build(DefaultLog.INSTANCE);
Defaults defaults = defaultBuilder.build(DefaultLog.getInstance());
configuration.setFrom(defaults);
if (baseDirectory != null) {
configuration.setReportable(getDirectory(baseDirectory, configuration));
Expand Down Expand Up @@ -466,15 +498,15 @@ static FilenameFilter parseExclusions(List<String> excludes) {
try {
orFilter.addFileFilter(new RegexFileFilter(exclusion));
} catch (PatternSyntaxException e) {
DefaultLog.INSTANCE.info("Will skip given exclusion '" + exclude + "' due to " + e.getMessage());
DefaultLog.getInstance().info("Will skip given exclusion '" + exclude + "' due to " + e.getMessage());
}
orFilter.addFileFilter(new NameFileFilter(exclusion));
if (exclude.contains("?") || exclude.contains("*")) {
orFilter.addFileFilter(WildcardFileFilter.builder().setWildcards(exclusion).get());
}
}
if (ignoredLines > 0) {
DefaultLog.INSTANCE.info("Ignored " + ignoredLines + " lines in your exclusion files as comments or empty lines.");
DefaultLog.getInstance().info("Ignored " + ignoredLines + " lines in your exclusion files as comments or empty lines.");
}
return new NotFileFilter(orFilter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public void setLog(Log log) {
}

public Log getLog() {
return log == null ? DefaultLog.INSTANCE : log;
return log == null ? DefaultLog.getInstance() : log;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@
* under the License. *
*/
package org.apache.rat.header;

import org.apache.rat.DeprecationReporter;

@Deprecated // since 0.17
@DeprecationReporter.Info(since = "0.17", forRemoval = true)
class ArrayCharFilter implements CharFilter {

private final char[] filtered;
private final int length;

protected ArrayCharFilter(final char[] filtered) {
super();
DeprecationReporter.logDeprecated(this.getClass());
this.filtered = filtered;
length = filtered.length;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
* under the License. *
*/
package org.apache.rat.header;

import org.apache.rat.DeprecationReporter;

@Deprecated // since 0.17
@DeprecationReporter.Info(since = "0.17", forRemoval = true)
interface CharFilter {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
*/
package org.apache.rat.header;

import org.apache.rat.DeprecationReporter;

import java.io.IOException;
import java.io.Reader;
import java.nio.CharBuffer;
@Deprecated // since 0.17
@DeprecationReporter.Info(since = "0.17", forRemoval = true)
class FilteringSequenceFactory {

private static final int BUFFER_CAPACITY = 5000;
Expand All @@ -34,6 +37,7 @@ public FilteringSequenceFactory(final CharFilter filter) {
}

public FilteringSequenceFactory(final int capacity, final CharFilter filter) {
DeprecationReporter.logDeprecated(this.getClass());
this.buffer = CharBuffer.allocate(capacity);
this.filter = filter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@
*/
package org.apache.rat.header;

import org.apache.rat.DeprecationReporter;

import java.util.regex.Pattern;

@Deprecated // since 0.17
@DeprecationReporter.Info(since = "0.17", forRemoval = true)
public class HeaderBean {

private Pattern headerPattern;
Expand All @@ -32,6 +35,7 @@ public HeaderBean() {

public HeaderBean(Pattern headerPattern, boolean match) {
super();
DeprecationReporter.logDeprecated(this.getClass());
this.headerPattern = headerPattern;
this.match = match;
}
Expand Down
Loading

0 comments on commit 008577f

Please sign in to comment.