Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Content wrongly truncated when adding header after package declaration #135

Closed
gevegab opened this issue May 16, 2018 · 4 comments
Closed

Comments

@gevegab
Copy link
Contributor

gevegab commented May 16, 2018

Hello

I have a java file that has a "package" and "import" on the same line, something like this:

package org.example.test; import java.io.File;

When the file is processed by the "update-file-header" goal, the content after the package declaration (the import) is wrongly truncated, and I obtain something like this (that doesn't compile any longer):

package org.example.test;

/*-
 * #%L
 * test.license.header
 * %%
 * Copyright (C) 2018 org.example
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
 * #L%
 */
mport java.io.File;

This is a problem to me because the java file is not hand-written, it is generated by a tool that doesn't allow me to control the generated format .. so I can not really workaround the problem

I have pinpointed the problem to this code on class JavaFileHeaderTransformer,

        String prolog = null;
        int startProlog = content.indexOf( "package" );
        if ( startProlog > -1 )
        {

            // package was detected
            int endProlog = content.indexOf( ";", startProlog );

            if ( endProlog > -1 )
            {

                // prolog end was detected

                int lastIndex = endProlog + 1;

                prolog = content.substring( 0, lastIndex );

                // prolog goes to next line
                prolog += getLineSeparator();

                if ( lastIndex == content.length() )
                {

                    // adding a new empty end line to the content
                    content += getLineSeparator();
                }


            }
        }

        if ( prolog == null )
        {

            // no prolog detected
            result = super.addHeader( header, content );
        }
        else
        {

            // prolog detected
            content = content.substring( prolog.length() );
            result = super.addHeader( prolog + getLineSeparator() + header, content );
        }
        return result;
    }

On windows, the line prolog += getLineSeparator(); adds two characters (CR+LF) to the prolog, so when it later does content = content.substring( prolog.length() ); one character is lost from the original content after the "package" declaration.

I attach a project with a single java file that shows the problem (the source directory contains the wrongly truncated java file and the original file before header insertion )
test.license.header.zip

Thank you

@ppalaga
Copy link
Contributor

ppalaga commented Jan 4, 2019

Thanks for the report. Could you please transform the attached project to an integration test under https://github.com/mojohaus/license-maven-plugin/tree/master/src/it ?

@gevegab
Copy link
Contributor Author

gevegab commented Jan 8, 2019

Hello,

I have created the integration test.

But I had some problems checking out the project, because I develop on Windows and the tests (both unit and integration) failed.

So I decided to fork the project to be sure not to introduce errors on your code base. I will create a cross-fork pull request for you to integrate.

Because of the problems, I have not tried a full build of the project, but I have tested using this command

mvn clean install -DskipTests -DskipITs -Prun-one-it -DitName=ISSUE-135

@ppalaga
Copy link
Contributor

ppalaga commented Jan 11, 2019

The windows CI finally started working and I was able to fix the hottest issues. So if you rebase on top of current master, you should basically be able to build on windows. These two integration tests are known to fail: #205 and #204 where #205 could be related to the current issue. Any help here or there would be appreciated, because I neither have Windows nor have any interests in the area of file header mojos.

@gevegab
Copy link
Contributor Author

gevegab commented Jan 11, 2019

Ok. I have rebased my branch on the current master and I am able to build on windows.

This issue actually happens on both windows and linux, I will propose you a fix next week.

I also identified the problems with issues #204 and #205 on windows, I commented there and will propose you a fix next week

@ppalaga ppalaga changed the title Update header of Java files : content is wrongly truncated when adding header after package declaration Content wrongly truncated when adding header after package declaration Jan 19, 2019
ppalaga added a commit that referenced this issue Jan 23, 2019
Fix #135 Content wrongly truncated when adding header after package
@ppalaga ppalaga added this to the 1.17 milestone Feb 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants