Updated checkstyle rule for headers, and corrected several incorrect headers.

This commit is contained in:
Randall Hauch 2016-01-25 18:59:25 -06:00
parent a0a8953d2a
commit eff1f665fa
4 changed files with 27 additions and 3 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2015-2016 Red Hat, Inc. and/or its affiliates.
* Copyright 2015 Debezium Authors.
*
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/

View File

@ -1,5 +1,5 @@
/*
* Copyright 2015-2016 Red Hat, Inc. and/or its affiliates.
* Copyright 2015 Debezium Authors.
*
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/

View File

@ -13,15 +13,19 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;
import java.util.regex.Pattern;
public class Header extends com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck {
private static final String YEAR_MATCHING_PATTERN = "${YYYY}";
private Set<String> excludedFileSet;
private String excludedFilesRegex;
private Pattern excludedFilesPattern;
private final String workingDirPath = new File(".").getAbsoluteFile().getParentFile().getAbsolutePath();
private final int workingDirPathLength = workingDirPath.length();
private Predicate<String> yearLineMatcher;
public Header() {
}
@ -59,6 +63,11 @@ public void setHeaderFile( String aFileName ) {
if (l == null) {
break;
}
if (l.contains(YEAR_MATCHING_PATTERN)) {
String prefix = l.substring(0, l.indexOf(YEAR_MATCHING_PATTERN));
String suffix = l.substring(l.indexOf(YEAR_MATCHING_PATTERN)+YEAR_MATCHING_PATTERN.length());
yearLineMatcher = (line)->line.startsWith(prefix) && line.endsWith(suffix);
}
sb.append(l).append("\\n");
}
super.setHeader(sb.toString());
@ -91,4 +100,19 @@ protected void processFiltered( File aFile,
if (isExcluded(aFile)) return;
super.processFiltered(aFile, aLines);
}
/**
* Checks if a code line matches the required header line.
* @param lineNumber the line number to check against the header
* @param line the line contents
* @return true if and only if the line matches the required header line
*/
@Override
protected boolean isMatch(int lineNumber, String line) {
if ( super.isMatch(lineNumber, line)) return true;
// Otherwise it does not match, so see if the line contain our "${year}" string
if ( yearLineMatcher != null && yearLineMatcher.test(line) ) return true;
return false;
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2015 Debezium Authors.
* Copyright ${YYYY} Debezium Authors.
*
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/