Creating a Custom Rule in Codeac to Ban Raw Types in Java
Learn how to forbid raw types in Java by writing a custom Codeac rule in YAML, running the analysis, and verifying the violation appears in the report.
02 Nov 2025, 03:12 UTC

Useful answer
To prevent the use of raw types in a Java codebase, add a custom rule written in Codeac’s DSL to a YAML configuration file. When the analysis runs, Codeac will flag any class, field, or method that uses a raw type and report the violation inline in the IDE or CI output.
How the mechanism works
Codeac parses each source file into an abstract syntax tree (AST). The custom rule you define contains a pattern written in the DSL that matches specific AST nodes. During analysis, Codeac walks the AST, evaluates the pattern against every node, and emits a violation whenever the pattern matches.
Worked configuration example
Create a file named .codeac/rules/raw-types.yml in the repository root:
# .codeac/rules/raw-types.yml
rules:
- id: java-raw-type-ban
description: "Disallow raw types (e.g., List instead of List<String>)"
severity: error
language: java
pattern: |
TypeDeclaration
> TypeParameterList? # optional generics
> (PrimitiveType | ReferenceType) # the type being declared
> !TypeArgumentList # must NOT have type arguments
Explanation of the pattern:
TypeDeclarationmatches any class, interface, enum, or annotation.- The optional
TypeParameterListallows the rule to ignore already‑parameterized types. - The final clause
!TypeArgumentListensures the rule only triggers when the type is used without angle‑bracket arguments – i.e., a raw type.
Add the rule set to the main Codeac configuration (codeac.yml):
# codeac.yml
rules:
- import: .codeac/rules/raw-types.yml
Running the analysis and verifying the result
- Install the CLI (requires read access to the repository and execute permission):
curl -L https://get.codeac.io/cli | sh - Create a minimal Java sample that contains a raw type:
// src/example/RawExample.java package example; import java.util.List; public class RawExample { List items; // raw type – should trigger the rule } - Place the rule files as shown above.
- Run the analysis** (needs read access to source files and write permission for the output directory):
codeac analyze --src src --output report.json - Check the report** – you should see an entry similar to:
{ "file": "src/example/RawExample.java", "line": 6, "message": "Disallow raw types (e.g., List instead of List)", "ruleId": "java-raw-type-ban" }
If the violation appears, the custom rule is active. If no violation is reported, verify that the DSL syntax matches the version of Codeac you are running (see limits below).
Limits and common mistakes
Version sensitivity
The DSL evolves between Codeac releases. A pattern written for Codeac 2.x may fail to compile under 3.x if the node names or operators changed. Always consult the release notes for your version before committing a rule.
Performance impact
Patterns that are too broad (e.g., matching ReferenceType without constraints) are evaluated against every AST node, which can increase analysis time dramatically, especially in large projects. Keep patterns as specific as possible and test on a representative subset before scaling.
False positives
The DSL does not fully capture language nuances such as wildcard generics (List<?>) or macro‑generated code. A pattern that ignores TypeArgumentList may flag uses of raw types that are intentionally hidden behind a wildcard. To reduce false positives, refine the pattern with additional context, such as checking the enclosing method signature.
Typical configuration errors
- Incorrect indentation: YAML is space‑sensitive; mixing tabs and spaces will cause a parse error.
- Missing
languagefield**: Without it, Codeac defaults to treating the pattern as language‑agnostic, which usually yields no matches. - Overly permissive severity**: Setting
severity: infomay hide the violation in CI pipelines that only fail onwarningor higher.
Practical way to check the result
After running codeac analyze, open the generated report (JSON, SARIF, or the CLI’s table output) and search for the rule ID you defined (java-raw-type-ban). If the entry appears with the expected file and line number, the rule is working. If the report is empty, double‑check the DSL syntax against the version‑specific documentation and ensure the rule file is imported correctly.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.