Skip to content

Commit

Permalink
Updated checkstyle rule for headers, and corrected several incorrect …
Browse files Browse the repository at this point in the history
…headers.
  • Loading branch information
rhauch committed Jan 26, 2016
1 parent a0a8953 commit eff1f66
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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;
}

}
2 changes: 1 addition & 1 deletion support/checkstyle/src/main/resources/debezium.header
Original file line number Diff line number Diff line change
@@ -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
*/

0 comments on commit eff1f66

Please sign in to comment.