You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@netbeans.apache.org by "ASF GitHub Bot (JIRA)" <ji...@apache.org> on 2017/11/19 20:56:01 UTC

[jira] [Commented] (NETBEANS-161) Incorrect license header introducer

    [ https://issues.apache.org/jira/browse/NETBEANS-161?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16258616#comment-16258616 ] 

ASF GitHub Bot commented on NETBEANS-161:
-----------------------------------------

lbruun opened a new pull request #282: [NETBEANS-161] Changed license header introducer to simple slash-star
URL: https://github.com/apache/incubator-netbeans/pull/282
 
 
   The new Apache license header on all .java, .cpp, .c files (etc) started with slash-star-star (the Javadoc introducer). While strictly speaking this is acceptable, the header is not a Javadoc comment, and no present or future Javadoc parser should attempt to regard as a Javadoc comment. Therefore all such occurences are changed to simple slash-star introducer.
   
   For future reference: The following snippet was used to change the files. I tried bash+sed but it never gave me correct result, so I resorted to good old Java. As I bonus, the snippet also checks for Windows line endings.
   
   ``` java
   package org.apache.netbeans.sourceutils;
   
   import java.io.File;
   import java.io.IOException;
   import java.nio.charset.StandardCharsets;
   import java.nio.file.FileVisitResult;
   import java.nio.file.Files;
   import java.nio.file.Path;
   import java.nio.file.SimpleFileVisitor;
   import java.nio.file.attribute.BasicFileAttributes;
   import java.util.ArrayList;
   import java.util.Arrays;
   import java.util.HashMap;
   import java.util.List;
   import java.util.Map;
   import java.util.Set;
   import java.util.TreeSet;
   
   public class ChgLicHeader extends SimpleFileVisitor<Path> {
   
       private static final List<String> EXCLUDES = new ArrayList<>(Arrays.asList(
               ".gif", ".png", ".jpg", ".PNG", ".pdf",
               ".jar", ".zip", ".ico", ".icns", ".wav",
               ".idb", ".odt", ".gz", ".jar"));
   
       private static final byte[] AL_JAVADOC_INTRO = "/**\n * Licensed to the Apache Software Foundation"
               .getBytes(StandardCharsets.US_ASCII);
       
       private Map<String, Set<Path>> filesChanged = new HashMap<>();
       private Set<Path> windowsLineEndingFiles = new TreeSet<>();  
   
       public static void main(String[] args) throws IOException {
   
           System.out.println("Path: " + args[0]);
           Path path = new File(args[0]).toPath();
           ChgLicHeader fileVisitor = new ChgLicHeader();
           Files.walkFileTree(path, fileVisitor);
   
           System.out.println("Files changed: ");
           for (String ext : fileVisitor.getFileChanged().keySet()) {
               System.out.println("  " + ext);
               for (Path file : fileVisitor.getFileChanged().get(ext)) {
                   System.out.println("        " + path.relativize(file));
               }
           }
   
           System.out.println();
           System.out.println();
   
           System.out.println("Files with (supposedly) CRLF line endings: ");
           for (Path file : fileVisitor.getWindowsLineEndingFiles()) {
               System.out.println("  " + path.relativize(file));
           }
       }
   
       @Override
       public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
   
           if (attrs.isRegularFile()) {
               for (String excludedExt : EXCLUDES) {
                   if (file.getFileName().toString().endsWith(excludedExt)) {
                       return FileVisitResult.CONTINUE;
                   }
               }
               if (!usesUnixLineEndings(file)) {
                   windowsLineEndingFiles.add(file);
               }
   
               checkAndRewrite(file);
           }
           return FileVisitResult.CONTINUE;
       }
   
       @Override
       public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
           if (dir.endsWith(".git")) {
               return FileVisitResult.SKIP_SUBTREE;
           }
           return super.preVisitDirectory(dir, attrs);
       }
   
       private void addChangedFileByExtension(String ext, Path file) {
           Set<Path> set = filesChanged.get(ext);
           if (set == null) {
               set = new TreeSet<>();
               filesChanged.put(ext, set);
           }
           set.add(file);
       }
   
       private void checkAndRewrite(Path file) throws IOException {
           byte[] buf = Files.readAllBytes(file);
   
           if (usesApacheHeaderWithJavadocIntroducer(buf)) {
               String fileNameStr = file.getFileName().toString();
               int pos = fileNameStr.lastIndexOf('.');
               if (pos > 0) {
                   addChangedFileByExtension(fileNameStr.substring(pos), file);
               }
   
               // New array, which is a copy but with 3rd byte excluded
               byte[] newBuf = new byte[buf.length - 1];
               System.arraycopy(buf, 0, newBuf, 0, 2);
               System.arraycopy(buf, 3, newBuf, 2, buf.length - 3);
   
               Files.write(file, newBuf);  // rewrite file
           }
       }
   
       /**
        * Check if file (as represented by byte buffer) has the ASF license header,
        * and the header begins with the Javadoc introducer.
        */
       private boolean usesApacheHeaderWithJavadocIntroducer(byte[] buf) throws IOException {
   
           if (buf.length >= AL_JAVADOC_INTRO.length) {
               boolean match = true;
               for (int i = 0; i < AL_JAVADOC_INTRO.length; i++) {
                   if (buf[i] != AL_JAVADOC_INTRO[i]) {
                       match = false;
                       break;
                   }
               }
               return match;
           }
           return false;
       }
   
       private boolean usesUnixLineEndings(Path file) throws IOException {
           int noOfLF = 0;
           int noOfCRLF = 0;
           byte[] buf = Files.readAllBytes(file);
   
           for (int i = 0; i < buf.length; i++) {
   
               if (buf[i] == '\n') {
                   noOfLF++;
               }
               if (buf[i] == '\r') {
                   i++;
                   if (i < buf.length && buf[i] == '\n') {
                       noOfCRLF++;
                   }
                   noOfLF++;
               }
           }
           return (noOfCRLF == 0);
       }
   
       public Map<String, Set<Path>> getFileChanged() {
           return filesChanged;
       }
   
       public Set<Path> getWindowsLineEndingFiles() {
           return windowsLineEndingFiles;
       }
   }
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


> Incorrect license header introducer
> -----------------------------------
>
>                 Key: NETBEANS-161
>                 URL: https://issues.apache.org/jira/browse/NETBEANS-161
>             Project: NetBeans
>          Issue Type: Bug
>            Reporter: lbruun
>            Assignee: lbruun
>            Priority: Trivial
>              Labels: pull-request-available
>         Attachments: HeaderJavadocIntroducer.png
>
>
> The new Apache license header on {{.java}}, {{.cpp}}, etc files start with the Javadoc introducer (slash-star-star), rather than just the simple comment introducer (slash-star).
> This has some minor side-effects with e.g. source code formatting in the IDE. 
> Here's a real-life example where this is really unhealthy:
> !HeaderJavadocIntroducer.png!
> Result in this case is that the license header becomes Javadoc.



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)