@@ -142,7 +142,7 @@ public FormatterStep build() {
142142 throw new IllegalStateException (yearMode .toString ());
143143 }
144144 return new Runtime (headerLazy .get (), delimiter , yearSeparator , updateYear , skipLinesMatching );
145- }, step -> step ::format );
145+ }, step -> FormatterFunc . needsFile ( step ::format ) );
146146 }
147147
148148 if (contentPattern == null ) {
@@ -214,6 +214,8 @@ private static class Runtime implements Serializable {
214214 private final boolean updateYearWithLatest ;
215215 private final boolean licenseHeaderWithRange ;
216216
217+ private static final Pattern FILENAME_PATTERN = Pattern .compile ("\\ $FILE" );
218+
217219 /** The license that we'd like enforced. */
218220 private Runtime (String licenseHeader , String delimiter , String yearSeparator , boolean updateYearWithLatest , @ Nullable String skipLinesMatching ) {
219221 if (delimiter .contains ("\n " )) {
@@ -266,9 +268,9 @@ private static Optional<String> getYearToken(String licenseHeader) {
266268 }
267269
268270 /** Formats the given string. */
269- private String format (String raw ) {
271+ private String format (String raw , File file ) {
270272 if (skipLinesMatching == null ) {
271- return addOrUpdateLicenseHeader (raw );
273+ return addOrUpdateLicenseHeader (raw , file );
272274 } else {
273275 String [] lines = raw .split ("\n " );
274276 StringBuilder skippedLinesBuilder = new StringBuilder ();
@@ -287,11 +289,11 @@ private String format(String raw) {
287289 remainingLinesBuilder .append (line ).append ('\n' );
288290 }
289291 }
290- return skippedLinesBuilder + addOrUpdateLicenseHeader (remainingLinesBuilder .toString ());
292+ return skippedLinesBuilder + addOrUpdateLicenseHeader (remainingLinesBuilder .toString (), file );
291293 }
292294 }
293295
294- private String addOrUpdateLicenseHeader (String raw ) {
296+ private String addOrUpdateLicenseHeader (String raw , File file ) {
295297 Matcher contentMatcher = delimiterPattern .matcher (raw );
296298 if (!contentMatcher .find ()) {
297299 throw new IllegalArgumentException ("Unable to find delimiter regex " + delimiterPattern );
@@ -305,13 +307,14 @@ private String addOrUpdateLicenseHeader(String raw) {
305307 return raw ;
306308 } else {
307309 // otherwise we'll have to add the header
308- return yearSepOrFull + content ;
310+ return replaceFileName ( yearSepOrFull , file ) + content ;
309311 }
310312 } else {
311313 // the yes year case is a bit harder
312314 int beforeYearIdx = raw .indexOf (beforeYear );
313315 int afterYearIdx = raw .indexOf (afterYear , beforeYearIdx + beforeYear .length () + 1 );
314316
317+ String header ;
315318 if (beforeYearIdx >= 0 && afterYearIdx >= 0 && afterYearIdx + afterYear .length () <= contentMatcher .start ()) {
316319 // and also ends with exactly the right header, so it's easy to parse the existing year
317320 String existingYear = raw .substring (beforeYearIdx + beforeYear .length (), afterYearIdx );
@@ -323,12 +326,13 @@ private String addOrUpdateLicenseHeader(String raw) {
323326 return raw ;
324327 }
325328 }
326- return beforeYear + newYear + afterYear + content ;
329+ header = beforeYear + newYear + afterYear ;
327330 } else {
328331 String newYear = calculateYearBySearching (raw .substring (0 , contentMatcher .start ()));
329332 // at worst, we just say that it was made today
330- return beforeYear + newYear + afterYear + content ;
333+ header = beforeYear + newYear + afterYear ;
331334 }
335+ return replaceFileName (header , file ) + content ;
332336 }
333337 }
334338 }
@@ -421,6 +425,10 @@ private String setLicenseHeaderYearsFromGitHistory(String raw, File file) throws
421425 return beforeYear + yearRange + afterYear + raw .substring (contentMatcher .start ());
422426 }
423427
428+ private String replaceFileName (String header , File file ) {
429+ return FILENAME_PATTERN .matcher (header ).replaceAll (file .getName ());
430+ }
431+
424432 private static String parseYear (String cmd , File file ) throws IOException {
425433 String fullCmd = cmd + " -- " + file .getAbsolutePath ();
426434 ProcessBuilder builder = new ProcessBuilder ().directory (file .getParentFile ());
0 commit comments