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

[FEATURE] Add @ConstExpr Annotation for build-time code-execution for java #3822

Open
Michael1297 opened this issue Feb 9, 2025 · 0 comments

Comments

@Michael1297
Copy link

Michael1297 commented Feb 9, 2025

Describe the feature
Simulates constexpr from C++11, build-time code-execution, from cppreference.com:

The @ConstExpr Annotation declares that it is possible to evaluate the value of the function or variable at compile time.

@ConstExpr
private static final long BUILD_TIME = System.currentTimeMillis();
@ConstExpr
private static final String SIMPLE_NAME = MyClass.class.getSimpleName();

// is replaced with:
private static final long BUILD_TIME = 1739114746283L; // value is an example, depends on when you run
private static final String SIMPLE_NAME = "MyClass";
@ConstExpr
private static final int MAX_WIDTH = Integer.parseInt(getProperties().getProperty("width", "1000"));
@ConstExpr
private static final int MAX_HEIGHT = Integer.parseInt(getProperties().getProperty("height", "1000"));    

private static Properties getProperties() {
    Properties properties = new Properties();
    try (FileInputStream fileInputStream = new FileInputStream(PROJECT_PROPERTIES_PATH)) {
        properties.load(fileInputStream);
    } catch (Exception e) {
        LOGGER.err("File not found: " + PROJECT_PROPERTIES_PATH);
    }
    return properties;
}

// is replaced with:
private static final int MAX_WIDTH = 5000;
private static final int MAX_HEIGHT = 5000;    

Describe the target audience
This feature would benefit Java developers developing mobile apps and games.

Additional context
Java libraries that implement this function:
https://github.com/junkdog/constexpr-java (unavailable in the maven repository)
https://github.com/Sipkab/jvm-constexpr (does not support maven/gradle)

Mention of this function
https://www.reddit.com/r/java/comments/5aci2a/constexprjava_buildtime_codeexecution_using_the/
https://discuss.kotlinlang.org/t/string-literals-way-to-compute-hashcode-at-compile-time/23745
https://youtrack.jetbrains.com/issue/KT-14652
https://discuss.kotlinlang.org/t/feature-request-compile-time-code-generator-annation/13719

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant