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

Annotation processor should validate final primitive and String fields (was: Annotation processor not working) #1387

Closed
xehpuk opened this issue Jul 9, 2021 · 3 comments · Fixed by #1711
Labels
status: help-wanted 🆘 theme: annotation-proc An issue or change related to the annotation processor type: enhancement ✨

Comments

@xehpuk
Copy link

xehpuk commented Jul 9, 2021

Source

@Option(names = {"-h", "--host"})
private final String hostname = "localhost"; // <-- notice the final

pom.xml

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <!-- annotationProcessorPaths requires maven-compiler-plugin version 3.5 or higher -->
                <version>3.8.1</version>
                <configuration>
                  <annotationProcessorPaths>
                    <path>
                      <groupId>info.picocli</groupId>
                      <artifactId>picocli-codegen</artifactId>
                      <version>4.6.1</version>
                    </path>
                  </annotationProcessorPaths>
                  <compilerArgs>
                    <arg>-Aproject=${project.groupId}/${project.artifactId}</arg>
                  </compilerArgs>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

mvn compile

[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------------< ... >---------------------------------
[INFO] Building ... 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ ... ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory ...\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ ... ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to ...\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.627 s
[INFO] Finished at: 2021-07-09T22:10:22+02:00
[INFO] ------------------------------------------------------------------------

Running

Exception in thread "main" picocli.CommandLine$InitializationException: Constant (final) primitive and String fields like private final java.lang.String ....hostname cannot be used as an @Option: compile-time constant inlining may hide new values written to it.
	at picocli.CommandLine$Model$CommandReflection.validateArgSpecMember(CommandLine.java:11571)
	at picocli.CommandLine$Model$CommandReflection.initFromAnnotatedTypedMembers(CommandLine.java:11516)
	at picocli.CommandLine$Model$CommandReflection.initFromAnnotatedFields(CommandLine.java:11466)
	at picocli.CommandLine$Model$CommandReflection.extractCommandSpec(CommandLine.java:11399)
	at picocli.CommandLine$Model$CommandSpec.forAnnotatedObject(CommandLine.java:6202)
	at picocli.CommandLine.<init>(CommandLine.java:227)
	at picocli.CommandLine.<init>(CommandLine.java:221)
	at picocli.CommandLine.<init>(CommandLine.java:196)
	at ....main(....java:44)
@remkop
Copy link
Owner

remkop commented Jul 9, 2021

Yes, I programmed the annotation processor to throw this exception for @Option-annotated final String fields and final primitive fields.

This is by design. :-)

@xehpuk
Copy link
Author

xehpuk commented Jul 9, 2021

The processor didn't throw any exception. It was thrown at runtime. Or am I misunderstanding?

@remkop
Copy link
Owner

remkop commented Jul 10, 2021

Yes you are right.
The exception is only thrown at runtime, not by the annotation processor. Thanks for pointing that out.

So, I guess that is the problem: this exception should not only be thrown at runtime, but also at compile time by the annotation processor. The current implementation is inconsistent. I agree.

Are you interested in providing a pull request for this?

@remkop remkop changed the title Annotation processor not working Annotation processor should validate final primitive and String fields (was: Annotation processor not working) Jul 13, 2021
@remkop remkop added theme: annotation-proc An issue or change related to the annotation processor type: enhancement ✨ status: help-wanted 🆘 labels Feb 14, 2022
remkop added a commit that referenced this issue Jun 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: help-wanted 🆘 theme: annotation-proc An issue or change related to the annotation processor type: enhancement ✨
Projects
None yet
2 participants