Skip to content

feat(errorprone): enforce no-java.lang.Math rule at compile time#130

Open
halibobo1205 wants to merge 2 commits into
release_v4.8.2from
feat/forbid-java-lang-math-errorprone
Open

feat(errorprone): enforce no-java.lang.Math rule at compile time#130
halibobo1205 wants to merge 2 commits into
release_v4.8.2from
feat/forbid-java-lang-math-errorprone

Conversation

@halibobo1205
Copy link
Copy Markdown
Owner

@halibobo1205 halibobo1205 commented May 29, 2026

User description

Replace the regex-based .github/workflows/math-check.yml scan with a custom ErrorProne BugChecker (ForbidJavaLangMath) in the :errorprone module. It resolves symbols on the type-attributed AST, so it catches every usage form (direct, fully-qualified, statically-imported, method references, field access) with no string/comment false positives. java.lang.StrictMath remains allowed.

  • add ForbidJavaLangMath BugChecker (auto-registered via @AutoService)
  • enable -Xep:ForbidJavaLangMath:ERROR in build.gradle
  • exempt the canonical x86 MathWrapper via @SuppressWarnings
  • delete .github/workflows/math-check.yml

Note: ErrorProne runs only on JDK 11+, so the rule is no longer enforced on the JDK 8 (x86_64) build path.


CodeAnt-AI Description

Enforce the no-java.lang.Math rule during Java builds and close a reflection bypass

What Changed

  • Direct use of java.lang.Math is now rejected during compilation, covering normal calls, fully qualified calls, static imports, method references, and field access
  • Math.class is now also blocked, so java.lang.Math can’t be reached through reflection
  • The old file-scanning workflow was removed
  • java.lang.StrictMath stays allowed, and the canonical x86 math wrapper is explicitly exempted

Impact

✅ Earlier detection of forbidden Math usage
✅ Fewer false alarms from comments and string text
✅ Less chance of consensus-breaking math slipping through

💡 Usage Guide

Checking Your Pull Request

Every time you make a pull request, our system automatically looks through it. We check for security issues, mistakes in how you're setting up your infrastructure, and common code problems. We do this to make sure your changes are solid and won't cause any trouble later.

Talking to CodeAnt AI

Got a question or need a hand with something in your pull request? You can easily get in touch with CodeAnt AI right here. Just type the following in a comment on your pull request, and replace "Your question here" with whatever you want to ask:

@codeant-ai ask: Your question here

This lets you have a chat with CodeAnt AI about your pull request, making it easier to understand and improve your code.

Example

@codeant-ai ask: Can you suggest a safer alternative to storing this secret?

Preserve Org Learnings with CodeAnt

You can record team preferences so CodeAnt AI applies them in future reviews. Reply directly to the specific CodeAnt AI suggestion (in the same thread) and replace "Your feedback here" with your input:

@codeant-ai: Your feedback here

This helps CodeAnt AI learn and adapt to your team's coding style and standards.

Example

@codeant-ai: Do not flag unused imports.

Retrigger review

Ask CodeAnt AI to review the PR again, by typing:

@codeant-ai: review

Check Your Repository Health

To analyze the health of your code repository, visit our dashboard at https://app.codeant.ai. This tool helps you identify potential issues and areas for improvement in your codebase, ensuring your repository maintains high standards of code health.

Replace the regex-based .github/workflows/math-check.yml scan with a
custom ErrorProne BugChecker (ForbidJavaLangMath) in the :errorprone
module. It resolves symbols on the type-attributed AST, so it catches
every usage form (direct, fully-qualified, statically-imported,
method references, field access) with no string/comment false
positives. java.lang.StrictMath remains allowed.

- add ForbidJavaLangMath BugChecker (auto-registered via @autoservice)
- enable -Xep:ForbidJavaLangMath:ERROR in build.gradle
- exempt the canonical x86 MathWrapper via @SuppressWarnings
- delete .github/workflows/math-check.yml

Note: ErrorProne runs only on JDK 11+, so the rule is no longer
enforced on the JDK 8 (x86_64) build path.
@codeant-ai
Copy link
Copy Markdown

codeant-ai Bot commented May 29, 2026

CodeAnt AI is reviewing your PR.


Thanks for using CodeAnt! 🎉

We're free for open-source projects. if you're enjoying it, help us grow by sharing.

Share on X ·
Reddit ·
LinkedIn

@halibobo1205
Copy link
Copy Markdown
Owner Author

@codex: review

@codeant-ai codeant-ai Bot added the size:L This PR changes 100-499 lines, ignoring generated files label May 29, 2026
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5d66743805

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread .github/workflows/math-check.yml
Comment thread errorprone/src/main/java/errorprone/ForbidJavaLangMath.java
Comment thread errorprone/src/main/java/errorprone/ForbidJavaLangMath.java
@codeant-ai
Copy link
Copy Markdown

codeant-ai Bot commented May 29, 2026

CodeAnt AI finished reviewing your PR.

matchMemberSelect only handled VarSymbol members, so the class literal
Math.class (and java.lang.Math.class) resolved to the Math ClassSymbol
and slipped through, leaving a reflective back door to java.lang.Math.
Detect .class selects whose qualifier is java.lang.Math explicitly.

Addresses codeant-ai review on ForbidJavaLangMath.java.
@halibobo1205
Copy link
Copy Markdown
Owner Author

@codex: review

@chatgpt-codex-connector
Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@halibobo1205
Copy link
Copy Markdown
Owner Author

@CodeAnt-AI: review

@codeant-ai
Copy link
Copy Markdown

codeant-ai Bot commented Jun 1, 2026

CodeAnt AI is running the review.


Thanks for using CodeAnt! 🎉

We're free for open-source projects. if you're enjoying it, help us grow by sharing.

Share on X ·
Reddit ·
LinkedIn

@codeant-ai codeant-ai Bot added size:L This PR changes 100-499 lines, ignoring generated files and removed size:L This PR changes 100-499 lines, ignoring generated files labels Jun 1, 2026
@codeant-ai
Copy link
Copy Markdown

codeant-ai Bot commented Jun 1, 2026

Sequence Diagram

This PR replaces the GitHub Actions regex scanner with an ErrorProne BugChecker that blocks direct java.lang.Math usage during compilation, while allowing explicitly suppressed wrapper code. The diagram shows how the checker participates in the build to either fail or pass compilation based on detected Math usage.

sequenceDiagram
    participant Developer
    participant Build as Build tool
    participant JavaCompiler
    participant Checker as ForbidJavaLangMath
    participant Source as Java source file

    Developer->>Build: Run Java build
    Build->>JavaCompiler: Compile sources with ErrorProne enabled
    JavaCompiler->>Checker: Invoke ForbidJavaLangMath on each source AST
    Checker->>Source: Inspect symbols for java.lang.Math usage

    alt Forbidden Math usage without suppression
        Checker-->>JavaCompiler: Report ForbidJavaLangMath error
        JavaCompiler-->>Developer: Build fails with error
    else Only allowed or suppressed usages
        Checker-->>JavaCompiler: No violations
        JavaCompiler-->>Developer: Build succeeds
    end
Loading

Generated by CodeAnt AI

@codeant-ai
Copy link
Copy Markdown

codeant-ai Bot commented Jun 1, 2026

CodeAnt AI finished running the review.


Thanks for using CodeAnt! 🎉

We're free for open-source projects. if you're enjoying it, help us grow by sharing.

Share on X ·
Reddit ·
LinkedIn

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L This PR changes 100-499 lines, ignoring generated files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant